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

Use hypothetical get_metric() contextmember to remove gnarliest code #11

Closed
wants to merge 4 commits into from

Conversation

jtcohen6
Copy link
Contributor

@jtcohen6 jtcohen6 commented Feb 17, 2022

Try get_metric() contextmember added in dbt-labs/dbt-core@bd6a4a1, as proposed/discussed in dbt-labs/dbt-core#4641 (comment)

This built-in get_metric() context member / macro:

  • Accepts a metric name as argument
  • Returns the full manifest (graph) entry for the metric
  • Converts model from a string ("ref(my_model)") to the actual relation that ref() call would return

I also switched the dbt_metrics_calendar_model variable to return the string name of a model, rather than a string containing ref(), because it was very slightly more convenient to write the calendar ref(). I don't feel too strongly about this.

Copy link
Contributor

@joellabes joellabes left a comment

Choose a reason for hiding this comment

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

Big fan 🤩 There's not much to comment on over this side, other than whether the calendar override should be a ref or a string

I don't feel too strongly about this.

By contrast, I feel reasonably strongly that it should be a ref - discussed on the line of code itself

Comment on lines -20 to -39
{% macro get_metric(metric_name) %}
{% if not execute %}
{% do return(None) %}
{% else %}
{% set metric_info = namespace(metric_id=none) %}
{% for metric in graph.metrics.values() %}
{% if metric.name == metric_name %}
{% set metric_info.metric_id = metric.unique_id %}
{% endif %}
{% endfor %}

{% if metric_info.metric_id is none %}
{% do exceptions.raise_compiler_error("Metric named '" ~ metric_name ~ "' not found") %}
{% endif %}


{% do return(graph.metrics[metric_info.metric_id]) %}
{% endif %}

{% endmacro %}
Copy link
Contributor

Choose a reason for hiding this comment

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

and stay out!

@@ -114,7 +115,7 @@ To do this, set the value of the `dbt_metrics_calendar_model` variable in your `
config-version: 2
[...]
vars:
dbt_metrics_calendar_model: ref('my_custom_table')
dbt_metrics_calendar_model: my_custom_table # BREAKING change: just the model name, no ref
Copy link
Contributor

Choose a reason for hiding this comment

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

This does make this very ergonomic, but it would be an outlier in terms of how all other refs are handled (cf relationships tests, other packages and metrics themselves) so I would prefer it to stay as a ref instead of an outlier if possible

@callum-mcdata
Copy link
Contributor

@jtcohen6 I believe we spoke about this and this PR should be closed out due to us moving forward with the metric function in dbt-labs/dbt-core#5027 (adding support for ratio metrics)?

If you can confirm I'll close this one out.

@jtcohen6
Copy link
Contributor Author

jtcohen6 commented Jun 3, 2022

@callum-mcdata Yes, that's my hope & aspiration!

@jtcohen6 jtcohen6 closed this Jun 3, 2022
@callum-mcdata callum-mcdata deleted the better-context-methods branch June 7, 2022 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes The CLA has been signed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants