RAIA™ Specification

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.

← Back to Developer Hub TEXTBOOK SPECIFICATION
RAIA™ (Real estate Artificial Intelligence Agent) Specification

The Communication Stats Engine calculates real-time metrics like message speed, response latency, and preferred channels to keep client interactions highly responsive.

🧩 Section A: In Plain English

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.

Architecture Specification

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 to tbl_persons(person_id) (Foreign Key with Cascade Delete)
  • organisation_id: Links to tbl_organisations(organisation_id)
  • channel: Text representation of channel (whatsapp, email, phone, sms, telegram)
  • event_count: Incremental counter of inbound events
  • last_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;

Repository Code

The live codebase implementation for this textbook logic sits within the following repository folder: