-
Notifications
You must be signed in to change notification settings - Fork 2
/
profiling_results.py
209 lines (184 loc) · 7 KB
/
profiling_results.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import typing
import streamlit as st
import testgen.ui.queries.profiling_queries as profiling_queries
import testgen.ui.services.form_service as fm
from testgen.common import date_service
from testgen.ui.components import widgets as testgen
from testgen.ui.navigation.page import Page
from testgen.ui.services import project_service
from testgen.ui.session import session
from testgen.ui.views.profiling_details import show_profiling_detail
FORM_DATA_WIDTH = 400
class ProfilingResultsPage(Page):
path = "profiling-runs:results"
can_activate: typing.ClassVar = [
lambda: session.authentication_status,
lambda: "run_id" in session.current_page_args or "profiling-runs",
]
def render(self, run_id: str, table_name: str | None = None, column_name: str | None = None, **_kwargs) -> None:
run_parentage = profiling_queries.lookup_db_parentage_from_run(
run_id
)
if not run_parentage:
self.router.navigate_with_warning(
f"Profiling run with ID '{run_id}' does not exist. Redirecting to list of Profiling Runs ...",
"profiling-runs",
)
return
run_date, table_group_id, table_group_name, project_code = run_parentage
run_date = date_service.get_timezoned_timestamp(st.session_state, run_date)
project_service.set_current_project(project_code)
testgen.page_header(
"Data Profiling Results",
"view-data-profiling-results",
breadcrumbs=[
{ "label": "Profiling Runs", "path": "profiling-runs", "params": { "project_code": project_code } },
{ "label": f"{table_group_name} | {run_date}" },
],
)
table_filter_column, column_filter_column, sort_column, export_button_column = st.columns(
[.3, .3, .08, .32], vertical_alignment="bottom"
)
with table_filter_column:
# Table Name filter
df = profiling_queries.run_table_lookup_query(table_group_id)
table_name = testgen.select(
options=df,
value_column="table_name",
default_value=table_name,
bind_to_query="table_name",
label="Table Name",
)
with column_filter_column:
# Column Name filter
df = profiling_queries.run_column_lookup_query(table_group_id, table_name)
column_name = testgen.select(
options=df,
value_column="column_name",
default_value=column_name,
bind_to_query="column_name",
label="Column Name",
disabled=not table_name,
)
with sort_column:
sortable_columns = (
("Schema Name", "p.schema_name"),
("Table Name", "p.table_name"),
("Column Name", "p.column_name"),
("Column Type", "p.column_type"),
("Semantic Data Type", "semantic_data_type"),
("Anomalies", "anomalies"),
)
default_sorting = [(sortable_columns[i][1], "ASC") for i in (0, 1, 2)]
sorting_columns = testgen.sorting_selector(sortable_columns, default_sorting)
# Use SQL wildcard to match all values
if not table_name:
table_name = "%%"
if not column_name:
column_name = "%%"
# Display main results grid
df = profiling_queries.get_profiling_detail(run_id, table_name, column_name, sorting_columns)
show_columns = [
"schema_name",
"table_name",
"column_name",
"column_type",
"semantic_data_type",
"anomalies",
]
# Show CREATE script button
if len(df) > 0 and table_name != "%%":
with st.expander("📜 **Table CREATE script with suggested datatypes**"):
st.code(generate_create_script(df), "sql")
selected_row = fm.render_grid_select(
df,
show_columns,
bind_to_query_name="selected",
bind_to_query_prop="id",
)
with export_button_column:
testgen.flex_row_end()
render_export_button(df)
# Display profiling for selected row
if not selected_row:
st.markdown(":orange[Select a row to see profiling details.]")
else:
show_profiling_detail(selected_row[0], FORM_DATA_WIDTH)
def render_export_button(df):
export_columns = [
"schema_name",
"table_name",
"column_name",
"position",
"column_type",
"general_type",
"semantic_table_type",
"semantic_data_type",
"datatype_suggestion",
"anomalies",
"record_ct",
"value_ct",
"distinct_value_ct",
"top_freq_values",
"null_value_ct",
"min_length",
"max_length",
"avg_length",
"distinct_std_value_ct",
"numeric_ct",
"date_ct",
"dummy_value_ct",
"zero_length_ct",
"lead_space_ct",
"quoted_value_ct",
"includes_digit_ct",
"embedded_space_ct",
"avg_embedded_spaces",
"min_text",
"max_text",
"std_pattern_match",
"distinct_pattern_ct",
"top_patterns",
"distinct_value_hash",
"min_value",
"min_value_over_0",
"max_value",
"avg_value",
"stdev_value",
"percentile_25",
"percentile_50",
"percentile_75",
"zero_value_ct",
"fractional_sum",
"min_date",
"max_date",
"before_1yr_date_ct",
"before_5yr_date_ct",
"within_1yr_date_ct",
"within_1mo_date_ct",
"future_date_ct",
"date_days_present",
"date_weeks_present",
"date_months_present",
"boolean_true_ct",
]
wrap_columns = ["top_freq_values", "top_patterns"]
caption = "{TIMESTAMP}"
fm.render_excel_export(df, export_columns, "Profiling Results", caption, wrap_columns)
def generate_create_script(df):
ddf = df[["schema_name", "table_name", "column_name", "column_type", "datatype_suggestion"]].copy()
ddf.fillna("", inplace=True)
ddf["comment"] = ddf.apply(
lambda row: "-- WAS " + row["column_type"] if row["column_type"] != row["datatype_suggestion"] else "", axis=1
)
max_len_name = ddf.apply(lambda row: len(row["column_name"]), axis=1).max() + 3
max_len_type = ddf.apply(lambda row: len(row["datatype_suggestion"]), axis=1).max() + 3
str_header = f"CREATE TABLE {df.at[0, 'schema_name']}.{ddf.at[0, 'table_name']} ( "
col_defs = ddf.apply(
lambda row: f" {row['column_name']:<{max_len_name}} {row['datatype_suggestion']:<{max_len_type}}, {row['comment']}",
axis=1,
).tolist()
str_footer = ");"
# Drop final comma in column definitions
col_defs[-1] = col_defs[-1].replace(", --", " --")
return "\n".join([str_header, *list(col_defs), str_footer])