Skip to content

Commit

Permalink
chore: update updateFilters to take filters as input
Browse files Browse the repository at this point in the history
  • Loading branch information
gtk-grafana committed Dec 20, 2024
1 parent a170789 commit 051229a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: variable.state.filters.slice(0) });
variable.updateFilters(variable.state.filters.slice(0));

expect(evtHandler).toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual('key1="val1"');
Expand All @@ -1008,7 +1008,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: variable.state.filters.slice(0) });
variable.updateFilters(variable.state.filters.slice(0));

expect(evtHandler).not.toHaveBeenCalled();
});
Expand All @@ -1025,7 +1025,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: variable.state.filters.slice(0) }, { forcePublish: true });
variable.updateFilters(variable.state.filters.slice(0), { forcePublish: true });

expect(evtHandler).toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual('key1="val1"');
Expand All @@ -1036,15 +1036,14 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
datasource: { uid: 'hello' },
applyMode: 'manual',
filters: [{ key: 'key1', operator: '=', value: 'val1' }],
filterExpression: 'hello filter expression',
});

variable.activate();

const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: [{ key: 'key2', operator: '=', value: 'val1' }] });
variable.updateFilters([{ key: 'key2', operator: '=', value: 'val1' }]);

expect(evtHandler).toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual(`key2="val1"`);
Expand All @@ -1063,7 +1062,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: [{ key: 'key2', operator: '=', value: 'val1' }] }, { skipPublish: true });
variable.updateFilters([{ key: 'key2', operator: '=', value: 'val1' }], { skipPublish: true });

expect(evtHandler).not.toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual(`key2="val1"`);
Expand All @@ -1081,7 +1080,7 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {
const evtHandler = jest.fn();
variable.subscribeToEvent(SceneVariableValueChangedEvent, evtHandler);

variable.updateFilters({ filters: [{ key: 'key2', operator: '=', value: 'val1' }] }, { skipPublish: true });
variable.updateFilters([{ key: 'key2', operator: '=', value: 'val1' }], { skipPublish: true });

expect(evtHandler).not.toHaveBeenCalled();
expect(variable.state.filterExpression).toEqual(`key2="val1"`);
Expand Down
31 changes: 23 additions & 8 deletions packages/scenes/src/variables/adhoc/AdHocFiltersVariable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,18 @@ export class AdHocFiltersVariable
}

public setState(update: Partial<AdHocFiltersVariableState>): void {
this.updateFilters(update);
let filterExpressionChanged = false;

if (update.filters && update.filters !== this.state.filters && !update.filterExpression) {
update.filterExpression = renderExpression(this.state.expressionBuilder, update.filters);
filterExpressionChanged = update.filterExpression !== this.state.filterExpression;
}

super.setState(update);

if (filterExpressionChanged) {
this.publishEvent(new SceneVariableValueChangedEvent(this), true);
}
}

/**
Expand All @@ -199,22 +210,26 @@ export class AdHocFiltersVariable
* allowing consumers to update the filters without triggering dependent data providers.
*/
public updateFilters(
update: Partial<AdHocFiltersVariableState>,
filters: AdHocFilterWithLabels[],
options?: {
skipPublish?: boolean;
forcePublish?: boolean;
forcePublish?: boolean
}
): void {
let filterExpressionChanged = false;
let filterExpression = undefined;

if (update.filters && update.filters !== this.state.filters && !update.filterExpression) {
update.filterExpression = renderExpression(this.state.expressionBuilder, update.filters);
filterExpressionChanged = update.filterExpression !== this.state.filterExpression;
if (filters && filters !== this.state.filters) {
filterExpression = renderExpression(this.state.expressionBuilder, filters);
filterExpressionChanged = filterExpression !== this.state.filterExpression;
}

super.setState(update);
super.setState({
filters,
filterExpression,
});

if ((filterExpressionChanged && options?.skipPublish !== true) || options?.forcePublish === true) {
if (filterExpressionChanged && options?.skipPublish !== true || options?.forcePublish) {
this.publishEvent(new SceneVariableValueChangedEvent(this), true);
}
}
Expand Down

0 comments on commit 051229a

Please sign in to comment.