cleaner logic for load/save analytics.json

This commit is contained in:
Paul Gauthier 2024-08-16 11:28:13 -07:00
parent 62d8190733
commit 0aad7b46f6
2 changed files with 46 additions and 45 deletions

View file

@ -25,16 +25,17 @@ class Analytics:
def __init__(self, enable=False, logfile=None, permanently_disable=False): def __init__(self, enable=False, logfile=None, permanently_disable=False):
self.logfile = logfile self.logfile = logfile
self.permanently_disable = permanently_disable
if not enable or permanently_disable: self.get_or_create_uuid()
if not enable or self.permanently_disable or permanently_disable:
self.mp = None self.mp = None
self.ph = None self.ph = None
if permanently_disable: if permanently_disable and not self.permanently_disable:
self.mark_as_permanently_disabled() self.permanently_disable = True
self.save_data()
return return
self.user_id = self.get_or_create_uuid()
if self.user_id and not self.permanently_disable: if self.user_id and not self.permanently_disable:
self.mp = Mixpanel(mixpanel_project_token) self.mp = Mixpanel(mixpanel_project_token)
self.ph = Posthog(project_api_key=posthog_project_api_key, host=posthog_host) self.ph = Posthog(project_api_key=posthog_project_api_key, host=posthog_host)
@ -44,35 +45,29 @@ class Analytics:
data_file.parent.mkdir(parents=True, exist_ok=True) data_file.parent.mkdir(parents=True, exist_ok=True)
return data_file return data_file
def mark_as_permanently_disabled(self):
data_file = self.get_data_file_path()
if data_file.exists():
with open(data_file, "r") as f:
data = json.load(f)
else:
data = {"uuid": str(uuid.uuid4())}
data["permanently_disabled"] = True
with open(data_file, "w") as f:
json.dump(data, f)
def get_or_create_uuid(self): def get_or_create_uuid(self):
self.load_data()
if self.user_id:
return
self.user_id = str(uuid.uuid4())
self.save_data()
def load_data(self):
data_file = self.get_data_file_path() data_file = self.get_data_file_path()
if data_file.exists(): if data_file.exists():
with open(data_file, "r") as f: try:
data = json.load(f) data = json.loads(data_file.read_text())
if "permanently_disabled" in data and data["permanently_disabled"]: self.permanently_disable = data.get("permanently_disable")
self.permanently_disable = True self.user_id = data.get("uuid")
self.mp = None except json.decoder.JSONDecodeError:
self.ph = None pass
return
return data["uuid"]
new_uuid = str(uuid.uuid4()) def save_data(self):
with open(data_file, "w") as f: data_file = self.get_data_file_path()
json.dump({"uuid": new_uuid}, f) data = dict(uuid=self.user_id, permanently_disable=self.permanently_disable)
return new_uuid data_file.write_text(json.dumps(data, indent=4))
def get_system_info(self): def get_system_info(self):
return { return {

View file

@ -13,7 +13,7 @@ a future release.
## Anonymous, no personal info ## Anonymous, no personal info
No personal information is collected: no user identity, none of your code, No personal information is collected: no user identity, none of your code, keys,
prompts or chat messages. prompts or chat messages.
Aider collects information on: Aider collects information on:
@ -32,13 +32,6 @@ features and commands are most used.
It also helps uncover bugs that users are experiencing, so that they can be fixed It also helps uncover bugs that users are experiencing, so that they can be fixed
in upcoming releases. in upcoming releases.
## Sample analytics data
To get a better sense of what type of data is collected, you can review some
[sample analytics logs](https://github.com/paul-gauthier/aider/blob/main/aider/website/assets/sample-analytics.jsonl).
These are the last 1,000 analytics events from the author's
personal use of aider, updated regularly.
## Enabling & disabling analytics ## Enabling & disabling analytics
You can opt out of analytics forever by running this command one time: You can opt out of analytics forever by running this command one time:
@ -48,7 +41,7 @@ aider --analytics-disable
``` ```
To enable analytics for a single session, you can run the command below. To enable analytics for a single session, you can run the command below.
This will *not* do anything if you have permanently disabled analytics with the previous This will *not* have any effect if you have permanently disabled analytics with the previous
command. command.
``` ```
@ -61,7 +54,25 @@ To disable analytics for a single session, you can run:
aider --no-analytics aider --no-analytics
``` ```
## Logging and inspecting analytics ## Details about data being collected
### Sample analytics data
To get a better sense of what type of data is collected, you can review some
[sample analytics logs](https://github.com/paul-gauthier/aider/blob/main/aider/website/assets/sample-analytics.jsonl).
These are the last 1,000 analytics events from the author's
personal use of aider, updated regularly.
### Analytics code
Since aider is open source, all the places where aider collects analytics
are visible in the source code.
They can be viewed using
[GitHub search](https://github.com/search?q=repo%3Apaul-gauthier%2Faider+%22.event%28%22&type=code).
### Logging and inspecting analytics
You can get a full log of the analytics that aider is collecting, You can get a full log of the analytics that aider is collecting,
in case you would like to audit or inspect this data. in case you would like to audit or inspect this data.
@ -76,11 +87,6 @@ If you want to just log analytics without reporting them, you can do:
aider --analytics-log filename.jsonl --no-analytics aider --analytics-log filename.jsonl --no-analytics
``` ```
Since aider is open source, all the places where aider reports analytics
are visible in the source code.
They can be easily viewed using
[GitHub search](https://github.com/search?q=repo%3Apaul-gauthier%2Faider+%22.event%28%22&type=code).
## Reporting issues ## Reporting issues