mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
refactor: simplifies the SQL query
This commit is contained in:
committed by
Vincent Bernat
parent
7805aad457
commit
38cbaedc3c
@@ -287,39 +287,25 @@ UNION DISTINCT
|
|||||||
c.r.Debug().Msg(columnName)
|
c.r.Debug().Msg(columnName)
|
||||||
sqlQuery := fmt.Sprintf(`
|
sqlQuery := fmt.Sprintf(`
|
||||||
SELECT label, detail FROM (
|
SELECT label, detail FROM (
|
||||||
SELECT toString(%s) AS label, dictGet('%s', 'name', %s) AS detail, 1 AS rank, count(*) AS c
|
SELECT toString(%s) AS label, if(Proto = 6, dictGet('%s', 'name', %s), dictGet('%s', 'name', %s) ) AS detail, 1 AS rank, count(*) AS c
|
||||||
FROM flows
|
FROM flows
|
||||||
WHERE TimeReceived > date_sub(minute, 1, now())
|
WHERE Proto IN (6, 17)
|
||||||
|
AND TimeReceived > date_sub(minute, 1, now())
|
||||||
AND detail != ''
|
AND detail != ''
|
||||||
AND positionCaseInsensitive(detail, $1) >= 1
|
AND positionCaseInsensitive(detail, $1) >= 1
|
||||||
AND Proto = 6
|
GROUP BY %s, Proto
|
||||||
GROUP BY %s
|
|
||||||
ORDER BY COUNT(*) DESC
|
|
||||||
LIMIT %d
|
|
||||||
UNION DISTINCT
|
|
||||||
SELECT toString(%s) AS label, dictGet(%s, 'name', %s) AS detail, 1 AS rank, count(*) AS c
|
|
||||||
FROM flows
|
|
||||||
WHERE TimeReceived > date_sub(minute, 1, now())
|
|
||||||
AND detail != ''
|
|
||||||
AND positionCaseInsensitive(detail, $1) >= 1
|
|
||||||
AND Proto = 17
|
|
||||||
GROUP BY %s
|
|
||||||
ORDER BY COUNT(*) DESC
|
ORDER BY COUNT(*) DESC
|
||||||
LIMIT %d
|
LIMIT %d
|
||||||
UNION DISTINCT
|
UNION DISTINCT
|
||||||
SELECT toString(port) AS label, name AS detail, 2 AS rank, 0 AS c
|
SELECT toString(port) AS label, name AS detail, 2 AS rank, 0 AS c
|
||||||
FROM tcp
|
FROM (
|
||||||
WHERE positionCaseInsensitive(name, $1) >= 1
|
SELECT port, name FROM tcp UNION DISTINCT SELECT port, name FROM tcp
|
||||||
ORDER BY positionCaseInsensitive(name, $1) ASC, port ASC
|
)
|
||||||
LIMIT %d
|
|
||||||
UNION DISTINCT
|
|
||||||
SELECT toString(port) AS label, name AS detail, 2 AS rank, 0 AS c
|
|
||||||
FROM udp
|
|
||||||
WHERE positionCaseInsensitive(name, $1) >= 1
|
WHERE positionCaseInsensitive(name, $1) >= 1
|
||||||
ORDER BY positionCaseInsensitive(name, $1) ASC, port ASC
|
ORDER BY positionCaseInsensitive(name, $1) ASC, port ASC
|
||||||
LIMIT %d
|
LIMIT %d
|
||||||
) GROUP BY rank, label, detail ORDER BY rank ASC, MAX(c) DESC, MIN(rowNumberInBlock()) ASC LIMIT %d`,
|
) GROUP BY rank, label, detail ORDER BY rank ASC, MAX(c) DESC, MIN(rowNumberInBlock()) ASC LIMIT %d`,
|
||||||
columnName, schema.DictionaryTCP, columnName, columnName, input.Limit, columnName, schema.DictionaryUDP, columnName, columnName, input.Limit, input.Limit, input.Limit, input.Limit,
|
columnName, schema.DictionaryTCP, columnName, schema.DictionaryUDP, columnName, columnName, input.Limit, input.Limit, input.Limit,
|
||||||
)
|
)
|
||||||
if err := c.d.ClickHouseDB.Select(ctx, &results, sqlQuery, input.Prefix); err != nil {
|
if err := c.d.ClickHouseDB.Select(ctx, &results, sqlQuery, input.Prefix); err != nil {
|
||||||
c.r.Err(err).Msg("unable to query database")
|
c.r.Err(err).Msg("unable to query database")
|
||||||
|
|||||||
@@ -135,34 +135,20 @@ LIMIT 20`, "6540").
|
|||||||
mockConn.EXPECT().
|
mockConn.EXPECT().
|
||||||
Select(gomock.Any(), gomock.Any(), `
|
Select(gomock.Any(), gomock.Any(), `
|
||||||
SELECT label, detail FROM (
|
SELECT label, detail FROM (
|
||||||
SELECT toString(SrcPort) AS label, dictGet('tcp', 'name', SrcPort) AS detail, 1 AS rank, count(*) AS c
|
SELECT toString(SrcPort) AS label, if(Proto = 6, dictGet('tcp', 'name', SrcPort), dictGet('udp', 'name', SrcPort) ) AS detail, 1 AS rank, count(*) AS c
|
||||||
FROM flows
|
FROM flows
|
||||||
WHERE TimeReceived > date_sub(minute, 1, now())
|
WHERE Proto IN (6, 17)
|
||||||
|
AND TimeReceived > date_sub(minute, 1, now())
|
||||||
AND detail != ''
|
AND detail != ''
|
||||||
AND positionCaseInsensitive(detail, $1) >= 1
|
AND positionCaseInsensitive(detail, $1) >= 1
|
||||||
AND Proto = 6
|
GROUP BY SrcPort, Proto
|
||||||
GROUP BY SrcPort
|
|
||||||
ORDER BY COUNT(*) DESC
|
|
||||||
LIMIT 20
|
|
||||||
UNION DISTINCT
|
|
||||||
SELECT toString(SrcPort) AS label, dictGet(udp, 'name', SrcPort) AS detail, 1 AS rank, count(*) AS c
|
|
||||||
FROM flows
|
|
||||||
WHERE TimeReceived > date_sub(minute, 1, now())
|
|
||||||
AND detail != ''
|
|
||||||
AND positionCaseInsensitive(detail, $1) >= 1
|
|
||||||
AND Proto = 17
|
|
||||||
GROUP BY SrcPort
|
|
||||||
ORDER BY COUNT(*) DESC
|
ORDER BY COUNT(*) DESC
|
||||||
LIMIT 20
|
LIMIT 20
|
||||||
UNION DISTINCT
|
UNION DISTINCT
|
||||||
SELECT toString(port) AS label, name AS detail, 2 AS rank, 0 AS c
|
SELECT toString(port) AS label, name AS detail, 2 AS rank, 0 AS c
|
||||||
FROM tcp
|
FROM (
|
||||||
WHERE positionCaseInsensitive(name, $1) >= 1
|
SELECT port, name FROM tcp UNION DISTINCT SELECT port, name FROM tcp
|
||||||
ORDER BY positionCaseInsensitive(name, $1) ASC, port ASC
|
)
|
||||||
LIMIT 20
|
|
||||||
UNION DISTINCT
|
|
||||||
SELECT toString(port) AS label, name AS detail, 2 AS rank, 0 AS c
|
|
||||||
FROM udp
|
|
||||||
WHERE positionCaseInsensitive(name, $1) >= 1
|
WHERE positionCaseInsensitive(name, $1) >= 1
|
||||||
ORDER BY positionCaseInsensitive(name, $1) ASC, port ASC
|
ORDER BY positionCaseInsensitive(name, $1) ASC, port ASC
|
||||||
LIMIT 20
|
LIMIT 20
|
||||||
|
|||||||
Reference in New Issue
Block a user