RuleTest.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { css } from '@emotion/css';
  2. import React, { useState } from 'react';
  3. import { dataFrameFromJSON, getDisplayProcessor, GrafanaTheme } from '@grafana/data';
  4. import { getBackendSrv, config } from '@grafana/runtime';
  5. import { Button, CodeEditor, Table, useStyles, Field } from '@grafana/ui';
  6. import { ChannelFrame, Rule } from './types';
  7. interface Props {
  8. rule: Rule;
  9. }
  10. export const RuleTest: React.FC<Props> = (props) => {
  11. const [response, setResponse] = useState<ChannelFrame[]>();
  12. const [data, setData] = useState<string>();
  13. const styles = useStyles(getStyles);
  14. const onBlur = (text: string) => {
  15. setData(text);
  16. };
  17. const onClick = () => {
  18. getBackendSrv()
  19. .post(`api/live/pipeline-convert-test`, {
  20. channelRules: [props.rule],
  21. channel: props.rule.pattern,
  22. data: data,
  23. })
  24. .then((data: any) => {
  25. const t = data.channelFrames as any[];
  26. if (t) {
  27. setResponse(
  28. t.map((f) => {
  29. const frame = dataFrameFromJSON(f.frame);
  30. for (const field of frame.fields) {
  31. field.display = getDisplayProcessor({ field, theme: config.theme2 });
  32. }
  33. return { channel: f.channel, frame };
  34. })
  35. );
  36. }
  37. })
  38. .catch((e) => {
  39. setResponse(e);
  40. });
  41. };
  42. return (
  43. <div>
  44. <CodeEditor
  45. height={100}
  46. value=""
  47. showLineNumbers={true}
  48. readOnly={false}
  49. language="json"
  50. showMiniMap={false}
  51. onBlur={onBlur}
  52. />
  53. <Button onClick={onClick} className={styles.margin}>
  54. Test
  55. </Button>
  56. {response?.length &&
  57. response.map((r) => (
  58. <Field key={r.channel} label={r.channel}>
  59. <Table data={r.frame} width={700} height={Math.min(10 * r.frame.length + 10, 150)} showTypeIcons></Table>
  60. </Field>
  61. ))}
  62. </div>
  63. );
  64. };
  65. const getStyles = (theme: GrafanaTheme) => {
  66. return {
  67. margin: css`
  68. margin-bottom: 15px;
  69. `,
  70. };
  71. };