-
Notifications
You must be signed in to change notification settings - Fork 47
/
get_metric_tree.sql
69 lines (58 loc) · 2.98 KB
/
get_metric_tree.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{% macro get_metric_tree(metric_list)%}
{# We are creating the metric tree here - this includes all the leafs (first level parents)
, the derived metrics, and the full combination of them both #}
{# This line creates the metric tree dictionary and the full_set key.
Full Set contains ALL metrics that are referenced, which includes metrics in the macro
AND all parent/derived metrics. #}
{%- set metric_tree = {'full_set':[]} %}
{# The parent set is a list of parent metrics that are NOT derived metrics. IE if
metric C is built off of metric A and B, A and B would be the parent metrics because they
are both upstream of Metric C AND not derived metrics themselves. #}
{%- do metric_tree.update({'parent_set':[]}) -%}
{# The derived set is a list of derived metrics. This includes all derived metrics referenced
in the macro itself OR upstream of the metrics referenced in the macro #}
{%- do metric_tree.update({'derived_set':[]}) -%}
{# The base set is the list of metrics that are provided into the macro #}
{%- do metric_tree.update({'base_set':[]}) -%}
{# The ordered derived set is the list of derived metrics that are ordered based on their
node depth. So if Metric C were downstream of Metric A and B, which were also derived metrics,
Metric C would have the value of 999 (max depth) and A and B would have 998, representing that they
are one depth upstream #}
{%- do metric_tree.update({'ordered_derived_set':{}}) -%}
{% set base_set_list = []%}
{% for metric in metric_list %}
{%- do base_set_list.append(metric.name) -%}
{%- set metric_tree = metrics.update_metric_tree(metric ,metric_tree) -%}
{% endfor %}
{%- do metric_tree.update({'base_set':base_set_list}) -%}
{# Now we will iterate over the metric tree and make it a unique list to account for duplicates #}
{% set full_set = [] %}
{% set parent_set = [] %}
{% set derived_set = [] %}
{% set base_set = [] %}
{% for metric_name in metric_tree['full_set']|unique%}
{% do full_set.append(metric_name)%}
{% endfor %}
{%- do metric_tree.update({'full_set':full_set}) -%}
{% for metric_name in metric_tree['parent_set']|unique%}
{% do parent_set.append(metric_name)%}
{% endfor %}
{%- do metric_tree.update({'parent_set':parent_set}) -%}
{% for metric_name in metric_tree['derived_set']|unique%}
{% do derived_set.append(metric_name)%}
{% endfor %}
{%- do metric_tree.update({'derived_set':derived_set}) -%}
{% for metric in metric_tree['parent_set']|unique%}
{%- do metric_tree['ordered_derived_set'].pop(metric) -%}
{% endfor %}
{# This section overrides the derived set by ordering the metrics on their depth so they
can be correctly referenced in the downstream sql query #}
{% set ordered_expression_list = []%}
{% for item in metric_tree['ordered_derived_set']|dictsort(false, 'value') %}
{% if item[0] in metric_tree["derived_set"]%}
{% do ordered_expression_list.append(item[0])%}
{% endif %}
{% endfor %}
{%- do metric_tree.update({'derived_set':ordered_expression_list}) -%}
{%- do return(metric_tree) -%}
{% endmacro %}