-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime-datasource): add runtime datasource (#649)
* feat(runtime-datasource): add runtime datasource * feat(runtime-datasource): add error check
- Loading branch information
Showing
4 changed files
with
65 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { DataQueryRequest, DataQueryResponse, TestDataSourceResponse } from '@grafana/data'; | ||
import { getDataSourceSrv } from '@grafana/runtime'; | ||
import { RuntimeDataSource, SceneObject, sceneUtils } from '@grafana/scenes'; | ||
import { DataQuery } from '@grafana/schema'; | ||
import { Observable, isObservable } from 'rxjs'; | ||
import { getDataSource } from './scenes'; | ||
|
||
export const WRAPPED_LOKI_DS_UID = 'wrapped-loki-ds-uid'; | ||
|
||
type SceneDataQueryRequest = DataQueryRequest<DataQuery> & { | ||
scopedVars?: { __sceneObject?: { valueOf: () => SceneObject } }; | ||
}; | ||
|
||
class WrappedLokiDatasource extends RuntimeDataSource<DataQuery> { | ||
constructor(pluginId: string, uid: string) { | ||
super(pluginId, uid); | ||
} | ||
|
||
query(request: SceneDataQueryRequest): Promise<DataQueryResponse> | Observable<DataQueryResponse> { | ||
return new Observable<DataQueryResponse>((subscriber) => { | ||
if (!request.scopedVars?.__sceneObject) { | ||
throw new Error('Scene object not found in request'); | ||
} | ||
|
||
getDataSourceSrv() | ||
.get(getDataSource(request.scopedVars.__sceneObject.valueOf())) | ||
.then((ds) => { | ||
// override the target datasource to Loki | ||
request.targets = request.targets.map((target) => { | ||
target.datasource = ds; | ||
return target; | ||
}); | ||
|
||
// query the datasource and return either observable or promise | ||
const dsResponse = ds.query(request); | ||
if (isObservable(dsResponse)) { | ||
dsResponse.subscribe(subscriber); | ||
} else { | ||
dsResponse.then((response) => { | ||
subscriber.next(response); | ||
subscriber.complete(); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
testDatasource(): Promise<TestDataSourceResponse> { | ||
return Promise.resolve({ status: 'success', message: 'Data source is working', title: 'Success' }); | ||
} | ||
} | ||
|
||
export function init() { | ||
sceneUtils.registerRuntimeDataSource({ | ||
dataSource: new WrappedLokiDatasource('wrapped-loki-ds', WRAPPED_LOKI_DS_UID), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters