@studion/infra-code-blocks
Version:
Studion common infra components
62 lines (61 loc) • 1.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GrafanaDashboardBuilder = void 0;
const pulumi = require("@pulumi/pulumi");
const grafana = require("@pulumiverse/grafana");
const merge_with_defaults_1 = require("../../../shared/merge-with-defaults");
const defaults = {
timezone: 'browser',
refresh: '10s',
};
class GrafanaDashboardBuilder {
name;
panels = [];
configuration = {};
title;
variables = [];
constructor(name) {
this.name = name;
}
withConfig(options) {
this.configuration = options;
return this;
}
withTitle(title) {
this.title = title;
return this;
}
addVariable(variable) {
this.variables.push(variable);
return this;
}
addPanel(panel) {
this.panels.push(panel);
return this;
}
build() {
if (!this.title) {
throw new Error('Dashboard title is required. Call withTitle() to set it.');
}
if (!this.panels.length) {
throw new Error('At least one panel is required. Call addPanel() to add a panel.');
}
const { name, title, panels, variables } = this;
const options = (0, merge_with_defaults_1.mergeWithDefaults)(defaults, this.configuration);
return (folder, opts) => {
return new grafana.oss.Dashboard(`${name}-dashboard`, {
folder: folder?.uid,
configJson: pulumi.jsonStringify({
title,
timezone: options.timezone,
refresh: options.refresh,
panels,
templating: {
list: variables,
},
}),
}, opts);
};
}
}
exports.GrafanaDashboardBuilder = GrafanaDashboardBuilder;