Skip to content

Conversation

@idastambuk
Copy link
Contributor

@idastambuk idastambuk commented May 27, 2024

What this PR does / why we need it:
Previously, we were interpolating variables in the query() method manually. This adds several methods to the datasource that are called by code Grafana whenever variable and ad hoc interpolation is necessary:

  • applyTemplateVariables - this is a method that is called by Grafana during the super.query() call, which means we don't have to do this manually in our datasource. The method implements template variable interpolation as well as ad hoc queries following the approach in Elastic search from this PR
  • interpolateVariablesInQueries called from multiple places in core grafana, didn't look too much into it but most datasources implement it. Elastic here
  • copied processing for ad hoc filters for lucene queries added in Elastic in this PR
  • added processing for PPL ad hoc filters. This is mostly copied from what we had, with the difference of adding removeFilterFromPPLQuery, PPLQueryHasFilter, toggleQueryFilterForPPL etc. that are now used for both adding ad hoc filters in dashboards and in Explore
  • also fixes Unable to parse interval #389 by interpolating the stringified query

Which issue(s) this PR fixes:

Fixes #379, #389

Special notes for your reviewer:

  • this was mostly tested with backend flow, but the frontend flow seems to be improved as well, mainly when adding ad hoc queries in Explore

How to test

  1. In Dashboard run Lucene "Raw data" query with table panel and click on magnifying glasses in the table cells.
  • + adds a filter
  • + again removes a filter
  • - filters out a field.
  1. Make sure that executed query (using query inspector) has filters as query.query. The query won't have filters in the UI, just the query inspector
  2. Switch to Explore and do the filtering in the same way. In Explore, the query should be shown in the UI Query field
  3. Repeat 1-3 for PPL queries
@idastambuk idastambuk requested a review from a team as a code owner May 27, 2024 15:47
@idastambuk idastambuk requested review from iwysiu and njvrzm and removed request for a team May 27, 2024 15:47
expect(mockedSuperQuery).toHaveBeenCalled();
});

it('should send interpolated query to backend', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved these tests below, to test applyTemplateVariables

}

// Frontend flow
const targetsWithInterpolatedVariables = request.targets.map((target) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added interpolating template variables because adhoc filters are later added for Lucene and PPL in the frontend flow, e.g.
this.queryBuilder.buildPPLQuery(target, adhocFilters, queryString)

@idastambuk idastambuk requested a review from kevinwcyu May 27, 2024 16:09
Copy link
Contributor

@iwysiu iwysiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I hit + twice for a value in the dashboard panel it didn't remove the filter, but in Explore it did. Is that an expected difference (because of the migration) or is that a bug?

return lucene.toString(removeNodeFromTree(ast, node));
}

function removeNodeFromTree(ast: AST, node: NodeTerm): AST {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to group all the tree functions together and all the query functions together

value: filter.options.value,
operator: '!=',
};
// If the opposite filter is present, remove it before adding the new one.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the logic is
FILTER_FOR: remove if present, add if not
FILTER_OUT: remove opposite if present and add filter (always)
I'm not familar with this feature, is that how it's supposed to be? Is FILTER_OUT the only time when we care if the opposite exists, and it doesn't care if it's already been added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand, that's the case for ad hoc filters, yes. Filter out is basically "only show != value"

@idastambuk
Copy link
Contributor Author

When I hit + twice for a value in the dashboard panel it didn't remove the filter, but in Explore it did. Is that an expected difference (because of the migration) or is that a bug?

This is expected, should have mentioned it. Grafana in those cases still passes the same filters without removing existing ones. I assume this is because you can remove the filters from the variable list above the panels.

@idastambuk idastambuk requested a review from iwysiu May 31, 2024 09:05
Copy link
Contributor

@iwysiu iwysiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple more new lines

"resolvedVariable | where `bar` = 'baz' and `job` != 'grafana' and `bytes` > 50 and `count` < 100 and `timestamp` = timestamp('2020-11-22 16:40:43.000000')"
);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newlines before and after this describe block

expect(query).toBe("`test` = 'test1'");
});
});
describe('Escaping characters in adhoc filter', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline before and after this describe block

@idastambuk idastambuk merged commit c8067fc into main Jun 4, 2024
@idastambuk idastambuk deleted the ad-hoc branch June 4, 2024 16:26
@idastambuk idastambuk mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants