suggestions.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { VisualizationSuggestionsBuilder } from '@grafana/data';
  2. import { BigValueColorMode, BigValueGraphMode } from '@grafana/ui';
  3. import { SuggestionName } from 'app/types/suggestions';
  4. import { StatPanelOptions } from './types';
  5. export class StatSuggestionsSupplier {
  6. getSuggestionsForData(builder: VisualizationSuggestionsBuilder) {
  7. const { dataSummary: ds } = builder;
  8. if (!ds.hasData) {
  9. return;
  10. }
  11. const list = builder.getListAppender<StatPanelOptions, {}>({
  12. name: SuggestionName.Stat,
  13. pluginId: 'stat',
  14. options: {},
  15. fieldConfig: {
  16. defaults: {
  17. unit: 'short',
  18. custom: {},
  19. },
  20. overrides: [],
  21. },
  22. cardOptions: {
  23. previewModifier: (s) => {
  24. if (s.options!.reduceOptions.values) {
  25. s.options!.reduceOptions.limit = 1;
  26. }
  27. },
  28. },
  29. });
  30. // String and number field with low row count show individual rows
  31. if (ds.hasStringField && ds.hasNumberField && ds.frameCount === 1 && ds.rowCountTotal < 10) {
  32. list.append({
  33. name: SuggestionName.Stat,
  34. options: {
  35. reduceOptions: {
  36. values: true,
  37. calcs: [],
  38. fields: '/.*/',
  39. },
  40. },
  41. });
  42. list.append({
  43. name: SuggestionName.StatColoredBackground,
  44. options: {
  45. reduceOptions: {
  46. values: true,
  47. calcs: [],
  48. fields: '/.*/',
  49. },
  50. colorMode: BigValueColorMode.Background,
  51. },
  52. });
  53. }
  54. // Just a single string field
  55. if (ds.stringFieldCount === 1 && ds.frameCount === 1 && ds.rowCountTotal < 10 && ds.fieldCount === 1) {
  56. list.append({
  57. name: SuggestionName.Stat,
  58. options: {
  59. reduceOptions: {
  60. values: true,
  61. calcs: [],
  62. fields: '/.*/',
  63. },
  64. colorMode: BigValueColorMode.None,
  65. },
  66. });
  67. }
  68. if (ds.hasNumberField && ds.hasTimeField) {
  69. list.append({
  70. options: {
  71. reduceOptions: {
  72. values: false,
  73. calcs: ['lastNotNull'],
  74. },
  75. },
  76. });
  77. list.append({
  78. name: SuggestionName.StatColoredBackground,
  79. options: {
  80. reduceOptions: {
  81. values: false,
  82. calcs: ['lastNotNull'],
  83. },
  84. graphMode: BigValueGraphMode.None,
  85. colorMode: BigValueColorMode.Background,
  86. },
  87. });
  88. }
  89. }
  90. }