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

feat(jinja): metric macro #27582

Merged
merged 5 commits into from
Mar 25, 2024
Merged

Conversation

Vitor-Avila
Copy link
Contributor

SUMMARY

This PR adds a new Jinja macro: {{ metric('metric_key', dataset_id) }}. This macro can be used to retrieve metric syntax from a dataset, to be re-used in charts/SQL Lab/etc.

This is useful to avoid copy/paste and provide a centralized place for metric management. Some use-cases:

  • Overwriting a metric label in the chart level: Users can use the macro as a custom SQL metric to define a different label in the chart without changing the dataset configuration.
  • Combining metrics: Users could combine metrics (for example {{ metric('revenue') }} - {{ metric('cost') }}.
  • Using metrics in SQL Lab: Retrieve the metric syntax in SQL Lab for ad-hoc querying.
  • Reusing metrics across datasets: When creating virtual datasets that are "variations" of a curated physical dataset, this macro can help retrieving metric syntax syntax from the curated dataset into the virtual one.

The dataset_id parameter is optional and if not provided Superset will attempt to get it from the current context.

I also noticed there were both test_jinja_context.py and jinja_context_test.py, so I unified the two.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

After

Video attached to Google Drive (Exceeds 10MB)

TESTING INSTRUCTIONS

Both unit and integration tests added. For manual testing:

  1. Create a metric in any dataset.
  2. Create a chart using this dataset.
  3. Define a metric using custom SQL: {{ metric("metric_key") }}, replacing metric_key with the key used to create the metric in step 1.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags: ENABLE_TEMPLATE_PROCESSING.
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@github-actions github-actions bot added the doc Namespace | Anything related to documentation label Mar 20, 2024
Copy link

codecov bot commented Mar 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.85%. Comparing base (f274c47) to head (98d8979).
Report is 1393 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #27582      +/-   ##
==========================================
+ Coverage   67.46%   69.85%   +2.38%     
==========================================
  Files        1910     1911       +1     
  Lines       74802    74982     +180     
  Branches     8345     8345              
==========================================
+ Hits        50467    52375    +1908     
+ Misses      22284    20556    -1728     
  Partials     2051     2051              
Flag Coverage Δ
hive 48.92% <6.89%> (?)
mysql 77.88% <65.51%> (-0.14%) ⬇️
postgres 78.02% <65.51%> (-0.12%) ⬇️
presto 53.64% <6.89%> (?)
python 83.14% <100.00%> (+4.87%) ⬆️
sqlite 77.46% <65.51%> (-0.12%) ⬇️
unit 56.77% <100.00%> (?)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rusackas rusackas requested a review from eschutho March 20, 2024 16:57
if chart:
return chart.datasource_id
if dataset_id := form_data.get("url_params", {}).get("datasource_id"):
return dataset_id
Copy link
Member

Choose a reason for hiding this comment

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

this could be None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's a good catch! I was thinking that you must associate a dataset to a chart when creating one, but forgot it could be deleted after. Improved that if chart to if chart and chart.datasource_id to avoid returning None.

On the second part tho (with the walrus operator) if url_params["datasource_id"] is None, this if block condition won't be met and therefore it won't return. I added a test case covering this specific scenario.

if chart_id := (
form_data.get("slice_id") or form_data.get("url_params", {}).get("slice_id")
):
chart_data = ChartDAO.find_by_id(chart_id)
Copy link
Member

Choose a reason for hiding this comment

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

chart_id could be None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Similarly, the walrus operator condition here shouldn't be met so we wouldn't return None. Also added a test case for this scenario.

@Vitor-Avila Vitor-Avila requested a review from dpgaspar March 22, 2024 22:01
@dpgaspar dpgaspar merged commit d874225 into apache:master Mar 25, 2024
38 checks passed
sadpandajoe pushed a commit to preset-io/superset that referenced this pull request Mar 26, 2024
sadpandajoe pushed a commit to preset-io/superset that referenced this pull request Mar 26, 2024
sadpandajoe pushed a commit to preset-io/superset that referenced this pull request Mar 28, 2024
sadpandajoe pushed a commit to preset-io/superset that referenced this pull request Apr 1, 2024
sadpandajoe pushed a commit to preset-io/superset that referenced this pull request Apr 2, 2024
EandrewJones pushed a commit to UMD-ARLIS/superset that referenced this pull request Apr 5, 2024
EnxDev pushed a commit to EnxDev/superset that referenced this pull request Apr 12, 2024
betodealmeida pushed a commit that referenced this pull request Apr 25, 2024
(cherry picked from commit d874225)
qleroy pushed a commit to qleroy/superset that referenced this pull request Apr 28, 2024
eschutho pushed a commit that referenced this pull request Jun 5, 2024
(cherry picked from commit d874225)
vinothkumar66 pushed a commit to vinothkumar66/superset that referenced this pull request Nov 11, 2024
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 4.1.0 labels Nov 27, 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 doc Namespace | Anything related to documentation size/XXL 🚢 4.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants