573 lines
16 KiB
Go
573 lines
16 KiB
Go
package events
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// StatisticsEventType 统计事件类型
|
|
type StatisticsEventType string
|
|
|
|
const (
|
|
// 指标相关事件
|
|
MetricCreatedEventType StatisticsEventType = "statistics.metric.created"
|
|
MetricUpdatedEventType StatisticsEventType = "statistics.metric.updated"
|
|
MetricAggregatedEventType StatisticsEventType = "statistics.metric.aggregated"
|
|
|
|
// 报告相关事件
|
|
ReportCreatedEventType StatisticsEventType = "statistics.report.created"
|
|
ReportGenerationStartedEventType StatisticsEventType = "statistics.report.generation_started"
|
|
ReportCompletedEventType StatisticsEventType = "statistics.report.completed"
|
|
ReportFailedEventType StatisticsEventType = "statistics.report.failed"
|
|
ReportExpiredEventType StatisticsEventType = "statistics.report.expired"
|
|
|
|
// 仪表板相关事件
|
|
DashboardCreatedEventType StatisticsEventType = "statistics.dashboard.created"
|
|
DashboardUpdatedEventType StatisticsEventType = "statistics.dashboard.updated"
|
|
DashboardActivatedEventType StatisticsEventType = "statistics.dashboard.activated"
|
|
DashboardDeactivatedEventType StatisticsEventType = "statistics.dashboard.deactivated"
|
|
)
|
|
|
|
// BaseStatisticsEvent 统计事件基础结构
|
|
type BaseStatisticsEvent struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Version string `json:"version"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
Source string `json:"source"`
|
|
AggregateID string `json:"aggregate_id"`
|
|
AggregateType string `json:"aggregate_type"`
|
|
Metadata map[string]interface{} `json:"metadata"`
|
|
Payload interface{} `json:"payload"`
|
|
|
|
// DDD特有字段
|
|
DomainVersion string `json:"domain_version"`
|
|
CausationID string `json:"causation_id"`
|
|
CorrelationID string `json:"correlation_id"`
|
|
}
|
|
|
|
// 实现 Event 接口
|
|
func (e *BaseStatisticsEvent) GetID() string {
|
|
return e.ID
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetType() string {
|
|
return e.Type
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetVersion() string {
|
|
return e.Version
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetTimestamp() time.Time {
|
|
return e.Timestamp
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetPayload() interface{} {
|
|
return e.Payload
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetMetadata() map[string]interface{} {
|
|
return e.Metadata
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetSource() string {
|
|
return e.Source
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetAggregateID() string {
|
|
return e.AggregateID
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetAggregateType() string {
|
|
return e.AggregateType
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetDomainVersion() string {
|
|
return e.DomainVersion
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetCausationID() string {
|
|
return e.CausationID
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) GetCorrelationID() string {
|
|
return e.CorrelationID
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) Marshal() ([]byte, error) {
|
|
return json.Marshal(e)
|
|
}
|
|
|
|
func (e *BaseStatisticsEvent) Unmarshal(data []byte) error {
|
|
return json.Unmarshal(data, e)
|
|
}
|
|
|
|
// ================ 指标相关事件 ================
|
|
|
|
// MetricCreatedEvent 指标创建事件
|
|
type MetricCreatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
MetricID string `json:"metric_id"`
|
|
MetricType string `json:"metric_type"`
|
|
MetricName string `json:"metric_name"`
|
|
Value float64 `json:"value"`
|
|
Dimension string `json:"dimension"`
|
|
Date time.Time `json:"date"`
|
|
}
|
|
|
|
func NewMetricCreatedEvent(metricID, metricType, metricName, dimension string, value float64, date time.Time, correlationID string) *MetricCreatedEvent {
|
|
return &MetricCreatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(MetricCreatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: metricID,
|
|
AggregateType: "StatisticsMetric",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"metric_id": metricID,
|
|
"metric_type": metricType,
|
|
"metric_name": metricName,
|
|
"dimension": dimension,
|
|
},
|
|
},
|
|
MetricID: metricID,
|
|
MetricType: metricType,
|
|
MetricName: metricName,
|
|
Value: value,
|
|
Dimension: dimension,
|
|
Date: date,
|
|
}
|
|
}
|
|
|
|
func (e *MetricCreatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"metric_id": e.MetricID,
|
|
"metric_type": e.MetricType,
|
|
"metric_name": e.MetricName,
|
|
"value": e.Value,
|
|
"dimension": e.Dimension,
|
|
"date": e.Date,
|
|
}
|
|
}
|
|
|
|
// MetricUpdatedEvent 指标更新事件
|
|
type MetricUpdatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
MetricID string `json:"metric_id"`
|
|
OldValue float64 `json:"old_value"`
|
|
NewValue float64 `json:"new_value"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func NewMetricUpdatedEvent(metricID string, oldValue, newValue float64, correlationID string) *MetricUpdatedEvent {
|
|
return &MetricUpdatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(MetricUpdatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: metricID,
|
|
AggregateType: "StatisticsMetric",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"metric_id": metricID,
|
|
},
|
|
},
|
|
MetricID: metricID,
|
|
OldValue: oldValue,
|
|
NewValue: newValue,
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (e *MetricUpdatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"metric_id": e.MetricID,
|
|
"old_value": e.OldValue,
|
|
"new_value": e.NewValue,
|
|
"updated_at": e.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
// MetricAggregatedEvent 指标聚合事件
|
|
type MetricAggregatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
MetricType string `json:"metric_type"`
|
|
Dimension string `json:"dimension"`
|
|
AggregatedAt time.Time `json:"aggregated_at"`
|
|
RecordCount int `json:"record_count"`
|
|
TotalValue float64 `json:"total_value"`
|
|
}
|
|
|
|
func NewMetricAggregatedEvent(metricType, dimension string, recordCount int, totalValue float64, correlationID string) *MetricAggregatedEvent {
|
|
return &MetricAggregatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(MetricAggregatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: uuid.New().String(),
|
|
AggregateType: "StatisticsMetric",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"metric_type": metricType,
|
|
"dimension": dimension,
|
|
},
|
|
},
|
|
MetricType: metricType,
|
|
Dimension: dimension,
|
|
AggregatedAt: time.Now(),
|
|
RecordCount: recordCount,
|
|
TotalValue: totalValue,
|
|
}
|
|
}
|
|
|
|
func (e *MetricAggregatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"metric_type": e.MetricType,
|
|
"dimension": e.Dimension,
|
|
"aggregated_at": e.AggregatedAt,
|
|
"record_count": e.RecordCount,
|
|
"total_value": e.TotalValue,
|
|
}
|
|
}
|
|
|
|
// ================ 报告相关事件 ================
|
|
|
|
// ReportCreatedEvent 报告创建事件
|
|
type ReportCreatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
ReportID string `json:"report_id"`
|
|
ReportType string `json:"report_type"`
|
|
Title string `json:"title"`
|
|
Period string `json:"period"`
|
|
UserRole string `json:"user_role"`
|
|
}
|
|
|
|
func NewReportCreatedEvent(reportID, reportType, title, period, userRole, correlationID string) *ReportCreatedEvent {
|
|
return &ReportCreatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(ReportCreatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: reportID,
|
|
AggregateType: "StatisticsReport",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"report_id": reportID,
|
|
"report_type": reportType,
|
|
"user_role": userRole,
|
|
},
|
|
},
|
|
ReportID: reportID,
|
|
ReportType: reportType,
|
|
Title: title,
|
|
Period: period,
|
|
UserRole: userRole,
|
|
}
|
|
}
|
|
|
|
func (e *ReportCreatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"report_id": e.ReportID,
|
|
"report_type": e.ReportType,
|
|
"title": e.Title,
|
|
"period": e.Period,
|
|
"user_role": e.UserRole,
|
|
}
|
|
}
|
|
|
|
// ReportGenerationStartedEvent 报告生成开始事件
|
|
type ReportGenerationStartedEvent struct {
|
|
*BaseStatisticsEvent
|
|
ReportID string `json:"report_id"`
|
|
GeneratedBy string `json:"generated_by"`
|
|
StartedAt time.Time `json:"started_at"`
|
|
}
|
|
|
|
func NewReportGenerationStartedEvent(reportID, generatedBy, correlationID string) *ReportGenerationStartedEvent {
|
|
return &ReportGenerationStartedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(ReportGenerationStartedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: reportID,
|
|
AggregateType: "StatisticsReport",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"report_id": reportID,
|
|
"generated_by": generatedBy,
|
|
},
|
|
},
|
|
ReportID: reportID,
|
|
GeneratedBy: generatedBy,
|
|
StartedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (e *ReportGenerationStartedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"report_id": e.ReportID,
|
|
"generated_by": e.GeneratedBy,
|
|
"started_at": e.StartedAt,
|
|
}
|
|
}
|
|
|
|
// ReportCompletedEvent 报告完成事件
|
|
type ReportCompletedEvent struct {
|
|
*BaseStatisticsEvent
|
|
ReportID string `json:"report_id"`
|
|
CompletedAt time.Time `json:"completed_at"`
|
|
ContentSize int `json:"content_size"`
|
|
}
|
|
|
|
func NewReportCompletedEvent(reportID string, contentSize int, correlationID string) *ReportCompletedEvent {
|
|
return &ReportCompletedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(ReportCompletedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: reportID,
|
|
AggregateType: "StatisticsReport",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"report_id": reportID,
|
|
},
|
|
},
|
|
ReportID: reportID,
|
|
CompletedAt: time.Now(),
|
|
ContentSize: contentSize,
|
|
}
|
|
}
|
|
|
|
func (e *ReportCompletedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"report_id": e.ReportID,
|
|
"completed_at": e.CompletedAt,
|
|
"content_size": e.ContentSize,
|
|
}
|
|
}
|
|
|
|
// ReportFailedEvent 报告失败事件
|
|
type ReportFailedEvent struct {
|
|
*BaseStatisticsEvent
|
|
ReportID string `json:"report_id"`
|
|
Reason string `json:"reason"`
|
|
FailedAt time.Time `json:"failed_at"`
|
|
}
|
|
|
|
func NewReportFailedEvent(reportID, reason, correlationID string) *ReportFailedEvent {
|
|
return &ReportFailedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(ReportFailedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: reportID,
|
|
AggregateType: "StatisticsReport",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"report_id": reportID,
|
|
},
|
|
},
|
|
ReportID: reportID,
|
|
Reason: reason,
|
|
FailedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (e *ReportFailedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"report_id": e.ReportID,
|
|
"reason": e.Reason,
|
|
"failed_at": e.FailedAt,
|
|
}
|
|
}
|
|
|
|
// ================ 仪表板相关事件 ================
|
|
|
|
// DashboardCreatedEvent 仪表板创建事件
|
|
type DashboardCreatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
DashboardID string `json:"dashboard_id"`
|
|
Name string `json:"name"`
|
|
UserRole string `json:"user_role"`
|
|
CreatedBy string `json:"created_by"`
|
|
}
|
|
|
|
func NewDashboardCreatedEvent(dashboardID, name, userRole, createdBy, correlationID string) *DashboardCreatedEvent {
|
|
return &DashboardCreatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(DashboardCreatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: dashboardID,
|
|
AggregateType: "StatisticsDashboard",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"dashboard_id": dashboardID,
|
|
"user_role": userRole,
|
|
"created_by": createdBy,
|
|
},
|
|
},
|
|
DashboardID: dashboardID,
|
|
Name: name,
|
|
UserRole: userRole,
|
|
CreatedBy: createdBy,
|
|
}
|
|
}
|
|
|
|
func (e *DashboardCreatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"dashboard_id": e.DashboardID,
|
|
"name": e.Name,
|
|
"user_role": e.UserRole,
|
|
"created_by": e.CreatedBy,
|
|
}
|
|
}
|
|
|
|
// DashboardUpdatedEvent 仪表板更新事件
|
|
type DashboardUpdatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
DashboardID string `json:"dashboard_id"`
|
|
UpdatedBy string `json:"updated_by"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
Changes map[string]interface{} `json:"changes"`
|
|
}
|
|
|
|
func NewDashboardUpdatedEvent(dashboardID, updatedBy string, changes map[string]interface{}, correlationID string) *DashboardUpdatedEvent {
|
|
return &DashboardUpdatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(DashboardUpdatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: dashboardID,
|
|
AggregateType: "StatisticsDashboard",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"dashboard_id": dashboardID,
|
|
"updated_by": updatedBy,
|
|
},
|
|
},
|
|
DashboardID: dashboardID,
|
|
UpdatedBy: updatedBy,
|
|
UpdatedAt: time.Now(),
|
|
Changes: changes,
|
|
}
|
|
}
|
|
|
|
func (e *DashboardUpdatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"dashboard_id": e.DashboardID,
|
|
"updated_by": e.UpdatedBy,
|
|
"updated_at": e.UpdatedAt,
|
|
"changes": e.Changes,
|
|
}
|
|
}
|
|
|
|
// DashboardActivatedEvent 仪表板激活事件
|
|
type DashboardActivatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
DashboardID string `json:"dashboard_id"`
|
|
ActivatedBy string `json:"activated_by"`
|
|
ActivatedAt time.Time `json:"activated_at"`
|
|
}
|
|
|
|
func NewDashboardActivatedEvent(dashboardID, activatedBy, correlationID string) *DashboardActivatedEvent {
|
|
return &DashboardActivatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(DashboardActivatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: dashboardID,
|
|
AggregateType: "StatisticsDashboard",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"dashboard_id": dashboardID,
|
|
"activated_by": activatedBy,
|
|
},
|
|
},
|
|
DashboardID: dashboardID,
|
|
ActivatedBy: activatedBy,
|
|
ActivatedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (e *DashboardActivatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"dashboard_id": e.DashboardID,
|
|
"activated_by": e.ActivatedBy,
|
|
"activated_at": e.ActivatedAt,
|
|
}
|
|
}
|
|
|
|
// DashboardDeactivatedEvent 仪表板停用事件
|
|
type DashboardDeactivatedEvent struct {
|
|
*BaseStatisticsEvent
|
|
DashboardID string `json:"dashboard_id"`
|
|
DeactivatedBy string `json:"deactivated_by"`
|
|
DeactivatedAt time.Time `json:"deactivated_at"`
|
|
}
|
|
|
|
func NewDashboardDeactivatedEvent(dashboardID, deactivatedBy, correlationID string) *DashboardDeactivatedEvent {
|
|
return &DashboardDeactivatedEvent{
|
|
BaseStatisticsEvent: &BaseStatisticsEvent{
|
|
ID: uuid.New().String(),
|
|
Type: string(DashboardDeactivatedEventType),
|
|
Version: "1.0",
|
|
Timestamp: time.Now(),
|
|
Source: "statistics-service",
|
|
AggregateID: dashboardID,
|
|
AggregateType: "StatisticsDashboard",
|
|
DomainVersion: "1.0",
|
|
CorrelationID: correlationID,
|
|
Metadata: map[string]interface{}{
|
|
"dashboard_id": dashboardID,
|
|
"deactivated_by": deactivatedBy,
|
|
},
|
|
},
|
|
DashboardID: dashboardID,
|
|
DeactivatedBy: deactivatedBy,
|
|
DeactivatedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (e *DashboardDeactivatedEvent) GetPayload() interface{} {
|
|
return map[string]interface{}{
|
|
"dashboard_id": e.DashboardID,
|
|
"deactivated_by": e.DeactivatedBy,
|
|
"deactivated_at": e.DeactivatedAt,
|
|
}
|
|
}
|