跳转到主要内容

自定义数据源

如果您想查询自定义资源API,您可以注册运行时数据源。这样做的好处是您可以利用SceneQueryRunner

SceneQueryRunner为您做了很多复杂的工作,例如

  • 等待变量完成(如果您的查询依赖于它们)
  • 当时间范围改变时重新执行变量
  • 确定变量在非活动状态时是否已更改
class MyCustomDS extends RuntimeDataSource {
query(request: DataQueryRequest<DataQuery>): Promise<DataQueryResponse> | Observable<DataQueryResponse> {
return Promise.resolve({
state: LoadingState.Done,
data: [
{
fields: [{ name: 'Values', type: FieldType.number, values: [1, 2, 3], config: {} }],
length: 3,
},
],
});
}

testDatasource(): Promise<TestDataSourceResponse> {
return Promise.resolve({ status: 'success', message: 'OK' });
}
}

// Important to specify a unique pluginId and uid for your data source that is unlikely to confict with any other scene app plugin.
sceneUtils.registerRuntimeDataSource({ dataSource: new MyCustomDS('my-custom-ds', 'my-custom-ds-uid') });

您现在可以使用此数据源在SceneQueryRunner查询中使用相同的uid。如果您想在同一SceneQueryRunner中混合查询标准数据源和自定义数据源,请使用混合数据源。

示例

$data: new SceneQueryRunner({
datasource: { uid: '-- Mixed --' },
queries: [
{ refId: 'A', datasource: { uid: 'my-prometheus' }, expr: '<my prometheus query>' },
{ refId: 'B', datasource: { uid: 'my-custom-ds-uid' }, expr: '<my prometheus query>' },
],
});