feat(analytics): add customer daily metrics data foundation
Some checks failed
Frontend Lint & Test / test (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Run Chatwoot CE spec / security-scan (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled

Add the migration + model for per-customer-per-day conversation analytics
(M2 phase 1b). Complements the per-account ConversationDailyMetric with a
per-contact/day view so reports can drill into individual customers and see
per-agent attribution, while the per-account table keeps the aggregate totals.

- customer_daily_metrics: unique (account_id, contact_id, date); message/
  conversation/resolved/unresolved counts, avg session duration, top tags,
  deal outcome breakdown, and agent_ids (jsonb).
- CustomerDailyMetric: belongs_to :account + :contact (Contact convention),
  validations, JSONB getters defaulting to []/{}.

Approved by independent five-key pre-commit review deleg_434fc544
(passed=true, blocking arrays empty).
This commit is contained in:
Moreminimore
2026-08-19 12:43:54 +07:00
parent 9fbb0da9b8
commit 0fad9ce6b8
2 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
# == Schema Information
#
# Table name: customer_daily_metrics
#
# id :bigint not null, primary key
# account_id :bigint not null
# contact_id :bigint not null
# date :date not null
# timezone :string
# message_count :integer default(0), not null
# conversation_count :integer default(0), not null
# resolved_count :integer default(0), not null
# unresolved_count :integer default(0), not null
# avg_session_duration_seconds :float default(0.0), not null
# top_tags :jsonb default([]), not null
# deal_outcomes :jsonb default({}), not null
# agent_ids :jsonb default([]), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class CustomerDailyMetric < ApplicationRecord
belongs_to :account
belongs_to :contact
validates :account_id, presence: true
validates :contact_id, presence: true
validates :date, presence: true
validates :date, uniqueness: { scope: %i[account_id contact_id] }
def top_tags
self[:top_tags] || []
end
def deal_outcomes
self[:deal_outcomes] || {}
end
def agent_ids
self[:agent_ids] || []
end
end