Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-chart-echarts): Apply temporary filters to Query B in explore #18998

Merged
merged 4 commits into from
Mar 4, 2022

Conversation

kgabryje
Copy link
Member

@kgabryje kgabryje commented Mar 2, 2022

SUMMARY

When going from the dashboard to Explore view with extra filters (native filters or filter box), the filters are only appended to the first query. This PR iterates through form data and adds extra filters to every field that matches pattern adhoc_filters.*.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Before:
image

After:
image

TESTING INSTRUCTIONS

  1. Create a mixed timeseries chart and add it to a dashboard
  2. Go to that dashboard and apply some native filters
  3. Click View chart in explore on mixed timeseries chart
  4. Verify that native filter is applied in both Query A and Query B adhoc filter control

ADDITIONAL INFORMATION

DB migration stats:

Current: 0.42 s
10+: 0.44 s
100+: 0.26 s
1000+: 0.25 s

This PR replaces #18740

https://app.shortcut.com/preset/story/37242/explore-mixed-time-series-chart-native-filter-only-apply-to-query-a-but-not-query-b-when-open-mixed-time-series-chart-from

@codecov
Copy link

codecov bot commented Mar 2, 2022

Codecov Report

Merging #18998 (26d9363) into master (299b5dc) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #18998   +/-   ##
=======================================
  Coverage   66.56%   66.56%           
=======================================
  Files        1641     1641           
  Lines       63495    63497    +2     
  Branches     6425     6425           
=======================================
+ Hits        42265    42267    +2     
  Misses      19550    19550           
  Partials     1680     1680           
Flag Coverage Δ
hive 52.59% <0.00%> (-0.01%) ⬇️
javascript 51.37% <ø> (ø)
mysql 81.81% <100.00%> (+<0.01%) ⬆️
postgres 81.86% <100.00%> (+<0.01%) ⬆️
presto 52.43% <0.00%> (-0.01%) ⬇️
python 82.29% <100.00%> (+<0.01%) ⬆️
sqlite 81.55% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...chart-controls/src/shared-controls/dndControls.tsx 35.89% <ø> (ø)
...et-ui-chart-controls/src/shared-controls/index.tsx 36.36% <ø> (ø)
superset/utils/core.py 90.25% <100.00%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 299b5dc...26d9363. Read the comment docs.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

⚠️ @kgabryje Your base branch master has just also updated superset/migrations.

Please consider rebasing your branch to avoid db migration conflicts.

superset/utils/core.py Outdated Show resolved Hide resolved
@@ -1056,6 +1056,16 @@ def form_data_to_adhoc(form_data: Dict[str, Any], clause: str) -> AdhocFilterCla
return result


def append_filters_to_adhoc_filters(
Copy link
Member

Choose a reason for hiding this comment

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

No place for reuse. We don't need to create functions.

Copy link
Member Author

Choose a reason for hiding this comment

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

good point!

Comment on lines +1100 to +1090
for key, value in form_data.items():
if re.match("adhoc_filter.*", key):
value.extend(
simple_filter_to_adhoc({"isExtra": True, **fltr}) # type: ignore
for fltr in append_filters
if fltr
)
Copy link
Member

@ktmud ktmud Mar 3, 2022

Choose a reason for hiding this comment

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

Suggested change
for key, value in form_data.items():
if re.match("adhoc_filter.*", key):
value.extend(
simple_filter_to_adhoc({"isExtra": True, **fltr}) # type: ignore
for fltr in append_filters
if fltr
)
for key in ["adhoc_filters", "adhoc_filters_b"]:
if key in form_data:
form_data[key] = (form_data.get(key) or []) + [
simple_filter_to_adhoc({"isExtra": True, **fltr}) # type: ignore
for fltr in append_filters if fltr
]

I think we can be more explicit about the field names to avoid looping through all fields?

Copy link
Member

Choose a reason for hiding this comment

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

@ktmud in offline discussions I was the person who proposed using a regex to loop through this, so don't blame @kgabryje for this 😄 My reasoning went something like this:

  1. it's fairly uncommon for there to be more than 20 controls. So performance impact of looping through all control keys is negligible
  2. While I know that this is put here mostly for the Mixed Timeseries chart, I don't see any reason why someone wouldn't consider having 3 (or more) query sections. Only supporting one additional control called adhoc_filters_b, but not, say, adhoc_filters_c or adhoc_filters_2, feels slightly arbitrary. The logic behind my reasoning here was, "any control that starts with adhoc_filters should be considered an adhoc filter control, and extra filters should apply to them all". While both solutions solve this specific problem, I still feel the regex solution is less coupled to the Mixed Timeseries chart than only matching ["adhoc_filters", "adhoc_filters_b"]

Having said that, this was really just a minor nit and likely irrelevant in the grand scheme of things, so I won't get hung up on this either way 🙂

Copy link
Member

Choose a reason for hiding this comment

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

Another argument for using more explicit field names is minimizing unintentional side-effects. While the probability is small, there is no mechanism preventing someone from naming a non adhoc-filter field with adhoc_filter in the name. The opposite is also true---one may name an adhoc filters field without it starting with adhoc_filter. We've seen it for metrics before, which is why we have the METRIC_KEYS constant.

Copy link
Member

Choose a reason for hiding this comment

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

This is true. However, naming a control that doesn't contain adhoc filters with the prefix "adhoc_filters" feels less likely than than the opposite.

Copy link
Member

Choose a reason for hiding this comment

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

True.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for discussion. Do we agree that there's no action/change required here?

Copy link
Member

Choose a reason for hiding this comment

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

I still believe in general regex should be used sparingly and explicit is better than implicit. Since you can't stop someone from naming a new filter field things like "series_b_filters", the bug may still come back even with a regex.

In any case, we should need a consistent way of mapping field names to field types because most of the time what you want is "do something special based on filter type", not filed names. One obvious solution is to create an ADHOC_FILTER_KEYS constant and put it together with METRIC_KEYS in some constants.py file, but I agree this by itself probably doesn't make much difference on the grander scheme of things.

Copy link
Member

Choose a reason for hiding this comment

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

I agree that we should make all these things as explicit as possible, at a minimum documenting magic behavior like this. While on this subject, the whole notion of QueryFormData automatically mapping properties like metric and secondary_metric to metrics in QueryObject is really opaque, and should probably both 1) be more thoroughly documented 2) be phased out in coming major versions. But in the short term I propose adding documentation around this functionality to at least explain how this stuff works right now.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2022

⚠️ @kgabryje Your base branch master has just also updated superset/migrations.

Please consider rebasing your branch to avoid db migration conflicts.

1 similar comment
@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2022

⚠️ @kgabryje Your base branch master has just also updated superset/migrations.

Please consider rebasing your branch to avoid db migration conflicts.

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

LGTM

@raags
Copy link

raags commented Mar 11, 2022

How to get this backported to 1.4? Or check in which version this fix would be included in?

villebro pushed a commit that referenced this pull request Apr 3, 2022
…ore (#18998)

* fix(explore): Extra filters not applied to query b in mixed timeseries

* Add return type

* Apply review comment

* Fix non-dnd filters

(cherry picked from commit 9f834e8)
@mistercrunch mistercrunch added 🍒 1.5.1 🍒 1.5.2 🍒 1.5.3 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 2.0.0 labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels lts-v1 size/L 🍒 1.5.0 🍒 1.5.1 🍒 1.5.2 🍒 1.5.3 🚢 2.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[chart][mixed time series chart] Mixed time series chart only show dashboard filter from query A
6 participants