Skip to content

Commit 802f0a5

Browse files
committed
feat: flow graph can show multiple lines
1 parent 60f94fc commit 802f0a5

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

pkg/sls-plugin.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -377,22 +377,27 @@ func (ds *SlsDatasource) BuildFlowGraphV2(logs []map[string]string, xcol string,
377377
return
378378
}
379379
ds.SortLogs(logs, xcol)
380-
metricName := ycols[1]
380+
metricNames := strings.Split(ycols[1], ",")
381+
metricMap := make(map[string]bool)
382+
for _, n := range metricNames {
383+
metricMap[n] = true
384+
}
381385
var labelNames []string
382386
for k := range logs[0] {
383-
if k != "__source__" && k != "__time__" && k != metricName && k != xcol {
387+
if k != "__source__" && k != "__time__" && !metricMap[k] && k != xcol {
384388
labelNames = append(labelNames, k)
385389
}
386390
}
387391
sort.Strings(labelNames)
388392
timeFields := make(map[string][]time.Time)
389-
metricFields := make(map[string][]float64)
390-
393+
nameMetricFields := make(map[string]map[string][]float64)
394+
for _, n := range metricNames {
395+
nameMetricFields[n] = make(map[string][]float64)
396+
}
391397
frameLabelsMap := make(map[string]map[string]string)
392398

393399
for _, alog := range logs {
394400
timeVal := alog[xcol]
395-
metricVal := alog[metricName]
396401
labels := map[string]string{}
397402
labelsKey := ""
398403
for _, l := range labelNames {
@@ -407,20 +412,29 @@ func (ds *SlsDatasource) BuildFlowGraphV2(logs []map[string]string, xcol string,
407412
} else {
408413
timeFields[labelsKey] = []time.Time{toTime(timeVal)}
409414
}
410-
floatV, err := strconv.ParseFloat(metricVal, 64)
411-
if err != nil {
412-
log.DefaultLogger.Info("BuildFlowGraphV2", "ParseFloat", err, "value", metricVal)
413-
}
414-
if _, ok := metricFields[labelsKey]; ok {
415-
metricFields[labelsKey] = append(metricFields[labelsKey], floatV)
416-
} else {
417-
metricFields[labelsKey] = []float64{floatV}
415+
for _, n := range metricNames {
416+
metricVal := alog[n]
417+
floatV, err := strconv.ParseFloat(metricVal, 64)
418+
if err != nil {
419+
log.DefaultLogger.Info("BuildFlowGraphV2", "ParseFloat", err, "value", metricVal)
420+
}
421+
if _, ok := nameMetricFields[n][labelsKey]; ok {
422+
nameMetricFields[n][labelsKey] = append(nameMetricFields[n][labelsKey], floatV)
423+
} else {
424+
nameMetricFields[n][labelsKey] = []float64{floatV}
425+
}
418426
}
419427
}
420428
for k, v := range timeFields {
421429
frame := data.NewFrame("")
422430
frame.Fields = append(frame.Fields, data.NewField("Time", nil, v))
423-
frame.Fields = append(frame.Fields, data.NewField("Value", frameLabelsMap[k], metricFields[k]))
431+
if len(metricNames) == 1 {
432+
frame.Fields = append(frame.Fields, data.NewField("Value", frameLabelsMap[k], nameMetricFields[metricNames[0]][k]))
433+
} else {
434+
for _, n := range metricNames {
435+
frame.Fields = append(frame.Fields, data.NewField(n, frameLabelsMap[k], nameMetricFields[n][k]))
436+
}
437+
}
424438
*frames = append(*frames, frame)
425439
}
426440
}

0 commit comments

Comments
 (0)