123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import React, { FC, useMemo, useState } from 'react';
- import { useObservable } from 'react-use';
- import AutoSizer from 'react-virtualized-auto-sizer';
- import {
- ApplyFieldOverrideOptions,
- DataTransformerConfig,
- dateMath,
- FieldColorModeId,
- NavModelItem,
- PanelData,
- } from '@grafana/data';
- import { Button, Table } from '@grafana/ui';
- import { config } from 'app/core/config';
- import { useAppNotification } from 'app/core/copy/appNotification';
- import { QueryGroupOptions } from 'app/types';
- import Page from '../../core/components/Page/Page';
- import { PanelRenderer } from '../panel/components/PanelRenderer';
- import { QueryGroup } from '../query/components/QueryGroup';
- import { PanelQueryRunner } from '../query/state/PanelQueryRunner';
- interface State {
- queryRunner: PanelQueryRunner;
- queryOptions: QueryGroupOptions;
- data?: PanelData;
- }
- export const TestStuffPage: FC = () => {
- const [state, setState] = useState<State>(getDefaultState());
- const { queryOptions, queryRunner } = state;
- const onRunQueries = () => {
- const timeRange = { from: 'now-1h', to: 'now' };
- queryRunner.run({
- queries: queryOptions.queries,
- datasource: queryOptions.dataSource,
- timezone: 'browser',
- timeRange: { from: dateMath.parse(timeRange.from)!, to: dateMath.parse(timeRange.to)!, raw: timeRange },
- maxDataPoints: queryOptions.maxDataPoints ?? 100,
- minInterval: queryOptions.minInterval,
- });
- };
- const onOptionsChange = (queryOptions: QueryGroupOptions) => {
- setState({ ...state, queryOptions });
- };
- /**
- * Subscribe to data
- */
- const observable = useMemo(() => queryRunner.getData({ withFieldConfig: true, withTransforms: true }), [queryRunner]);
- const data = useObservable(observable);
- const node: NavModelItem = {
- id: 'test-page',
- text: 'Test page',
- icon: 'dashboard',
- subTitle: 'FOR TESTING!',
- url: 'sandbox/test',
- };
- const notifyApp = useAppNotification();
- return (
- <Page navModel={{ node: node, main: node }}>
- <Page.Contents>
- {data && (
- <AutoSizer style={{ width: '100%', height: '600px' }}>
- {({ width }) => {
- return (
- <div>
- <PanelRenderer
- title="Hello"
- pluginId="timeseries"
- width={width}
- height={300}
- data={data}
- options={{}}
- fieldConfig={{ defaults: {}, overrides: [] }}
- 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}
- />
- </div>
- <div style={{ display: 'flex', gap: '1em' }}>
- <Button onClick={() => notifyApp.success('Success toast', 'some more text goes here')} variant="primary">
- Success
- </Button>
- <Button
- onClick={() => notifyApp.warning('Warning toast', 'some more text goes here', 'bogus-trace-99999')}
- variant="secondary"
- >
- Warning
- </Button>
- <Button
- onClick={() => notifyApp.error('Error toast', 'some more text goes here', 'bogus-trace-fdsfdfsfds')}
- variant="destructive"
- >
- Error
- </Button>
- </div>
- </Page.Contents>
- </Page>
- );
- };
- export function getDefaultState(): State {
- const options: ApplyFieldOverrideOptions = {
- fieldConfig: {
- defaults: {
- color: {
- mode: FieldColorModeId.PaletteClassic,
- },
- },
- overrides: [],
- },
- replaceVariables: (v: string) => v,
- theme: config.theme2,
- };
- const dataConfig = {
- getTransformations: () => [] as DataTransformerConfig[],
- getFieldOverrideOptions: () => options,
- getDataSupport: () => ({ annotations: false, alertStates: false }),
- };
- return {
- queryRunner: new PanelQueryRunner(dataConfig),
- queryOptions: {
- queries: [],
- dataSource: {
- name: 'gdev-testdata',
- },
- maxDataPoints: 100,
- },
- };
- }
- export default TestStuffPage;
|