Skip to content

Commit da9f548

Browse files
author
ci bot
committed
Merge branch 'telemetry-fix' into 'enterprise'
fix(analytics): simplify source datapoint - address review feedback See merge request dkinternal/testgen/dataops-testgen!210
2 parents a9f7dec + cce9a8a commit da9f548

14 files changed

Lines changed: 59 additions & 65 deletions

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export TG_DECRYPT_SALT=<encryption_salt>
135135
export TESTGEN_USERNAME=<username>
136136
export TESTGEN_PASSWORD=<password>
137137

138+
# Set an arbitrary base64-encoded string to be used for signing authentication tokens
139+
export TG_JWT_HASHING_KEY=<base64_key>
140+
138141
# Set an accessible path for storing application logs
139142
export TESTGEN_LOG_FILE_PATH=<path_for_logs>
140143
```

docs/local_development.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Create a `local.env` file with the following environment variables, replacing th
6464
export TESTGEN_DEBUG=yes
6565
export TESTGEN_LOG_TO_FILE=no
6666
export TG_ANALYTICS=no
67+
export TG_JWT_HASHING_KEY=<base64_key>
6768
export TESTGEN_USERNAME=<username>
6869
export TESTGEN_PASSWORD=<password>
6970
export TG_DECRYPT_SALT=<decrypt_salt>

testgen/__main__.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,12 @@ def cli(ctx: Context, verbose: bool):
113113
help="The identifier for the table group used during a profile run. Use a table_group_id shown in list-table-groups.",
114114
default=None,
115115
)
116-
@click.option(
117-
"-s",
118-
"--source",
119-
required=False,
120-
type=click.STRING,
121-
default="cli",
122-
)
123-
def run_profile(configuration: Configuration, table_group_id: str, source: str):
116+
def run_profile(configuration: Configuration, table_group_id: str):
124117
click.echo(f"run-profile with table_group_id: {table_group_id}")
125118
spinner = None
126119
if not configuration.verbose:
127120
spinner = MoonSpinner("Processing ... ")
128-
message = run_profiling_queries(table_group_id, spinner=spinner, source=source)
121+
message = run_profiling_queries(table_group_id, spinner=spinner)
129122
click.echo("\n" + message)
130123

131124

@@ -152,17 +145,10 @@ def run_profile(configuration: Configuration, table_group_id: str, source: str):
152145
required=False,
153146
default=None,
154147
)
155-
@click.option(
156-
"-s",
157-
"--source",
158-
required=False,
159-
type=click.STRING,
160-
default="cli",
161-
)
162148
@pass_configuration
163-
def run_test_generation(configuration: Configuration, table_group_id: str, test_suite_key: str, generation_set: str, source: str):
149+
def run_test_generation(configuration: Configuration, table_group_id: str, test_suite_key: str, generation_set: str):
164150
LOG.info("CurrentStep: Generate Tests - Main Procedure")
165-
message = run_test_gen_queries(table_group_id, test_suite_key, generation_set, source)
151+
message = run_test_gen_queries(table_group_id, test_suite_key, generation_set)
166152
LOG.info("Current Step: Generate Tests - Main Procedure Complete")
167153
click.echo("\n" + message)
168154

@@ -184,20 +170,13 @@ def run_test_generation(configuration: Configuration, table_group_id: str, test_
184170
required=False,
185171
default=settings.DEFAULT_TEST_SUITE_KEY,
186172
)
187-
@click.option(
188-
"-s",
189-
"--source",
190-
required=False,
191-
type=click.STRING,
192-
default="cli",
193-
)
194173
@pass_configuration
195-
def run_tests(configuration: Configuration, project_key: str, test_suite_key: str, source: str):
174+
def run_tests(configuration: Configuration, project_key: str, test_suite_key: str):
196175
click.echo(f"run-tests for suite: {test_suite_key}")
197176
spinner = None
198177
if not configuration.verbose:
199178
spinner = MoonSpinner("Processing ... ")
200-
message = run_execution_steps(project_key, test_suite_key, spinner=spinner, source=source)
179+
message = run_execution_steps(project_key, test_suite_key, spinner=spinner)
201180
click.echo("\n" + message)
202181

203182

@@ -660,6 +639,7 @@ def run_ui():
660639
"--",
661640
f"{'--debug' if settings.IS_DEBUG else ''}",
662641
],
642+
env={**os.environ, "TG_JOB_SOURCE": "UI"}
663643
)
664644
except Exception:
665645
LOG.exception(f"Testgen UI exited with status code {status_code}")

testgen/commands/run_execute_cat_tests.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from datetime import UTC, datetime
33

4+
from testgen import settings
45
from testgen.commands.queries.execute_cat_tests_query import CCATExecutionSQL
56
from testgen.commands.run_refresh_score_cards_results import run_refresh_score_cards_results
67
from testgen.common import (
@@ -62,20 +63,13 @@ def ParseCATResults(clsCATExecute):
6263
RunActionQueryList("DKTG", [strQuery])
6364

6465

65-
def FinalizeTestRun(clsCATExecute: CCATExecutionSQL, source: str):
66+
def FinalizeTestRun(clsCATExecute: CCATExecutionSQL):
6667
_, row_counts = RunActionQueryList(("DKTG"), [
6768
clsCATExecute.FinalizeTestResultsSQL(),
6869
clsCATExecute.PushTestRunStatusUpdateSQL(),
6970
clsCATExecute.FinalizeTestSuiteUpdateSQL(),
7071
])
71-
72-
MixpanelService().send_event(
73-
"run-tests",
74-
source=source,
75-
sql_flavor=clsCATExecute.flavor,
76-
test_count=row_counts[0],
77-
duration=(datetime.now(UTC) - date_service.parse_now(clsCATExecute.run_date)).total_seconds(),
78-
)
72+
end_time = datetime.now(UTC)
7973

8074
RunActionQueryList(("DKTG"), [
8175
clsCATExecute.CalcPrevalenceTestResultsSQL(),
@@ -88,9 +82,18 @@ def FinalizeTestRun(clsCATExecute: CCATExecutionSQL, source: str):
8882
refresh_date=date_service.parse_now(clsCATExecute.run_date),
8983
)
9084

85+
MixpanelService().send_event(
86+
"run-tests",
87+
source=settings.ANALYTICS_JOB_SOURCE,
88+
sql_flavor=clsCATExecute.flavor,
89+
test_count=row_counts[0],
90+
run_duration=(end_time - date_service.parse_now(clsCATExecute.run_date)).total_seconds(),
91+
scoring_duration=(datetime.now(UTC) - end_time).total_seconds(),
92+
)
93+
9194

9295
def run_cat_test_queries(
93-
dctParms, strTestRunID, strTestTime, strProjectCode, strTestSuite, error_msg, minutes_offset=0, spinner=None, source=None
96+
dctParms, strTestRunID, strTestTime, strProjectCode, strTestSuite, error_msg, minutes_offset=0, spinner=None
9497
):
9598
booErrors = False
9699

@@ -160,4 +163,4 @@ def run_cat_test_queries(
160163

161164
finally:
162165
LOG.info("Finalizing test run")
163-
FinalizeTestRun(clsCATExecute, source)
166+
FinalizeTestRun(clsCATExecute)

testgen/commands/run_execute_tests.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ def run_execution_steps_in_background(project_code, test_suite):
104104
background_thread = threading.Thread(
105105
target=run_execution_steps,
106106
args=(project_code, test_suite),
107-
kwargs={"source": "ui"},
108107
)
109108
background_thread.start()
110109
else:
@@ -118,7 +117,6 @@ def run_execution_steps(
118117
test_suite: str,
119118
minutes_offset: int=0,
120119
spinner: Spinner=None,
121-
source: str | None=None,
122120
) -> str:
123121
# Initialize required parms for all steps
124122
has_errors = False
@@ -169,7 +167,7 @@ def run_execution_steps(
169167

170168
LOG.info("CurrentStep: Execute Step - CAT Test Execution")
171169
if run_cat_test_queries(
172-
test_exec_params, test_run_id, test_time, project_code, test_suite, error_msg, minutes_offset, spinner, source
170+
test_exec_params, test_run_id, test_time, project_code, test_suite, error_msg, minutes_offset, spinner
173171
):
174172
has_errors = True
175173

testgen/commands/run_generate_tests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import logging
22

3+
from testgen import settings
34
from testgen.commands.queries.generate_tests_query import CDeriveTestsSQL
45
from testgen.common import AssignConnectParms, RetrieveDBResultsToDictList, RetrieveTestGenParms, RunActionQueryList
56
from testgen.common.mixpanel_service import MixpanelService
67

78
LOG = logging.getLogger("testgen")
89

910

10-
def run_test_gen_queries(strTableGroupsID, strTestSuite, strGenerationSet=None, source=None):
11+
def run_test_gen_queries(strTableGroupsID, strTestSuite, strGenerationSet=None):
1112
if strTableGroupsID is None:
1213
raise ValueError("Table Group ID was not specified")
1314

@@ -112,7 +113,7 @@ def run_test_gen_queries(strTableGroupsID, strTestSuite, strGenerationSet=None,
112113

113114
MixpanelService().send_event(
114115
"generate-tests",
115-
source=source,
116+
source=settings.ANALYTICS_JOB_SOURCE,
116117
sql_flavor=clsTests.sql_flavor,
117118
generation_set=clsTests.generation_set,
118119
)

testgen/commands/run_profiling_bridge.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ def run_profiling_in_background(table_group_id):
238238
empty_cache()
239239
background_thread = threading.Thread(
240240
target=run_profiling_queries,
241-
args=(table_group_id),
242-
kwargs={"source": "ui"},
241+
args=(table_group_id,),
243242
)
244243
background_thread.start()
245244
else:
@@ -248,7 +247,7 @@ def run_profiling_in_background(table_group_id):
248247
subprocess.Popen(script) # NOQA S603
249248

250249

251-
def run_profiling_queries(strTableGroupsID, spinner=None, source=None):
250+
def run_profiling_queries(strTableGroupsID, spinner=None):
252251
if strTableGroupsID is None:
253252
raise ValueError("Table Group ID was not specified")
254253

@@ -504,16 +503,7 @@ def run_profiling_queries(strTableGroupsID, spinner=None, source=None):
504503
RunActionQueryList("DKTG", [
505504
clsProfiling.GetProfileRunInfoRecordUpdateQuery(),
506505
])
507-
508-
MixpanelService().send_event(
509-
"run-profiling",
510-
source=source,
511-
sql_flavor=clsProfiling.flavor,
512-
sampling=clsProfiling.profile_use_sampling == "Y",
513-
table_count=table_count,
514-
column_count=column_count,
515-
duration=(datetime.now(UTC) - date_service.parse_now(clsProfiling.run_date)).total_seconds(),
516-
)
506+
end_time = datetime.now(UTC)
517507

518508
RunActionQueryList("DKTG", [
519509
clsProfiling.GetAnomalyScoringRollupRunQuery(),
@@ -525,6 +515,17 @@ def run_profiling_queries(strTableGroupsID, spinner=None, source=None):
525515
refresh_date=date_service.parse_now(clsProfiling.run_date),
526516
)
527517

518+
MixpanelService().send_event(
519+
"run-profiling",
520+
source=settings.ANALYTICS_JOB_SOURCE,
521+
sql_flavor=clsProfiling.flavor,
522+
sampling=clsProfiling.profile_use_sampling == "Y",
523+
table_count=table_count,
524+
column_count=column_count,
525+
run_duration=(end_time - date_service.parse_now(clsProfiling.run_date)).total_seconds(),
526+
scoring_duration=(datetime.now(UTC) - end_time).total_seconds(),
527+
)
528+
528529
return f"""
529530
Profiling completed {"with errors. Check log for details." if has_errors else "successfully."}
530531
Run ID: {profiling_run_id}

testgen/common/mixpanel_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def instance_id(self):
3535

3636
@cached_property
3737
def distinct_id(self):
38-
return self._hash_value(session.username or self.instance_id)
38+
return self._hash_value(session.username or "")
3939

4040
def _hash_value(self, value: bytes | str, digest_size: int = 8) -> str:
4141
if isinstance(value, str):

testgen/scheduler/cli_scheduler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23
import signal
34
import subprocess
45
import sys
@@ -84,7 +85,7 @@ def start_job(self, job: CliJob, triggering_time: datetime) -> None:
8485

8586
LOG.info("Executing '%s'", " ".join(exec_cmd))
8687

87-
proc = subprocess.Popen(exec_cmd, start_new_session=True) # noqa: S603
88+
proc = subprocess.Popen(exec_cmd, start_new_session=True, env={**os.environ, "TG_JOB_SOURCE": "SCHEDULER"}) # noqa: S603
8889
threading.Thread(target=self._proc_wrapper, args=(proc,)).start()
8990

9091
def _proc_wrapper(self, proc: subprocess.Popen):

testgen/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@
485485
Disables sending usage data when set to any value except "true" and "yes". Defaults to "yes"
486486
"""
487487

488+
ANALYTICS_JOB_SOURCE: str = os.getenv("TG_JOB_SOURCE", "CLI")
489+
"""
490+
Identifies the job trigger for analytics purposes.
491+
"""
492+
488493
JWT_HASHING_KEY_B64: str = os.getenv("TG_JWT_HASHING_KEY")
489494
"""
490495
Random key used to sign/verify the authentication token

0 commit comments

Comments
 (0)