getAllSuggestions.test.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import {
  2. DataFrame,
  3. FieldType,
  4. getDefaultTimeRange,
  5. LoadingState,
  6. PanelData,
  7. PanelPluginMeta,
  8. toDataFrame,
  9. VisualizationSuggestion,
  10. } from '@grafana/data';
  11. import { config } from 'app/core/config';
  12. import { SuggestionName } from 'app/types/suggestions';
  13. import { getAllSuggestions, panelsToCheckFirst } from './getAllSuggestions';
  14. jest.unmock('app/core/core');
  15. jest.unmock('app/features/plugins/plugin_loader');
  16. for (const pluginId of panelsToCheckFirst) {
  17. config.panels[pluginId] = {
  18. module: `app/plugins/panel/${pluginId}/module`,
  19. } as any;
  20. }
  21. config.panels['text'] = {
  22. id: 'text',
  23. name: 'Text',
  24. skipDataQuery: true,
  25. info: {
  26. description: 'pretty decent plugin',
  27. logos: { small: 'small/logo', large: 'large/logo' },
  28. },
  29. } as PanelPluginMeta;
  30. class ScenarioContext {
  31. data: DataFrame[] = [];
  32. suggestions: VisualizationSuggestion[] = [];
  33. setData(scenarioData: DataFrame[]) {
  34. this.data = scenarioData;
  35. beforeAll(async () => {
  36. await this.run();
  37. });
  38. }
  39. async run() {
  40. const panelData: PanelData = {
  41. series: this.data,
  42. state: LoadingState.Done,
  43. timeRange: getDefaultTimeRange(),
  44. };
  45. this.suggestions = await getAllSuggestions(panelData);
  46. }
  47. names() {
  48. return this.suggestions.map((x) => x.name);
  49. }
  50. }
  51. function scenario(name: string, setup: (ctx: ScenarioContext) => void) {
  52. describe(name, () => {
  53. const ctx = new ScenarioContext();
  54. setup(ctx);
  55. });
  56. }
  57. scenario('No series', (ctx) => {
  58. ctx.setData([]);
  59. it('should return correct suggestions', () => {
  60. expect(ctx.names()).toEqual([SuggestionName.Table, SuggestionName.TextPanel]);
  61. });
  62. });
  63. scenario('No rows', (ctx) => {
  64. ctx.setData([
  65. toDataFrame({
  66. fields: [
  67. { name: 'Time', type: FieldType.time, values: [] },
  68. { name: 'Max', type: FieldType.number, values: [] },
  69. ],
  70. }),
  71. ]);
  72. it('should return correct suggestions', () => {
  73. expect(ctx.names()).toEqual([SuggestionName.Table]);
  74. });
  75. });
  76. scenario('Single frame with time and number field', (ctx) => {
  77. ctx.setData([
  78. toDataFrame({
  79. fields: [
  80. { name: 'Time', type: FieldType.time, values: [1, 2, 3, 4, 5] },
  81. { name: 'Max', type: FieldType.number, values: [1, 10, 50, 2, 5] },
  82. ],
  83. }),
  84. ]);
  85. it('should return correct suggestions', () => {
  86. expect(ctx.names()).toEqual([
  87. SuggestionName.LineChart,
  88. SuggestionName.LineChartSmooth,
  89. SuggestionName.AreaChart,
  90. SuggestionName.LineChartGradientColorScheme,
  91. SuggestionName.BarChart,
  92. SuggestionName.BarChartGradientColorScheme,
  93. SuggestionName.Gauge,
  94. SuggestionName.GaugeNoThresholds,
  95. SuggestionName.Stat,
  96. SuggestionName.StatColoredBackground,
  97. SuggestionName.BarGaugeBasic,
  98. SuggestionName.BarGaugeLCD,
  99. SuggestionName.Table,
  100. SuggestionName.StateTimeline,
  101. SuggestionName.StatusHistory,
  102. ]);
  103. });
  104. it('Bar chart suggestion should be using timeseries panel', () => {
  105. expect(ctx.suggestions.find((x) => x.name === SuggestionName.BarChart)?.pluginId).toBe('timeseries');
  106. });
  107. it('Stat panels have reduce values disabled', () => {
  108. for (const suggestion of ctx.suggestions) {
  109. if (suggestion.options?.reduceOptions?.values) {
  110. throw new Error(`Suggestion ${suggestion.name} reduce.values set to true when it should be false`);
  111. }
  112. }
  113. });
  114. });
  115. scenario('Single frame with time 2 number fields', (ctx) => {
  116. ctx.setData([
  117. toDataFrame({
  118. fields: [
  119. { name: 'Time', type: FieldType.time, values: [1, 2, 3, 4, 5] },
  120. { name: 'ServerA', type: FieldType.number, values: [1, 10, 50, 2, 5] },
  121. { name: 'ServerB', type: FieldType.number, values: [1, 10, 50, 2, 5] },
  122. ],
  123. }),
  124. ]);
  125. it('should return correct suggestions', () => {
  126. expect(ctx.names()).toEqual([
  127. SuggestionName.LineChart,
  128. SuggestionName.LineChartSmooth,
  129. SuggestionName.AreaChartStacked,
  130. SuggestionName.AreaChartStackedPercent,
  131. SuggestionName.BarChartStacked,
  132. SuggestionName.BarChartStackedPercent,
  133. SuggestionName.Gauge,
  134. SuggestionName.GaugeNoThresholds,
  135. SuggestionName.Stat,
  136. SuggestionName.StatColoredBackground,
  137. SuggestionName.PieChart,
  138. SuggestionName.PieChartDonut,
  139. SuggestionName.BarGaugeBasic,
  140. SuggestionName.BarGaugeLCD,
  141. SuggestionName.Table,
  142. SuggestionName.StateTimeline,
  143. SuggestionName.StatusHistory,
  144. ]);
  145. });
  146. it('Stat panels have reduceOptions.values disabled', () => {
  147. for (const suggestion of ctx.suggestions) {
  148. if (suggestion.options?.reduceOptions?.values) {
  149. throw new Error(`Suggestion ${suggestion.name} reduce.values set to true when it should be false`);
  150. }
  151. }
  152. });
  153. });
  154. scenario('Single time series with 100 data points', (ctx) => {
  155. ctx.setData([
  156. toDataFrame({
  157. fields: [
  158. { name: 'Time', type: FieldType.time, values: [...Array(100).keys()] },
  159. { name: 'ServerA', type: FieldType.number, values: [...Array(100).keys()] },
  160. ],
  161. }),
  162. ]);
  163. it('should not suggest bar chart', () => {
  164. expect(ctx.suggestions.find((x) => x.name === SuggestionName.BarChart)).toBe(undefined);
  165. });
  166. });
  167. scenario('30 time series with 100 data points', (ctx) => {
  168. ctx.setData(
  169. repeatFrame(
  170. 30,
  171. toDataFrame({
  172. fields: [
  173. { name: 'Time', type: FieldType.time, values: [...Array(100).keys()] },
  174. { name: 'ServerA', type: FieldType.number, values: [...Array(100).keys()] },
  175. ],
  176. })
  177. )
  178. );
  179. it('should not suggest timeline', () => {
  180. expect(ctx.suggestions.find((x) => x.pluginId === 'state-timeline')).toBe(undefined);
  181. });
  182. });
  183. scenario('50 time series with 100 data points', (ctx) => {
  184. ctx.setData(
  185. repeatFrame(
  186. 50,
  187. toDataFrame({
  188. fields: [
  189. { name: 'Time', type: FieldType.time, values: [...Array(100).keys()] },
  190. { name: 'ServerA', type: FieldType.number, values: [...Array(100).keys()] },
  191. ],
  192. })
  193. )
  194. );
  195. it('should not suggest gauge', () => {
  196. expect(ctx.suggestions.find((x) => x.pluginId === 'gauge')).toBe(undefined);
  197. });
  198. });
  199. scenario('Single frame with string and number field', (ctx) => {
  200. ctx.setData([
  201. toDataFrame({
  202. fields: [
  203. { name: 'Name', type: FieldType.string, values: ['Hugo', 'Dominik', 'Marcus'] },
  204. { name: 'ServerA', type: FieldType.number, values: [1, 2, 3] },
  205. ],
  206. }),
  207. ]);
  208. it('should return correct suggestions', () => {
  209. expect(ctx.names()).toEqual([
  210. SuggestionName.BarChart,
  211. SuggestionName.BarChartHorizontal,
  212. SuggestionName.Gauge,
  213. SuggestionName.GaugeNoThresholds,
  214. SuggestionName.Stat,
  215. SuggestionName.StatColoredBackground,
  216. SuggestionName.PieChart,
  217. SuggestionName.PieChartDonut,
  218. SuggestionName.BarGaugeBasic,
  219. SuggestionName.BarGaugeLCD,
  220. SuggestionName.Table,
  221. ]);
  222. });
  223. it('Stat/Gauge/BarGauge/PieChart panels to have reduceOptions.values enabled', () => {
  224. for (const suggestion of ctx.suggestions) {
  225. if (suggestion.options?.reduceOptions && !suggestion.options?.reduceOptions?.values) {
  226. throw new Error(`Suggestion ${suggestion.name} reduce.values set to false when it should be true`);
  227. }
  228. }
  229. });
  230. });
  231. scenario('Single frame with string and 2 number field', (ctx) => {
  232. ctx.setData([
  233. toDataFrame({
  234. fields: [
  235. { name: 'Name', type: FieldType.string, values: ['Hugo', 'Dominik', 'Marcus'] },
  236. { name: 'ServerA', type: FieldType.number, values: [1, 2, 3] },
  237. { name: 'ServerB', type: FieldType.number, values: [1, 2, 3] },
  238. ],
  239. }),
  240. ]);
  241. it('should return correct suggestions', () => {
  242. expect(ctx.names()).toEqual([
  243. SuggestionName.BarChart,
  244. SuggestionName.BarChartStacked,
  245. SuggestionName.BarChartStackedPercent,
  246. SuggestionName.BarChartHorizontal,
  247. SuggestionName.BarChartHorizontalStacked,
  248. SuggestionName.BarChartHorizontalStackedPercent,
  249. SuggestionName.Gauge,
  250. SuggestionName.GaugeNoThresholds,
  251. SuggestionName.Stat,
  252. SuggestionName.StatColoredBackground,
  253. SuggestionName.PieChart,
  254. SuggestionName.PieChartDonut,
  255. SuggestionName.BarGaugeBasic,
  256. SuggestionName.BarGaugeLCD,
  257. SuggestionName.Table,
  258. ]);
  259. });
  260. });
  261. scenario('Single frame with only string field', (ctx) => {
  262. ctx.setData([
  263. toDataFrame({
  264. fields: [{ name: 'Name', type: FieldType.string, values: ['Hugo', 'Dominik', 'Marcus'] }],
  265. }),
  266. ]);
  267. it('should return correct suggestions', () => {
  268. expect(ctx.names()).toEqual([SuggestionName.Stat, SuggestionName.Table]);
  269. });
  270. it('Stat panels have reduceOptions.fields set to show all fields', () => {
  271. for (const suggestion of ctx.suggestions) {
  272. if (suggestion.options?.reduceOptions) {
  273. expect(suggestion.options.reduceOptions.fields).toBe('/.*/');
  274. }
  275. }
  276. });
  277. });
  278. scenario('Given default loki logs data', (ctx) => {
  279. ctx.setData([
  280. toDataFrame({
  281. fields: [
  282. { name: 'ts', type: FieldType.time, values: ['2021-11-11T13:38:45.440Z', '2021-11-11T13:38:45.190Z'] },
  283. {
  284. name: 'line',
  285. type: FieldType.string,
  286. values: [
  287. 't=2021-11-11T14:38:45+0100 lvl=dbug msg="Client connected" logger=live user=1 client=ee79155b-a8d1-4730-bcb3-94d8690df35c',
  288. 't=2021-11-11T14:38:45+0100 lvl=dbug msg="Adding CSP header to response" logger=http.server cfg=0xc0005fed00',
  289. ],
  290. labels: { filename: '/var/log/grafana/grafana.log', job: 'grafana' },
  291. },
  292. ],
  293. meta: {
  294. preferredVisualisationType: 'logs',
  295. },
  296. }),
  297. ]);
  298. it('should return correct suggestions', () => {
  299. expect(ctx.names()).toEqual([SuggestionName.Logs, SuggestionName.Table]);
  300. });
  301. });
  302. function repeatFrame(count: number, frame: DataFrame): DataFrame[] {
  303. const frames: DataFrame[] = [];
  304. for (let i = 0; i < count; i++) {
  305. frames.push(frame);
  306. }
  307. return frames;
  308. }