SingleStatPanel.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Libraries
  2. import React, { PureComponent, CSSProperties } from 'react';
  3. // Types
  4. import { SingleStatOptions, SingleStatBaseOptions } from './types';
  5. import { DisplayValue, PanelProps, NullValueMode, calculateStats } 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 display = 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. return data.map(table => {
  23. // Support last_time? Add that to the migration? last, but differnt column
  24. if (stat === 'name') {
  25. // Should not really be possible anymore?
  26. return display(table.name);
  27. }
  28. return display(
  29. calculateStats({
  30. table,
  31. columnIndex: 0, // Hardcoded for now!
  32. stats: [stat], // The stats to calculate
  33. nullValueMode: NullValueMode.Null,
  34. })[stat]
  35. );
  36. });
  37. };
  38. export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>> {
  39. renderValue = (value: DisplayValue, width: number, height: number): JSX.Element => {
  40. const style: CSSProperties = {};
  41. style.margin = '0 auto';
  42. style.fontSize = '250%';
  43. style.textAlign = 'center';
  44. if (value.color) {
  45. style.color = value.color;
  46. }
  47. return (
  48. <div style={{ width, height }}>
  49. <div style={style}>{value.text}</div>
  50. </div>
  51. );
  52. };
  53. getProcessedValues = (): DisplayValue[] => {
  54. return getSingleStatValues(this.props);
  55. };
  56. render() {
  57. const { height, width, options, data, renderCounter } = this.props;
  58. return (
  59. <ProcessedValuesRepeater
  60. getProcessedValues={this.getProcessedValues}
  61. renderValue={this.renderValue}
  62. width={width}
  63. height={height}
  64. source={data}
  65. renderCounter={renderCounter}
  66. orientation={options.orientation}
  67. />
  68. );
  69. }
  70. }