adjust the grouping structure

This commit is contained in:
liugq 2023-06-12 17:09:20 +08:00
parent 2a8c16c421
commit edab287f40
3 changed files with 18 additions and 20 deletions

View File

@ -70,11 +70,11 @@ type MetricItem struct {
type MetricDataItem struct { type MetricDataItem struct {
Timestamp interface{} `json:"timestamp,omitempty"` Timestamp interface{} `json:"timestamp,omitempty"`
Value interface{} `json:"value"` Value interface{} `json:"value"`
Group string `json:"group,omitempty"` Groups []string `json:"groups,omitempty"`
} }
type MetricData struct { type MetricData struct {
Group string `json:"group,omitempty"` Groups []string `json:"groups,omitempty"`
Data map[string][]MetricDataItem Data map[string][]MetricDataItem
} }

View File

@ -224,7 +224,7 @@ func getMetricData(metric *insight.Metric) (interface{}, error) {
}else { }else {
for _, md := range metricData { for _, md := range metricData {
targetData := insight.MetricData{ targetData := insight.MetricData{
Group: md.Group, Groups: md.Groups,
Data: map[string][]insight.MetricDataItem{}, Data: map[string][]insight.MetricDataItem{},
} }
expression, err := govaluate.NewEvaluableExpression(formula) expression, err := govaluate.NewEvaluableExpression(formula)
@ -281,7 +281,7 @@ func getMetricData(metric *insight.Metric) (interface{}, error) {
for _, md := range targetMetricData { for _, md := range targetMetricData {
for _, v := range md.Data { for _, v := range md.Data {
for _, mitem := range v { for _, mitem := range v {
mitem.Group = md.Group mitem.Groups = md.Groups
result = append(result, mitem) result = append(result, mitem)
} }
} }

View File

@ -196,21 +196,21 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) {
func CollectMetricData(agg interface{}, timeBeforeGroup bool) []insight.MetricData { func CollectMetricData(agg interface{}, timeBeforeGroup bool) []insight.MetricData {
metricData := []insight.MetricData{} metricData := []insight.MetricData{}
if timeBeforeGroup { if timeBeforeGroup {
collectMetricDataOther(agg, "", &metricData, nil) collectMetricDataOther(agg, nil, &metricData, nil)
} else { } else {
collectMetricData(agg, "", &metricData) collectMetricData(agg, nil, &metricData)
} }
return metricData return metricData
} }
// timeBeforeGroup => false // timeBeforeGroup => false
func collectMetricData(agg interface{}, groupValues string, metricData *[]insight.MetricData) { func collectMetricData(agg interface{}, groupValues []string, metricData *[]insight.MetricData) {
if aggM, ok := agg.(map[string]interface{}); ok { if aggM, ok := agg.(map[string]interface{}); ok {
if timeBks, ok := aggM["time_buckets"].(map[string]interface{}); ok { if timeBks, ok := aggM["time_buckets"].(map[string]interface{}); ok {
if bks, ok := timeBks["buckets"].([]interface{}); ok { if bks, ok := timeBks["buckets"].([]interface{}); ok {
md := insight.MetricData{ md := insight.MetricData{
Data: map[string][]insight.MetricDataItem{}, Data: map[string][]insight.MetricDataItem{},
Group: groupValues, Groups: groupValues,
} }
for _, bk := range bks { for _, bk := range bks {
if bkM, ok := bk.(map[string]interface{}); ok { if bkM, ok := bk.(map[string]interface{}); ok {
@ -234,7 +234,7 @@ func collectMetricData(agg interface{}, groupValues string, metricData *[]insigh
} else { } else {
md := insight.MetricData{ md := insight.MetricData{
Data: map[string][]insight.MetricDataItem{}, Data: map[string][]insight.MetricDataItem{},
Group: groupValues, Groups: groupValues,
} }
for k, v := range aggM { for k, v := range aggM {
if k == "key" || k == "doc_count" { if k == "key" || k == "doc_count" {
@ -245,10 +245,9 @@ func collectMetricData(agg interface{}, groupValues string, metricData *[]insigh
for _, bk := range bks { for _, bk := range bks {
if bkVal, ok := bk.(map[string]interface{}); ok { if bkVal, ok := bk.(map[string]interface{}); ok {
var currentGroup = fmt.Sprintf("%v", bkVal["key"]) var currentGroup = fmt.Sprintf("%v", bkVal["key"])
newGroupValues := currentGroup newGroupValues := make([]string, 0, len(groupValues)+1)
if groupValues != "" { newGroupValues = append(newGroupValues, groupValues...)
newGroupValues = fmt.Sprintf("%s-%s", groupValues, currentGroup) newGroupValues = append(newGroupValues, currentGroup)
}
collectMetricData(bk, newGroupValues, metricData) collectMetricData(bk, newGroupValues, metricData)
} }
} }
@ -268,13 +267,13 @@ func collectMetricData(agg interface{}, groupValues string, metricData *[]insigh
} }
// timeBeforeGroup => true // timeBeforeGroup => true
func collectMetricDataOther(agg interface{}, groupValues string, metricData *[]insight.MetricData, timeKey interface{}) { func collectMetricDataOther(agg interface{}, groupValues []string, metricData *[]insight.MetricData, timeKey interface{}) {
if aggM, ok := agg.(map[string]interface{}); ok { if aggM, ok := agg.(map[string]interface{}); ok {
if timeBks, ok := aggM["time_buckets"].(map[string]interface{}); ok { if timeBks, ok := aggM["time_buckets"].(map[string]interface{}); ok {
if bks, ok := timeBks["buckets"].([]interface{}); ok { if bks, ok := timeBks["buckets"].([]interface{}); ok {
md := insight.MetricData{ md := insight.MetricData{
Data: map[string][]insight.MetricDataItem{}, Data: map[string][]insight.MetricDataItem{},
Group: groupValues, Groups: groupValues,
} }
for _, bk := range bks { for _, bk := range bks {
if bkM, ok := bk.(map[string]interface{}); ok { if bkM, ok := bk.(map[string]interface{}); ok {
@ -299,17 +298,16 @@ func collectMetricDataOther(agg interface{}, groupValues string, metricData *[]i
} else { } else {
md := insight.MetricData{ md := insight.MetricData{
Data: map[string][]insight.MetricDataItem{}, Data: map[string][]insight.MetricDataItem{},
Group: groupValues, Groups: groupValues,
} }
if bks, ok := aggM["buckets"].([]interface{}); ok { if bks, ok := aggM["buckets"].([]interface{}); ok {
for _, bk := range bks { for _, bk := range bks {
if bkVal, ok := bk.(map[string]interface{}); ok { if bkVal, ok := bk.(map[string]interface{}); ok {
currentGroup := util.ToString(bkVal["key"]) currentGroup := util.ToString(bkVal["key"])
newGroupValues := currentGroup newGroupValues := make([]string, 0, len(groupValues)+1)
if groupValues != "" { newGroupValues = append(newGroupValues, groupValues...)
newGroupValues = fmt.Sprintf("%s-%s", groupValues, currentGroup) newGroupValues = append(newGroupValues, currentGroup)
}
collectMetricDataOther(bk, newGroupValues, metricData, timeKey) collectMetricDataOther(bk, newGroupValues, metricData, timeKey)
} }
} }