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