-
Notifications
You must be signed in to change notification settings - Fork 33
/
print_profile_docs.sql
65 lines (49 loc) · 2.43 KB
/
print_profile_docs.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
{% macro print_profile_docs(relation=none, relation_name=none, docs_name=none, schema=none, database=none, exclude_measures=[], include_columns=[], exclude_columns=[], max_rows=none, max_columns=13, max_column_width=30, max_precision=none, where_clause=none) %}
{% if execute %}
{%- set results = dbt_profiler.get_profile_table(relation=relation, relation_name=relation_name, schema=schema, database=database, exclude_measures=exclude_measures, include_columns=include_columns, exclude_columns=exclude_columns, where_clause=where_clause) -%}
{% if docs_name is none %}
{% set docs_name = 'dbt_profiler__' + relation_name %}
{% endif %}
{%- set startdocs = '{% docs ' ~ docs_name ~ ' %}' -%}
{%- set enddocs = '{% enddocs %}' -%}
{# Check if macro is called in dbt Cloud? #}
{%- if flags.WHICH == 'rpc' -%}
{%- set is_dbt_cloud = true -%}
{%- else -%}
{%- set is_dbt_cloud = false -%}
{%- endif -%}
{% if not is_dbt_cloud %}
{{ print(startdocs) }}
{% do results.print_table(max_rows=max_rows, max_columns=max_columns, max_column_width=max_column_width, max_precision=max_precision) %}
{{ print(enddocs) }}
{% else %}
{%- set profile_docs=[] -%}
{% do profile_docs.append(startdocs) -%}
{% do profile_docs.append('') %}
{# Get header from column names #}
{%- set headers = results.column_names -%}
{%- set header = [] -%}
{%- set horizontal_line = [] -%}
{% for i in range(0,headers|length) %}
{% do header.append(headers[i]) %}
{% do horizontal_line.append('---') %}
{% endfor %}
{% do profile_docs.append('| ' ~ header|join(' | ') ~ ' |') %}
{% do profile_docs.append('| ' ~ horizontal_line|join(' | ') ~ ' |') %}
{# Get row values #}
{% for row in results.rows %}
{%- set list_row = [''] -%}
{% for val in row.values() %}
{% do list_row.append(val) %}
{% endfor %}
{% do profile_docs.append(list_row|join(' | ') ~ ' |') %}
{% endfor %}
{% do profile_docs.append('') %}
{% do profile_docs.append(enddocs) %}
{# Join profile docs #}
{%- set joined = profile_docs | join ('\n') -%}
{{ log(joined, info=True) }}
{% do return(joined) %}
{% endif %}
{% endif %}
{% endmacro %}