TestStuffPage.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import React, { FC, useMemo, useState } from 'react';
  2. import { useObservable } from 'react-use';
  3. import AutoSizer from 'react-virtualized-auto-sizer';
  4. import {
  5. ApplyFieldOverrideOptions,
  6. DataTransformerConfig,
  7. dateMath,
  8. FieldColorModeId,
  9. NavModelItem,
  10. PanelData,
  11. } from '@grafana/data';
  12. import { Button, Table } from '@grafana/ui';
  13. import { config } from 'app/core/config';
  14. import { useAppNotification } from 'app/core/copy/appNotification';
  15. import { QueryGroupOptions } from 'app/types';
  16. import Page from '../../core/components/Page/Page';
  17. import { PanelRenderer } from '../panel/components/PanelRenderer';
  18. import { QueryGroup } from '../query/components/QueryGroup';
  19. import { PanelQueryRunner } from '../query/state/PanelQueryRunner';
  20. interface State {
  21. queryRunner: PanelQueryRunner;
  22. queryOptions: QueryGroupOptions;
  23. data?: PanelData;
  24. }
  25. export const TestStuffPage: FC = () => {
  26. const [state, setState] = useState<State>(getDefaultState());
  27. const { queryOptions, queryRunner } = state;
  28. const onRunQueries = () => {
  29. const timeRange = { from: 'now-1h', to: 'now' };
  30. queryRunner.run({
  31. queries: queryOptions.queries,
  32. datasource: queryOptions.dataSource,
  33. timezone: 'browser',
  34. timeRange: { from: dateMath.parse(timeRange.from)!, to: dateMath.parse(timeRange.to)!, raw: timeRange },
  35. maxDataPoints: queryOptions.maxDataPoints ?? 100,
  36. minInterval: queryOptions.minInterval,
  37. });
  38. };
  39. const onOptionsChange = (queryOptions: QueryGroupOptions) => {
  40. setState({ ...state, queryOptions });
  41. };
  42. /**
  43. * Subscribe to data
  44. */
  45. const observable = useMemo(() => queryRunner.getData({ withFieldConfig: true, withTransforms: true }), [queryRunner]);
  46. const data = useObservable(observable);
  47. const node: NavModelItem = {
  48. id: 'test-page',
  49. text: 'Test page',
  50. icon: 'dashboard',
  51. subTitle: 'FOR TESTING!',
  52. url: 'sandbox/test',
  53. };
  54. const notifyApp = useAppNotification();
  55. return (
  56. <Page navModel={{ node: node, main: node }}>
  57. <Page.Contents>
  58. {data && (
  59. <AutoSizer style={{ width: '100%', height: '600px' }}>
  60. {({ width }) => {
  61. return (
  62. <div>
  63. <PanelRenderer
  64. title="Hello"
  65. pluginId="timeseries"
  66. width={width}
  67. height={300}
  68. data={data}
  69. options={{}}
  70. fieldConfig={{ defaults: {}, overrides: [] }}
  71. timeZone="browser"
  72. />
  73. <Table data={data.series[0]} width={width} height={300} />
  74. </div>
  75. );
  76. }}
  77. </AutoSizer>
  78. )}
  79. <div style={{ marginTop: '16px', height: '45%' }}>
  80. <QueryGroup
  81. options={queryOptions}
  82. queryRunner={queryRunner}
  83. onRunQueries={onRunQueries}
  84. onOptionsChange={onOptionsChange}
  85. />
  86. </div>
  87. <div style={{ display: 'flex', gap: '1em' }}>
  88. <Button onClick={() => notifyApp.success('Success toast', 'some more text goes here')} variant="primary">
  89. Success
  90. </Button>
  91. <Button
  92. onClick={() => notifyApp.warning('Warning toast', 'some more text goes here', 'bogus-trace-99999')}
  93. variant="secondary"
  94. >
  95. Warning
  96. </Button>
  97. <Button
  98. onClick={() => notifyApp.error('Error toast', 'some more text goes here', 'bogus-trace-fdsfdfsfds')}
  99. variant="destructive"
  100. >
  101. Error
  102. </Button>
  103. </div>
  104. </Page.Contents>
  105. </Page>
  106. );
  107. };
  108. export function getDefaultState(): State {
  109. const options: ApplyFieldOverrideOptions = {
  110. fieldConfig: {
  111. defaults: {
  112. color: {
  113. mode: FieldColorModeId.PaletteClassic,
  114. },
  115. },
  116. overrides: [],
  117. },
  118. replaceVariables: (v: string) => v,
  119. theme: config.theme2,
  120. };
  121. const dataConfig = {
  122. getTransformations: () => [] as DataTransformerConfig[],
  123. getFieldOverrideOptions: () => options,
  124. getDataSupport: () => ({ annotations: false, alertStates: false }),
  125. };
  126. return {
  127. queryRunner: new PanelQueryRunner(dataConfig),
  128. queryOptions: {
  129. queries: [],
  130. dataSource: {
  131. name: 'gdev-testdata',
  132. },
  133. maxDataPoints: 100,
  134. },
  135. };
  136. }
  137. export default TestStuffPage;