Skip to content

Commit

Permalink
优化数据源查询范围函数以支持自定义时间段
Browse files Browse the repository at this point in the history
对`datasource`包中的查询范围逻辑进行了修改,使其能够处理由`types.Time`对象指定的自定义起始和结束时间。之前,该功能仅支持从当前时间回溯的固定时间段。此次更新允许更灵活的时间段选择,以适应不同的数据查询需求。
  • Loading branch information
aide-cloud committed Sep 26, 2024
1 parent 1f917c0 commit 66048fe
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pkg/houyi/datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/aide-family/moon/pkg/util/types"
"github.com/aide-family/moon/pkg/vobj"
"github.com/aide-family/moon/pkg/watch"

"github.com/go-kratos/kratos/v2/log"
)

Expand Down Expand Up @@ -67,8 +66,10 @@ type EvalFunc func(ctx context.Context, expr string, duration *types.Duration) (
func MetricEval(items ...MetricDatasource) EvalFunc {
return func(ctx context.Context, expr string, duration *types.Duration) (map[watch.Indexer]*Point, error) {
evalRes := make(map[watch.Indexer]*Point)
endAt := time.Now()
startAt := types.NewTime(endAt.Add(-duration.Duration.AsDuration()))
for _, item := range items {
list, err := metricEval(ctx, item, expr, duration)
list, err := metricEval(ctx, item, expr, startAt.Unix(), endAt.Unix())
if err != nil {
log.Warnw("eval", err)
continue
Expand All @@ -81,10 +82,8 @@ func MetricEval(items ...MetricDatasource) EvalFunc {
}
}

func metricEval(ctx context.Context, d MetricDatasource, expr string, duration *types.Duration) (map[watch.Indexer]*Point, error) {
endAt := time.Now()
startAt := types.NewTime(endAt.Add(-duration.Duration.AsDuration()))
queryRange, err := d.QueryRange(ctx, expr, startAt.Unix(), endAt.Unix(), d.Step())
func metricEval(ctx context.Context, d MetricDatasource, expr string, startAt, endAt int64) (map[watch.Indexer]*Point, error) {
queryRange, err := d.QueryRange(ctx, expr, startAt, endAt, d.Step())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 66048fe

Please sign in to comment.