Comms Statistics Tracking & Channel Preferences
The Communication Stats Engine calculates real-time metrics like message speed, response latency, and preferred channels to keep client interactions highly responsive.
The Communication Stats Engine calculates real-time metrics like message speed, response latency, and preferred channels to keep client interactions highly responsive.
Comms Statistics Tracking & Channel Preferences
This specification covers the pipeline blueprints for backfilling historical contact counts and tracking active inbound communication channels without data loss.
Comms Statistics Tracking & Channel Preferences
Specifications and pipeline blueprints for backfilling historical contact counts and tracking active inbound channels without data loss.
⚡ Pipeline Architecture Map
[Legacy Importer / Sync Files] [Live Channels Webhooks]
| |
v v
+-----------------------------+ +-----------------------------+
| tbl_conversation_messages | | Webhook Handler Router |
| Direction = 'inbound' | | e.g. /api/webhooks/telegram |
+-----------------------------+ +-----------------------------+
| |
| (One-Time SQL Backfill) | (Triggers increment_comms_stat)
v v
+----------------------------------------------------------------+
| tbl_persons_comms_stats |
| ON CONFLICT (person_id, channel) |
| DO UPDATE SET event_count = count + 1 |
+----------------------------------------------------------------+
|
v
+----------------------------------------------------------------+
| v_persons_effective_prefs |
| Resolves comm_preferred vs calculated channel |
+----------------------------------------------------------------+
1. Database Model & Flow Design
The communication stats system records the count of inbound communication events per person per channel. This metrics layer is defined under tbl_persons_comms_stats (introduced in V152) and used to determine active tenant/landlord response metrics.
Table Schema: public.tbl_persons_comms_stats
id: Unique UUID (Primary Key)person_id: Links totbl_persons(person_id)(Foreign Key with Cascade Delete)organisation_id: Links totbl_organisations(organisation_id)channel: Text representation of channel (whatsapp,email,phone,sms,telegram)event_count: Incremental counter of inbound eventslast_contact_at: Timestamp of last incoming interaction
Repository Code
The live codebase implementation for this specification sits within the following repository folder:
2. One-Time Historical Backfill (Executed)
Historical messages (imported from Slack and legacy databases) are stored in tbl_conversation_messages. We executed a one-time transactional backfill script to aggregate and pre-populate statistics for all past interactions, avoiding starting from zero.
SQL Backfill Statement
INSERT INTO public.tbl_persons_comms_stats (person_id, organisation_id, channel, event_count, last_contact_at) SELECT c.person_id, c.organisation_id, COALESCE(m.channel, c.channel, 'whatsapp'), COUNT(*), MAX(m.created_at) FROM public.tbl_conversation_messages m JOIN public.tbl_conversations c ON m.conversation_id = c.conversation_id WHERE m.direction = 'inbound' AND c.person_id IS NOT NULL GROUP BY c.person_id, c.organisation_id, COALESCE(m.channel, c.channel, 'whatsapp') ON CONFLICT (person_id, channel) DO UPDATE SET event_count = EXCLUDED.event_count, last_contact_at = EXCLUDED.last_contact_at;
3. Future Ongoing Tracking (No Gaps)
To prevent gaps in calculations going forward, the system enforces a strict contract: **Every inbound webhook router must invoke the Postgres race-safe counter RPC increment_comms_stat()**.
RPC Implementation
CREATE OR REPLACE FUNCTION public.increment_comms_stat(
p_person_id UUID,
p_channel TEXT,
p_organisation_id UUID
) RETURNS void AS $$
BEGIN
INSERT INTO public.tbl_persons_comms_stats
(person_id, organisation_id, channel, event_count, last_contact_at)
VALUES
(p_person_id, p_organisation_id, p_channel, 1, now())
ON CONFLICT (person_id, channel)
DO UPDATE SET
event_count = tbl_persons_comms_stats.event_count + 1,
last_contact_at = now();
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
Architecture Details
Component-based UI architecture built on React/Next.js with Tailwind CSS. The system uses a design token system for consistent theming, a component library for reusable UI elements, and server-side rendering for performance. State management follows a unidirectional data flow pattern.
- Core Component: UI component system and design token engine
- Data Flow: User Input → Component State → Action Dispatch → Data Fetch → Re-render → Display
- Integration Points: Next.js, Tailwind CSS, React context, REST API layer
Configuration Reference
Key configuration parameters for this system:
| Parameter | Type | Default | Description |
|---|---|---|---|
| theme_mode | string | light | UI theme mode (light, dark, system) |
| animation_enabled | boolean | true | Enable/disable UI animations |
| grid_columns | integer | 12 | Layout grid column count |
Figure 2: UI component architecture — component interaction and data flow.
Troubleshooting
| Issue | Cause | Resolution |
|---|---|---|
| Layout shift on page load | Missing dimensions on dynamic content | Add explicit width/height or aspect-ratio CSS |
| Theme mismatch | CSS variable not defined in current theme | Ensure all CSS variables are defined in both light and dark themes |
This component is part of the RAIA (Real estate Artificial Intelligence Agent) platform mono-repo. Implementation details, API contracts, and integration tests are available in the repository.