2021-04-21 06:57:17 -05:00
|
|
|
import {
|
|
|
|
ApplyFieldOverrideOptions,
|
|
|
|
DataTransformerConfig,
|
|
|
|
dateMath,
|
|
|
|
FieldColorModeId,
|
|
|
|
NavModelItem,
|
|
|
|
PanelData,
|
|
|
|
} from '@grafana/data';
|
2021-05-05 03:44:31 -05:00
|
|
|
import { LegendDisplayMode, Table, TimeSeries } from '@grafana/ui';
|
2020-12-02 08:42:54 -06:00
|
|
|
import { config } from 'app/core/config';
|
|
|
|
import React, { FC, useMemo, useState } from 'react';
|
|
|
|
import { useObservable } from 'react-use';
|
|
|
|
import { QueryGroup } from '../query/components/QueryGroup';
|
|
|
|
import { PanelQueryRunner } from '../query/state/PanelQueryRunner';
|
2021-01-19 07:04:54 -06:00
|
|
|
import { QueryGroupOptions } from 'app/types';
|
2021-04-21 06:57:17 -05:00
|
|
|
import Page from '../../core/components/Page/Page';
|
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
2020-11-26 11:12:02 -06:00
|
|
|
|
|
|
|
interface State {
|
2020-12-02 08:42:54 -06:00
|
|
|
queryRunner: PanelQueryRunner;
|
|
|
|
queryOptions: QueryGroupOptions;
|
|
|
|
data?: PanelData;
|
2020-11-26 11:12:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export const TestStuffPage: FC = () => {
|
2020-12-02 08:42:54 -06:00
|
|
|
const [state, setState] = useState<State>(getDefaultState());
|
2020-12-04 07:24:55 -06:00
|
|
|
const { queryOptions, queryRunner } = state;
|
2020-12-02 08:42:54 -06:00
|
|
|
|
|
|
|
const onRunQueries = () => {
|
|
|
|
const timeRange = { from: 'now-1h', to: 'now' };
|
|
|
|
|
|
|
|
queryRunner.run({
|
2020-12-04 07:24:55 -06:00
|
|
|
queries: queryOptions.queries,
|
|
|
|
datasource: queryOptions.dataSource.name!,
|
2020-12-02 08:42:54 -06:00
|
|
|
timezone: 'browser',
|
|
|
|
timeRange: { from: dateMath.parse(timeRange.from)!, to: dateMath.parse(timeRange.to)!, raw: timeRange },
|
|
|
|
maxDataPoints: queryOptions.maxDataPoints ?? 100,
|
|
|
|
minInterval: queryOptions.minInterval,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-04 07:24:55 -06:00
|
|
|
const onOptionsChange = (queryOptions: QueryGroupOptions) => {
|
2020-12-02 08:42:54 -06:00
|
|
|
setState({ ...state, queryOptions });
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to data
|
|
|
|
*/
|
2021-03-25 06:42:14 -05:00
|
|
|
const observable = useMemo(() => queryRunner.getData({ withFieldConfig: true, withTransforms: true }), [queryRunner]);
|
2020-12-02 08:42:54 -06:00
|
|
|
const data = useObservable(observable);
|
2020-11-26 11:12:02 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
const node: NavModelItem = {
|
|
|
|
id: 'test-page',
|
|
|
|
text: 'Test page',
|
|
|
|
icon: 'dashboard',
|
|
|
|
subTitle: 'FOR TESTING!',
|
|
|
|
url: 'sandbox/test',
|
|
|
|
};
|
2020-11-26 11:12:02 -06:00
|
|
|
|
2021-04-21 06:57:17 -05:00
|
|
|
return (
|
|
|
|
<Page navModel={{ node: node, main: node }}>
|
|
|
|
<Page.Contents>
|
|
|
|
{data && (
|
|
|
|
<AutoSizer style={{ width: '100%', height: '600px' }}>
|
|
|
|
{({ width }) => {
|
|
|
|
return (
|
|
|
|
<div>
|
2021-05-05 03:44:31 -05:00
|
|
|
<TimeSeries
|
2021-04-21 06:57:17 -05:00
|
|
|
width={width}
|
|
|
|
height={300}
|
2021-05-05 03:44:31 -05:00
|
|
|
frames={data.series}
|
2021-04-21 06:57:17 -05:00
|
|
|
legend={{ displayMode: LegendDisplayMode.List, placement: 'bottom', calcs: [] }}
|
|
|
|
timeRange={data.timeRange}
|
|
|
|
timeZone="browser"
|
|
|
|
/>
|
|
|
|
<Table data={data.series[0]} width={width} height={300} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</AutoSizer>
|
|
|
|
)}
|
|
|
|
<div style={{ marginTop: '16px', height: '45%' }}>
|
|
|
|
<QueryGroup
|
|
|
|
options={queryOptions}
|
|
|
|
queryRunner={queryRunner}
|
|
|
|
onRunQueries={onRunQueries}
|
|
|
|
onOptionsChange={onOptionsChange}
|
2021-01-15 02:14:50 -06:00
|
|
|
/>
|
2020-12-02 08:42:54 -06:00
|
|
|
</div>
|
2021-04-21 06:57:17 -05:00
|
|
|
</Page.Contents>
|
|
|
|
</Page>
|
2020-11-26 11:12:02 -06:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getDefaultState(): State {
|
2020-12-02 08:42:54 -06:00
|
|
|
const options: ApplyFieldOverrideOptions = {
|
|
|
|
fieldConfig: {
|
|
|
|
defaults: {
|
|
|
|
color: {
|
|
|
|
mode: FieldColorModeId.PaletteClassic,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
overrides: [],
|
|
|
|
},
|
|
|
|
replaceVariables: (v: string) => v,
|
2021-04-29 05:44:06 -05:00
|
|
|
theme: config.theme2,
|
2020-12-02 08:42:54 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
const dataConfig = {
|
|
|
|
getTransformations: () => [] as DataTransformerConfig[],
|
|
|
|
getFieldOverrideOptions: () => options,
|
2021-05-03 01:52:05 -05:00
|
|
|
getDataSupport: () => ({ annotations: false, alertStates: false }),
|
2020-12-02 08:42:54 -06:00
|
|
|
};
|
2020-11-26 11:12:02 -06:00
|
|
|
|
|
|
|
return {
|
2020-12-02 08:42:54 -06:00
|
|
|
queryRunner: new PanelQueryRunner(dataConfig),
|
|
|
|
queryOptions: {
|
2020-12-04 07:24:55 -06:00
|
|
|
queries: [],
|
|
|
|
dataSource: {
|
|
|
|
name: 'gdev-testdata',
|
|
|
|
},
|
2020-12-02 08:42:54 -06:00
|
|
|
maxDataPoints: 100,
|
|
|
|
},
|
2020-11-26 11:12:02 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TestStuffPage;
|