InspectStatsTable.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import {
  4. FieldType,
  5. formattedValueToString,
  6. getDisplayProcessor,
  7. GrafanaTheme2,
  8. QueryResultMetaStat,
  9. TimeZone,
  10. } from '@grafana/data';
  11. import { stylesFactory, useTheme2 } from '@grafana/ui';
  12. interface InspectStatsTableProps {
  13. timeZone: TimeZone;
  14. name: string;
  15. stats: QueryResultMetaStat[];
  16. }
  17. export const InspectStatsTable: React.FC<InspectStatsTableProps> = ({ timeZone, name, stats }) => {
  18. const theme = useTheme2();
  19. const styles = getStyles(theme);
  20. if (!stats || !stats.length) {
  21. return null;
  22. }
  23. return (
  24. <div className={styles.wrapper}>
  25. <div className="section-heading">{name}</div>
  26. <table className="filter-table width-30">
  27. <tbody>
  28. {stats.map((stat, index) => {
  29. return (
  30. <tr key={`${stat.displayName}-${index}`}>
  31. <td>{stat.displayName}</td>
  32. <td className={styles.cell}>{formatStat(stat, timeZone, theme)}</td>
  33. </tr>
  34. );
  35. })}
  36. </tbody>
  37. </table>
  38. </div>
  39. );
  40. };
  41. function formatStat(stat: QueryResultMetaStat, timeZone: TimeZone, theme: GrafanaTheme2): string {
  42. const display = getDisplayProcessor({
  43. field: {
  44. type: FieldType.number,
  45. config: stat,
  46. },
  47. theme,
  48. timeZone,
  49. });
  50. return formattedValueToString(display(stat.value));
  51. }
  52. const getStyles = stylesFactory((theme: GrafanaTheme2) => {
  53. return {
  54. wrapper: css`
  55. padding-bottom: ${theme.spacing(2)};
  56. `,
  57. cell: css`
  58. text-align: right;
  59. `,
  60. };
  61. });