|
| 1 | +{% macro develop(develop_yml, grain, dimensions=[], secondary_calculations=[], start_date=None, end_date=None, where=None) -%} |
| 2 | + {{ return(adapter.dispatch('develop', 'metrics')(develop_yml, grain, dimensions, secondary_calculations, start_date, end_date, where)) }} |
| 3 | +{% endmacro %} |
| 4 | + |
| 5 | + |
| 6 | +{% macro default__develop(develop_yml, grain, dimensions=[], secondary_calculations=[], start_date=None, end_date=None, where=None) -%} |
| 7 | + -- Need this here, since the actual ref is nested within loops/conditions: |
| 8 | + -- depends on: {{ ref(var('dbt_metrics_calendar_model', 'dbt_metrics_default_calendar')) }} |
| 9 | + |
| 10 | + {%- if not execute %} |
| 11 | + {%- do return("not execute") %} |
| 12 | + {%- endif %} |
| 13 | + |
| 14 | + {% set develop_yml = fromyaml(develop_yml)%} |
| 15 | + |
| 16 | + {# ############ |
| 17 | + VALIDATION OF PROVIDED YML - Gotta make sure the metric looks good! |
| 18 | + ############ #} |
| 19 | + |
| 20 | + {% if develop_yml["metrics"] | length > 1%} |
| 21 | + {%- do exceptions.raise_compiler_error("The develop macro only supports testing a single macro.") %} |
| 22 | + {% endif %} |
| 23 | + |
| 24 | + {% set metric_definition = develop_yml["metrics"][0] %} |
| 25 | + |
| 26 | + {%- if not metric_definition["name"] %} |
| 27 | + {%- do exceptions.raise_compiler_error("The provided yml is missing a name") %} |
| 28 | + {%- endif %} |
| 29 | + |
| 30 | + {%- if not metric_definition["model"] %} |
| 31 | + {%- do exceptions.raise_compiler_error("The provided yml is missing a model") %} |
| 32 | + {%- endif %} |
| 33 | + |
| 34 | + {%- if not metric_definition["timestamp"] %} |
| 35 | + {%- do exceptions.raise_compiler_error("The provided yml is missing a timestamp") %} |
| 36 | + {%- endif %} |
| 37 | + |
| 38 | + {%- if not metric_definition["time_grains"] %} |
| 39 | + {%- do exceptions.raise_compiler_error("The provided yml is missing time grains") %} |
| 40 | + {%- endif %} |
| 41 | + |
| 42 | + {%- if grain not in metric_definition["time_grains"] %} |
| 43 | + {%- do exceptions.raise_compiler_error("The selected grain is missing from the metric definition yml") %} |
| 44 | + {%- endif %} |
| 45 | + |
| 46 | + {%- if not metric_definition["type"] %} |
| 47 | + {%- do exceptions.raise_compiler_error("The provided yml is missing a metric type") %} |
| 48 | + {%- endif %} |
| 49 | + |
| 50 | + {%- if metric_definition["type"] == 'expression' %} |
| 51 | + {%- do exceptions.raise_compiler_error("The develop macro does not support expression metrics") %} |
| 52 | + {%- endif %} |
| 53 | + |
| 54 | + {%- if not metric_definition["sql"] %} |
| 55 | + {%- do exceptions.raise_compiler_error("The provided yml is missing a sql field") %} |
| 56 | + {%- endif %} |
| 57 | + |
| 58 | + {% for dim in dimensions %} |
| 59 | + {% if dim not in metric_definition["dimensions"] %} |
| 60 | + {%- do exceptions.raise_compiler_error("The macro provided dimension is missing from the metric definition") %} |
| 61 | + {% endif %} |
| 62 | + {% endfor %} |
| 63 | + |
| 64 | + {# ############ |
| 65 | + VALIDATION OF MACRO INPUTS - Making sure we have a provided grain! |
| 66 | + ############ #} |
| 67 | + |
| 68 | + {%- if not grain %} |
| 69 | + {%- do exceptions.raise_compiler_error("No date grain provided") %} |
| 70 | + {%- endif %} |
| 71 | + |
| 72 | + {# ############ |
| 73 | + VARIABLE SETTING - Creating the faux metric tree and faux metric list. The faux fur of 2022 |
| 74 | + ############ #} |
| 75 | + |
| 76 | + {% set metric_list = [metric_definition["name"]] %} |
| 77 | + {% set metric_tree = metrics.get_faux_metric_tree(metric_list) %} |
| 78 | + {% set metric_type = metric_definition["type"]%} |
| 79 | + |
| 80 | + {# ############ |
| 81 | + SECONDARY CALCULATION VALIDATION - Gotta make sure the secondary calcs are good! |
| 82 | + ############ #} |
| 83 | + |
| 84 | + {%- for calc_config in secondary_calculations if calc_config.aggregate %} |
| 85 | + {%- do metrics.validate_aggregate_coherence(metric_type, calc_config.aggregate) %} |
| 86 | + {%- endfor %} |
| 87 | + |
| 88 | + {%- for calc_config in secondary_calculations if calc_config.period %} |
| 89 | + {%- do metrics.validate_grain_order(grain, calc_config.period) %} |
| 90 | + {%- endfor %} |
| 91 | + |
| 92 | + {# ############ |
| 93 | + VARIABLES FOR SQL GEN - More variables we need for sql gen |
| 94 | + ############ #} |
| 95 | + |
| 96 | + {%- set calendar_dimensions = metrics.get_calendar_dimension_list() -%} |
| 97 | + {%- set non_calendar_dimensions = metrics.get_non_calendar_dimension_list(dimensions,calendar_dimensions) -%} |
| 98 | + {%- set relevant_periods = metrics.get_relevent_periods(grain, secondary_calculations) %} |
| 99 | + |
| 100 | + {# ############ |
| 101 | + SQL GENERATION - Lets build that SQL! |
| 102 | + ############ #} |
| 103 | + |
| 104 | + {%- set sql = metrics.get_metric_sql( |
| 105 | + metric_list=metric_list, |
| 106 | + grain=grain, |
| 107 | + dimensions=dimensions, |
| 108 | + secondary_calculations=secondary_calculations, |
| 109 | + start_date=start_date, |
| 110 | + end_date=end_date, |
| 111 | + where=where, |
| 112 | + initiated_by='develop', |
| 113 | + metric_definition=metric_definition, |
| 114 | + metric_tree=metric_tree, |
| 115 | + calendar_dimensions=calendar_dimensions, |
| 116 | + non_calendar_dimensions=non_calendar_dimensions, |
| 117 | + relevant_periods=relevant_periods |
| 118 | + ) %} |
| 119 | + ({{ sql }}) metric_subq |
| 120 | +{%- endmacro %} |
0 commit comments