2020-12-04 07:24:55 -06:00
|
|
|
import { ApplyFieldOverrideOptions, DataTransformerConfig, dateMath, FieldColorModeId, PanelData } from '@grafana/data';
|
2021-01-15 02:14:50 -06:00
|
|
|
import { GraphNG, LegendDisplayMode, Table } 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 { QueryGroupOptions } from '../query/components/QueryGroupOptions';
|
|
|
|
import { PanelQueryRunner } from '../query/state/PanelQueryRunner';
|
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
|
|
|
|
*/
|
|
|
|
const observable = useMemo(() => queryRunner.getData({ withFieldConfig: true, withTransforms: true }), []);
|
|
|
|
const data = useObservable(observable);
|
2020-11-26 11:12:02 -06:00
|
|
|
|
|
|
|
return (
|
2020-12-02 08:42:54 -06:00
|
|
|
<div style={{ padding: '30px 50px' }} className="page-scrollbar-wrapper">
|
|
|
|
<h3>New page</h3>
|
|
|
|
<div>
|
|
|
|
<QueryGroup
|
|
|
|
options={queryOptions}
|
|
|
|
queryRunner={queryRunner}
|
|
|
|
onRunQueries={onRunQueries}
|
2020-12-04 07:24:55 -06:00
|
|
|
onOptionsChange={onOptionsChange}
|
2020-12-02 08:42:54 -06:00
|
|
|
/>
|
|
|
|
</div>
|
2020-11-26 11:12:02 -06:00
|
|
|
|
2020-12-02 08:42:54 -06:00
|
|
|
{data && (
|
|
|
|
<div style={{ padding: '16px' }}>
|
2021-01-15 02:14:50 -06:00
|
|
|
<GraphNG
|
|
|
|
width={1200}
|
|
|
|
height={300}
|
|
|
|
data={data.series}
|
|
|
|
legend={{ displayMode: LegendDisplayMode.List, placement: 'bottom', calcs: [] }}
|
|
|
|
timeRange={data.timeRange}
|
|
|
|
timeZone="browser"
|
|
|
|
/>
|
2020-12-02 08:42:54 -06:00
|
|
|
<hr></hr>
|
|
|
|
<Table data={data.series[0]} width={1200} height={300} />
|
|
|
|
</div>
|
|
|
|
)}
|
2020-11-26 11:12:02 -06:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
theme: config.theme,
|
|
|
|
};
|
|
|
|
|
|
|
|
const dataConfig = {
|
|
|
|
getTransformations: () => [] as DataTransformerConfig[],
|
|
|
|
getFieldOverrideOptions: () => options,
|
|
|
|
};
|
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;
|