3
0
mirror of https://github.com/grafana/grafana.git synced 2025-02-25 18:55:37 -06:00
grafana/public/app/plugins/datasource/dashboard/runSharedRequest.ts
Dominik Prokop 9b7748ec13
Chore: Reorg packages ()
Primarily- moving majority of the types and utils from @grafana/ui to @grafana/data

* Move types from grafana-ui to grafana-data

* Move valueFormats to grafana-data

* Move utils from grafana-ui to grafana-data

* Update imports in grafana-ui

* revert data's tsconfig change

* Update imports in grafana-runtime

* Fix import paths in grafana-ui

* Move rxjs to devDeps

* Core import updates batch 1

* Import updates batch 2

* Imports fix batch 3

* Imports fixes batch i don't know

* Fix imorts in grafana-toolkit

* Fix imports after master merge
2019-10-31 10:48:05 +01:00

80 lines
2.4 KiB
TypeScript

import { Observable } from 'rxjs';
import { QueryRunnerOptions } from 'app/features/dashboard/state/PanelQueryRunner';
import { DashboardQuery, SHARED_DASHBODARD_QUERY } from './types';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { LoadingState, DefaultTimeRange, DataQuery, PanelData, DataSourceApi } from '@grafana/data';
export function isSharedDashboardQuery(datasource: string | DataSourceApi) {
if (!datasource) {
// default datasource
return false;
}
if (datasource === SHARED_DASHBODARD_QUERY) {
return true;
}
const ds = datasource as DataSourceApi;
return ds.meta && ds.meta.name === SHARED_DASHBODARD_QUERY;
}
export function runSharedRequest(options: QueryRunnerOptions): Observable<PanelData> {
return new Observable<PanelData>(subscriber => {
const dashboard = getDashboardSrv().getCurrent();
const listenToPanelId = getPanelIdFromQuery(options.queries);
if (!listenToPanelId) {
subscriber.next(getQueryError('Missing panel reference ID'));
return null;
}
const currentPanel = dashboard.getPanelById(options.panelId);
const listenToPanel = dashboard.getPanelById(listenToPanelId);
if (!listenToPanel) {
subscriber.next(getQueryError('Unknown Panel: ' + listenToPanelId));
return null;
}
const listenToRunner = listenToPanel.getQueryRunner();
const subscription = listenToRunner.getData(false).subscribe({
next: (data: PanelData) => {
console.log('got data from other panel', data);
subscriber.next(data);
},
});
// If we are in fullscreen the other panel will not execute any queries
// So we have to trigger it from here
if (currentPanel.fullscreen) {
const { datasource, targets } = listenToPanel;
const modified = {
...options,
datasource,
panelId: listenToPanelId,
queries: targets,
};
listenToRunner.run(modified);
}
return () => {
console.log('runSharedRequest unsubscribe');
subscription.unsubscribe();
};
});
}
function getPanelIdFromQuery(queries: DataQuery[]): number | undefined {
if (!queries || !queries.length) {
return undefined;
}
return (queries[0] as DashboardQuery).panelId;
}
function getQueryError(msg: string): PanelData {
return {
state: LoadingState.Error,
series: [],
error: { message: msg },
timeRange: DefaultTimeRange,
};
}