Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIN-729 persist device ids. #861

Merged
merged 6 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
persist device id so that we dont keep getting new random numbers (a …
…situation that can happen with uuid.getnode())
  • Loading branch information
lionsardesai committed Dec 16, 2022
commit 643e0d86f0c240b125ababfbbbf497fd4f40702e
19 changes: 14 additions & 5 deletions lineapy/utils/analytics/usage_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,20 @@ def _device_id() -> str:
# For now though, this works as our goal is to simply tie a device to a semi-stable id
# instead of a constantly changing random number. NOTE: getnode can and does return random
# numbers as well however the only case discussed when that happens is in android where it
# does not have permissions to access macids. This should not be a case we need to deal with
# so going with this until we see weirdness
# does not have permissions to access macids. This seems to happen in more cases as we have
# observed more device ids for the same user than we expected. To fix this, we'll persist
# an id to the .lineapy folder for future use. This might give us a bit more stability

return str(
uuid.uuid3(uuid.NAMESPACE_X500, str(uuid.getnode()))
) # hash the device mac id
device_id = ""
with open(options.safe_get("dev_id"), "w+") as f:
device_id = f.read()
if not device_id:
device_id = str(
uuid.uuid3(uuid.NAMESPACE_X500, str(uuid.getnode()))
) # hash the device mac id
f.write(device_id)

return device_id


@lru_cache(maxsize=1)
Expand Down Expand Up @@ -166,3 +174,4 @@ def tag(tag_name: str):
# lineapy.tag("demo_name") # change "demo_name" with actual demo name.

track(TagEvent(tag_name))
track(TagEvent(tag_name))
lionsardesai marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions lineapy/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

LINEAPY_FOLDER_NAME = ".lineapy"
LOG_FILE_NAME = "lineapy.log"
DEVICE_ID_FILE_NAME = ".devid"
CONFIG_FILE_NAME = "lineapy_config.json"
FILE_PICKLER_BASEDIR = "linea_pickles"
DB_FILE_NAME = "db.sqlite"
Expand Down Expand Up @@ -92,6 +93,7 @@ def __init__(
self.do_not_track = do_not_track
self.logging_level = logging_level
self.logging_file = logging_file
self.dev_id = None
self.mlflow_registry_uri = mlflow_registry_uri
self.mlflow_tracking_uri = mlflow_tracking_uri
self.default_ml_models_storage_backend = (
Expand Down Expand Up @@ -288,6 +290,19 @@ def safe_get_folder(name) -> FilePath:
)
return safe_get_folder("customized_annotation_folder")

elif name == "dev_id":
if self.dev_id is None:
dev_id_file = Path(safe_get_folder("home_dir")).joinpath(
DEVICE_ID_FILE_NAME
)
self.set(
"dev_id",
dev_id_file,
verbose=False,
)
else:
dev_id_file = logging_file = Path(str(self.dev_id))
return dev_id_file
else:
return self.get(name)

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/utils/test_usage_tracking.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest.mock import ANY, patch

import pytest

from lineapy.utils.analytics.event_schemas import SaveEvent
from lineapy.utils.analytics.usage_tracking import (
_amplitude_url,
Expand All @@ -10,6 +12,7 @@
_user_id,
track,
)
from lineapy.utils.config import options


@patch("lineapy.utils.analytics.usage_tracking.requests.post")
Expand Down Expand Up @@ -93,3 +96,13 @@ def test_send_amplitude_event_adds_userdata(mock_post):
headers=ANY,
timeout=ANY,
)


@pytest.mark.folder(options.safe_get("home_dir"))
def test_device_id_persisted(move_folder):
devid_path = options.safe_get("dev_id")
# should not need to remove the old file since move folder is creating a new one for us
# call the device id function.
new_dev_id = _device_id()
with open(devid_path, "r") as f:
assert f.read() == new_dev_id