Skip to content

Commit

Permalink
feat: es 查询兼容返回数值为空的情况 --story=121063969 (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamcleren authored Dec 5, 2024
1 parent 37c22b7 commit 5ee9a2a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pkg/unify-query/tsdb/elasticsearch/agg_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ func (a *aggFormat) reset() {
a.items = append(a.items, a.item)
}

// 增加值判断,如果返回值为 null 的情况,则认为该值为空点,无需加入到 samples 中
func (a *aggFormat) setMetricValue(v *float64) {
if v == nil {
return
}

a.item.value = *v
a.reset()
}

// idx 是层级信息,默认为 len(a.aggInfoList), 因为聚合结果跟聚合列表是相反的,通过聚合层级递归解析 data 里面的内容
// 例如该查询 sum(count_over_time(metric[1m])) by (dim-1, dim-2) 的聚合层级为:dim-1, dim-2, time range, count
func (a *aggFormat) ts(idx int, data elastic.Aggregations) error {
Expand Down Expand Up @@ -151,44 +161,38 @@ func (a *aggFormat) ts(idx int, data elastic.Aggregations) error {
switch info.FuncType {
case Min:
if valueMetric, ok := data.Min(info.Name); ok && valueMetric != nil {
a.item.value = *valueMetric.Value
a.reset()
a.setMetricValue(valueMetric.Value)
} else {
return fmt.Errorf("%s is empty", info.Name)
}
case Sum:
if valueMetric, ok := data.Sum(info.Name); ok && valueMetric != nil {
a.item.value = *valueMetric.Value
a.reset()
a.setMetricValue(valueMetric.Value)
} else {
return fmt.Errorf("%s is empty", info.Name)
}
case Avg:
if valueMetric, ok := data.Avg(info.Name); ok && valueMetric != nil {
a.item.value = *valueMetric.Value
a.reset()
a.setMetricValue(valueMetric.Value)
} else {
return fmt.Errorf("%s is empty", info.Name)
}
case Cardinality:
if valueMetric, ok := data.Cardinality(info.Name); ok && valueMetric != nil {
a.item.value = *valueMetric.Value
a.reset()
a.setMetricValue(valueMetric.Value)
} else {
return fmt.Errorf("%s is empty", info.Name)
}
case Max:
if valueMetric, ok := data.Max(info.Name); ok && valueMetric != nil {
a.item.value = *valueMetric.Value
a.reset()
a.setMetricValue(valueMetric.Value)
} else {
return fmt.Errorf("%s is empty", info.Name)
}
case Count:
if valueMetric, ok := data.ValueCount(info.Name); ok && valueMetric != nil {
// 计算数量需要造数据
a.item.value = *valueMetric.Value
a.reset()
a.setMetricValue(valueMetric.Value)
} else {
return fmt.Errorf("%s is empty", info.Name)
}
Expand Down
Loading

0 comments on commit 5ee9a2a

Please sign in to comment.