suggestions.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { ThresholdsMode, VisualizationSuggestionsBuilder } from '@grafana/data';
  2. import { SuggestionName } from 'app/types/suggestions';
  3. import { GaugeOptions } from './types';
  4. export class GaugeSuggestionsSupplier {
  5. getSuggestionsForData(builder: VisualizationSuggestionsBuilder) {
  6. const { dataSummary } = builder;
  7. if (!dataSummary.hasData || !dataSummary.hasNumberField) {
  8. return;
  9. }
  10. // for many fields / series this is probably not a good fit
  11. if (dataSummary.numberFieldCount >= 50) {
  12. return;
  13. }
  14. const list = builder.getListAppender<GaugeOptions, {}>({
  15. name: SuggestionName.Gauge,
  16. pluginId: 'gauge',
  17. options: {},
  18. fieldConfig: {
  19. defaults: {
  20. thresholds: {
  21. steps: [
  22. { value: -Infinity, color: 'green' },
  23. { value: 70, color: 'orange' },
  24. { value: 85, color: 'red' },
  25. ],
  26. mode: ThresholdsMode.Percentage,
  27. },
  28. custom: {},
  29. },
  30. overrides: [],
  31. },
  32. cardOptions: {
  33. previewModifier: (s) => {
  34. if (s.options!.reduceOptions.values) {
  35. s.options!.reduceOptions.limit = 2;
  36. }
  37. },
  38. },
  39. });
  40. if (dataSummary.hasStringField && dataSummary.frameCount === 1 && dataSummary.rowCountTotal < 10) {
  41. list.append({
  42. name: SuggestionName.Gauge,
  43. options: {
  44. reduceOptions: {
  45. values: true,
  46. calcs: [],
  47. },
  48. },
  49. });
  50. list.append({
  51. name: SuggestionName.GaugeNoThresholds,
  52. options: {
  53. reduceOptions: {
  54. values: true,
  55. calcs: [],
  56. },
  57. showThresholdMarkers: false,
  58. },
  59. });
  60. } else {
  61. list.append({
  62. name: SuggestionName.Gauge,
  63. options: {
  64. reduceOptions: {
  65. values: false,
  66. calcs: ['lastNotNull'],
  67. },
  68. },
  69. });
  70. list.append({
  71. name: SuggestionName.GaugeNoThresholds,
  72. options: {
  73. reduceOptions: {
  74. values: false,
  75. calcs: ['lastNotNull'],
  76. },
  77. showThresholdMarkers: false,
  78. },
  79. });
  80. }
  81. }
  82. }