SingleStatPanel.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Libraries
  2. import React, { PureComponent, CSSProperties } from 'react';
  3. // Types
  4. import { SingleStatOptions, SingleStatBaseOptions } from './types';
  5. import { DisplayValue, PanelProps, processTimeSeries, NullValueMode, guessColumnTypes, ColumnType } from '@grafana/ui';
  6. import { config } from 'app/core/config';
  7. import { getDisplayProcessor } from '@grafana/ui';
  8. import { ProcessedValuesRepeater } from './ProcessedValuesRepeater';
  9. export const getSingleStatValues = (props: PanelProps<SingleStatBaseOptions>): DisplayValue[] => {
  10. const { data, replaceVariables, options } = props;
  11. const { valueOptions, valueMappings } = options;
  12. const { unit, decimals, stat } = valueOptions;
  13. const processor = getDisplayProcessor({
  14. unit,
  15. decimals,
  16. mappings: valueMappings,
  17. thresholds: options.thresholds,
  18. prefix: replaceVariables(valueOptions.prefix),
  19. suffix: replaceVariables(valueOptions.suffix),
  20. theme: config.theme,
  21. });
  22. const values: DisplayValue[] = [];
  23. for (let t = 0; t < data.length; t++) {
  24. const table = guessColumnTypes(data[t]);
  25. for (let i = 0; i < table.columns.length; i++) {
  26. const column = table.columns[i];
  27. // Show all columns that are not 'time'
  28. if (column.type === ColumnType.number) {
  29. const series = processTimeSeries({
  30. data: [table],
  31. xColumn: i,
  32. yColumn: i,
  33. nullValueMode: NullValueMode.Null,
  34. })[0];
  35. const value = stat !== 'name' ? series.stats[stat] : series.label;
  36. values.push(processor(value));
  37. }
  38. }
  39. }
  40. if (values.length === 0) {
  41. throw { message: 'Could not find numeric data' };
  42. }
  43. return values;
  44. };
  45. export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
  46. renderValue = (value: DisplayValue, width: number, height: number): JSX.Element => {
  47. const style: CSSProperties = {};
  48. style.margin = '0 auto';
  49. style.fontSize = '250%';
  50. style.textAlign = 'center';
  51. if (value.color) {
  52. style.color = value.color;
  53. }
  54. return (
  55. <div style={{ width, height }}>
  56. <div style={style}>{value.text}</div>
  57. </div>
  58. );
  59. };
  60. getProcessedValues = (): DisplayValue[] => {
  61. return getSingleStatValues(this.props);
  62. };
  63. render() {
  64. const { height, width, options, data, renderCounter } = this.props;
  65. return (
  66. <ProcessedValuesRepeater
  67. getProcessedValues={this.getProcessedValues}
  68. renderValue={this.renderValue}
  69. width={width}
  70. height={height}
  71. source={data}
  72. renderCounter={renderCounter}
  73. orientation={options.orientation}
  74. />
  75. );
  76. }
  77. }