package statistics import ( "fmt" "time" ) // ================ 命令对象 ================ // CreateMetricCommand 创建指标命令 type CreateMetricCommand struct { MetricType string `json:"metric_type" validate:"required" comment:"指标类型"` MetricName string `json:"metric_name" validate:"required" comment:"指标名称"` Dimension string `json:"dimension" comment:"统计维度"` Value float64 `json:"value" validate:"min=0" comment:"指标值"` Metadata string `json:"metadata" comment:"额外维度信息"` Date time.Time `json:"date" validate:"required" comment:"统计日期"` } // UpdateMetricCommand 更新指标命令 type UpdateMetricCommand struct { ID string `json:"id" validate:"required" comment:"指标ID"` Value float64 `json:"value" validate:"min=0" comment:"新指标值"` } // DeleteMetricCommand 删除指标命令 type DeleteMetricCommand struct { ID string `json:"id" validate:"required" comment:"指标ID"` } // GenerateReportCommand 生成报告命令 type GenerateReportCommand struct { ReportType string `json:"report_type" validate:"required" comment:"报告类型"` Title string `json:"title" validate:"required" comment:"报告标题"` Period string `json:"period" validate:"required" comment:"统计周期"` UserRole string `json:"user_role" validate:"required" comment:"用户角色"` StartDate time.Time `json:"start_date" comment:"开始日期"` EndDate time.Time `json:"end_date" comment:"结束日期"` Filters map[string]interface{} `json:"filters" comment:"过滤条件"` GeneratedBy string `json:"generated_by" validate:"required" comment:"生成者ID"` } // CreateDashboardCommand 创建仪表板命令 type CreateDashboardCommand struct { Name string `json:"name" validate:"required" comment:"仪表板名称"` Description string `json:"description" comment:"仪表板描述"` UserRole string `json:"user_role" validate:"required" comment:"用户角色"` Layout string `json:"layout" comment:"布局配置"` Widgets string `json:"widgets" comment:"组件配置"` Settings string `json:"settings" comment:"设置配置"` RefreshInterval int `json:"refresh_interval" validate:"min=30" comment:"刷新间隔(秒)"` AccessLevel string `json:"access_level" comment:"访问级别"` CreatedBy string `json:"created_by" validate:"required" comment:"创建者ID"` } // UpdateDashboardCommand 更新仪表板命令 type UpdateDashboardCommand struct { ID string `json:"id" validate:"required" comment:"仪表板ID"` Name string `json:"name" comment:"仪表板名称"` Description string `json:"description" comment:"仪表板描述"` Layout string `json:"layout" comment:"布局配置"` Widgets string `json:"widgets" comment:"组件配置"` Settings string `json:"settings" comment:"设置配置"` RefreshInterval int `json:"refresh_interval" validate:"min=30" comment:"刷新间隔(秒)"` AccessLevel string `json:"access_level" comment:"访问级别"` UpdatedBy string `json:"updated_by" validate:"required" comment:"更新者ID"` } // SetDefaultDashboardCommand 设置默认仪表板命令 type SetDefaultDashboardCommand struct { DashboardID string `json:"dashboard_id" validate:"required" comment:"仪表板ID"` UserRole string `json:"user_role" validate:"required" comment:"用户角色"` UpdatedBy string `json:"updated_by" validate:"required" comment:"更新者ID"` } // ActivateDashboardCommand 激活仪表板命令 type ActivateDashboardCommand struct { DashboardID string `json:"dashboard_id" validate:"required" comment:"仪表板ID"` ActivatedBy string `json:"activated_by" validate:"required" comment:"激活者ID"` } // DeactivateDashboardCommand 停用仪表板命令 type DeactivateDashboardCommand struct { DashboardID string `json:"dashboard_id" validate:"required" comment:"仪表板ID"` DeactivatedBy string `json:"deactivated_by" validate:"required" comment:"停用者ID"` } // DeleteDashboardCommand 删除仪表板命令 type DeleteDashboardCommand struct { DashboardID string `json:"dashboard_id" validate:"required" comment:"仪表板ID"` DeletedBy string `json:"deleted_by" validate:"required" comment:"删除者ID"` } // ExportDataCommand 导出数据命令 type ExportDataCommand struct { Format string `json:"format" validate:"required" comment:"导出格式"` MetricType string `json:"metric_type" validate:"required" comment:"指标类型"` StartDate time.Time `json:"start_date" validate:"required" comment:"开始日期"` EndDate time.Time `json:"end_date" validate:"required" comment:"结束日期"` Dimension string `json:"dimension" comment:"统计维度"` GroupBy string `json:"group_by" comment:"分组维度"` Filters map[string]interface{} `json:"filters" comment:"过滤条件"` Columns []string `json:"columns" comment:"导出列"` IncludeCharts bool `json:"include_charts" comment:"是否包含图表"` ExportedBy string `json:"exported_by" validate:"required" comment:"导出者ID"` } // TriggerAggregationCommand 触发数据聚合命令 type TriggerAggregationCommand struct { MetricType string `json:"metric_type" validate:"required" comment:"指标类型"` Period string `json:"period" validate:"required" comment:"聚合周期"` StartDate time.Time `json:"start_date" comment:"开始日期"` EndDate time.Time `json:"end_date" comment:"结束日期"` Force bool `json:"force" comment:"是否强制重新聚合"` TriggeredBy string `json:"triggered_by" validate:"required" comment:"触发者ID"` } // Validate 验证触发聚合命令 func (c *TriggerAggregationCommand) Validate() error { if c.MetricType == "" { return fmt.Errorf("指标类型不能为空") } if c.Period == "" { return fmt.Errorf("聚合周期不能为空") } if c.TriggeredBy == "" { return fmt.Errorf("触发者ID不能为空") } // 验证周期类型 validPeriods := []string{"hourly", "daily", "weekly", "monthly"} isValidPeriod := false for _, period := range validPeriods { if c.Period == period { isValidPeriod = true break } } if !isValidPeriod { return fmt.Errorf("不支持的聚合周期: %s", c.Period) } return nil } // ================ 查询对象 ================ // GetMetricsQuery 获取指标查询 type GetMetricsQuery struct { MetricType string `json:"metric_type" form:"metric_type" comment:"指标类型"` MetricName string `json:"metric_name" form:"metric_name" comment:"指标名称"` Dimension string `json:"dimension" form:"dimension" comment:"统计维度"` StartDate time.Time `json:"start_date" form:"start_date" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" comment:"结束日期"` Limit int `json:"limit" form:"limit" comment:"限制数量"` Offset int `json:"offset" form:"offset" comment:"偏移量"` SortBy string `json:"sort_by" form:"sort_by" comment:"排序字段"` SortOrder string `json:"sort_order" form:"sort_order" comment:"排序顺序"` } // GetRealtimeMetricsQuery 获取实时指标查询 type GetRealtimeMetricsQuery struct { MetricType string `json:"metric_type" form:"metric_type" validate:"required" comment:"指标类型"` TimeRange string `json:"time_range" form:"time_range" comment:"时间范围"` Dimension string `json:"dimension" form:"dimension" comment:"统计维度"` } // GetHistoricalMetricsQuery 获取历史指标查询 type GetHistoricalMetricsQuery struct { MetricType string `json:"metric_type" form:"metric_type" validate:"required" comment:"指标类型"` MetricName string `json:"metric_name" form:"metric_name" comment:"指标名称"` Dimension string `json:"dimension" form:"dimension" comment:"统计维度"` StartDate time.Time `json:"start_date" form:"start_date" validate:"required" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" validate:"required" comment:"结束日期"` Period string `json:"period" form:"period" comment:"统计周期"` Limit int `json:"limit" form:"limit" comment:"限制数量"` Offset int `json:"offset" form:"offset" comment:"偏移量"` AggregateBy string `json:"aggregate_by" form:"aggregate_by" comment:"聚合维度"` GroupBy string `json:"group_by" form:"group_by" comment:"分组维度"` } // GetDashboardDataQuery 获取仪表板数据查询 type GetDashboardDataQuery struct { UserRole string `json:"user_role" form:"user_role" validate:"required" comment:"用户角色"` Period string `json:"period" form:"period" comment:"统计周期"` StartDate time.Time `json:"start_date" form:"start_date" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" comment:"结束日期"` MetricTypes []string `json:"metric_types" form:"metric_types" comment:"指标类型列表"` Dimensions []string `json:"dimensions" form:"dimensions" comment:"统计维度列表"` } // GetReportsQuery 获取报告查询 type GetReportsQuery struct { ReportType string `json:"report_type" form:"report_type" comment:"报告类型"` UserRole string `json:"user_role" form:"user_role" comment:"用户角色"` Status string `json:"status" form:"status" comment:"报告状态"` Period string `json:"period" form:"period" comment:"统计周期"` StartDate time.Time `json:"start_date" form:"start_date" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" comment:"结束日期"` Limit int `json:"limit" form:"limit" comment:"限制数量"` Offset int `json:"offset" form:"offset" comment:"偏移量"` SortBy string `json:"sort_by" form:"sort_by" comment:"排序字段"` SortOrder string `json:"sort_order" form:"sort_order" comment:"排序顺序"` GeneratedBy string `json:"generated_by" form:"generated_by" comment:"生成者ID"` } // GetDashboardsQuery 获取仪表板查询 type GetDashboardsQuery struct { UserRole string `json:"user_role" form:"user_role" comment:"用户角色"` IsDefault *bool `json:"is_default" form:"is_default" comment:"是否默认"` IsActive *bool `json:"is_active" form:"is_active" comment:"是否激活"` AccessLevel string `json:"access_level" form:"access_level" comment:"访问级别"` CreatedBy string `json:"created_by" form:"created_by" comment:"创建者ID"` Name string `json:"name" form:"name" comment:"仪表板名称"` Limit int `json:"limit" form:"limit" comment:"限制数量"` Offset int `json:"offset" form:"offset" comment:"偏移量"` SortBy string `json:"sort_by" form:"sort_by" comment:"排序字段"` SortOrder string `json:"sort_order" form:"sort_order" comment:"排序顺序"` } // GetReportQuery 获取单个报告查询 type GetReportQuery struct { ReportID string `json:"report_id" form:"report_id" validate:"required" comment:"报告ID"` } // GetDashboardQuery 获取单个仪表板查询 type GetDashboardQuery struct { DashboardID string `json:"dashboard_id" form:"dashboard_id" validate:"required" comment:"仪表板ID"` } // GetMetricQuery 获取单个指标查询 type GetMetricQuery struct { MetricID string `json:"metric_id" form:"metric_id" validate:"required" comment:"指标ID"` } // CalculateGrowthRateQuery 计算增长率查询 type CalculateGrowthRateQuery struct { MetricType string `json:"metric_type" form:"metric_type" validate:"required" comment:"指标类型"` MetricName string `json:"metric_name" form:"metric_name" validate:"required" comment:"指标名称"` CurrentPeriod time.Time `json:"current_period" form:"current_period" validate:"required" comment:"当前周期"` PreviousPeriod time.Time `json:"previous_period" form:"previous_period" validate:"required" comment:"上一周期"` } // CalculateTrendQuery 计算趋势查询 type CalculateTrendQuery struct { MetricType string `json:"metric_type" form:"metric_type" validate:"required" comment:"指标类型"` MetricName string `json:"metric_name" form:"metric_name" validate:"required" comment:"指标名称"` StartDate time.Time `json:"start_date" form:"start_date" validate:"required" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" validate:"required" comment:"结束日期"` } // CalculateCorrelationQuery 计算相关性查询 type CalculateCorrelationQuery struct { MetricType1 string `json:"metric_type1" form:"metric_type1" validate:"required" comment:"指标类型1"` MetricName1 string `json:"metric_name1" form:"metric_name1" validate:"required" comment:"指标名称1"` MetricType2 string `json:"metric_type2" form:"metric_type2" validate:"required" comment:"指标类型2"` MetricName2 string `json:"metric_name2" form:"metric_name2" validate:"required" comment:"指标名称2"` StartDate time.Time `json:"start_date" form:"start_date" validate:"required" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" validate:"required" comment:"结束日期"` } // CalculateMovingAverageQuery 计算移动平均查询 type CalculateMovingAverageQuery struct { MetricType string `json:"metric_type" form:"metric_type" validate:"required" comment:"指标类型"` MetricName string `json:"metric_name" form:"metric_name" validate:"required" comment:"指标名称"` StartDate time.Time `json:"start_date" form:"start_date" validate:"required" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" validate:"required" comment:"结束日期"` WindowSize int `json:"window_size" form:"window_size" validate:"min=1" comment:"窗口大小"` } // CalculateSeasonalityQuery 计算季节性查询 type CalculateSeasonalityQuery struct { MetricType string `json:"metric_type" form:"metric_type" validate:"required" comment:"指标类型"` MetricName string `json:"metric_name" form:"metric_name" validate:"required" comment:"指标名称"` StartDate time.Time `json:"start_date" form:"start_date" validate:"required" comment:"开始日期"` EndDate time.Time `json:"end_date" form:"end_date" validate:"required" comment:"结束日期"` } // ================ 响应对象 ================ // CommandResponse 命令响应 type CommandResponse struct { Success bool `json:"success" comment:"是否成功"` Message string `json:"message" comment:"响应消息"` Data interface{} `json:"data" comment:"响应数据"` Error string `json:"error,omitempty" comment:"错误信息"` } // QueryResponse 查询响应 type QueryResponse struct { Success bool `json:"success" comment:"是否成功"` Message string `json:"message" comment:"响应消息"` Data interface{} `json:"data" comment:"响应数据"` Meta map[string]interface{} `json:"meta" comment:"元数据"` Error string `json:"error,omitempty" comment:"错误信息"` } // ListResponse 列表响应 type ListResponse struct { Success bool `json:"success" comment:"是否成功"` Message string `json:"message" comment:"响应消息"` Data ListDataDTO `json:"data" comment:"数据列表"` Meta map[string]interface{} `json:"meta" comment:"元数据"` Error string `json:"error,omitempty" comment:"错误信息"` } // ListDataDTO 列表数据DTO type ListDataDTO struct { Total int64 `json:"total" comment:"总数"` Page int `json:"page" comment:"页码"` Size int `json:"size" comment:"每页数量"` Items []interface{} `json:"items" comment:"数据列表"` } // ================ 验证方法 ================ // Validate 验证创建指标命令 func (c *CreateMetricCommand) Validate() error { if c.MetricType == "" { return fmt.Errorf("指标类型不能为空") } if c.MetricName == "" { return fmt.Errorf("指标名称不能为空") } if c.Value < 0 { return fmt.Errorf("指标值不能为负数") } if c.Date.IsZero() { return fmt.Errorf("统计日期不能为空") } return nil } // Validate 验证更新指标命令 func (c *UpdateMetricCommand) Validate() error { if c.ID == "" { return fmt.Errorf("指标ID不能为空") } if c.Value < 0 { return fmt.Errorf("指标值不能为负数") } return nil } // Validate 验证生成报告命令 func (c *GenerateReportCommand) Validate() error { if c.ReportType == "" { return fmt.Errorf("报告类型不能为空") } if c.Title == "" { return fmt.Errorf("报告标题不能为空") } if c.Period == "" { return fmt.Errorf("统计周期不能为空") } if c.UserRole == "" { return fmt.Errorf("用户角色不能为空") } if c.GeneratedBy == "" { return fmt.Errorf("生成者ID不能为空") } return nil } // Validate 验证创建仪表板命令 func (c *CreateDashboardCommand) Validate() error { if c.Name == "" { return fmt.Errorf("仪表板名称不能为空") } if c.UserRole == "" { return fmt.Errorf("用户角色不能为空") } if c.CreatedBy == "" { return fmt.Errorf("创建者ID不能为空") } if c.RefreshInterval < 30 { return fmt.Errorf("刷新间隔不能少于30秒") } return nil } // Validate 验证更新仪表板命令 func (c *UpdateDashboardCommand) Validate() error { if c.ID == "" { return fmt.Errorf("仪表板ID不能为空") } if c.UpdatedBy == "" { return fmt.Errorf("更新者ID不能为空") } if c.RefreshInterval < 30 { return fmt.Errorf("刷新间隔不能少于30秒") } return nil } // Validate 验证导出数据命令 func (c *ExportDataCommand) Validate() error { if c.Format == "" { return fmt.Errorf("导出格式不能为空") } if c.MetricType == "" { return fmt.Errorf("指标类型不能为空") } if c.StartDate.IsZero() || c.EndDate.IsZero() { return fmt.Errorf("开始日期和结束日期不能为空") } if c.StartDate.After(c.EndDate) { return fmt.Errorf("开始日期不能晚于结束日期") } if c.ExportedBy == "" { return fmt.Errorf("导出者ID不能为空") } return nil }