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

64 lines
2.2 KiB
Go

package reporting
import (
"time"
)
// TimeseriesPoint represents a single data point in a timeseries.
type TimeseriesPoint struct {
Timestamp time.Time `json:"timestamp"`
Value float64 `json:"value"`
}
// AggregateResult holds a single aggregate value for a metric.
type AggregateResult struct {
Metric string `json:"metric"`
Value float64 `json:"value"`
}
// SummaryResult holds the summary (totals/averages) for a metric over a range.
type SummaryResult struct {
Metric string `json:"metric"`
Total float64 `json:"total"`
Average float64 `json:"average"`
Count int `json:"count"`
}
// DataSourceParams defines the parameters for a data source query.
// Reference: Chatwoot Reports::DataSource — for(account, metric, dimension_type, dimension_id,
// range, group_by, timezone_offset, business_hours)
type DataSourceParams struct {
AccountID uint
Metric string
DimensionType DimensionType
DimensionID uint
Since time.Time
Until time.Time
GroupBy string // day, week, month
TimezoneOffset int // hours offset from UTC
BusinessHours bool
}
// DataSource is the interface for fetching report data (timeseries, aggregate, summary).
// Reference: Chatwoot Reports::DataSource base class.
type DataSource interface {
// Timeseries returns a time-bucketed series of data points.
Timeseries(params DataSourceParams) ([]TimeseriesPoint, error)
// Aggregate returns a single aggregate value for the metric.
Aggregate(params DataSourceParams) (*AggregateResult, error)
// Summary returns total, average, and count for the metric over the range.
Summary(params DataSourceParams) (*SummaryResult, error)
}
// RawDataSource queries raw reporting_events for timeseries/aggregate/summary.
// Reference: Chatwoot Reports::RawDataSource
type RawDataSource struct{}
// NewRawDataSource creates a new raw data source.
func NewRawDataSource() *RawDataSource {
return &RawDataSource{}
}
// Note: Timeseries, Aggregate, Summary implementations on RawDataSource will be
// wired through ReportingService which holds the DB connection.
// The DataSource interface allows swapping between RawDataSource and RollupDataSource.