From dcfbea33769959f91b228926b416c7c1edd136eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 2 Jul 2021 22:25:51 +0100 Subject: [PATCH] chore(api): refactor grid label select source --- cmd/karma/api_test.go | 5 +++++ cmd/karma/views.go | 15 ++++++++++++- internal/models/api.go | 1 + .../Grid/AlertGrid/GridLabelSelect.test.tsx | 1 + .../Grid/AlertGrid/GridLabelSelect.tsx | 21 +------------------ ui/src/Models/APITypes.ts | 1 + ui/src/Stores/AlertStore.ts | 8 +++++++ ui/src/__fixtures__/Fetch.ts | 1 + 8 files changed, 32 insertions(+), 21 deletions(-) diff --git a/cmd/karma/api_test.go b/cmd/karma/api_test.go index ae8a1f4e8..89bbe91ad 100644 --- a/cmd/karma/api_test.go +++ b/cmd/karma/api_test.go @@ -1088,6 +1088,11 @@ func TestVerifyAllGroups(t *testing.T) { if diff := cmp.Diff(expectedReceivers, ur.Receivers); diff != "" { t.Errorf("Incorrect receivers list (-want +got):\n%s", diff) } + + expectedLabelNames := []string{"alertname", "cluster", "disk", "instance", "ip", "job"} + if diff := cmp.Diff(expectedLabelNames, ur.LabelNames); diff != "" { + t.Errorf("Incorrect labelNames list (-want +got):\n%s", diff) + } } } diff --git a/cmd/karma/views.go b/cmd/karma/views.go index b77d96a67..35af05fcc 100644 --- a/cmd/karma/views.go +++ b/cmd/karma/views.go @@ -245,12 +245,20 @@ func alerts(w http.ResponseWriter, r *http.Request) { } var matches int + labelMap := map[string]struct{}{} for _, ag := range filtered { perGridAlertGroup := map[string]*models.AlertGroup{} + for k := range ag.Labels { + labelMap[k] = struct{}{} + } for _, alert := range ag.Alerts { alert := alert // scopelint pin + for k := range alert.Labels { + labelMap[k] = struct{}{} + } + allReceivers[alert.Receiver] = true matches++ @@ -442,7 +450,6 @@ func alerts(w http.ResponseWriter, r *http.Request) { } } - //resp.AlertGroups = sortAlertGroups(c, alerts) v, _ := lookupQueryString(r, "gridSortReverse") gridSortReverse := v == "1" sortedGrids := sortGrids(r, gridLabel, grids, gridSortReverse) @@ -477,6 +484,12 @@ func alerts(w http.ResponseWriter, r *http.Request) { } sort.Strings(receivers) + resp.LabelNames = make([]string, 0, len(labelMap)) + for label := range labelMap { + resp.LabelNames = append(resp.LabelNames, label) + } + sort.Strings(resp.LabelNames) + resp.Grids = sortedGrids resp.Silences = silences resp.Colors = colors diff --git a/internal/models/api.go b/internal/models/api.go index 3a5b165e3..78eb7bf6d 100644 --- a/internal/models/api.go +++ b/internal/models/api.go @@ -349,6 +349,7 @@ type AlertsResponse struct { Silences map[string]map[string]Silence `json:"silences"` Grids []APIGrid `json:"grids"` TotalAlerts int `json:"totalAlerts"` + LabelNames []string `json:"labelNames"` Colors LabelsColorMap `json:"colors"` Filters []Filter `json:"filters"` Counters LabelNameStatsList `json:"counters"` diff --git a/ui/src/Components/Grid/AlertGrid/GridLabelSelect.test.tsx b/ui/src/Components/Grid/AlertGrid/GridLabelSelect.test.tsx index ec0a0401b..ee2f905ed 100644 --- a/ui/src/Components/Grid/AlertGrid/GridLabelSelect.test.tsx +++ b/ui/src/Components/Grid/AlertGrid/GridLabelSelect.test.tsx @@ -33,6 +33,7 @@ beforeEach(() => { unprocessed: 0, }, }; + alertStore.data.setLabelNames(["alertname", "job", "cluster"]); jest.useFakeTimers(); }); diff --git a/ui/src/Components/Grid/AlertGrid/GridLabelSelect.tsx b/ui/src/Components/Grid/AlertGrid/GridLabelSelect.tsx index 54fd4a0ee..b4a29a5cb 100644 --- a/ui/src/Components/Grid/AlertGrid/GridLabelSelect.tsx +++ b/ui/src/Components/Grid/AlertGrid/GridLabelSelect.tsx @@ -45,25 +45,6 @@ const GridLabelNameSelect: FC<{ inputValue: string, callback: (options: OptionT[]) => void ) => { - const labelNames: { [key: string]: boolean } = {}; - - alertStore.data.grids.forEach((grid) => { - labelNames[grid.labelName] = true; - grid.alertGroups.forEach((group) => { - Object.keys(group.labels).forEach((name) => { - labelNames[name] = true; - }); - Object.keys(group.shared.labels).forEach((name) => { - labelNames[name] = true; - }); - group.alerts.forEach((alert) => { - Object.keys(alert.labels).forEach((name) => { - labelNames[name] = true; - }); - }); - }); - }); - const autoEnabled = settingsStore.multiGridConfig.config.gridLabel === "@auto"; const options = [ @@ -71,7 +52,7 @@ const GridLabelNameSelect: FC<{ (val) => val.value !== "@auto" || (val.value === "@auto" && !autoEnabled) ), - ...Object.keys(labelNames) + ...alertStore.data.labelNames .filter( (labelName) => autoEnabled === true || diff --git a/ui/src/Models/APITypes.ts b/ui/src/Models/APITypes.ts index 0c39b70c6..440c9c5ef 100644 --- a/ui/src/Models/APITypes.ts +++ b/ui/src/Models/APITypes.ts @@ -201,6 +201,7 @@ export interface APIAlertsResponseT { upstreams: APIAlertsResponseUpstreamsT; silences: APIAlertsResponseSilenceMapT; grids: APIGridT[]; + labelNames: string[]; totalAlerts: number; colors: APIAlertsResponseColorsT; filters: APIFilterT[]; diff --git a/ui/src/Stores/AlertStore.ts b/ui/src/Stores/AlertStore.ts index 9d87169d3..4f59799e6 100644 --- a/ui/src/Stores/AlertStore.ts +++ b/ui/src/Stores/AlertStore.ts @@ -139,6 +139,8 @@ interface AlertStoreDataT { colors: APIAlertsResponseColorsT; counters: APILabelCounterT[]; grids: APIGridT[]; + labelNames: string[]; + setLabelNames: (v: string[]) => void; silences: APIAlertsResponseSilenceMapT; upstreams: APIAlertsResponseUpstreamsT; receivers: string[]; @@ -291,6 +293,10 @@ class AlertStore { colors: {} as APIAlertsResponseColorsT, counters: [] as APILabelCounterT[], grids: [] as APIGridT[], + labelNames: [] as string[], + setLabelNames(v: string[]) { + this.labelNames = v; + }, silences: {} as APIAlertsResponseSilenceMapT, upstreams: { counters: { total: 0, healthy: 0, failed: 0 }, @@ -394,6 +400,7 @@ class AlertStore { setCounters: action.bound, setReceivers: action.bound, setColors: action.bound, + setLabelNames: action.bound, }, { name: "API Response data" } ); @@ -663,6 +670,7 @@ class AlertStore { updates.colors = result.colors; updates.counters = result.counters; updates.grids = result.grids; + updates.labelNames = result.labelNames; updates.silences = result.silences; updates.upstreams = result.upstreams; updates.receivers = result.receivers; diff --git a/ui/src/__fixtures__/Fetch.ts b/ui/src/__fixtures__/Fetch.ts index bff357396..50b1acc32 100644 --- a/ui/src/__fixtures__/Fetch.ts +++ b/ui/src/__fixtures__/Fetch.ts @@ -30,6 +30,7 @@ const EmptyAPIResponse = (): APIAlertsResponseT => ({ }, silences: { default: {} }, grids: [], + labelNames: ["alertname", "job", "cluster"], receivers: ["by-cluster-service", "by-name"], totalAlerts: 0, colors: {},