Files
grafana/public/app/features/panel/state/getAllSuggestions.test.ts
Torkel Ödegaard 54af57b8e6 VisualizationSelection: Real previews of suitable visualisation and options based on current data (#40527)
* Initial pass to move panel state to it's own, and make it by key not panel.id

* Progress

* Not making much progress, having panel.key be mutable is causing a lot of issues

* Think this is starting to work

* Began fixing tests

* Add selector

* Bug fixes and changes to cleanup, and fixing all flicking when switching library panels

* Removed console.log

* fixes after merge

* fixing tests

* fixing tests

* Added new test for changePlugin thunk

* Initial struture in place

* responding to state changes in another part of the state

* bha

* going in a different direction

* This is getting exciting

* minor

* More structure

* More real

* Added builder to reduce boiler plate

* Lots of progress

* Adding more visualizations

* More smarts

* tweaks

* suggestions

* Move to separate view

* Refactoring to builder concept

* Before hover preview test

* Increase line width in preview

* More suggestions

* Removed old elements of onSuggestVisualizations

* Don't call suggestion suppliers if there is no data

* Restore card styles to only borders

* Changing supplier interface to support data vs option suggestion scenario

* Renamed functions

* Add dynamic width support

* not sure about this

* Improve suggestions

* Improve suggestions

* Single grid/list

* Store vis select pane & size

* Prep for option suggestions

* more suggestions

* Name/title option for preview cards

* Improve barchart suggestions

* Support suggestions when there are no data

* Minor change

* reverted some changes

* Improve suggestions for stacking

* Removed size option

* starting on unit tests, hit cyclic dependency issue

* muuu

* First test for getting suggestion seems to work, going to bed

* add missing file

* A basis for more unit tests

* More tests

* More unit tests

* Fixed unit tests

* Update

* Some extreme scenarios

* Added basic e2e test

* Added another unit test for changePanelPlugin action

* More cleanup

* Minor tweak

* add wait to e2e test

* Renamed function and cleanup of unused function

* Adding search support and adding search test to e2e test
2021-10-25 13:55:06 +02:00

306 lines
8.7 KiB
TypeScript

import {
DataFrame,
FieldType,
getDefaultTimeRange,
LoadingState,
PanelData,
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;
}
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, SuggestionName.DashboardList]);
});
});
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, SuggestionName.TextPanel, SuggestionName.DashboardList]);
});
});
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.BarChart,
SuggestionName.Gauge,
SuggestionName.GaugeNoThresholds,
SuggestionName.Stat,
SuggestionName.StatColoredBackground,
SuggestionName.BarGaugeBasic,
SuggestionName.BarGaugeLCD,
SuggestionName.Table,
SuggestionName.StateTimeline,
]);
});
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,
]);
});
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 string 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.StatColoredBackground, 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('/.*/');
}
}
});
});
function repeatFrame(count: number, frame: DataFrame): DataFrame[] {
const frames: DataFrame[] = [];
for (let i = 0; i < count; i++) {
frames.push(frame);
}
return frames;
}