mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-01 02:12:01 +08:00
chore: suggestion
This commit is contained in:
parent
2d9e5a9aca
commit
049828a9bb
@ -36,9 +36,27 @@ func ApplySeriesLimit(series []*TimeSeries, orderBy []OrderBy, limit int) []*Tim
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache series values and labels
|
||||||
|
seriesValues := make(map[*TimeSeries]float64, len(series))
|
||||||
|
seriesLabels := make(map[*TimeSeries]map[string]string, len(series))
|
||||||
|
|
||||||
|
for _, s := range series {
|
||||||
|
seriesValues[s] = calculateSeriesValue(s)
|
||||||
|
// Cache all labels for this series
|
||||||
|
labelMap := make(map[string]string)
|
||||||
|
for _, label := range s.Labels {
|
||||||
|
if strVal, ok := label.Value.(string); ok {
|
||||||
|
labelMap[label.Key.Name] = strVal
|
||||||
|
} else {
|
||||||
|
labelMap[label.Key.Name] = convertValueToString(label.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
seriesLabels[s] = labelMap
|
||||||
|
}
|
||||||
|
|
||||||
// Sort the series based on the order criteria
|
// Sort the series based on the order criteria
|
||||||
sort.SliceStable(series, func(i, j int) bool {
|
sort.SliceStable(series, func(i, j int) bool {
|
||||||
return compareSeries(series[i], series[j], effectiveOrderBy)
|
return compareSeries(series[i], series[j], effectiveOrderBy, seriesValues, seriesLabels)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Apply limit if specified
|
// Apply limit if specified
|
||||||
@ -51,15 +69,14 @@ func ApplySeriesLimit(series []*TimeSeries, orderBy []OrderBy, limit int) []*Tim
|
|||||||
|
|
||||||
// compareSeries compares two time series based on the order criteria
|
// compareSeries compares two time series based on the order criteria
|
||||||
// Returns true if series i should come before series j
|
// Returns true if series i should come before series j
|
||||||
func compareSeries(seriesI, seriesJ *TimeSeries, orderBy []OrderBy) bool {
|
func compareSeries(seriesI, seriesJ *TimeSeries, orderBy []OrderBy, seriesValues map[*TimeSeries]float64, seriesLabels map[*TimeSeries]map[string]string) bool {
|
||||||
for _, order := range orderBy {
|
for _, order := range orderBy {
|
||||||
columnName := order.Key.Name
|
columnName := order.Key.Name
|
||||||
direction := order.Direction
|
direction := order.Direction
|
||||||
|
|
||||||
if columnName == DefaultOrderByKey {
|
if columnName == DefaultOrderByKey {
|
||||||
// Sort based on aggregated values
|
valueI := seriesValues[seriesI]
|
||||||
valueI := calculateSeriesValue(seriesI)
|
valueJ := seriesValues[seriesJ]
|
||||||
valueJ := calculateSeriesValue(seriesJ)
|
|
||||||
|
|
||||||
if valueI != valueJ {
|
if valueI != valueJ {
|
||||||
if direction == OrderDirectionAsc {
|
if direction == OrderDirectionAsc {
|
||||||
@ -69,9 +86,8 @@ func compareSeries(seriesI, seriesJ *TimeSeries, orderBy []OrderBy) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Sort based on labels
|
labelI, existsI := seriesLabels[seriesI][columnName]
|
||||||
labelI, existsI := findLabelValue(seriesI, columnName)
|
labelJ, existsJ := seriesLabels[seriesJ][columnName]
|
||||||
labelJ, existsJ := findLabelValue(seriesJ, columnName)
|
|
||||||
|
|
||||||
if existsI != existsJ {
|
if existsI != existsJ {
|
||||||
// Handle missing labels - non-existent labels come first
|
// Handle missing labels - non-existent labels come first
|
||||||
@ -132,22 +148,6 @@ func calculateSeriesValue(series *TimeSeries) float64 {
|
|||||||
return sum / count
|
return sum / count
|
||||||
}
|
}
|
||||||
|
|
||||||
// findLabelValue finds the value of a label with the given key in a time series
|
|
||||||
// Returns the label value and whether it was found
|
|
||||||
func findLabelValue(series *TimeSeries, key string) (string, bool) {
|
|
||||||
for _, label := range series.Labels {
|
|
||||||
if label.Key.Name == key {
|
|
||||||
// Convert label value to string
|
|
||||||
if strVal, ok := label.Value.(string); ok {
|
|
||||||
return strVal, true
|
|
||||||
}
|
|
||||||
// Handle non-string values by converting to string
|
|
||||||
return convertValueToString(label.Value), true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertValueToString converts various types to string for comparison
|
// convertValueToString converts various types to string for comparison
|
||||||
func convertValueToString(value any) string {
|
func convertValueToString(value any) string {
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user