Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit afff6f1

Browse files
committed
option to opt into email notifications
1 parent a06d698 commit afff6f1

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

data_diff/dbt.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
import pydantic
88
import rich
9-
from rich.prompt import Confirm
9+
from rich.prompt import Confirm, Prompt
1010

1111
from . import connect_to_table, diff_tables, Algorithm
1212
from .cloud import DatafoldAPI, TCloudApiDataDiff, TCloudApiOrgMeta, get_or_create_data_source
1313
from .dbt_parser import DbtParser, PROJECT_FILE
1414
from .tracking import (
15+
bool_ask_for_email,
16+
create_email_signup_event_json,
1517
set_entrypoint_name,
1618
set_dbt_user_id,
1719
set_dbt_version,
@@ -63,9 +65,7 @@ def dbt_diff(
6365
config_prod_schema = datadiff_variables.get("prod_schema")
6466
config_prod_custom_schema = datadiff_variables.get("prod_custom_schema")
6567
datasource_id = datadiff_variables.get("datasource_id")
66-
set_dbt_user_id(dbt_parser.dbt_user_id)
67-
set_dbt_version(dbt_parser.dbt_version)
68-
set_dbt_project_id(dbt_parser.dbt_project_id)
68+
_initialize_events(dbt_parser.dbt_user_id, dbt_parser.dbt_version, dbt_parser.dbt_project_id)
6969

7070
if datadiff_variables.get("custom_schemas") is not None:
7171
logger.warning(
@@ -389,3 +389,23 @@ def _cloud_diff(diff_vars: TDiffVars, datasource_id: int, api: DatafoldAPI, org_
389389

390390
def _diff_output_base(dev_path: str, prod_path: str) -> str:
391391
return f"\n[green]{prod_path} <> {dev_path}[/] \n"
392+
393+
394+
def _initialize_events(dbt_user_id: Optional[str], dbt_version: Optional[str], dbt_project_id: Optional[str]) -> None:
395+
set_dbt_user_id(dbt_user_id)
396+
set_dbt_version(dbt_version)
397+
set_dbt_project_id(dbt_project_id)
398+
_email_signup()
399+
400+
401+
def _email_signup() -> None:
402+
if bool_ask_for_email():
403+
email_input = Prompt.ask(
404+
"\nWould you like to be notified when a new data-diff version is available?\n\nEnter email or leave blank to opt out (we'll only ask once).\n",
405+
default="",
406+
show_default=False,
407+
)
408+
email = email_input.strip()
409+
if email:
410+
event_json = create_email_signup_event_json(email)
411+
run_as_daemon(send_event_json, event_json)

data_diff/tracking.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@ def _load_profile():
3737
return conf
3838

3939

40+
def bool_ask_for_email() -> bool:
41+
"""
42+
Checks the .datadiff.toml profile file for the asked_for_email key
43+
44+
Returns False immediately if --no-tracking
45+
46+
If found, return False (already asked for email)
47+
48+
If not found, add a key "asked_for_email", and return True (we should ask for email)
49+
50+
Returns:
51+
bool: decision on whether to prompt the user for their email
52+
"""
53+
if g_tracking_enabled:
54+
profile = _load_profile()
55+
56+
if "asked_for_email" not in profile:
57+
profile["asked_for_email"] = ""
58+
with open(DEFAULT_PROFILE, "w") as conf:
59+
toml.dump(profile, conf)
60+
return True
61+
return False
62+
63+
4064
g_tracking_enabled = True
4165
g_anonymous_id = None
4266

@@ -148,6 +172,22 @@ def create_end_event_json(
148172
}
149173

150174

175+
def create_email_signup_event_json(email: str) -> Dict[str, Any]:
176+
return {
177+
"event": "os_diff_email_opt_in",
178+
"properties": {
179+
"distinct_id": get_anonymous_id(),
180+
"token": TOKEN,
181+
"time": time(),
182+
"data_diff_version:": __version__,
183+
"entrypoint_name": entrypoint_name,
184+
"email": email,
185+
"dbt_user_id": dbt_user_id,
186+
"dbt_project_id": dbt_project_id,
187+
},
188+
}
189+
190+
151191
def send_event_json(event_json):
152192
if not g_tracking_enabled:
153193
raise RuntimeError("Won't send; tracking is disabled!")

0 commit comments

Comments
 (0)