mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Public Dashboards: Pubdash panels get data from pubdash api (#50556)
* Public dashboard query API * Create new API on service for building metric request * Flesh out testing, implement BuildPublicDashboardMetricRequest * Test for errors and missing panels * WIP: Test for multiple datasources * Refactor tests, add supporting code for multiple datasources * Gets the panel data from the pubdash query api * Adds tests to make sure we get the correct api url from retrieving panel data * Public dashboard query API * Create new API on service for building metric request * Flesh out testing, implement BuildPublicDashboardMetricRequest * Test for errors and missing panels * WIP: Test for multiple datasources * Refactor tests, add supporting code for multiple datasources * Handle queries from multiple datasources * Replace dashboard time range with pubdash time range settings * Fix comments from review, build failure * removes changes to DataSourceWithBackend.ts regarding getting the pubdash panel query url. Going to do this in a new class, PublicDashboardDataSource.ts * Include pubdash Uid in dashboard meta * Creates new PublicDashboardDataSource.ts and adds test * Passes pubdash uid down to PanelQueryRunner.ts to a PublicDashboardDatasource can be chosen when were looking at a public dashboard * removes comment * checks for error when unmarshalling json * Only replace dashboard time settings with pubdash time settings when pubdash time settings exist * formatting and added comment Co-authored-by: Jesse Weaver <jesse.weaver@grafana.com> Co-authored-by: Jeff Levin <jeff@levinology.com>
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import { catchError, Observable, of, switchMap } from 'rxjs';
|
||||
|
||||
import { DataQuery, DataQueryRequest, DataQueryResponse, DataSourceApi, PluginMeta } from '@grafana/data';
|
||||
import { BackendDataSourceResponse, getBackendSrv, toDataQueryResponse } from '@grafana/runtime';
|
||||
|
||||
export class PublicDashboardDataSource extends DataSourceApi<any> {
|
||||
constructor() {
|
||||
super({
|
||||
name: 'public-ds',
|
||||
id: 1,
|
||||
type: 'public-ds',
|
||||
meta: {} as PluginMeta,
|
||||
uid: '1',
|
||||
jsonData: {},
|
||||
access: 'proxy',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Ideally final -- any other implementation may not work as expected
|
||||
*/
|
||||
query(request: DataQueryRequest<any>): Observable<DataQueryResponse> {
|
||||
const { intervalMs, maxDataPoints, range, requestId, publicDashboardUid, panelId } = request;
|
||||
let targets = request.targets;
|
||||
|
||||
const queries = targets.map((q) => {
|
||||
return {
|
||||
...q,
|
||||
publicDashboardUid,
|
||||
intervalMs,
|
||||
maxDataPoints,
|
||||
};
|
||||
});
|
||||
|
||||
// Return early if no queries exist
|
||||
if (!queries.length) {
|
||||
return of({ data: [] });
|
||||
}
|
||||
|
||||
const body: any = { queries, publicDashboardUid, panelId };
|
||||
|
||||
if (range) {
|
||||
body.range = range;
|
||||
body.from = range.from.valueOf().toString();
|
||||
body.to = range.to.valueOf().toString();
|
||||
}
|
||||
|
||||
return getBackendSrv()
|
||||
.fetch<BackendDataSourceResponse>({
|
||||
url: `/api/public/dashboards/${publicDashboardUid}/panels/${panelId}/query`,
|
||||
method: 'POST',
|
||||
data: body,
|
||||
requestId,
|
||||
})
|
||||
.pipe(
|
||||
switchMap((raw) => {
|
||||
return of(toDataQueryResponse(raw, queries as DataQuery[]));
|
||||
}),
|
||||
catchError((err) => {
|
||||
return of(toDataQueryResponse(err));
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
testDatasource(): Promise<any> {
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user