-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatafold_api_demo.py
More file actions
171 lines (147 loc) · 6.68 KB
/
Copy pathdatafold_api_demo.py
File metadata and controls
171 lines (147 loc) · 6.68 KB
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
"""
Demo script to run a Datafold data diff in Snowflake with simple API calls
"""
import os
import time
from pydantic import BaseModel, field_validator
from typing import Any, List
import requests
from tabulate import tabulate
from termcolor import colored
from halo import Halo
# TODO: replace with your own Datafold API key and host URL
host = os.getenv("HOST_URL", "app.datafold.com")
datafold_api_key = os.getenv(
"DATAFOLD_API_KEY"
) # replace with your own Datafold API key
# TODO: replace with your own data diff configs
data_source1_id = 4932 # replace with your own data source id
data_source2_id = 4932 # replace with your own data source id
table1 = ["DEMO", "CORE", "DIM_ORGS"]
table2 = ["DEMO", "PR", "DIM_ORGS"]
pk_columns = ["ORG_ID"] # replace with your own primary key columns
class DataDiffConfigs(BaseModel):
data_source1_id: int
data_source2_id: int
table1: List[str]
table2: List[str]
pk_columns: List[str]
@field_validator("table1", "table2")
def validate_table(cls, value):
if len(value) != 3:
raise ValueError(
'Exactly 3 objects are required for table1 and table2 inputs: ["DATABASE", "SCHEMA", "TABLE/VIEW NAME"]'
)
return value
data_diff_configs = DataDiffConfigs(
data_source1_id=data_source1_id,
data_source2_id=data_source2_id,
table1=table1,
table2=table2,
pk_columns=pk_columns
)
class DataDiff:
def __init__(
self, host: str, datafold_api_key: str
):
self.session = requests.Session()
self.host = host
self.session.headers["Authorization"] = f"Key {datafold_api_key}"
def create_diff(self, data_diff_configs: DataDiffConfigs) -> int:
resp = self.session.post(
f"https://{self.host}/api/v1/datadiffs", json=data_diff_configs.model_dump()
)
resp.raise_for_status()
data = resp.json()
url = colored(f"https://{self.host}/datadiffs/{data['id']}", "blue")
print(f"Started Datafold Data Diff: {url}")
return data["id"]
def get_diff_summary(self, id: int) -> dict[str, Any]:
resp = self.session.get(
f"https://{self.host}/api/v1/datadiffs/{id}/summary_results"
)
resp.raise_for_status()
data = resp.json()
return data
def wait_for_results(self, id: int) -> dict[str, Any]:
spinner = Halo(text="Running", spinner="dots", color="green")
start_time = time.time()
try:
spinner.start()
while True:
summary = self.get_diff_summary(id)
if summary["status"] in ("success", "error"):
elapsed_time = format(time.time() - start_time, ".2f")
spinner.succeed(
f"Completed with status: {summary['status']}. Total run time: {elapsed_time} seconds"
)
return summary
elapsed_seconds = int(time.time() - start_time)
spinner.text = f"Running... {elapsed_seconds} seconds"
time.sleep(1)
finally:
spinner.stop()
def print_diff_summary(self, results: dict[str, Any], data_diff_configs: DataDiffConfigs):
# For "pks"
headers_pks = ["Stats", data_diff_configs.table1, data_diff_configs.table2]
rows_pks = []
for key, value in results["pks"].items():
rows_pks.append([key] + value)
print(f"\nData Diff For Primary Keys: {data_diff_configs.pk_columns}")
print(tabulate(rows_pks, headers=headers_pks, tablefmt="grid"))
# For "values"
headers_values = ["Stats", "Values"]
rows_values = []
for key, value in results["values"].items():
if key != "columns_diff_stats" and not isinstance(value, list):
rows_values.append([key, value])
print("\nData Diff Values Summary:")
print(tabulate(rows_values, headers=headers_values, tablefmt="grid"))
# For "columns_diff_stats"
headers_diff_stats = ["Column Name", "Match"]
rows_diff_stats = []
for diff_stat in results["values"]["columns_diff_stats"]:
rows_diff_stats.append([diff_stat["column_name"], diff_stat["match"]])
print("\nData Diff Column Difference Statistics:")
print(tabulate(rows_diff_stats, headers=headers_diff_stats, tablefmt="grid"))
# Dependencies
print("\nDependencies:")
for key, dependency_dict in results['dependencies'].items():
if dependency_dict: # Check if the dictionary is not empty
for subkey, item_list in dependency_dict.items():
if item_list: # Check this list is not empty
# get keys of the first dictionary in the list
all_headers = set().union(*(item.keys() for item in item_list))
data = []
for item in item_list:
row = []
for header in all_headers:
val = item.get(header)
if isinstance(val, list) and len(val) > 0 and isinstance(val[0], dict):
# assuming each dictionary has a consistent structure
val = ', '.join([f"{v['name']} ({v['uid']})" for v in val])
row.append(val)
data.append(row)
print(f"\n{subkey.capitalize()} Dependencies:") # print a subkey name
print(tabulate(data, headers=all_headers, tablefmt='grid')) # print a table
else:
print(f"\n{subkey.capitalize()} Dependencies are not available.") # print the message for empty lists
else:
print(f"\n{key.capitalize()} Dependencies are not available.")
# Schema
headers_schema = ["Stats", data_diff_configs.table1, data_diff_configs.table2]
rows_schema = []
for key, value in results["schema"].items():
if isinstance(value, list) and len(value) == 2:
rows_schema.append([key, value[0], value[1]])
else:
rows_schema.append([key, value, ''])
print("\nSchema Diff Summary:")
print(tabulate(rows_schema, headers=headers_schema, tablefmt="grid"))
def run_data_diff(self, data_diff_configs: DataDiffConfigs):
diff_id = self.create_diff(data_diff_configs)
results = self.wait_for_results(diff_id)
self.print_diff_summary(results, data_diff_configs)
if __name__ == "__main__":
datadiff = DataDiff(host, datafold_api_key)
datadiff.run_data_diff(data_diff_configs)