Use unit for sorting topX instead of always using bytes

This commit is contained in:
netixx
2024-07-16 18:03:20 +02:00
committed by Vincent Bernat
parent b8368501d7
commit a312bc307e
4 changed files with 16 additions and 3 deletions

View File

@@ -134,16 +134,18 @@ func (input graphLineHandlerInput) toSQL1(axis int, options toSQL1Options) strin
dimensionsInterpolate = "emptyArrayString()" dimensionsInterpolate = "emptyArrayString()"
} }
// With // With
withStr := "" withStr := ""
if !options.skipWithClause { if !options.skipWithClause {
with := []string{fmt.Sprintf("source AS (%s)", input.sourceSelect())} with := []string{fmt.Sprintf("source AS (%s)", input.sourceSelect())}
if len(dimensions) > 0 { if len(dimensions) > 0 {
with = append(with, fmt.Sprintf( with = append(with, fmt.Sprintf(
"rows AS (SELECT %s FROM source WHERE %s GROUP BY %s ORDER BY SUM(Bytes) DESC LIMIT %d)", "rows AS (SELECT %s FROM source WHERE %s GROUP BY %s ORDER BY SUM(%s) DESC LIMIT %d)",
strings.Join(dimensions, ", "), strings.Join(dimensions, ", "),
where, where,
strings.Join(dimensions, ", "), strings.Join(dimensions, ", "),
metricForTopSort(input.Units),
input.Limit)) input.Limit))
} }
if len(with) > 0 { if len(with) > 0 {

View File

@@ -32,3 +32,13 @@ func (c *Component) fixQueryColumnName(name string) string {
} }
return "" return ""
} }
func metricForTopSort(inputUnit string) string {
switch inputUnit {
case "pps":
return "Packets"
default:
return "Bytes"
}
}

View File

@@ -59,10 +59,11 @@ func (input graphSankeyHandlerInput) toSQL() (string, error) {
fmt.Sprintf("source AS (%s)", input.sourceSelect()), fmt.Sprintf("source AS (%s)", input.sourceSelect()),
fmt.Sprintf(`(SELECT MAX(TimeReceived) - MIN(TimeReceived) FROM source WHERE %s) AS range`, where), fmt.Sprintf(`(SELECT MAX(TimeReceived) - MIN(TimeReceived) FROM source WHERE %s) AS range`, where),
fmt.Sprintf( fmt.Sprintf(
"rows AS (SELECT %s FROM source WHERE %s GROUP BY %s ORDER BY SUM(Bytes) DESC LIMIT %d)", "rows AS (SELECT %s FROM source WHERE %s GROUP BY %s ORDER BY SUM(%s) DESC LIMIT %d)",
strings.Join(dimensions, ", "), strings.Join(dimensions, ", "),
where, where,
strings.Join(dimensions, ", "), strings.Join(dimensions, ", "),
metricForTopSort(input.Units),
input.Limit), input.Limit),
} }

View File

@@ -107,7 +107,7 @@ ORDER BY xps DESC
WITH WITH
source AS (SELECT * FROM {{ .Table }} SETTINGS asterisk_include_alias_columns = 1), source AS (SELECT * FROM {{ .Table }} SETTINGS asterisk_include_alias_columns = 1),
(SELECT MAX(TimeReceived) - MIN(TimeReceived) FROM source WHERE {{ .Timefilter }}) AS range, (SELECT MAX(TimeReceived) - MIN(TimeReceived) FROM source WHERE {{ .Timefilter }}) AS range,
rows AS (SELECT SrcAS, ExporterName FROM source WHERE {{ .Timefilter }} GROUP BY SrcAS, ExporterName ORDER BY SUM(Bytes) DESC LIMIT 5) rows AS (SELECT SrcAS, ExporterName FROM source WHERE {{ .Timefilter }} GROUP BY SrcAS, ExporterName ORDER BY SUM(Packets) DESC LIMIT 5)
SELECT SELECT
{{ .Units }}/range AS xps, {{ .Units }}/range AS xps,
[if(SrcAS IN (SELECT SrcAS FROM rows), concat(toString(SrcAS), ': ', dictGetOrDefault('asns', 'name', SrcAS, '???')), 'Other'), [if(SrcAS IN (SELECT SrcAS FROM rows), concat(toString(SrcAS), ': ', dictGetOrDefault('asns', 'name', SrcAS, '???')), 'Other'),