mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* Add UMLs * Add rendered diagrams * Move QueryCtrl to flux * Remove redundant param in the reducer * Use named imports for lodash and fix typing for GraphiteTagOperator * Add missing async/await * Extract providers to a separate file * Clean up async await * Rename controller functions back to main * Simplify creating actions * Re-order controller functions * Separate helpers from actions * Rename vars * Simplify helpers * Move controller methods to state reducers * Remove docs (they are added in design doc) * Move actions.ts to state folder * Add docs * Add old methods stubs for easier review * Check how state dependencies will be mapped * Rename state to store * Rename state to store * Rewrite spec tests for Graphite Query Controller * Update docs * Update docs * Add GraphiteTextEditor * Add play button * Add AddGraphiteFunction * Use Segment to simplify AddGraphiteFunction * Memoize function defs * Fix useCallback deps * Update public/app/plugins/datasource/graphite/state/helpers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/helpers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/helpers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/providers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/providers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/providers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/providers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/providers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Update public/app/plugins/datasource/graphite/state/providers.ts Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Add more type definitions * Remove submitOnClickAwayOption This behavior is actually needed to remove parameters in functions * Load function definitions before parsing the target on initial load * Add button padding * Fix loading function definitions * Change targetChanged to updateQuery to avoid mutating state directly It's also needed for extra refresh/runQuery execution as handleTargetChanged doesn't handle changing the raw query * Fix updating query after adding a function * Simplify updating function params * Migrate function editor to react * Simplify setting Segment Select min width * Remove unnecessary changes to SegmentInput * Extract view logic to a helper and update types definitions * Clean up types * Update FuncDef types and add tests * Show red border for unknown functions * Autofocus on new params * Extract params mapping to a helper * Split code between params and function editor * Focus on the first param when a function is added even if it's an optional argument * Add function editor tests * Remove todo marker * Fix adding new functions * Allow empty value in selects for removing function params * Add placeholders and fix styling * Add more docs * Create basic implementation for metrics and tags * Post merge fixes These files are not .ts * Remove mapping to Angular dropdowns * Simplify mapping tag names, values and operators * Simplify mapping metrics * Fix removing tags and autocomplete * Simplify debouncing providers * Ensure options are loaded twice and segment is opened * Remove focusing new segments logic (not supported by React's segment) * Clean up * Move debouncing to components * Simplify mapping to selectable options * Add docs * use getStyles * remove redundant async/await * Remove * remove redundant async/await * Remove console.log and silent test console output * Do not display the name of the selected dropdown option * Use block docs for better doc generation * Handle undefined values provided for autocomplete Co-authored-by: Giordano Ricci <me@giordanoricci.com>
455 lines
16 KiB
TypeScript
455 lines
16 KiB
TypeScript
import { dispatch } from 'app/store/store';
|
|
import { uiSegmentSrv } from 'app/core/services/segment_srv';
|
|
import gfunc from '../gfunc';
|
|
import { GraphiteQueryCtrl } from '../query_ctrl';
|
|
import { TemplateSrvStub } from 'test/specs/helpers';
|
|
import { silenceConsoleOutput } from 'test/core/utils/silenceConsoleOutput';
|
|
import { actions } from '../state/actions';
|
|
import { getAltSegmentsSelectables, getTagsSelectables, getTagsAsSegmentsSelectables } from '../state/providers';
|
|
import { GraphiteSegment } from '../types';
|
|
|
|
jest.mock('app/core/utils/promiseToDigest', () => ({
|
|
promiseToDigest: (scope: any) => {
|
|
return (p: Promise<any>) => p;
|
|
},
|
|
}));
|
|
|
|
jest.mock('app/store/store', () => ({
|
|
dispatch: jest.fn(),
|
|
}));
|
|
const mockDispatch = dispatch as jest.Mock;
|
|
|
|
/**
|
|
* Simulate switching to text editor, changing the query and switching back to visual editor
|
|
*/
|
|
async function changeTarget(ctx: any, target: string): Promise<void> {
|
|
await ctx.ctrl.toggleEditorMode();
|
|
await ctx.ctrl.dispatch(actions.updateQuery({ query: target }));
|
|
await ctx.ctrl.dispatch(actions.runQuery());
|
|
await ctx.ctrl.toggleEditorMode();
|
|
}
|
|
|
|
describe('GraphiteQueryCtrl', () => {
|
|
const ctx = {
|
|
datasource: {
|
|
metricFindQuery: jest.fn(() => Promise.resolve([])),
|
|
getFuncDefs: jest.fn(() => Promise.resolve(gfunc.getFuncDefs('1.0'))),
|
|
getFuncDef: gfunc.getFuncDef,
|
|
waitForFuncDefsLoaded: jest.fn(() => Promise.resolve(null)),
|
|
createFuncInstance: gfunc.createFuncInstance,
|
|
getTagsAutoComplete: jest.fn().mockReturnValue(Promise.resolve([])),
|
|
},
|
|
target: { target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)' },
|
|
panelCtrl: {
|
|
refresh: jest.fn(),
|
|
},
|
|
} as any;
|
|
|
|
ctx.panelCtrl.panel = {
|
|
targets: [ctx.target],
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
jest.clearAllMocks();
|
|
GraphiteQueryCtrl.prototype.target = ctx.target;
|
|
GraphiteQueryCtrl.prototype.datasource = ctx.datasource;
|
|
GraphiteQueryCtrl.prototype.panelCtrl = ctx.panelCtrl;
|
|
|
|
ctx.ctrl = new GraphiteQueryCtrl(
|
|
{ $digest: jest.fn() },
|
|
{} as any,
|
|
//@ts-ignore
|
|
new uiSegmentSrv({ trustAsHtml: (html) => html }, { highlightVariablesAsHtml: () => {} }),
|
|
//@ts-ignore
|
|
new TemplateSrvStub()
|
|
);
|
|
|
|
// resolve async code called by the constructor
|
|
await Promise.resolve();
|
|
});
|
|
|
|
describe('init', () => {
|
|
it('should validate metric key exists', () => {
|
|
expect(ctx.datasource.metricFindQuery.mock.calls[0][0]).toBe('test.prod.*');
|
|
});
|
|
|
|
it('should not delete last segment if no metrics are found', () => {
|
|
expect(ctx.ctrl.state.segments[2].value).not.toBe('select metric');
|
|
expect(ctx.ctrl.state.segments[2].value).toBe('*');
|
|
});
|
|
|
|
it('should parse expression and build function model', () => {
|
|
expect(ctx.ctrl.state.queryModel.functions.length).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('when toggling edit mode to raw and back again', () => {
|
|
beforeEach(async () => {
|
|
await ctx.ctrl.toggleEditorMode();
|
|
await ctx.ctrl.toggleEditorMode();
|
|
});
|
|
|
|
it('should validate metric key exists', () => {
|
|
const lastCallIndex = ctx.datasource.metricFindQuery.mock.calls.length - 1;
|
|
expect(ctx.datasource.metricFindQuery.mock.calls[lastCallIndex][0]).toBe('test.prod.*');
|
|
});
|
|
|
|
it('should delete last segment if no metrics are found', () => {
|
|
expect(ctx.ctrl.state.segments[0].value).toBe('test');
|
|
expect(ctx.ctrl.state.segments[1].value).toBe('prod');
|
|
expect(ctx.ctrl.state.segments[2].value).toBe('select metric');
|
|
});
|
|
|
|
it('should parse expression and build function model', () => {
|
|
expect(ctx.ctrl.state.queryModel.functions.length).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('when middle segment value of test.prod.* is changed', () => {
|
|
beforeEach(async () => {
|
|
const segment: GraphiteSegment = { type: 'metric', value: 'test', expandable: true };
|
|
await ctx.ctrl.dispatch(actions.segmentValueChanged({ segment: segment, index: 1 }));
|
|
});
|
|
|
|
it('should validate metric key exists', () => {
|
|
const lastCallIndex = ctx.datasource.metricFindQuery.mock.calls.length - 1;
|
|
expect(ctx.datasource.metricFindQuery.mock.calls[lastCallIndex][0]).toBe('test.test.*');
|
|
});
|
|
|
|
it('should delete last segment if no metrics are found', () => {
|
|
expect(ctx.ctrl.state.segments[0].value).toBe('test');
|
|
expect(ctx.ctrl.state.segments[1].value).toBe('test');
|
|
expect(ctx.ctrl.state.segments[2].value).toBe('select metric');
|
|
});
|
|
|
|
it('should parse expression and build function model', () => {
|
|
expect(ctx.ctrl.state.queryModel.functions.length).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('when adding function', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, 'test.prod.*.count');
|
|
await ctx.ctrl.dispatch(actions.addFunction({ name: 'aliasByNode' }));
|
|
});
|
|
|
|
it('should add function with correct node number', () => {
|
|
expect(ctx.ctrl.state.queryModel.functions[0].params[0]).toBe(2);
|
|
});
|
|
|
|
it('should update target', () => {
|
|
expect(ctx.ctrl.state.target.target).toBe('aliasByNode(test.prod.*.count, 2)');
|
|
});
|
|
|
|
it('should call refresh', () => {
|
|
expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('when adding function before any metric segment', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: true }]);
|
|
await changeTarget(ctx, '');
|
|
await ctx.ctrl.dispatch(actions.addFunction({ name: 'asPercent' }));
|
|
});
|
|
|
|
it('should add function and remove select metric link', () => {
|
|
expect(ctx.ctrl.state.segments.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('when initializing a target with single param func using variable', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([]);
|
|
await changeTarget(ctx, 'movingAverage(prod.count, $var)');
|
|
});
|
|
|
|
it('should add 2 segments', () => {
|
|
expect(ctx.ctrl.state.segments.length).toBe(2);
|
|
});
|
|
|
|
it('should add function param', () => {
|
|
expect(ctx.ctrl.state.queryModel.functions[0].params.length).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('when initializing target without metric expression and function with series-ref', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([]);
|
|
await changeTarget(ctx, 'asPercent(metric.node.count, #A)');
|
|
});
|
|
|
|
it('should add segments', () => {
|
|
expect(ctx.ctrl.state.segments.length).toBe(3);
|
|
});
|
|
|
|
it('should have correct func params', () => {
|
|
expect(ctx.ctrl.state.queryModel.functions[0].params.length).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('when getting altSegments and metricFindQuery returns empty array', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([]);
|
|
await changeTarget(ctx, 'test.count');
|
|
ctx.altSegments = await getAltSegmentsSelectables(ctx.ctrl.state, 1, '');
|
|
});
|
|
|
|
it('should have no segments', () => {
|
|
expect(ctx.altSegments.length).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('when autocomplete for metric names is not available', () => {
|
|
silenceConsoleOutput();
|
|
beforeEach(() => {
|
|
ctx.ctrl.state.datasource.getTagsAutoComplete = jest.fn().mockReturnValue(Promise.resolve([]));
|
|
ctx.ctrl.state.datasource.metricFindQuery = jest.fn().mockReturnValue(
|
|
new Promise(() => {
|
|
throw new Error();
|
|
})
|
|
);
|
|
});
|
|
|
|
it('getAltSegmentsSelectables should handle autocomplete errors', async () => {
|
|
await expect(async () => {
|
|
await getAltSegmentsSelectables(ctx.ctrl.state, 0, 'any');
|
|
expect(mockDispatch).toBeCalledWith(
|
|
expect.objectContaining({
|
|
type: 'appNotifications/notifyApp',
|
|
})
|
|
);
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('getAltSegmentsSelectables should display the error message only once', async () => {
|
|
await getAltSegmentsSelectables(ctx.ctrl.state, 0, 'any');
|
|
expect(mockDispatch.mock.calls.length).toBe(1);
|
|
|
|
await getAltSegmentsSelectables(ctx.ctrl.state, 0, 'any');
|
|
expect(mockDispatch.mock.calls.length).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('when autocomplete for tags is not available', () => {
|
|
silenceConsoleOutput();
|
|
beforeEach(() => {
|
|
ctx.datasource.metricFindQuery = jest.fn().mockReturnValue(Promise.resolve([]));
|
|
ctx.datasource.getTagsAutoComplete = jest.fn().mockReturnValue(
|
|
new Promise(() => {
|
|
throw new Error();
|
|
})
|
|
);
|
|
});
|
|
|
|
it('getTagsSelectables should handle autocomplete errors', async () => {
|
|
await expect(async () => {
|
|
await getTagsSelectables(ctx.ctrl.state, 0, 'any');
|
|
expect(mockDispatch).toBeCalledWith(
|
|
expect.objectContaining({
|
|
type: 'appNotifications/notifyApp',
|
|
})
|
|
);
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('getTagsSelectables should display the error message only once', async () => {
|
|
await getTagsSelectables(ctx.ctrl.state, 0, 'any');
|
|
expect(mockDispatch.mock.calls.length).toBe(1);
|
|
|
|
await getTagsSelectables(ctx.ctrl.state, 0, 'any');
|
|
expect(mockDispatch.mock.calls.length).toBe(1);
|
|
});
|
|
|
|
it('getTagsAsSegmentsSelectables should handle autocomplete errors', async () => {
|
|
await expect(async () => {
|
|
await getTagsAsSegmentsSelectables(ctx.ctrl.state, 'any');
|
|
expect(mockDispatch).toBeCalledWith(
|
|
expect.objectContaining({
|
|
type: 'appNotifications/notifyApp',
|
|
})
|
|
);
|
|
}).not.toThrow();
|
|
});
|
|
|
|
it('getTagsAsSegmentsSelectables should display the error message only once', async () => {
|
|
await getTagsAsSegmentsSelectables(ctx.ctrl.state, 'any');
|
|
expect(mockDispatch.mock.calls.length).toBe(1);
|
|
|
|
await getTagsAsSegmentsSelectables(ctx.ctrl.state, 'any');
|
|
expect(mockDispatch.mock.calls.length).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('targetChanged', () => {
|
|
beforeEach(async () => {
|
|
const newQuery = 'aliasByNode(scaleToSeconds(test.prod.*, 1), 2)';
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, newQuery);
|
|
});
|
|
|
|
it('should rebuild target after expression model', () => {
|
|
expect(ctx.ctrl.state.target.target).toBe('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
|
|
});
|
|
|
|
it('should call panelCtrl.refresh', () => {
|
|
expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('when updating targets with nested query', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, 'scaleToSeconds(#A, 60)');
|
|
});
|
|
|
|
it('should add function params', () => {
|
|
expect(ctx.ctrl.state.queryModel.segments.length).toBe(1);
|
|
expect(ctx.ctrl.state.queryModel.segments[0].value).toBe('#A');
|
|
|
|
expect(ctx.ctrl.state.queryModel.functions[0].params.length).toBe(1);
|
|
expect(ctx.ctrl.state.queryModel.functions[0].params[0]).toBe(60);
|
|
});
|
|
|
|
it('target should remain the same', () => {
|
|
expect(ctx.ctrl.state.target.target).toBe('scaleToSeconds(#A, 60)');
|
|
});
|
|
|
|
it('targetFull should include nested queries', async () => {
|
|
ctx.ctrl.state.panelCtrl.panel.targets = [
|
|
{
|
|
target: 'nested.query.count',
|
|
refId: 'A',
|
|
},
|
|
];
|
|
|
|
await changeTarget(ctx, ctx.target.target);
|
|
|
|
expect(ctx.ctrl.state.target.target).toBe('scaleToSeconds(#A, 60)');
|
|
|
|
expect(ctx.ctrl.state.target.targetFull).toBe('scaleToSeconds(nested.query.count, 60)');
|
|
});
|
|
});
|
|
|
|
describe('when updating target used in other query', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
ctx.ctrl.target.refId = 'A';
|
|
await changeTarget(ctx, 'metrics.foo.count');
|
|
|
|
ctx.ctrl.state.panelCtrl.panel.targets = [ctx.ctrl.target, { target: 'sumSeries(#A)', refId: 'B' }];
|
|
|
|
await changeTarget(ctx, 'metrics.bar.count');
|
|
});
|
|
|
|
it('targetFull of other query should update', () => {
|
|
expect(ctx.ctrl.state.panelCtrl.panel.targets[1].targetFull).toBe('sumSeries(metrics.bar.count)');
|
|
});
|
|
});
|
|
|
|
describe('when adding seriesByTag function', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, '');
|
|
await ctx.ctrl.dispatch(actions.addFunction({ name: 'seriesByTag' }));
|
|
});
|
|
|
|
it('should update functions', () => {
|
|
expect(ctx.ctrl.state.queryModel.getSeriesByTagFuncIndex()).toBe(0);
|
|
});
|
|
|
|
it('should update seriesByTagUsed flag', () => {
|
|
expect(ctx.ctrl.state.queryModel.seriesByTagUsed).toBe(true);
|
|
});
|
|
|
|
it('should update target', () => {
|
|
expect(ctx.ctrl.state.target.target).toBe('seriesByTag()');
|
|
});
|
|
|
|
it('should call refresh', () => {
|
|
expect(ctx.panelCtrl.refresh).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('when parsing seriesByTag function', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, "seriesByTag('tag1=value1', 'tag2!=~value2')");
|
|
});
|
|
|
|
it('should add tags', () => {
|
|
const expected = [
|
|
{ key: 'tag1', operator: '=', value: 'value1' },
|
|
{ key: 'tag2', operator: '!=~', value: 'value2' },
|
|
];
|
|
expect(ctx.ctrl.state.queryModel.tags).toEqual(expected);
|
|
});
|
|
|
|
it('should add plus button', () => {
|
|
expect(ctx.ctrl.state.addTagSegments.length).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('when tag added', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, 'seriesByTag()');
|
|
await ctx.ctrl.dispatch(actions.addNewTag({ segment: { value: 'tag1' } }));
|
|
});
|
|
|
|
it('should update tags with default value', () => {
|
|
const expected = [{ key: 'tag1', operator: '=', value: '' }];
|
|
expect(ctx.ctrl.state.queryModel.tags).toEqual(expected);
|
|
});
|
|
|
|
it('should update target', () => {
|
|
const expected = "seriesByTag('tag1=')";
|
|
expect(ctx.ctrl.state.target.target).toEqual(expected);
|
|
});
|
|
});
|
|
|
|
describe('when tag changed', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, "seriesByTag('tag1=value1', 'tag2!=~value2')");
|
|
await ctx.ctrl.dispatch(
|
|
actions.tagChanged({ tag: { key: 'tag1', operator: '=', value: 'new_value' }, index: 0 })
|
|
);
|
|
});
|
|
|
|
it('should update tags', () => {
|
|
const expected = [
|
|
{ key: 'tag1', operator: '=', value: 'new_value' },
|
|
{ key: 'tag2', operator: '!=~', value: 'value2' },
|
|
];
|
|
expect(ctx.ctrl.state.queryModel.tags).toEqual(expected);
|
|
});
|
|
|
|
it('should update target', () => {
|
|
const expected = "seriesByTag('tag1=new_value', 'tag2!=~value2')";
|
|
expect(ctx.ctrl.state.target.target).toEqual(expected);
|
|
});
|
|
});
|
|
|
|
describe('when tag removed', () => {
|
|
beforeEach(async () => {
|
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
|
await changeTarget(ctx, "seriesByTag('tag1=value1', 'tag2!=~value2')");
|
|
await ctx.ctrl.dispatch(
|
|
actions.tagChanged({ tag: { key: ctx.ctrl.state.removeTagValue, operator: '=', value: '' }, index: 0 })
|
|
);
|
|
});
|
|
|
|
it('should update tags', () => {
|
|
const expected = [{ key: 'tag2', operator: '!=~', value: 'value2' }];
|
|
expect(ctx.ctrl.state.queryModel.tags).toEqual(expected);
|
|
});
|
|
|
|
it('should update target', () => {
|
|
const expected = "seriesByTag('tag2!=~value2')";
|
|
expect(ctx.ctrl.state.target.target).toEqual(expected);
|
|
});
|
|
});
|
|
});
|