Files
gochat/internal/model/reporting_event.go
T
2026-06-04 15:44:48 +08:00

80 lines
3.7 KiB
Go

package model
import (
"time"
)
// ReportingEvent represents a raw event for real-time analytics.
// Reference: Chatwoot ReportingEvent model — captures first_response, resolution_time,
// reply_time, avg_first_response_time, avg_resolution_time, resolutions_count,
// bot_resolutions_count, bot_handoffs_count.
type ReportingEvent struct {
Base
AccountID uint `gorm:"index;not null" json:"account_id"`
Name string `gorm:"size:100;index;not null" json:"name"` // metric name: first_response, resolution_time, etc.
Value float64 `json:"value"`
ValueInBusinessHours float64 `json:"value_in_business_hours"`
ConversationID *uint `gorm:"index" json:"conversation_id,omitempty"`
InboxID *uint `gorm:"index" json:"inbox_id,omitempty"`
UserID *uint `gorm:"index" json:"user_id,omitempty"`
EventStartTime time.Time `gorm:"index" json:"event_start_time"`
EventEndTime time.Time `gorm:"index" json:"event_end_time"`
Account *Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
Inbox *Inbox `gorm:"foreignKey:InboxID" json:"inbox,omitempty"`
Conversation *Conversation `gorm:"foreignKey:ConversationID" json:"conversation,omitempty"`
User *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}
func (ReportingEvent) TableName() string { return "reporting_events" }
// DimensionType represents the dimension category for rollup aggregation.
type DimensionType string
const (
DimensionAccount DimensionType = "account"
DimensionAgent DimensionType = "agent"
DimensionInbox DimensionType = "inbox"
DimensionTeam DimensionType = "team"
DimensionLabel DimensionType = "label"
DimensionConversation DimensionType = "conversation"
)
// MetricName constants are used as string identifiers for reporting event queries.
const (
MetricNameFirstResponse = "first_response"
MetricNameReplyTime = "reply_time"
MetricNameResolutionTime = "resolution_time"
MetricNameBotResolutionsCount = "bot_resolutions_count"
MetricNameBotHandoffsCount = "bot_handoffs_count"
MetricBotResolutions = "bot_resolutions"
MetricBotHandoffs = "bot_handoffs"
)
// RollupMetric represents the metric names used in rollup records.
type RollupMetric string
const (
MetricResolutionsCount RollupMetric = "resolutions_count"
MetricFirstResponse RollupMetric = "first_response"
MetricResolutionTime RollupMetric = "resolution_time"
MetricReplyTime RollupMetric = "reply_time"
MetricBotResolutionsCount RollupMetric = "bot_resolutions_count"
MetricBotHandoffsCount RollupMetric = "bot_handoffs_count"
)
// ReportingEventsRollup stores pre-aggregated reporting data per day/dimension/metric.
// Reference: Chatwoot ReportingEventsRollup — unique index on (account_id, date, dimension_type, dimension_id, metric).
type ReportingEventsRollup struct {
Base
AccountID uint `gorm:"uniqueIndex:idx_rollup_unique;not null" json:"account_id"`
Date time.Time `gorm:"uniqueIndex:idx_rollup_unique;type:date;not null" json:"date"`
DimensionType DimensionType `gorm:"uniqueIndex:idx_rollup_unique;size:20;not null" json:"dimension_type"`
DimensionID uint `gorm:"uniqueIndex:idx_rollup_unique;not null" json:"dimension_id"`
Metric RollupMetric `gorm:"uniqueIndex:idx_rollup_unique;size:50;not null" json:"metric"`
Count int64 `json:"count"`
SumValue float64 `json:"sum_value"`
SumValueBusinessHours float64 `json:"sum_value_business_hours"`
}
func (ReportingEventsRollup) TableName() string { return "reporting_events_rollups" }