Skip to content

Commit 874ae16

Browse files
committed
assigned guid for cli user
1 parent bb50b4a commit 874ae16

3 files changed

Lines changed: 46 additions & 19 deletions

File tree

agentstack/telemetry.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@
2121
# cool of you to allow telemetry <3
2222
#
2323
# - braelyn
24+
import json
2425
import os
2526
import platform
2627
import socket
28+
import uuid
29+
from pathlib import Path
2730
from typing import Optional
2831
import psutil
2932
import requests
3033
from agentstack import conf
3134
from agentstack.auth import get_stored_token
32-
from agentstack.utils import get_telemetry_opt_out, get_framework, get_version
35+
from agentstack.utils import get_telemetry_opt_out, get_framework, get_version, get_base_dir
3336

34-
TELEMETRY_URL = 'https://api.agentstack.sh/telemetry'
37+
# TELEMETRY_URL = 'https://api.agentstack.sh/telemetry'
38+
TELEMETRY_URL = 'http://localhost:3000/telemetry'
39+
USER_GUID_FILE_PATH = get_base_dir() / ".cli-user-guid"
3540

3641

3742
def collect_machine_telemetry(command: str):
@@ -46,6 +51,7 @@ def collect_machine_telemetry(command: str):
4651
'cpu_count': psutil.cpu_count(logical=True),
4752
'memory': psutil.virtual_memory().total,
4853
'agentstack_version': get_version(),
54+
'cli_user_id': _get_cli_user_guid()
4955
}
5056

5157
if command != "init":
@@ -97,4 +103,25 @@ def update_telemetry(id: int, result: int, message: Optional[str] = None):
97103
try:
98104
requests.put(TELEMETRY_URL, json={"id": id, "result": result, "message": message})
99105
except Exception:
106+
pass
107+
108+
def _get_cli_user_guid() -> str:
109+
if Path(USER_GUID_FILE_PATH).exists():
110+
try:
111+
with open(USER_GUID_FILE_PATH, 'r') as f:
112+
return f.read()
113+
except (json.JSONDecodeError, PermissionError):
114+
return "unknown"
115+
116+
# make new cli user guid
117+
try:
118+
# Create directory if it doesn't exist
119+
USER_GUID_FILE_PATH.parent.mkdir(parents=True, exist_ok=True)
120+
121+
guid = str(uuid.uuid4())
122+
with open(USER_GUID_FILE_PATH, 'w') as f:
123+
f.write(guid)
124+
return guid
125+
except (OSError, PermissionError):
126+
# Silently fail in CI or when we can't write
100127
pass

agentstack/update.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,8 @@
55
from packaging.version import parse as parse_version, Version
66
import inquirer
77
from agentstack import log
8-
from agentstack.utils import term_color, get_version, get_framework
8+
from agentstack.utils import term_color, get_version, get_framework, get_base_dir
99
from agentstack import packaging
10-
from appdirs import user_data_dir
11-
12-
13-
def _get_base_dir():
14-
"""Try to get appropriate directory for storing update file"""
15-
try:
16-
base_dir = Path(user_data_dir("agentstack", "agency"))
17-
# Test if we can write to directory
18-
test_file = base_dir / '.test_write_permission'
19-
test_file.touch()
20-
test_file.unlink()
21-
except (RuntimeError, OSError, PermissionError):
22-
# In CI or when directory is not writable, use temp directory
23-
base_dir = Path(os.getenv('TEMP', '/tmp'))
24-
return base_dir
2510

2611

2712
AGENTSTACK_PACKAGE = 'agentstack'
@@ -35,7 +20,8 @@ def _get_base_dir():
3520
'TEAMCITY_VERSION',
3621
]
3722

38-
LAST_CHECK_FILE_PATH = _get_base_dir() / ".cli-last-update"
23+
LAST_CHECK_FILE_PATH = get_base_dir() / ".cli-last-update"
24+
USER_GUID_FILE_PATH = get_base_dir() / ".cli-user-guid"
3925
INSTALL_PATH = Path(sys.executable).parent.parent
4026
ENDPOINT_URL = "https://pypi.org/simple"
4127
CHECK_EVERY = 3600 # hour

agentstack/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import importlib.resources
99
from agentstack import conf
1010
from inquirer import errors as inquirer_errors
11+
from appdirs import user_data_dir
1112

1213

1314
def get_version(package: str = 'agentstack'):
@@ -118,3 +119,16 @@ def validator(_, answer):
118119
return True
119120

120121
return validator
122+
123+
def get_base_dir():
124+
"""Try to get appropriate directory for storing update file"""
125+
try:
126+
base_dir = Path(user_data_dir("agentstack", "agency"))
127+
# Test if we can write to directory
128+
test_file = base_dir / '.test_write_permission'
129+
test_file.touch()
130+
test_file.unlink()
131+
except (RuntimeError, OSError, PermissionError):
132+
# In CI or when directory is not writable, use temp directory
133+
base_dir = Path(os.getenv('TEMP', '/tmp'))
134+
return base_dir

0 commit comments

Comments
 (0)