From 391e8bc1165eda31f0a9493d451a6fa3b9716db3 Mon Sep 17 00:00:00 2001 From: Chenyu Li Date: Wed, 5 Oct 2022 16:02:30 -0700 Subject: [PATCH] initialize tracking in click --- core/dbt/cli/flags.py | 4 +++- core/dbt/cli/main.py | 4 +++- core/dbt/main.py | 2 +- core/dbt/tracking.py | 21 +++++++++------------ test/unit/test_tracking.py | 11 +++++++---- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/core/dbt/cli/flags.py b/core/dbt/cli/flags.py index 7ed32ec17fe..bd78b534fe5 100644 --- a/core/dbt/cli/flags.py +++ b/core/dbt/cli/flags.py @@ -44,7 +44,9 @@ def assign_params(ctx): # Support console DO NOT TRACK initiave if os.getenv("DO_NOT_TRACK", "").lower() in (1, "t", "true", "y", "yes"): - object.__setattr__(self, "ANONYMOUS_USAGE_STATS", False) + object.__setattr__(self, "SEND_ANONYMOUS_USAGE_STATS", False) + else: + object.__setattr__(self, "SEND_ANONYMOUS_USAGE_STATS", True) def __str__(self) -> str: return str(pf(self.__dict__)) diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py index 4953297ee25..470bd620a55 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -8,7 +8,7 @@ from dbt.cli.flags import Flags from dbt.events.functions import setup_event_logger from dbt.profiler import profiler -from dbt.tracking import initialize_from_flags_click +from dbt.tracking import initialize_from_flags as initialize_tracking import logging @@ -60,6 +60,8 @@ def cli(ctx, **kwargs): """ flags = Flags(invoked_subcommand=globals()[ctx.invoked_subcommand]) + initialize_tracking(flags) + # Logging # N.B. Legacy logger is not supported level_override = logging.WARN if ctx.invoked_subcommand in ("list", "ls") else None diff --git a/core/dbt/main.py b/core/dbt/main.py index be75760890e..1261389ff5c 100644 --- a/core/dbt/main.py +++ b/core/dbt/main.py @@ -178,7 +178,7 @@ def handle_and_check(args): # Set flags from args, user config, and env vars user_config = read_user_config(flags.PROFILES_DIR) # This is read again later flags.set_from_args(parsed, user_config) - dbt.tracking.initialize_from_flags() + dbt.tracking.initialize_from_flags(flags) # Set log_format from flags parsed.cls.set_log_format() diff --git a/core/dbt/tracking.py b/core/dbt/tracking.py index aafcfb09cb8..ef5d0ae57d8 100644 --- a/core/dbt/tracking.py +++ b/core/dbt/tracking.py @@ -16,7 +16,6 @@ TrackingInitializeFailure, ) from dbt import version as dbt_version -from dbt import flags from snowplow_tracker import Subject, Tracker, Emitter, logger as sp_logger from snowplow_tracker import SelfDescribingJson from datetime import datetime @@ -470,17 +469,15 @@ def process(self, record): ) -def initialize_from_flags(): - # Setting these used to be in UserConfig, but had to be moved here - if flags.SEND_ANONYMOUS_USAGE_STATS: - initialize_tracking(flags.PROFILES_DIR) - else: - do_not_track() - - -def initialize_from_flags_click(flags): +def initialize_from_flags(flags): # Setting these used to be in UserConfig, but had to be moved here + global active_user if flags.SEND_ANONYMOUS_USAGE_STATS: - initialize_tracking(flags.PROFILES_DIR) + active_user = User(flags.PROFILES_DIR) + try: + active_user.initialize() + except Exception: + fire_event(TrackingInitializeFailure()) + active_user = User(None) else: - do_not_track() + active_user = User(None) diff --git a/test/unit/test_tracking.py b/test/unit/test_tracking.py index a247734d53f..610b4f514fb 100644 --- a/test/unit/test_tracking.py +++ b/test/unit/test_tracking.py @@ -3,7 +3,7 @@ import shutil import tempfile import unittest - +from unittest.mock import MagicMock class TestTracking(unittest.TestCase): def setUp(self): @@ -16,7 +16,10 @@ def tearDown(self): def test_tracking_initial(self): assert dbt.tracking.active_user is None - dbt.tracking.initialize_tracking(self.tempdir) + flags = MagicMock() + flags.SEND_ANONYMOUS_USAGE_STATS = True + flags.PROFILES_DIR = self.tempdir + dbt.tracking.initialize_from_flags(flags) assert isinstance(dbt.tracking.active_user, dbt.tracking.User) invocation_id = dbt.tracking.active_user.invocation_id @@ -77,10 +80,10 @@ def test_initialize_from_flags(self): with self.subTest( send_aonymous_usage_stats=send_aonymous_usage_stats ): - dbt.tracking.flags.SEND_ANONYMOUS_USAGE_STATS = ( + dbt.flags.SEND_ANONYMOUS_USAGE_STATS = ( send_aonymous_usage_stats ) - dbt.tracking.initialize_from_flags() + dbt.tracking.initialize_from_flags(dbt.flags) assert dbt.tracking.active_user.do_not_track != send_aonymous_usage_stats