Grafana 基金会 SDK – TypeScript
一套工具、类型和 建设图书馆 用于在 TypeScript 中构建和操纵 Grafana 对象.
安装
yarn add '@grafana/grafana-foundation-sdk@~v0.0.22'
示例用途
更多实例 可在寄存器根处找到.
建造仪表板
import { DashboardBuilder, RowBuilder } from '@grafana/grafana-foundation-sdk/dashboard';
import { DataqueryBuilder } from '@grafana/grafana-foundation-sdk/prometheus';
import { PanelBuilder } from '@grafana/grafana-foundation-sdk/timeseries';
const builder = new DashboardBuilder('[TEST] Node Exporter / Raspberry')
.uid('test-dashboard-raspberry')
.tags(['generated', 'raspberrypi-node-integration'])
.refresh('1m')
.time({from: 'now-30m', to: 'now'})
.timezone('browser')
.withRow(new RowBuilder('Overview'))
.withPanel(
new PanelBuilder()
.title('Network Received')
.unit('bps')
.min(0)
.withTarget(
new DataqueryBuilder()
.expr('rate(node_network_receive_bytes_total{job="integrations/raspberrypi-node", device!="lo"}[$__rate_interval]) * 8')
.legendFormat("{{ device }}")
)
)
;
console.log(JSON.stringify(builder.build(), null, 2));
定义自定义查询类型
支持所有核心数据源及其查询类型的SDK飞船, 它可以为私人/第三方插件扩展.
要做到这一点,请为自定义查询定义一个类型和一个构建器:
// customQuery.ts
import { Builder, Dataquery } from '@grafana/grafana-foundation-sdk/cog';
export interface CustomQuery {
// refId and hide are expected on all queries
refId?: string;
hide?: boolean;
// query is specific to the CustomQuery type
query: string;
// Let cog know that CustomQuery is a Dataquery variant
_implementsDataqueryVariant(): void;
}
export const defaultCustomQuery = (): CustomQuery => ({
query: "",
_implementsDataqueryVariant() {},
});
export class CustomQueryBuilder implements Builder<Dataquery> {
private readonly internal: CustomQuery;
constructor(query: string) {
this.internal = defaultCustomQuery();
this.internal.query = query;
}
build(): CustomQuery {
return this.internal;
}
refId(refId: string): this {
this.internal.refId = refId;
return this;
}
hide(hide: boolean): this {
this.internal.hide = hide;
return this;
}
}
自定义查询类型现在可以照常用于构建一个仪表板:
import { DashboardBuilder, RowBuilder } from '@grafana/grafana-foundation-sdk/dashboard';
import { PanelBuilder as TimeSeriesBuilder } from "@grafana/grafana-foundation-sdk/timeseries";
import { CustomQueryBuilder } from "./customQuery";
const builder = new DashboardBuilder('Custom query type')
.uid('test-custom-query-type')
.refresh('1m')
.time({ from: 'now-30m', to: 'now' })
.withRow(new RowBuilder('Overview'))
.withPanel(
new TimeSeriesBuilder()
.title('Sample panel')
.withTarget(
new CustomQueryBuilder("query here")
)
);
console.log(JSON.stringify(builder.build(), null, 2));
定义自定义面板类型
支援所有核心板的SDK飞船可以扩展至 私人/第三方插件.
为此,定义自定义面板选项的类型和构建器:
// customPanel.ts
import * as dashboard from '@grafana/grafana-foundation-sdk/dashboard';
export interface CustomPanelOptions {
makeBeautiful?: boolean;
}
export const defaultCustomPanelOptions = (): CustomPanelOptions => ({
});
export class CustomPanelBuilder extends dashboard.PanelBuilder {
constructor() {
super();
this.internal.type = "custom-panel"; // panel plugin ID
}
makeBeautiful(): this {
if (!this.internal.options) {
this.internal.options = defaultCustomPanelOptions();
}
this.internal.options.makeBeautiful = true;
return this;
}
}
现在可以像往常一样使用自定义面板类型来构建一个仪表板:
import { DashboardBuilder, RowBuilder } from '@grafana/grafana-foundation-sdk/dashboard';
import { CustomPanelBuilder } from "./customPanel";
const builder = new DashboardBuilder('Custom panel type')
.uid('test-custom-panel-type')
.refresh('1m')
.time({ from: 'now-30m', to: 'now' })
.withRow(new RowBuilder('Overview'))
.withPanel(
new CustomPanelBuilder()
.title('Sample custom panel')
.makeBeautiful()
);
console.log(JSON.stringify(builder.build(), null, 2));
成熟度
此寄存器中的代码应被视为"公开预览". 虽然Grafana Labs在生产中使用了它,但它仍在积极开发中.
[注] 虫子和问题完全由工程小组处理。 没有待命支助或苏丹解放军.