console/widgets: fix last flow widget when disabling some columns

This commit is contained in:
Vincent Bernat
2023-01-19 21:48:58 +01:00
parent 0239d7f003
commit e0395fc1a5
2 changed files with 36 additions and 10 deletions

View File

@@ -11,21 +11,39 @@ import (
"time"
"github.com/gin-gonic/gin"
"akvorado/common/schema"
)
func (c *Component) widgetFlowLastHandlerFunc(gc *gin.Context) {
ctx := c.t.Context(gc.Request.Context())
query := `
SELECT *
EXCEPT (DstCommunities, DstLargeCommunities),
arrayMap(c -> concat(toString(bitShiftRight(c, 16)), ':',
toString(bitAnd(c, 0xffff))), DstCommunities) AS DstCommunities,
arrayMap(c -> concat(toString(bitAnd(bitShiftRight(c, 64), 0xffffffff)), ':',
replace := map[schema.ColumnKey]string{
schema.ColumnDstCommunities: `arrayMap(c -> concat(toString(bitShiftRight(c, 16)), ':',
toString(bitAnd(c, 0xffff))), DstCommunities)`,
schema.ColumnDstLargeCommunities: `arrayMap(c -> concat(toString(bitAnd(bitShiftRight(c, 64), 0xffffffff)), ':',
toString(bitAnd(bitShiftRight(c, 32), 0xffffffff)), ':',
toString(bitAnd(c, 0xffffffff))), DstLargeCommunities) AS DstLargeCommunities
toString(bitAnd(c, 0xffffffff))), DstLargeCommunities)`,
}
selectClause := []string{"SELECT *"}
except := []string{}
for k := range replace {
if column, ok := c.d.Schema.LookupColumnByKey(k); ok && column.Disabled {
delete(replace, k)
} else if ok {
except = append(except, k.String())
}
}
if len(except) > 0 {
selectClause = []string{fmt.Sprintf("SELECT * EXCEPT (%s)", strings.Join(except, ", "))}
}
for k, replacement := range replace {
selectClause = append(selectClause, fmt.Sprintf("%s AS %s", replacement, k))
}
query := fmt.Sprintf(`
%s
FROM flows
WHERE TimeReceived=(SELECT MAX(TimeReceived) FROM flows)
LIMIT 1`
LIMIT 1`, strings.Join(selectClause, ",\n "))
gc.Header("X-SQL-Query", query)
// Do not increase counter for this one.
rows, err := c.d.ClickHouseDB.Conn.Query(ctx, query)

View File

@@ -23,8 +23,16 @@ func TestWidgetLastFlow(t *testing.T) {
ctrl := gomock.NewController(t)
mockRows := mocks.NewMockRows(ctrl)
mockConn.EXPECT().Query(gomock.Any(),
gomock.Any()).
mockConn.EXPECT().Query(gomock.Any(), `
SELECT * EXCEPT (DstCommunities, DstLargeCommunities),
arrayMap(c -> concat(toString(bitShiftRight(c, 16)), ':',
toString(bitAnd(c, 0xffff))), DstCommunities) AS DstCommunities,
arrayMap(c -> concat(toString(bitAnd(bitShiftRight(c, 64), 0xffffffff)), ':',
toString(bitAnd(bitShiftRight(c, 32), 0xffffffff)), ':',
toString(bitAnd(c, 0xffffffff))), DstLargeCommunities) AS DstLargeCommunities
FROM flows
WHERE TimeReceived=(SELECT MAX(TimeReceived) FROM flows)
LIMIT 1`).
Return(mockRows, nil)
mockRows.EXPECT().Next().Return(true)
mockRows.EXPECT().Close()