mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Graphite: Update text editor state on initial load (#37202)
* Update current query when props change * Remove managed state in GraphiteTextEditor * Fix tests
This commit is contained in:
parent
75bd0bbacd
commit
e8eb73f271
@ -1,4 +1,4 @@
|
|||||||
import React, { useCallback, useState } from 'react';
|
import React, { useCallback } from 'react';
|
||||||
import { QueryField } from '@grafana/ui';
|
import { QueryField } from '@grafana/ui';
|
||||||
import { actions } from '../state/actions';
|
import { actions } from '../state/actions';
|
||||||
import { Dispatch } from 'redux';
|
import { Dispatch } from 'redux';
|
||||||
@ -9,19 +9,24 @@ type Props = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function GraphiteTextEditor({ rawQuery, dispatch }: Props) {
|
export function GraphiteTextEditor({ rawQuery, dispatch }: Props) {
|
||||||
const [currentQuery, updateCurrentQuery] = useState<string>(rawQuery);
|
const updateQuery = useCallback(
|
||||||
|
(query: string) => {
|
||||||
|
dispatch(actions.updateQuery({ query }));
|
||||||
|
},
|
||||||
|
[dispatch]
|
||||||
|
);
|
||||||
|
|
||||||
const applyChanges = useCallback(() => {
|
const runQuery = useCallback(() => {
|
||||||
dispatch(actions.updateQuery({ query: currentQuery }));
|
dispatch(actions.runQuery());
|
||||||
}, [dispatch, currentQuery]);
|
}, [dispatch]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<QueryField
|
<QueryField
|
||||||
query={rawQuery}
|
query={rawQuery}
|
||||||
onChange={updateCurrentQuery}
|
onChange={updateQuery}
|
||||||
onBlur={applyChanges}
|
onBlur={runQuery}
|
||||||
onRunQuery={applyChanges}
|
onRunQuery={runQuery}
|
||||||
placeholder={'Enter a Graphite query (run with Shift+Enter)'}
|
placeholder={'Enter a Graphite query (run with Shift+Enter)'}
|
||||||
portalOrigin="graphite"
|
portalOrigin="graphite"
|
||||||
/>
|
/>
|
||||||
|
@ -142,7 +142,7 @@ export class GraphiteQueryCtrl extends QueryCtrl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async targetTextChanged(event: ChangeEvent<HTMLInputElement>) {
|
async targetTextChanged(event: ChangeEvent<HTMLInputElement>) {
|
||||||
await this.dispatch(actions.updateQuery({ query: event.target.value }));
|
// WIP: removed, handled by GraphiteTextEditor
|
||||||
}
|
}
|
||||||
|
|
||||||
updateModelTarget() {
|
updateModelTarget() {
|
||||||
|
@ -4,6 +4,7 @@ import gfunc from '../gfunc';
|
|||||||
import { GraphiteQueryCtrl } from '../query_ctrl';
|
import { GraphiteQueryCtrl } from '../query_ctrl';
|
||||||
import { TemplateSrvStub } from 'test/specs/helpers';
|
import { TemplateSrvStub } from 'test/specs/helpers';
|
||||||
import { silenceConsoleOutput } from 'test/core/utils/silenceConsoleOutput';
|
import { silenceConsoleOutput } from 'test/core/utils/silenceConsoleOutput';
|
||||||
|
import { actions } from '../state/actions';
|
||||||
|
|
||||||
jest.mock('app/core/utils/promiseToDigest', () => ({
|
jest.mock('app/core/utils/promiseToDigest', () => ({
|
||||||
promiseToDigest: (scope: any) => {
|
promiseToDigest: (scope: any) => {
|
||||||
@ -16,12 +17,13 @@ jest.mock('app/store/store', () => ({
|
|||||||
}));
|
}));
|
||||||
const mockDispatch = dispatch as jest.Mock;
|
const mockDispatch = dispatch as jest.Mock;
|
||||||
|
|
||||||
async function changeTarget(ctx: any, target: string, refId?: string): Promise<void> {
|
/**
|
||||||
|
* 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.toggleEditorMode();
|
||||||
ctx.ctrl.state.target.target = target;
|
await ctx.ctrl.dispatch(actions.updateQuery({ query: target }));
|
||||||
if (refId) {
|
await ctx.ctrl.dispatch(actions.runQuery());
|
||||||
ctx.ctrl.state.target.refId = refId;
|
|
||||||
}
|
|
||||||
await ctx.ctrl.toggleEditorMode();
|
await ctx.ctrl.toggleEditorMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,9 +285,6 @@ describe('GraphiteQueryCtrl', () => {
|
|||||||
const newQuery = 'aliasByNode(scaleToSeconds(test.prod.*, 1), 2)';
|
const newQuery = 'aliasByNode(scaleToSeconds(test.prod.*, 1), 2)';
|
||||||
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
ctx.ctrl.state.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
||||||
await changeTarget(ctx, newQuery);
|
await changeTarget(ctx, newQuery);
|
||||||
await ctx.ctrl.targetTextChanged({
|
|
||||||
target: { value: newQuery },
|
|
||||||
} as any);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should rebuild target after expression model', () => {
|
it('should rebuild target after expression model', () => {
|
||||||
@ -323,7 +322,7 @@ describe('GraphiteQueryCtrl', () => {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
await ctx.ctrl.targetTextChanged({ target: { value: 'nested.query.count' } } as any);
|
await changeTarget(ctx, ctx.target.target);
|
||||||
|
|
||||||
expect(ctx.ctrl.state.target.target).toBe('scaleToSeconds(#A, 60)');
|
expect(ctx.ctrl.state.target.target).toBe('scaleToSeconds(#A, 60)');
|
||||||
|
|
||||||
@ -334,15 +333,16 @@ describe('GraphiteQueryCtrl', () => {
|
|||||||
describe('when updating target used in other query', () => {
|
describe('when updating target used in other query', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
ctx.ctrl.datasource.metricFindQuery = () => Promise.resolve([{ expandable: false }]);
|
||||||
await changeTarget(ctx, 'metrics.a.count', 'A');
|
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' }];
|
ctx.ctrl.state.panelCtrl.panel.targets = [ctx.ctrl.target, { target: 'sumSeries(#A)', refId: 'B' }];
|
||||||
|
|
||||||
await ctx.ctrl.targetTextChanged({ target: { value: 'metrics.a.count' } } as any);
|
await changeTarget(ctx, 'metrics.bar.count');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('targetFull of other query should update', () => {
|
it('targetFull of other query should update', () => {
|
||||||
expect(ctx.ctrl.state.panelCtrl.panel.targets[1].targetFull).toBe('sumSeries(metrics.a.count)');
|
expect(ctx.ctrl.state.panelCtrl.panel.targets[1].targetFull).toBe('sumSeries(metrics.bar.count)');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -28,6 +28,7 @@ const updateFunctionParam = createAction<{ func: FuncInstance }>('update-functio
|
|||||||
|
|
||||||
// Text editor
|
// Text editor
|
||||||
const updateQuery = createAction<{ query: string }>('update-query');
|
const updateQuery = createAction<{ query: string }>('update-query');
|
||||||
|
const runQuery = createAction('run-current-query');
|
||||||
const toggleEditorMode = createAction('toggle-editor');
|
const toggleEditorMode = createAction('toggle-editor');
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
@ -41,5 +42,6 @@ export const actions = {
|
|||||||
moveFunction,
|
moveFunction,
|
||||||
updateFunctionParam,
|
updateFunctionParam,
|
||||||
updateQuery,
|
updateQuery,
|
||||||
|
runQuery,
|
||||||
toggleEditorMode,
|
toggleEditorMode,
|
||||||
};
|
};
|
||||||
|
@ -151,6 +151,8 @@ const reducer = async (action: Action, state: GraphiteQueryEditorState): Promise
|
|||||||
if (actions.updateQuery.match(action)) {
|
if (actions.updateQuery.match(action)) {
|
||||||
state.target.target = action.payload.query;
|
state.target.target = action.payload.query;
|
||||||
handleTargetChanged(state);
|
handleTargetChanged(state);
|
||||||
|
}
|
||||||
|
if (actions.runQuery.match(action)) {
|
||||||
// handleTargetChanged() builds target from segments/tags/functions only,
|
// handleTargetChanged() builds target from segments/tags/functions only,
|
||||||
// it doesn't handle refresh when target is change explicitly
|
// it doesn't handle refresh when target is change explicitly
|
||||||
state.panelCtrl.refresh();
|
state.panelCtrl.refresh();
|
||||||
|
Loading…
Reference in New Issue
Block a user