123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- import {
- DataFrame,
- FieldType,
- getDefaultTimeRange,
- LoadingState,
- PanelData,
- PanelPluginMeta,
- toDataFrame,
- VisualizationSuggestion,
- } from '@grafana/data';
- import { config } from 'app/core/config';
- import { SuggestionName } from 'app/types/suggestions';
- import { getAllSuggestions, panelsToCheckFirst } from './getAllSuggestions';
- jest.unmock('app/core/core');
- jest.unmock('app/features/plugins/plugin_loader');
- for (const pluginId of panelsToCheckFirst) {
- config.panels[pluginId] = {
- module: `app/plugins/panel/${pluginId}/module`,
- } as any;
- }
- config.panels['text'] = {
- id: 'text',
- name: 'Text',
- skipDataQuery: true,
- info: {
- description: 'pretty decent plugin',
- logos: { small: 'small/logo', large: 'large/logo' },
- },
- } as PanelPluginMeta;
- class ScenarioContext {
- data: DataFrame[] = [];
- suggestions: VisualizationSuggestion[] = [];
- setData(scenarioData: DataFrame[]) {
- this.data = scenarioData;
- beforeAll(async () => {
- await this.run();
- });
- }
- async run() {
- const panelData: PanelData = {
- series: this.data,
- state: LoadingState.Done,
- timeRange: getDefaultTimeRange(),
- };
- this.suggestions = await getAllSuggestions(panelData);
- }
- names() {
- return this.suggestions.map((x) => x.name);
- }
- }
- function scenario(name: string, setup: (ctx: ScenarioContext) => void) {
- describe(name, () => {
- const ctx = new ScenarioContext();
- setup(ctx);
- });
- }
- scenario('No series', (ctx) => {
- ctx.setData([]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([SuggestionName.Table, SuggestionName.TextPanel]);
- });
- });
- scenario('No rows', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [
- { name: 'Time', type: FieldType.time, values: [] },
- { name: 'Max', type: FieldType.number, values: [] },
- ],
- }),
- ]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([SuggestionName.Table]);
- });
- });
- scenario('Single frame with time and number field', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [
- { name: 'Time', type: FieldType.time, values: [1, 2, 3, 4, 5] },
- { name: 'Max', type: FieldType.number, values: [1, 10, 50, 2, 5] },
- ],
- }),
- ]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([
- SuggestionName.LineChart,
- SuggestionName.LineChartSmooth,
- SuggestionName.AreaChart,
- SuggestionName.LineChartGradientColorScheme,
- SuggestionName.BarChart,
- SuggestionName.BarChartGradientColorScheme,
- SuggestionName.Gauge,
- SuggestionName.GaugeNoThresholds,
- SuggestionName.Stat,
- SuggestionName.StatColoredBackground,
- SuggestionName.BarGaugeBasic,
- SuggestionName.BarGaugeLCD,
- SuggestionName.Table,
- SuggestionName.StateTimeline,
- SuggestionName.StatusHistory,
- ]);
- });
- it('Bar chart suggestion should be using timeseries panel', () => {
- expect(ctx.suggestions.find((x) => x.name === SuggestionName.BarChart)?.pluginId).toBe('timeseries');
- });
- it('Stat panels have reduce values disabled', () => {
- for (const suggestion of ctx.suggestions) {
- if (suggestion.options?.reduceOptions?.values) {
- throw new Error(`Suggestion ${suggestion.name} reduce.values set to true when it should be false`);
- }
- }
- });
- });
- scenario('Single frame with time 2 number fields', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [
- { name: 'Time', type: FieldType.time, values: [1, 2, 3, 4, 5] },
- { name: 'ServerA', type: FieldType.number, values: [1, 10, 50, 2, 5] },
- { name: 'ServerB', type: FieldType.number, values: [1, 10, 50, 2, 5] },
- ],
- }),
- ]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([
- SuggestionName.LineChart,
- SuggestionName.LineChartSmooth,
- SuggestionName.AreaChartStacked,
- SuggestionName.AreaChartStackedPercent,
- SuggestionName.BarChartStacked,
- SuggestionName.BarChartStackedPercent,
- SuggestionName.Gauge,
- SuggestionName.GaugeNoThresholds,
- SuggestionName.Stat,
- SuggestionName.StatColoredBackground,
- SuggestionName.PieChart,
- SuggestionName.PieChartDonut,
- SuggestionName.BarGaugeBasic,
- SuggestionName.BarGaugeLCD,
- SuggestionName.Table,
- SuggestionName.StateTimeline,
- SuggestionName.StatusHistory,
- ]);
- });
- it('Stat panels have reduceOptions.values disabled', () => {
- for (const suggestion of ctx.suggestions) {
- if (suggestion.options?.reduceOptions?.values) {
- throw new Error(`Suggestion ${suggestion.name} reduce.values set to true when it should be false`);
- }
- }
- });
- });
- scenario('Single time series with 100 data points', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [
- { name: 'Time', type: FieldType.time, values: [...Array(100).keys()] },
- { name: 'ServerA', type: FieldType.number, values: [...Array(100).keys()] },
- ],
- }),
- ]);
- it('should not suggest bar chart', () => {
- expect(ctx.suggestions.find((x) => x.name === SuggestionName.BarChart)).toBe(undefined);
- });
- });
- scenario('30 time series with 100 data points', (ctx) => {
- ctx.setData(
- repeatFrame(
- 30,
- toDataFrame({
- fields: [
- { name: 'Time', type: FieldType.time, values: [...Array(100).keys()] },
- { name: 'ServerA', type: FieldType.number, values: [...Array(100).keys()] },
- ],
- })
- )
- );
- it('should not suggest timeline', () => {
- expect(ctx.suggestions.find((x) => x.pluginId === 'state-timeline')).toBe(undefined);
- });
- });
- scenario('50 time series with 100 data points', (ctx) => {
- ctx.setData(
- repeatFrame(
- 50,
- toDataFrame({
- fields: [
- { name: 'Time', type: FieldType.time, values: [...Array(100).keys()] },
- { name: 'ServerA', type: FieldType.number, values: [...Array(100).keys()] },
- ],
- })
- )
- );
- it('should not suggest gauge', () => {
- expect(ctx.suggestions.find((x) => x.pluginId === 'gauge')).toBe(undefined);
- });
- });
- scenario('Single frame with string and number field', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [
- { name: 'Name', type: FieldType.string, values: ['Hugo', 'Dominik', 'Marcus'] },
- { name: 'ServerA', type: FieldType.number, values: [1, 2, 3] },
- ],
- }),
- ]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([
- SuggestionName.BarChart,
- SuggestionName.BarChartHorizontal,
- SuggestionName.Gauge,
- SuggestionName.GaugeNoThresholds,
- SuggestionName.Stat,
- SuggestionName.StatColoredBackground,
- SuggestionName.PieChart,
- SuggestionName.PieChartDonut,
- SuggestionName.BarGaugeBasic,
- SuggestionName.BarGaugeLCD,
- SuggestionName.Table,
- ]);
- });
- it('Stat/Gauge/BarGauge/PieChart panels to have reduceOptions.values enabled', () => {
- for (const suggestion of ctx.suggestions) {
- if (suggestion.options?.reduceOptions && !suggestion.options?.reduceOptions?.values) {
- throw new Error(`Suggestion ${suggestion.name} reduce.values set to false when it should be true`);
- }
- }
- });
- });
- scenario('Single frame with string and 2 number field', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [
- { name: 'Name', type: FieldType.string, values: ['Hugo', 'Dominik', 'Marcus'] },
- { name: 'ServerA', type: FieldType.number, values: [1, 2, 3] },
- { name: 'ServerB', type: FieldType.number, values: [1, 2, 3] },
- ],
- }),
- ]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([
- SuggestionName.BarChart,
- SuggestionName.BarChartStacked,
- SuggestionName.BarChartStackedPercent,
- SuggestionName.BarChartHorizontal,
- SuggestionName.BarChartHorizontalStacked,
- SuggestionName.BarChartHorizontalStackedPercent,
- SuggestionName.Gauge,
- SuggestionName.GaugeNoThresholds,
- SuggestionName.Stat,
- SuggestionName.StatColoredBackground,
- SuggestionName.PieChart,
- SuggestionName.PieChartDonut,
- SuggestionName.BarGaugeBasic,
- SuggestionName.BarGaugeLCD,
- SuggestionName.Table,
- ]);
- });
- });
- scenario('Single frame with only string field', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [{ name: 'Name', type: FieldType.string, values: ['Hugo', 'Dominik', 'Marcus'] }],
- }),
- ]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([SuggestionName.Stat, SuggestionName.Table]);
- });
- it('Stat panels have reduceOptions.fields set to show all fields', () => {
- for (const suggestion of ctx.suggestions) {
- if (suggestion.options?.reduceOptions) {
- expect(suggestion.options.reduceOptions.fields).toBe('/.*/');
- }
- }
- });
- });
- scenario('Given default loki logs data', (ctx) => {
- ctx.setData([
- toDataFrame({
- fields: [
- { name: 'ts', type: FieldType.time, values: ['2021-11-11T13:38:45.440Z', '2021-11-11T13:38:45.190Z'] },
- {
- name: 'line',
- type: FieldType.string,
- values: [
- 't=2021-11-11T14:38:45+0100 lvl=dbug msg="Client connected" logger=live user=1 client=ee79155b-a8d1-4730-bcb3-94d8690df35c',
- 't=2021-11-11T14:38:45+0100 lvl=dbug msg="Adding CSP header to response" logger=http.server cfg=0xc0005fed00',
- ],
- labels: { filename: '/var/log/grafana/grafana.log', job: 'grafana' },
- },
- ],
- meta: {
- preferredVisualisationType: 'logs',
- },
- }),
- ]);
- it('should return correct suggestions', () => {
- expect(ctx.names()).toEqual([SuggestionName.Logs, SuggestionName.Table]);
- });
- });
- function repeatFrame(count: number, frame: DataFrame): DataFrame[] {
- const frames: DataFrame[] = [];
- for (let i = 0; i < count; i++) {
- frames.push(frame);
- }
- return frames;
- }
|