193 lines
3.7 KiB
Go
193 lines
3.7 KiB
Go
package engine
|
|
|
|
import "time"
|
|
|
|
// QueryType 定义查询类型
|
|
type QueryType int
|
|
|
|
const (
|
|
// QueryTypeRaw 原始数据查询
|
|
QueryTypeRaw QueryType = iota
|
|
// QueryTypeAggregate 聚合查询
|
|
QueryTypeAggregate
|
|
// QueryTypeLatest 最新值查询
|
|
QueryTypeLatest
|
|
// QueryTypeTags 标签查询
|
|
QueryTypeTags
|
|
// QueryTypeMetadata 元数据查询
|
|
QueryTypeMetadata
|
|
)
|
|
|
|
// Query 定义查询参数
|
|
type Query struct {
|
|
// 查询类型
|
|
Type QueryType
|
|
|
|
// 时间范围
|
|
StartTime int64
|
|
EndTime int64
|
|
|
|
// 序列标识
|
|
SeriesID string
|
|
DeviceID string
|
|
MetricCode string
|
|
|
|
// 标签过滤
|
|
TagFilters []TagFilter
|
|
|
|
// 聚合选项
|
|
Aggregation AggregationType
|
|
AggInterval time.Duration
|
|
IncludeRawData bool
|
|
|
|
// 结果限制
|
|
Limit int
|
|
Offset int
|
|
|
|
// 其他查询选项
|
|
Options map[string]interface{}
|
|
}
|
|
|
|
// TagFilter 定义标签过滤条件
|
|
type TagFilter struct {
|
|
Key string
|
|
Operator FilterOperator
|
|
Value string
|
|
}
|
|
|
|
// FilterOperator 定义过滤操作符
|
|
type FilterOperator int
|
|
|
|
const (
|
|
// OpEqual 等于
|
|
OpEqual FilterOperator = iota
|
|
// OpNotEqual 不等于
|
|
OpNotEqual
|
|
// OpRegex 正则匹配
|
|
OpRegex
|
|
// OpGreaterThan 大于
|
|
OpGreaterThan
|
|
// OpLessThan 小于
|
|
OpLessThan
|
|
)
|
|
|
|
// AggregationType 定义聚合类型
|
|
type AggregationType int
|
|
|
|
const (
|
|
// AggNone 无聚合
|
|
AggNone AggregationType = iota
|
|
// AggSum 求和
|
|
AggSum
|
|
// AggAvg 平均值
|
|
AggAvg
|
|
// AggMin 最小值
|
|
AggMin
|
|
// AggMax 最大值
|
|
AggMax
|
|
// AggCount 计数
|
|
AggCount
|
|
)
|
|
|
|
// QueryResult 定义查询结果接口
|
|
type QueryResult interface {
|
|
// Type 返回查询结果类型
|
|
Type() QueryType
|
|
}
|
|
|
|
// TimeSeriesResult 定义时间序列查询结果
|
|
type TimeSeriesResult struct {
|
|
SeriesID string
|
|
Points []DataPoint
|
|
}
|
|
|
|
// Type 实现QueryResult接口
|
|
func (r *TimeSeriesResult) Type() QueryType {
|
|
return QueryTypeRaw
|
|
}
|
|
|
|
// AggregateResult 定义聚合查询结果
|
|
type AggregateResult struct {
|
|
SeriesID string
|
|
Groups []AggregateGroup
|
|
}
|
|
|
|
// Type 实现QueryResult接口
|
|
func (r *AggregateResult) Type() QueryType {
|
|
return QueryTypeAggregate
|
|
}
|
|
|
|
// AggregateGroup 定义聚合组
|
|
type AggregateGroup struct {
|
|
StartTime int64
|
|
EndTime int64
|
|
Value float64
|
|
Count int
|
|
}
|
|
|
|
// QueryBuilder 提供流式API构建查询
|
|
type QueryBuilder struct {
|
|
query Query
|
|
}
|
|
|
|
// NewQueryBuilder 创建新的查询构建器
|
|
func NewQueryBuilder() *QueryBuilder {
|
|
return &QueryBuilder{
|
|
query: Query{
|
|
Options: make(map[string]interface{}),
|
|
},
|
|
}
|
|
}
|
|
|
|
// ForMetric 设置指标代码
|
|
func (b *QueryBuilder) ForMetric(metricCode string) *QueryBuilder {
|
|
b.query.MetricCode = metricCode
|
|
return b
|
|
}
|
|
|
|
// WithTimeRange 设置时间范围
|
|
func (b *QueryBuilder) WithTimeRange(start, end int64) *QueryBuilder {
|
|
b.query.StartTime = start
|
|
b.query.EndTime = end
|
|
return b
|
|
}
|
|
|
|
// WithTag 添加标签过滤
|
|
func (b *QueryBuilder) WithTag(key string, op FilterOperator, value string) *QueryBuilder {
|
|
b.query.TagFilters = append(b.query.TagFilters, TagFilter{
|
|
Key: key,
|
|
Operator: op,
|
|
Value: value,
|
|
})
|
|
return b
|
|
}
|
|
|
|
// WithAggregation 设置聚合选项
|
|
func (b *QueryBuilder) WithAggregation(agg AggregationType, interval time.Duration) *QueryBuilder {
|
|
b.query.Type = QueryTypeAggregate
|
|
b.query.Aggregation = agg
|
|
b.query.AggInterval = interval
|
|
return b
|
|
}
|
|
|
|
// WithLimit 设置结果限制
|
|
func (b *QueryBuilder) WithLimit(limit int) *QueryBuilder {
|
|
b.query.Limit = limit
|
|
return b
|
|
}
|
|
|
|
// WithOffset 设置结果偏移
|
|
func (b *QueryBuilder) WithOffset(offset int) *QueryBuilder {
|
|
b.query.Offset = offset
|
|
return b
|
|
}
|
|
|
|
// Build 构建查询对象
|
|
func (b *QueryBuilder) Build() Query {
|
|
// 如果没有设置查询类型,默认为原始数据查询
|
|
if b.query.Type == 0 {
|
|
b.query.Type = QueryTypeRaw
|
|
}
|
|
return b.query
|
|
}
|