PrepareTimeSeriesEditor.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { css } from '@emotion/css';
  2. import React, { useCallback } from 'react';
  3. import { GrafanaTheme2, SelectableValue, TransformerRegistryItem, TransformerUIProps } from '@grafana/data';
  4. import { InlineField, InlineFieldRow, Select, useStyles2 } from '@grafana/ui';
  5. import { prepareTimeSeriesTransformer, PrepareTimeSeriesOptions, timeSeriesFormat } from './prepareTimeSeries';
  6. const wideInfo = {
  7. label: 'Wide time series',
  8. value: timeSeriesFormat.TimeSeriesWide,
  9. description: 'Creates a single frame joined by time',
  10. info: (
  11. <ul>
  12. <li>Single frame</li>
  13. <li>1st field is shared time field</li>
  14. <li>Time in ascending order</li>
  15. <li>Multiple value fields of any type</li>
  16. </ul>
  17. ),
  18. };
  19. const manyInfo = {
  20. label: 'Multi-frame time series',
  21. value: timeSeriesFormat.TimeSeriesMany,
  22. description: 'Creates a new frame for each time/number pair',
  23. info: (
  24. <ul>
  25. <li>Multiple frames</li>
  26. <li>Each frame has two fields: time, value</li>
  27. <li>Time in ascending order</li>
  28. <li>String values are represented as labels</li>
  29. <li>All values are numeric</li>
  30. </ul>
  31. ),
  32. };
  33. const longInfo = {
  34. label: 'Long time series',
  35. value: timeSeriesFormat.TimeSeriesLong,
  36. description: 'Convert each frame to long format',
  37. info: (
  38. <ul>
  39. <li>Single frame</li>
  40. <li>1st field is time field</li>
  41. <li>Time in ascending order, but may have duplictes</li>
  42. <li>String values are represented as separate fields rather than as labels</li>
  43. <li>Multiple value fields may exist</li>
  44. </ul>
  45. ),
  46. };
  47. const formats: Array<SelectableValue<timeSeriesFormat>> = [wideInfo, manyInfo, longInfo];
  48. export function PrepareTimeSeriesEditor(props: TransformerUIProps<PrepareTimeSeriesOptions>): React.ReactElement {
  49. const { options, onChange } = props;
  50. const styles = useStyles2(getStyles);
  51. const onSelectFormat = useCallback(
  52. (value: SelectableValue<timeSeriesFormat>) => {
  53. onChange({
  54. ...options,
  55. format: value.value!,
  56. });
  57. },
  58. [onChange, options]
  59. );
  60. return (
  61. <>
  62. <InlineFieldRow>
  63. <InlineField label="Format" labelWidth={12}>
  64. <Select
  65. width={35}
  66. options={formats}
  67. value={formats.find((v) => v.value === options.format) || formats[0]}
  68. onChange={onSelectFormat}
  69. className="flex-grow-1"
  70. />
  71. </InlineField>
  72. </InlineFieldRow>
  73. <InlineFieldRow>
  74. <InlineField label="Info" labelWidth={12}>
  75. <div className={styles.info}>{(formats.find((v) => v.value === options.format) || formats[0]).info}</div>
  76. </InlineField>
  77. </InlineFieldRow>
  78. </>
  79. );
  80. }
  81. const getStyles = (theme: GrafanaTheme2) => ({
  82. info: css`
  83. margin-left: 20px;
  84. `,
  85. });
  86. export const prepareTimeseriesTransformerRegistryItem: TransformerRegistryItem<PrepareTimeSeriesOptions> = {
  87. id: prepareTimeSeriesTransformer.id,
  88. editor: PrepareTimeSeriesEditor,
  89. transformation: prepareTimeSeriesTransformer,
  90. name: prepareTimeSeriesTransformer.name,
  91. description: prepareTimeSeriesTransformer.description,
  92. help: `
  93. ### Use cases
  94. This takes query results and transforms them into a predictable timeseries format.
  95. This transformer may be especially useful when using old panels that only expect the
  96. many-frame timeseries format.
  97. `,
  98. };