Skip to content

Commit

Permalink
initialize tracking in click
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenyuLInx committed Oct 5, 2022
1 parent cd041b7 commit 391e8bc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
4 changes: 3 additions & 1 deletion core/dbt/cli/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
4 changes: 3 additions & 1 deletion core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
21 changes: 9 additions & 12 deletions core/dbt/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
11 changes: 7 additions & 4 deletions test/unit/test_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil
import tempfile
import unittest

from unittest.mock import MagicMock

class TestTracking(unittest.TestCase):
def setUp(self):
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 391e8bc

Please sign in to comment.