feat(sla): make sla breach events idempotent
This commit is contained in:
@@ -12,22 +12,22 @@ import (
|
||||
// Fields: first_response_time_threshold (FRT), next_response_time_threshold (NRT),
|
||||
// resolution_time_threshold (RT), only_during_business_hours
|
||||
type SlaPolicy struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID uint `gorm:"not null;index" json:"account_id"`
|
||||
Name string `gorm:"size:255;not null" json:"name"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
FirstResponseTimeThreshold int `gorm:"default:0" json:"first_response_time_threshold"` // FRT in minutes
|
||||
NextResponseTimeThreshold int `gorm:"default:0" json:"next_response_time_threshold"` // NRT in minutes
|
||||
ResolutionTimeThreshold int `gorm:"default:0" json:"resolution_time_threshold"` // RT in minutes
|
||||
OnlyDuringBusinessHours bool `gorm:"default:false" json:"only_during_business_hours"`
|
||||
PausedAt *time.Time `gorm:"index" json:"paused_at,omitempty"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AccountID uint `gorm:"not null;index" json:"account_id"`
|
||||
Name string `gorm:"size:255;not null" json:"name"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
FirstResponseTimeThreshold int `gorm:"default:0" json:"first_response_time_threshold"` // FRT in seconds
|
||||
NextResponseTimeThreshold int `gorm:"default:0" json:"next_response_time_threshold"` // NRT in seconds
|
||||
ResolutionTimeThreshold int `gorm:"default:0" json:"resolution_time_threshold"` // RT in seconds
|
||||
OnlyDuringBusinessHours bool `gorm:"default:false" json:"only_during_business_hours"`
|
||||
PausedAt *time.Time `gorm:"index" json:"paused_at,omitempty"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
|
||||
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
|
||||
AppliedSLAs []AppliedSLA `gorm:"foreignKey:SlaPolicyID" json:"applied_slas,omitempty"`
|
||||
SlaPolicyInboxes []SlaPolicyInbox `gorm:"foreignKey:SlaPolicyID" json:"inboxes,omitempty"`
|
||||
Account Account `gorm:"foreignKey:AccountID" json:"account,omitempty"`
|
||||
AppliedSLAs []AppliedSLA `gorm:"foreignKey:SlaPolicyID" json:"applied_slas,omitempty"`
|
||||
SlaPolicyInboxes []SlaPolicyInbox `gorm:"foreignKey:SlaPolicyID" json:"inboxes,omitempty"`
|
||||
}
|
||||
|
||||
func (SlaPolicy) TableName() string { return "sla_policies" }
|
||||
@@ -54,26 +54,26 @@ func (SlaPolicyInbox) TableName() string { return "sla_policy_inboxes" }
|
||||
// Reference: Chatwoot app/models/applied_sla.rb
|
||||
// Tracks SLA status, target timestamps (FRT/NRT/RT), and actual completion times.
|
||||
type AppliedSLA struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
SlaPolicyID uint `gorm:"not null;index" json:"sla_policy_id"`
|
||||
ConversationID uint `gorm:"not null;index" json:"conversation_id"`
|
||||
AccountID uint `gorm:"not null;index" json:"account_id"`
|
||||
SLAStatus SLAStatus `gorm:"size:50;not null;default:active" json:"sla_status"`
|
||||
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
SlaPolicyID uint `gorm:"not null;index" json:"sla_policy_id"`
|
||||
ConversationID uint `gorm:"not null;index" json:"conversation_id"`
|
||||
AccountID uint `gorm:"not null;index" json:"account_id"`
|
||||
SLAStatus SLAStatus `gorm:"size:50;not null;default:active" json:"sla_status"`
|
||||
// Target timestamps for FRT/NRT/RT (computed from policy + created_at)
|
||||
FRTTargetAt *time.Time `gorm:"index" json:"frt_target_at,omitempty"`
|
||||
NRTTargetAt *time.Time `gorm:"index" json:"nrt_target_at,omitempty"`
|
||||
RTTargetAt *time.Time `gorm:"index" json:"rt_target_at,omitempty"`
|
||||
FRTTargetAt *time.Time `gorm:"index" json:"frt_target_at,omitempty"`
|
||||
NRTTargetAt *time.Time `gorm:"index" json:"nrt_target_at,omitempty"`
|
||||
RTTargetAt *time.Time `gorm:"index" json:"rt_target_at,omitempty"`
|
||||
// Actual timestamps when targets were hit or violated
|
||||
FRTActualAt *time.Time `json:"frt_actual_at,omitempty"`
|
||||
NRTActualAt *time.Time `json:"nrt_actual_at,omitempty"`
|
||||
RTActualAt *time.Time `json:"rt_actual_at,omitempty"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
FRTActualAt *time.Time `json:"frt_actual_at,omitempty"`
|
||||
NRTActualAt *time.Time `json:"nrt_actual_at,omitempty"`
|
||||
RTActualAt *time.Time `json:"rt_actual_at,omitempty"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
|
||||
SlaPolicy SlaPolicy `gorm:"foreignKey:SlaPolicyID" json:"sla_policy,omitempty"`
|
||||
Conversation Conversation `gorm:"foreignKey:ConversationID" json:"conversation,omitempty"`
|
||||
SlaEvents []SlaEvent `gorm:"foreignKey:AppliedSlaID" json:"sla_events,omitempty"`
|
||||
SlaPolicy SlaPolicy `gorm:"foreignKey:SlaPolicyID" json:"sla_policy,omitempty"`
|
||||
Conversation Conversation `gorm:"foreignKey:ConversationID" json:"conversation,omitempty"`
|
||||
SlaEvents []SlaEvent `gorm:"foreignKey:AppliedSlaID" json:"sla_events,omitempty"`
|
||||
}
|
||||
|
||||
func (AppliedSLA) TableName() string { return "applied_slas" }
|
||||
@@ -82,20 +82,20 @@ func (AppliedSLA) TableName() string { return "applied_slas" }
|
||||
// Reference: Chatwoot app/models/sla_event.rb
|
||||
// Fields: event_type (frt/nrt/rt), meta (jsonb), account_id, conversation_id, inbox_id, sla_policy_id
|
||||
type SlaEvent struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AppliedSlaID uint `gorm:"not null;index" json:"applied_sla_id"`
|
||||
AccountID uint `gorm:"not null;index" json:"account_id"`
|
||||
ConversationID uint `gorm:"not null;index" json:"conversation_id"`
|
||||
InboxID uint `gorm:"not null;index" json:"inbox_id"`
|
||||
SlaPolicyID uint `gorm:"not null;index" json:"sla_policy_id"`
|
||||
EventType SLAEventType `gorm:"size:50;not null" json:"event_type"`
|
||||
Meta datatypes.JSON `gorm:"type:jsonb" json:"meta,omitempty"` // e.g. {"message_id": 123} for NRT events
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
ID uint `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
AppliedSlaID uint `gorm:"not null;index" json:"applied_sla_id"`
|
||||
AccountID uint `gorm:"not null;index" json:"account_id"`
|
||||
ConversationID uint `gorm:"not null;index" json:"conversation_id"`
|
||||
InboxID uint `gorm:"not null;index" json:"inbox_id"`
|
||||
SlaPolicyID uint `gorm:"not null;index" json:"sla_policy_id"`
|
||||
EventType SLAEventType `gorm:"size:50;not null" json:"event_type"`
|
||||
Meta datatypes.JSON `gorm:"type:jsonb" json:"meta,omitempty"` // e.g. {"message_id": 123} for NRT events
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
|
||||
AppliedSLA AppliedSLA `gorm:"foreignKey:AppliedSlaID" json:"applied_sla,omitempty"`
|
||||
Conversation Conversation `gorm:"foreignKey:ConversationID" json:"conversation,omitempty"`
|
||||
AppliedSLA AppliedSLA `gorm:"foreignKey:AppliedSlaID" json:"applied_sla,omitempty"`
|
||||
Conversation Conversation `gorm:"foreignKey:ConversationID" json:"conversation,omitempty"`
|
||||
}
|
||||
|
||||
func (SlaEvent) TableName() string { return "sla_events" }
|
||||
|
||||
Reference in New Issue
Block a user