feat: configure limit type (#1482)

* Add limit type field selection

* Take into account LimitType to generate SQL request for Line graph

Also, add LimitType in default configuration for console

* Take into account LimitType to generate SQL request for Sankey graph

* Refactor on SQL query used by line and sankey

* Add limitType description in doc

* Order by max in graphLine when limitType max is used

* Fix query when using top max

Revert some modifications, as they were no longer relevant with the query fixed.

* Rework way to sort by max in line graph type

* Add configuration validation on LimitType

---------

Co-authored-by: Dimitri Baudrier <github.52grm@simplelogin.com>
This commit is contained in:
dimbdr
2024-11-23 18:39:28 +01:00
committed by GitHub
parent 37226280a0
commit 6c0e8e1791
13 changed files with 929 additions and 378 deletions

View File

@@ -4,6 +4,7 @@
package console
import (
"fmt"
"strings"
"akvorado/common/schema"
@@ -32,3 +33,36 @@ func (c *Component) fixQueryColumnName(name string) string {
}
return ""
}
func selectSankeyRowsByLimitType(input graphSankeyHandlerInput, dimensions []string, where string) string {
return selectRowsByLimitType(input.graphCommonHandlerInput, dimensions, where)
}
func selectLineRowsByLimitType(input graphLineHandlerInput, dimensions []string, where string) string {
return selectRowsByLimitType(input.graphCommonHandlerInput, dimensions, where)
}
func selectRowsByLimitType(input graphCommonHandlerInput, dimensions []string, where string) string {
var rowsType string
var source string
var orderBy string
if input.LimitType == "Max" {
source = fmt.Sprintf("( SELECT %s AS sum_at_time FROM source WHERE %s GROUP BY %s )",
strings.Join(append(dimensions, "{{ .Units }}"), ", "),
where,
strings.Join(dimensions, ", "),
)
orderBy = "MAX(sum_at_time)"
} else {
source = fmt.Sprintf("source WHERE %s", where)
orderBy = "{{ .Units }}"
}
rowsType = fmt.Sprintf(
"rows AS (SELECT %s FROM %s GROUP BY %s ORDER BY %s DESC LIMIT %d)",
strings.Join(dimensions, ", "),
source,
strings.Join(dimensions, ", "),
orderBy,
input.Limit)
return rowsType
}