Files
grafana/public/app/plugins/datasource/dashboard/DashboardQueryEditor.tsx

198 lines
5.5 KiB
TypeScript
Raw Normal View History

// Libraries
import React, { PureComponent } from 'react';
// Types
import { Select } from '@grafana/ui';
import { DataQuery, DataQueryError, PanelData, DataFrame, SelectableValue } from '@grafana/data';
import { DashboardQuery } from './types';
import config from 'app/core/config';
import { css } from 'emotion';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import { PanelModel } from 'app/features/dashboard/state';
QueryProcessing: Observable query interface and RxJS for query & stream processing (#18899) * I needed to learn some rxjs and understand this more, so just playing around * Updated * Removed all the complete calls * Refactoring * StreamHandler -> observable start * progress * simple singal works * Handle update time range * added error handling * wrap old function * minor changes * handle data format in the subscribe function * Use replay subject to return last value to subscribers * Set loading state after no response in 50ms * added missing file * updated comment * Added cancelation of network requests * runRequest: Added unit test scenario framework * Progress on tests * minor refactor of unit tests * updated test * removed some old code * Shared queries work again, and also became so much simplier * unified query and observe methods * implict any fix * Fixed closed subject issue * removed comment * Use last returned data for loading state * WIP: Explore to runRequest makover step1 * Minor progress * Minor progress on explore and runRequest * minor progress * Things are starting to work in explore * Updated prometheus to use new observable query response, greatly simplified code * Revert refId change * Found better solution for key/refId/requestId problem * use observable with loki * tests compile * fix loki query prep * Explore: correct first response handling * Refactorings * Refactoring * Explore: Fixes LoadingState and GraphResults between runs (#18986) * Refactor: Adds state to DataQueryResponse * Fix: Fixes so we do not empty results before new data arrives Fixes: #17409 * Transformations work * observable test data * remove single() from loki promise * Fixed comment * Explore: Fixes failing Loki and Prometheus unit tests (#18995) * Tests: Makes datasource tests work again * Fix: Fixes loki datasource so highligthing works * Chore: Runs Prettier * Fixed query runner tests * Delay loading state indication to 200ms * Fixed test * fixed unit tests * Clear cached calcs * Fixed bug getProcesedDataFrames * Fix the correct test is a better idea * Fix: Fixes so queries in Explore are only run if Graph/Table is shown (#19000) * Fix: Fixes so queries in Explore are only run if Graph/Table is shown Fixes: #18618 * Refactor: Removes unnecessary condition * PanelData: provide legacy data only when needed (#19018) * no legacy * invert logic... now compiles * merge getQueryResponseData and getDataRaw * update comment about query editor * use single getData() function * only send legacy when it is used in explore * pre process rather than post process * pre process rather than post process * Minor refactoring * Add missing tags to test datasource response * MixedDatasource: Adds query observable pattern to MixedDatasource (#19037) * start mixed datasource * Refactor: Refactors into observable parttern * Tests: Fixes tests * Tests: Removes console.log * Refactor: Adds unique requestId
2019-09-12 17:28:46 +02:00
import { SHARED_DASHBODARD_QUERY } from './types';
import { getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { filterPanelDataToQuery } from 'app/features/dashboard/panel_editor/QueryEditorRow';
type ResultInfo = {
img: string; // The Datasource
refId: string;
query: string; // As text
data: DataFrame[];
error?: DataQueryError;
};
function getQueryDisplayText(query: DataQuery): string {
return JSON.stringify(query);
}
interface Props {
panel: PanelModel;
panelData: PanelData;
onChange: (query: DashboardQuery) => void;
}
type State = {
defaultDatasource: string;
results: ResultInfo[];
};
export class DashboardQueryEditor extends PureComponent<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
defaultDatasource: '',
results: [],
};
}
getQuery(): DashboardQuery {
const { panel } = this.props;
return panel.targets[0] as DashboardQuery;
}
async componentDidMount() {
this.componentDidUpdate(null);
}
async componentDidUpdate(prevProps: Props) {
const { panelData } = this.props;
if (!prevProps || prevProps.panelData !== panelData) {
const query = this.props.panel.targets[0] as DashboardQuery;
const defaultDS = await getDatasourceSrv().get(null);
const dashboard = getDashboardSrv().getCurrent();
const panel = dashboard.getPanelById(query.panelId);
if (!panel) {
this.setState({ defaultDatasource: defaultDS.name });
return;
}
const mainDS = await getDatasourceSrv().get(panel.datasource);
const info: ResultInfo[] = [];
for (const query of panel.targets) {
const ds = query.datasource ? await getDatasourceSrv().get(query.datasource) : mainDS;
const fmt = ds.getQueryDisplayText ? ds.getQueryDisplayText : getQueryDisplayText;
const qData = filterPanelDataToQuery(panelData, query.refId);
const queryData = qData ? qData : panelData;
info.push({
refId: query.refId,
query: fmt(query),
img: ds.meta.info.logos.small,
data: queryData.series,
error: queryData.error,
});
}
this.setState({ defaultDatasource: defaultDS.name, results: info });
}
}
onPanelChanged = (id: number) => {
const { onChange } = this.props;
const query = this.getQuery();
query.panelId = id;
onChange(query);
// Update the
this.props.panel.refresh();
};
renderQueryData(editURL: string) {
const { results } = this.state;
return (
<div>
{results.map((target, index) => {
return (
<div className="query-editor-row__header" key={index}>
<div className="query-editor-row__ref-id">
<img src={target.img} width={16} className={css({ marginRight: '8px' })} />
{target.refId}:
</div>
<div className="query-editor-row__collapsed-text">
<a href={editURL}>
{target.query}
&nbsp;
<i className="fa fa-external-link" />
</a>
</div>
</div>
);
})}
</div>
);
}
getPanelDescription = (panel: PanelModel): string => {
const { defaultDatasource } = this.state;
const dsname = panel.datasource ? panel.datasource : defaultDatasource;
if (panel.targets.length === 1) {
return '1 query to ' + dsname;
}
return panel.targets.length + ' queries to ' + dsname;
};
render() {
const dashboard = getDashboardSrv().getCurrent();
const query = this.getQuery();
let selected: SelectableValue<number>;
const panels: Array<SelectableValue<number>> = [];
for (const panel of dashboard.panels) {
const plugin = config.panels[panel.type];
if (!plugin) {
continue;
}
if (panel.targets && panel.datasource !== SHARED_DASHBODARD_QUERY) {
const item = {
value: panel.id,
label: panel.title ? panel.title : 'Panel ' + panel.id,
description: this.getPanelDescription(panel),
imgUrl: plugin.info.logos.small,
};
panels.push(item);
if (query.panelId === panel.id) {
selected = item;
}
}
}
if (panels.length < 1) {
return (
<div className={css({ padding: '10px' })}>
This dashboard does not have other panels. Add queries to other panels and try again
</div>
);
}
// Same as current URL, but different panelId
const editURL = `d/${dashboard.uid}/${dashboard.title}?&fullscreen&edit&panelId=${query.panelId}`;
return (
<div>
<div className="gf-form">
<div className="gf-form-label">Use results from panel</div>
<Select
placeholder="Choose Panel"
isSearchable={true}
options={panels}
value={selected}
onChange={item => this.onPanelChanged(item.value)}
/>
</div>
<div className={css({ padding: '16px' })}>{query.panelId && this.renderQueryData(editURL)}</div>
</div>
);
}
}