|
13 | 13 | {% do metric_name_to_type.update({metric.name: metric.type}) %} |
14 | 14 | {% endfor %} |
15 | 15 |
|
16 | | - |
17 | 16 | with monitored_table as ( |
18 | 17 | select * from {{ monitored_table }} |
19 | 18 | {% if metric_properties.where_expression %} where {{ metric_properties.where_expression }} {% endif %} |
|
26 | 25 | where edr_bucket_start >= {{ elementary.edr_cast_as_timestamp(min_bucket_start) }} |
27 | 26 | and edr_bucket_end <= {{ elementary.edr_cast_as_timestamp(max_bucket_end) }} |
28 | 27 | ), |
29 | | - |
30 | 28 | filtered_monitored_table as ( |
31 | 29 | select {{ column_obj.quoted }}, |
32 | 30 | {%- if dimensions -%} {{ elementary.select_dimensions_columns(dimensions, "dimension") }}, {%- endif -%} |
|
96 | 94 | ), |
97 | 95 |
|
98 | 96 | column_metrics_unpivot as ( |
99 | | - |
100 | | - {%- if column_metrics %} |
101 | | - {% for metric_name, metric_type in metric_name_to_type.items() %} |
102 | | - select |
103 | | - {{ elementary.const_as_string(column_obj.name) }} as edr_column_name, |
104 | | - bucket_start, |
105 | | - bucket_end, |
106 | | - {% if timestamp_column %} |
107 | | - {{ elementary.timediff("hour", "bucket_start", "bucket_end") }} as bucket_duration_hours, |
108 | | - {% else %} |
109 | | - {{ elementary.null_int() }} as bucket_duration_hours, |
110 | | - {% endif %} |
111 | | - {% if dimensions | length > 0 %} |
112 | | - {{ elementary.const_as_string(elementary.join_list(dimensions, separator='; ')) }} as dimension, |
113 | | - {{ elementary.list_concat_with_separator(prefixed_dimensions, separator='; ') }} as dimension_value, |
114 | | - {% else %} |
115 | | - {{ elementary.null_string() }} as dimension, |
116 | | - {{ elementary.null_string() }} as dimension_value, |
117 | | - {% endif %} |
118 | | - {{ elementary.edr_cast_as_float(metric_type) }} as metric_value, |
119 | | - {{ elementary.edr_cast_as_string(elementary.edr_quote(metric_name)) }} as metric_name, |
120 | | - {{ elementary.edr_cast_as_string(elementary.edr_quote(metric_type)) }} as metric_type |
121 | | - from column_metrics where {{ metric_type }} is not null |
122 | | - {% if not loop.last %} union all {% endif %} |
123 | | - {%- endfor %} |
124 | | - {%- else %} |
125 | | - {{ elementary.empty_table([('edr_column_name','string'),('bucket_start','timestamp'),('bucket_end','timestamp'),('bucket_duration_hours','int'),('dimension','string'),('dimension_value','string'),('metric_name','string'),('metric_type','string'),('metric_value','float')]) }} |
126 | | - {%- endif %} |
127 | | - |
| 97 | + {{ elementary.get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) }} |
128 | 98 | ), |
129 | 99 |
|
130 | 100 | metrics_final as ( |
131 | | - |
132 | 101 | select |
133 | 102 | {{ elementary.edr_cast_as_string(full_table_name_str) }} as full_table_name, |
134 | 103 | edr_column_name as column_name, |
|
143 | 112 | dimension_value, |
144 | 113 | {{elementary.dict_to_quoted_json(metric_properties) }} as metric_properties |
145 | 114 | from column_metrics_unpivot |
146 | | - |
147 | 115 | ) |
148 | 116 |
|
149 | 117 | select |
|
174 | 142 |
|
175 | 143 | {% endmacro %} |
176 | 144 |
|
| 145 | +{% macro get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) %} |
| 146 | + {{ return(adapter.dispatch('get_column_metrics_unpivot_query', 'elementary')(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions)) }} |
| 147 | +{% endmacro %} |
| 148 | + |
| 149 | +{% macro default__get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) %} |
| 150 | + {%- if column_metrics %} |
| 151 | + {% for metric_name, metric_type in metric_name_to_type.items() %} |
| 152 | + select |
| 153 | + {{ elementary.const_as_string(column_obj.name) }} as edr_column_name, |
| 154 | + bucket_start, |
| 155 | + bucket_end, |
| 156 | + {% if timestamp_column %} |
| 157 | + {{ elementary.timediff("hour", "bucket_start", "bucket_end") }} as bucket_duration_hours, |
| 158 | + {% else %} |
| 159 | + {{ elementary.null_int() }} as bucket_duration_hours, |
| 160 | + {% endif %} |
| 161 | + {% if dimensions | length > 0 %} |
| 162 | + {{ elementary.const_as_string(elementary.join_list(dimensions, separator='; ')) }} as dimension, |
| 163 | + {{ elementary.list_concat_with_separator(prefixed_dimensions, separator='; ') }} as dimension_value, |
| 164 | + {% else %} |
| 165 | + {{ elementary.null_string() }} as dimension, |
| 166 | + {{ elementary.null_string() }} as dimension_value, |
| 167 | + {% endif %} |
| 168 | + {{ elementary.edr_cast_as_float(metric_type) }} as metric_value, |
| 169 | + {{ elementary.edr_cast_as_string(elementary.edr_quote(metric_name)) }} as metric_name, |
| 170 | + {{ elementary.edr_cast_as_string(elementary.edr_quote(metric_type)) }} as metric_type |
| 171 | + from column_metrics where {{ metric_type }} is not null |
| 172 | + {% if not loop.last %} union all {% endif %} |
| 173 | + {%- endfor %} |
| 174 | + {%- else %} |
| 175 | + {{ elementary.empty_table([('edr_column_name','string'),('bucket_start','timestamp'),('bucket_end','timestamp'),('bucket_duration_hours','int'),('dimension','string'),('dimension_value','string'),('metric_name','string'),('metric_type','string'),('metric_value','float')]) }} |
| 176 | + {%- endif %} |
| 177 | +{% endmacro %} |
| 178 | + |
| 179 | +{% macro clickhouse__get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) %} |
| 180 | + {%- if column_metrics %} |
| 181 | + {% for metric_name, metric_type in metric_name_to_type.items() %} |
| 182 | + select |
| 183 | + {{ elementary.const_as_string(column_obj.name) }} as edr_column_name, |
| 184 | + bucket_start, |
| 185 | + bucket_end, |
| 186 | + {% if timestamp_column %} |
| 187 | + {{ elementary.timediff("hour", "bucket_start", "bucket_end") }} as bucket_duration_hours, |
| 188 | + {% else %} |
| 189 | + {{ elementary.null_int() }} as bucket_duration_hours, |
| 190 | + {% endif %} |
| 191 | + {% if dimensions | length > 0 %} |
| 192 | + {{ elementary.const_as_string(elementary.join_list(dimensions, separator='; ')) }} as dimension, |
| 193 | + {{ elementary.list_concat_with_separator(prefixed_dimensions, separator='; ') }} as dimension_value, |
| 194 | + {% else %} |
| 195 | + CAST(NULL AS Nullable(String)) as dimension, |
| 196 | + CAST(NULL AS Nullable(String)) as dimension_value, |
| 197 | + {% endif %} |
| 198 | + {{ elementary.edr_cast_as_float(metric_type) }} as metric_value, |
| 199 | + cast({{ elementary.edr_quote(metric_name) }} as Nullable({{ elementary.edr_type_string() }})) as metric_name, |
| 200 | + cast({{ elementary.edr_quote(metric_type) }} as Nullable({{ elementary.edr_type_string() }})) as metric_type |
| 201 | + from column_metrics where {{ metric_type }} is not null |
| 202 | + {% if not loop.last %} union all {% endif %} |
| 203 | + {%- endfor %} |
| 204 | + {%- else %} |
| 205 | + {{ elementary.empty_table([('edr_column_name','string'),('bucket_start','timestamp'),('bucket_end','timestamp'),('bucket_duration_hours','int'),('dimension','string'),('dimension_value','string'),('metric_name','string'),('metric_type','string'),('metric_value','float')]) }} |
| 206 | + {%- endif %} |
| 207 | +{% endmacro %} |
| 208 | + |
177 | 209 | {% macro select_dimensions_columns(dimension_columns, as_prefix="") %} |
178 | 210 | {% set select_statements %} |
179 | 211 | {%- for column in dimension_columns -%} |
|
0 commit comments