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: supporting jinja templating in saved metrics #15502

Merged
merged 4 commits into from Jul 5, 2021

Conversation

guydou
Copy link
Contributor

@guydou guydou commented Jul 1, 2021

SUMMARY

Adding the ability to use jinja templating also in saved metrics.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

image

TESTING INSTRUCTIONS

  1. A saved metric that uses a jinja expression
  2. Go to Chart and use that saved metric
  3. Look in the view query, and make sure the jinja expression was evaluated correctly

ADDITIONAL INFORMATION

  • Has associated issue:
  • 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

@codecov
Copy link

codecov bot commented Jul 1, 2021

Codecov Report

Merging #15502 (5a51522) into master (e606477) will decrease coverage by 0.37%.
The diff coverage is 59.46%.

❗ Current head 5a51522 differs from pull request most recent head 885e321. Consider uploading reports for the commit 885e321 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master   #15502      +/-   ##
==========================================
- Coverage   77.20%   76.82%   -0.38%     
==========================================
  Files         975      976       +1     
  Lines       50842    51306     +464     
  Branches     6728     6912     +184     
==========================================
+ Hits        39251    39417     +166     
- Misses      11376    11670     +294     
- Partials      215      219       +4     
Flag Coverage Δ
hive ?
mysql 81.59% <64.07%> (-0.06%) ⬇️
postgres 81.61% <64.07%> (-0.05%) ⬇️
presto 81.32% <63.10%> (-0.06%) ⬇️
python 81.88% <64.07%> (-0.31%) ⬇️
sqlite 81.22% <62.13%> (-0.07%) ⬇️

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

Impacted Files Coverage Δ
...et-frontend/src/SqlLab/reducers/getInitialState.js 50.00% <ø> (ø)
...rset-frontend/src/components/ErrorMessage/types.ts 100.00% <ø> (ø)
superset-frontend/src/dashboard/actions/hydrate.js 1.70% <0.00%> (-0.02%) ⬇️
...tersConfigModal/FiltersConfigForm/DefaultValue.tsx 20.83% <0.00%> (-4.17%) ⬇️
...ters/FiltersConfigModal/FiltersConfigForm/utils.ts 88.88% <0.00%> (ø)
...mponents/nativeFilters/FiltersConfigModal/utils.ts 75.34% <ø> (+7.84%) ⬆️
...nd/src/dashboard/containers/DashboardComponent.jsx 100.00% <ø> (ø)
...-frontend/src/dashboard/reducers/dashboardState.js 70.96% <0.00%> (-2.37%) ⬇️
...filters/components/GroupBy/GroupByFilterPlugin.tsx 0.00% <0.00%> (ø)
...ntend/src/filters/components/Range/controlPanel.ts 100.00% <ø> (ø)
... and 89 more

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 e606477...885e321. Read the comment docs.

Comment on lines 407 to 414
def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor:
return self.table.get_template_processor()

def get_sqla_col(self, label: Optional[str] = None) -> Column:
label = label or self.metric_name
sqla_col: ColumnClause = literal_column(self.expression)
sqla_col: ColumnClause = literal_column(
self.get_template_processor().process_template(self.expression)
)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for your contributions. It is not recommended to add the new instance method(get_template_processor) here.

Suggested change
def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor:
return self.table.get_template_processor()
def get_sqla_col(self, label: Optional[str] = None) -> Column:
label = label or self.metric_name
sqla_col: ColumnClause = literal_column(self.expression)
sqla_col: ColumnClause = literal_column(
self.get_template_processor().process_template(self.expression)
)
def get_sqla_col(self, label: Optional[str] = None) -> Column:
label = label or self.metric_name
tp = self.table.get_template_processor()
sqla_col = literal_column(tp.process_template(self.expression))
return self.table.make_sqla_column_compatible(sqla_col, label)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it,

Fixed the code,

Why it is not recommended to have new instance method? I am asking only yo expand my knowledge

Thanks

Copy link
Member

Choose a reason for hiding this comment

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

Hi Guy. The main reason is no more self.get_template_processor method call currently, so no need to extend this class. If there is a need for this in the future, we can easily extend it.

@zhaoyongjie
Copy link
Member

closes: #6983

Copy link
Member

@zhaoyongjie zhaoyongjie left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!

@zhaoyongjie zhaoyongjie merged commit bee386e into apache:master Jul 5, 2021
cccs-RyanS pushed a commit to CybercentreCanada/superset that referenced this pull request Dec 17, 2021
* feat: supporting jinja templating in saved metrics

* cutting the line length to fit requirements

* follwing PR I should have not created a new insance method

* running precommit locally and fixing the issue

Co-authored-by: Guy <guy@nexite.io>
QAlexBall pushed a commit to QAlexBall/superset that referenced this pull request Dec 29, 2021
* feat: supporting jinja templating in saved metrics

* cutting the line length to fit requirements

* follwing PR I should have not created a new insance method

* running precommit locally and fixing the issue

Co-authored-by: Guy <guy@nexite.io>
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
* feat: supporting jinja templating in saved metrics

* cutting the line length to fit requirements

* follwing PR I should have not created a new insance method

* running precommit locally and fixing the issue

Co-authored-by: Guy <guy@nexite.io>
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.3.0 labels Mar 12, 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 size/XS 🚢 1.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Jinja templating is ignored in Metrics definitions
3 participants