-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
042c129
commit c6670a8
Showing
7 changed files
with
363 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Jinja2 >= 3.1.4, <4 |
48 changes: 48 additions & 0 deletions
48
src/dashboard/get_chart_data/templates/filter_numeric.sql.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{%- import 'syntax.sql.jinja' as syntax -%} | ||
|
||
{%- set ns =namespace(matches=[]) -%} | ||
{%- set filters=['eq', 'ne', 'gt', 'gte', 'lt', 'lte'] -%} | ||
|
||
{%- macro render_filter( data, filter_type, bound) -%} | ||
{%- if filter_type == 'eq'-%} | ||
{{ data }} = {{ bound }} | ||
{%- elif filter_type == 'ne'-%} | ||
{{ data }} != {{ bound }} | ||
{%- elif filter_type == 'gt'-%} | ||
{{ data }} > {{ bound }} | ||
{%- elif filter_type == 'gte'-%} | ||
{{ data }} >= {{ bound }} | ||
{%- elif filter_type == 'lt'-%} | ||
{{ data }} < {{ bound }} | ||
{%- elif filter_type == 'lte'-%} | ||
{{ data }} <= {{ bound }} | ||
{%- else -%} | ||
not found {{ filter_type }} | ||
{%- endif -%} | ||
{%- endmacro -%} | ||
|
||
{%- macro is_filter_in_list(filter_configs) -%} | ||
{%- for f in filter_configs -%} | ||
{%- if f.filter_type in filters -%} | ||
{%- set ns.matches = ns.matches + [f] -%} | ||
{%- endif -%} | ||
{% endfor -%} | ||
{%- endmacro -%} | ||
|
||
{%- macro get_filters(filter_configs) -%} | ||
{{ is_filter_in_list(filter_configs) }} | ||
{%- for config in ns.matches %} | ||
{{ syntax.and_delineate(loop) }}{{ render_filter(config['data'],config['filter_type'],config['bound']) }} | ||
{%- endfor -%} | ||
{%- set ns.matches = [] -%} | ||
{%- endmacro -%} | ||
|
||
{%- macro has_filter(filter_configs) %} | ||
{{- is_filter_in_list(filter_configs) -}} | ||
{%- if ns.matches|count >0 -%} | ||
True | ||
{%- else -%} | ||
False | ||
{%- endif -%} | ||
{%- set ns.matches = [] -%} | ||
{%- endmacro -%} |
65 changes: 65 additions & 0 deletions
65
src/dashboard/get_chart_data/templates/get_chart_data.sql.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{%- import 'syntax.sql.jinja' as syntax -%} | ||
{%- import 'filter_numeric.sql.jinja' as numeric -%} | ||
{%-macro select_data(cumulus__none) -%} | ||
SELECT | ||
{%- if stratifier_column %} | ||
"{{ stratifier_column }}", | ||
{%- endif %} | ||
"{{ data_column }}" | ||
{%- if count_columns %},{%- endif %} | ||
{%- for column in count_columns %} | ||
SUM("{{ column }}") AS {{ column }}{{ syntax.comma_delineate(loop) }} | ||
{%- endfor%} | ||
FROM "{{ schema }}"."{{ data_package_id }}" | ||
WHERE | ||
{%- if coalesce_columns %} COALESCE ( | ||
{%- for column in coalesce_columns %} | ||
cast("{{ column }}" AS VARCHAR){{ syntax.comma_delineate(loop) }} | ||
{%- endfor%} | ||
) IS NOT NULL | ||
AND{%- endif%} "{{ data_column }}" IS NOT NULL | ||
{%- if not cumulus__none %} | ||
AND {{ data_column }} !='cumulus__none' | ||
{%- if filter_str %} | ||
AND {{ filter_str }} | ||
{%- endif %} | ||
{%- else %} | ||
AND {{ data_column }} ='cumulus__none' | ||
{%- endif %} | ||
{%- if stratifier_column %} | ||
AND "{{ stratifier_column }}" IS NOT NULL | ||
{%- endif %} | ||
GROUP BY | ||
{%- if stratifier_column %} | ||
"{{ stratifier_column }}", "{{ data_column }}" | ||
{%- else %} | ||
"{{ data_column }}" | ||
{%- endif %} | ||
ORDER BY | ||
{%- if stratifier_column %} | ||
"{{ stratifier_column }}", "{{ data_column }}" | ||
{%- else %} | ||
"{{ data_column }}" | ||
{%- endif %} | ||
{%- endmacro -%} | ||
WITH non_null AS ( | ||
{{ select_data(False) }} | ||
), | ||
has_null AS ( | ||
{{ select_data(True) }} | ||
) | ||
SELECT * FROM non_null | ||
{%- if numeric.has_filter(filter_configs) == 'True' %} | ||
WHERE {{ numeric.get_filters(filter_configs) }} | ||
{%- endif %} | ||
UNION | ||
SELECT * from has_null | ||
{%- if numeric.has_filter(filter_configs) == 'True' %} | ||
WHERE {{ numeric.get_filters(filter_configs) }} | ||
{%- endif %} | ||
ORDER BY | ||
{%- if stratifier_column %} | ||
"{{ stratifier_column }}", "{{ data_column }}" | ||
{%- else %} | ||
"{{ data_column }}" | ||
{%- endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{%- macro comma_delineate(loop) -%} | ||
{%- if not loop.last -%} | ||
, | ||
{%- endif -%} | ||
{%- endmacro -%} | ||
|
||
|
||
{# Note that the following two delineations are meant to be at the front of the string | ||
in a loop - this is to enable formatting in a WHERE statement like this: | ||
--- | ||
WHERE | ||
b.bar = a.foo | ||
AND b.baz != a.foo | ||
--- | ||
This is slightly easier to work with when debugging queries (and also | ||
conforms better to the mozilla style guide) | ||
#} | ||
{%- macro and_delineate(loop) -%} | ||
{%- if not loop.first -%}AND {% endif -%} | ||
{%- endmacro -%} | ||
|
||
{%- macro or_delineate(loop) -%} | ||
{%- if not loop.first -%}OR {% endif -%} | ||
{%- endmacro -%} |
Oops, something went wrong.