Skip to content

Commit 96e8335

Browse files
authored
feat(output): set default variables and allow features to ovewrite them if needed (#30)
* feat(variables): deduplicate variables to avoid conflicts when multiple sources set them Signed-off-by: Lorenzo Setale <lorenzo.setale@LEGO.com> * feat(features): adds variables for loki and prom prom when needed Signed-off-by: Lorenzo Setale <lorenzo.setale@LEGO.com> * feat(output): set default loki and prometheus to some valeu if not present Signed-off-by: Lorenzo Setale <lorenzo.setale@LEGO.com> * refactor: removes variables when the default ones are good Signed-off-by: Lorenzo Setale <lorenzo.setale@LEGO.com> * fix(variables): loki now uses the right regex to remove grafana cloud insight and name is correct Signed-off-by: Lorenzo Setale <lorenzo.setale@LEGO.com> * refactor: removes unused import Signed-off-by: Lorenzo Setale <lorenzo.setale@LEGO.com> --------- Signed-off-by: Lorenzo Setale <lorenzo.setale@LEGO.com>
1 parent e1a186a commit 96e8335

4 files changed

Lines changed: 67 additions & 29 deletions

File tree

src/components/output.tsx

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ import {
1313

1414
import { useEnv } from '../components/env.tsx';
1515

16+
const DEFAULT_VAR_LOKI_DATASOURCE = new DatasourceVariableBuilder("loki")
17+
.label("Logs Data source")
18+
.type("loki")
19+
.regex("(?!grafanacloud).+(?!-alert-state-history|-usage-insight)")
20+
.multi(false);
21+
22+
const DEFAULT_VAR_PROMETHEUS_DATASOURCE = new DatasourceVariableBuilder("prometheus")
23+
.label("Metrics Data source")
24+
.type("prometheus")
25+
.regex("(?!grafanacloud-usage|grafanacloud-ml-metrics).+")
26+
.multi(false);
27+
28+
1629
export default function Component({ goBack, goForward, dashboardData }) {
1730
const env = useEnv();
1831
const grafanaBaseurl = env?.["BUN_PUBLIC_GRAFANA_BASEURL"];
@@ -28,29 +41,36 @@ export default function Component({ goBack, goForward, dashboardData }) {
2841
new TimePickerBuilder()
2942
.refreshIntervals(["1m", "5m", "15m", "30m", "1h", "6h", "1d", "7d"])
3043
)
31-
.withVariable(
32-
new DatasourceVariableBuilder("prometheus")
33-
.label("Metrics Data source")
34-
.type("prometheus")
35-
.regex("(?!grafanacloud-usage|grafanacloud-ml-metrics).+")
36-
.multi(false)
37-
)
38-
.withVariable(
39-
new DatasourceVariableBuilder("loki")
40-
.label("Logs Data source")
41-
.type("loki")
42-
.regex("(?!grafanacloud.+usage-insights|grafanacloud.+alert-state-history).+")
43-
.multi(false)
44-
);
45-
46-
// Inject variables contributed by enabled features
44+
45+
// Inject variables contributed by enabled features, deduplicating by name
46+
// so that multiple features using the same datasource type (ex: prometheus,
47+
// loki) don't produce duplicate variables in the output.
48+
const seenVariables = new Map();
49+
// TODO: Mayyybe there is a better way for this to avoid dupes sources
4750
dashboardData.features.forEach((feat) => {
4851
if (!feat.enabled) return;
4952
feat.variables.forEach((variable) => {
50-
dashboard = dashboard.withVariable(variable);
53+
const built = variable.build();
54+
if (!seenVariables.has(built.name)) {
55+
seenVariables.set(built.name, variable);
56+
}
5157
});
5258
});
5359

60+
// Check and add loki and prometheus datasources if they were not set.
61+
62+
if(!seenVariables.has("loki")){
63+
seenVariables.set("loki", DEFAULT_VAR_LOKI_DATASOURCE);
64+
}
65+
66+
if(!seenVariables.has("prometheus")){
67+
seenVariables.set("prometheus", DEFAULT_VAR_PROMETHEUS_DATASOURCE);
68+
}
69+
70+
seenVariables.forEach((variable) => {
71+
dashboard = dashboard.withVariable(variable);
72+
});
73+
5474
dashboard = dashboard.withRow(new RowBuilder("Overview"));
5575

5676
dashboard = dashboard.withPanel(

src/features/logs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useState } from 'react';
22

3+
import { BigValueGraphMode } from '@grafana/grafana-foundation-sdk/common';
34
import { DataqueryBuilder as LokiDataqueryBuilder } from '@grafana/grafana-foundation-sdk/loki';
4-
import { PanelBuilder as LogsPanelBuilder } from '@grafana/grafana-foundation-sdk/logs';
55
import { LogsDedupStrategy } from '@grafana/grafana-foundation-sdk/common';
6+
import { PanelBuilder as LogsPanelBuilder } from '@grafana/grafana-foundation-sdk/logs';
67
import { PanelBuilder as StatsPanelBuilder } from '@grafana/grafana-foundation-sdk/stat';
7-
import { BigValueGraphMode } from '@grafana/grafana-foundation-sdk/common';
88

99
import {
1010
PanelBuilder as TextPanelBuilder,

src/features/metrics.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { PanelBuilder as TimeSeriesPanelBuilder } from '@grafana/grafana-foundation-sdk/timeseries';
1+
import { useState } from 'react';
2+
23
import { DataqueryBuilder as PrometheusDataqueryBuilder } from '@grafana/grafana-foundation-sdk/prometheus';
3-
import { VizLegendOptionsBuilder } from '@grafana/grafana-foundation-sdk/common';
44
import { PanelBuilder as TextPanelBuilder, TextMode } from '@grafana/grafana-foundation-sdk/text';
5-
6-
import { useState } from 'react';
5+
import { PanelBuilder as TimeSeriesPanelBuilder } from '@grafana/grafana-foundation-sdk/timeseries';
6+
import { VizLegendOptionsBuilder } from '@grafana/grafana-foundation-sdk/common';
77

88
import { usePersistentState } from '../lib/usePersistentState.ts';
99

src/features/novus.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { useState, useEffect, type ReactNode } from "react";
22
import { useAuth } from "react-oidc-context";
3-
import { useEnv } from "../components/env.tsx";
43

54
import { AutoComplete } from "primereact/autocomplete";
65

76
import {
8-
PanelBuilder,
9-
ConstantVariableBuilder,
107
AdHocVariableBuilder,
11-
VariableHide,
8+
ConstantVariableBuilder,
9+
DatasourceVariableBuilder,
10+
FieldColorModeId,
11+
MappingType,
12+
PanelBuilder,
1213
ThresholdsConfigBuilder,
1314
ThresholdsMode,
14-
MappingType,
15-
FieldColorModeId,
15+
VariableHide,
1616
} from "@grafana/grafana-foundation-sdk/dashboard";
1717

1818
import { DataqueryBuilder as PrometheusDataqueryBuilder } from "@grafana/grafana-foundation-sdk/prometheus";
@@ -21,6 +21,7 @@ import { PanelBuilder as TextPanelBuilder, TextMode } from "@grafana/grafana-fou
2121

2222
import { usePersistentState } from "../lib/usePersistentState.ts";
2323
import { queryPrometheus } from "../lib/prometheusQuerier.ts";
24+
import { useEnv } from "../components/env.tsx";
2425

2526
export const FeatureID = "novus";
2627
export const FeatureName = "Novus Runtime Information";
@@ -92,7 +93,24 @@ function buildVariables(runtime: string) {
9293
.build();
9394
podFilter.filters = [{ key: "pod", operator: "!~", value: "novus-.*" }];
9495

96+
// We know that Novus users have data for prom and loki in Novus' datasources
97+
// so we try to be smort and set them.
98+
const promDatasource = new DatasourceVariableBuilder("prometheus")
99+
.label("Metrics Data source")
100+
.hide(VariableHide.HideVariable)
101+
.type("prometheus")
102+
.regex(".+Novus.+")
103+
.multi(false);
104+
const lokiDatasource = new DatasourceVariableBuilder("loki")
105+
.label("Logs Data source")
106+
.hide(VariableHide.HideVariable)
107+
.type("loki")
108+
.regex(".+Novus.+")
109+
.multi(false)
110+
95111
return [
112+
promDatasource,
113+
lokiDatasource,
96114
new ConstantVariableBuilder("namespace")
97115
.label("Novus Runtime / namespace")
98116
.value(runtime),

0 commit comments

Comments
 (0)