console: fix a possible divide by 0 error

This is not the only problem we have.
This commit is contained in:
Vincent Bernat
2022-07-09 08:06:23 +02:00
parent d70f59c70f
commit f5e2e85d0d
4 changed files with 28 additions and 28 deletions

View File

@@ -32,16 +32,16 @@ func TestGraphQuerySQL(t *testing.T) {
},
Expected: `
SELECT
toStartOfInterval(TimeReceived, INTERVAL (intDiv(864, {resolution})*{resolution}) second) AS time,
SUM(Bytes*SamplingRate*8/(intDiv(864, {resolution})*{resolution})) AS xps,
toStartOfInterval(TimeReceived, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second) AS time,
SUM(Bytes*SamplingRate*8/max2(intDiv(864, {resolution})*{resolution}, 1)) AS xps,
emptyArrayString() AS dimensions
FROM {table}
WHERE {timefilter}
GROUP BY time, dimensions
ORDER BY time WITH FILL
FROM toStartOfInterval({timefilter.Start}, INTERVAL (intDiv(864, {resolution})*{resolution}) second)
FROM toStartOfInterval({timefilter.Start}, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second)
TO {timefilter.Stop}
STEP (intDiv(864, {resolution})*{resolution})`,
STEP toUInt32(max2(intDiv(864, {resolution})*{resolution}, 1))`,
}, {
Description: "no dimensions, no filters, l2 bps",
Input: graphHandlerInput{
@@ -54,16 +54,16 @@ ORDER BY time WITH FILL
},
Expected: `
SELECT
toStartOfInterval(TimeReceived, INTERVAL (intDiv(864, {resolution})*{resolution}) second) AS time,
SUM((Bytes+18*Packets)*SamplingRate*8/(intDiv(864, {resolution})*{resolution})) AS xps,
toStartOfInterval(TimeReceived, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second) AS time,
SUM((Bytes+18*Packets)*SamplingRate*8/max2(intDiv(864, {resolution})*{resolution}, 1)) AS xps,
emptyArrayString() AS dimensions
FROM {table}
WHERE {timefilter}
GROUP BY time, dimensions
ORDER BY time WITH FILL
FROM toStartOfInterval({timefilter.Start}, INTERVAL (intDiv(864, {resolution})*{resolution}) second)
FROM toStartOfInterval({timefilter.Start}, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second)
TO {timefilter.Stop}
STEP (intDiv(864, {resolution})*{resolution})`,
STEP toUInt32(max2(intDiv(864, {resolution})*{resolution}, 1))`,
}, {
Description: "no dimensions, no filters, pps",
Input: graphHandlerInput{
@@ -76,16 +76,16 @@ ORDER BY time WITH FILL
},
Expected: `
SELECT
toStartOfInterval(TimeReceived, INTERVAL (intDiv(864, {resolution})*{resolution}) second) AS time,
SUM(Packets*SamplingRate/(intDiv(864, {resolution})*{resolution})) AS xps,
toStartOfInterval(TimeReceived, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second) AS time,
SUM(Packets*SamplingRate/max2(intDiv(864, {resolution})*{resolution}, 1)) AS xps,
emptyArrayString() AS dimensions
FROM {table}
WHERE {timefilter}
GROUP BY time, dimensions
ORDER BY time WITH FILL
FROM toStartOfInterval({timefilter.Start}, INTERVAL (intDiv(864, {resolution})*{resolution}) second)
FROM toStartOfInterval({timefilter.Start}, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second)
TO {timefilter.Stop}
STEP (intDiv(864, {resolution})*{resolution})`,
STEP toUInt32(max2(intDiv(864, {resolution})*{resolution}, 1))`,
}, {
Description: "no dimensions",
Input: graphHandlerInput{
@@ -98,16 +98,16 @@ ORDER BY time WITH FILL
},
Expected: `
SELECT
toStartOfInterval(TimeReceived, INTERVAL (intDiv(864, {resolution})*{resolution}) second) AS time,
SUM(Bytes*SamplingRate*8/(intDiv(864, {resolution})*{resolution})) AS xps,
toStartOfInterval(TimeReceived, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second) AS time,
SUM(Bytes*SamplingRate*8/max2(intDiv(864, {resolution})*{resolution}, 1)) AS xps,
emptyArrayString() AS dimensions
FROM {table}
WHERE {timefilter} AND (DstCountry = 'FR' AND SrcCountry = 'US')
GROUP BY time, dimensions
ORDER BY time WITH FILL
FROM toStartOfInterval({timefilter.Start}, INTERVAL (intDiv(864, {resolution})*{resolution}) second)
FROM toStartOfInterval({timefilter.Start}, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second)
TO {timefilter.Stop}
STEP (intDiv(864, {resolution})*{resolution})`,
STEP toUInt32(max2(intDiv(864, {resolution})*{resolution}, 1))`,
}, {
Description: "no filters",
Input: graphHandlerInput{
@@ -126,16 +126,16 @@ ORDER BY time WITH FILL
WITH
rows AS (SELECT ExporterName, InIfProvider FROM {table} WHERE {timefilter} GROUP BY ExporterName, InIfProvider ORDER BY SUM(Bytes) DESC LIMIT 20)
SELECT
toStartOfInterval(TimeReceived, INTERVAL (intDiv(864, {resolution})*{resolution}) second) AS time,
SUM(Bytes*SamplingRate*8/(intDiv(864, {resolution})*{resolution})) AS xps,
toStartOfInterval(TimeReceived, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second) AS time,
SUM(Bytes*SamplingRate*8/max2(intDiv(864, {resolution})*{resolution}, 1)) AS xps,
if((ExporterName, InIfProvider) IN rows, [ExporterName, InIfProvider], ['Other', 'Other']) AS dimensions
FROM {table}
WHERE {timefilter}
GROUP BY time, dimensions
ORDER BY time WITH FILL
FROM toStartOfInterval({timefilter.Start}, INTERVAL (intDiv(864, {resolution})*{resolution}) second)
FROM toStartOfInterval({timefilter.Start}, INTERVAL max2(intDiv(864, {resolution})*{resolution}, 1) second)
TO {timefilter.Stop}
STEP (intDiv(864, {resolution})*{resolution})`,
STEP toUInt32(max2(intDiv(864, {resolution})*{resolution}, 1))`,
},
}
for _, tc := range cases {