Skip to content

Instantly share code, notes, and snippets.

@ErikBjare
Last active April 26, 2024 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErikBjare/bbd1db1f3fcb57733a88239e6f3450b7 to your computer and use it in GitHub Desktop.
Save ErikBjare/bbd1db1f3fcb57733a88239e6f3450b7 to your computer and use it in GitHub Desktop.
Reads a Wakatime export, and prints the hours spent on ActivityWatch projects, by project and by date.
"""
Reads a Wakatime export, and prints the hours spent on ActivityWatch projects, by project and by date.
Original author: @brayo-pip
Modifications by: @ErikBjare
"""
import csv
import json
import sys
from collections import Counter
from tabulate import tabulate
if len(sys.argv) == 2:
path = sys.argv[1]
else:
print("Usage: python analyze.py <path to JSON file>")
sys.exit(1)
file = open(path, "r")
data = json.load(file)
# store the last 50 days of data
data["days"] = data["days"][-50:]
hours_by_date: Counter[str] = Counter()
hours_by_project: Counter[str] = Counter()
for day in data["days"]:
date = day["date"]
for project in day["projects"]:
# Match ActivityWatch projects
name = project["name"].lower()
if name.startswith("activitywatch") or name.startswith("aw"):
for entity in project["entities"]:
hours_by_date[date] += entity["total_seconds"] / 60 / 60
hours_by_project[name] += entity["total_seconds"] / 60 / 60
# Compute total, filter out projects with less than 2 hours
hours_total = sum(v for v in hours_by_project.values() if v > 2)
header = ["Date", "Hours"]
print(tabulate(hours_by_date.items(), headers=header))
print()
print(tabulate(hours_by_project.most_common(10), headers=header))
print()
print(f"total hours spent on aw: {hours_total} hours")
with open("output.csv", "w", newline="") as file:
writer = csv.writer(file)
# Write the header
writer.writerow(header)
# Write the data
for date, hours in hours_by_date.items():
writer.writerow([date, hours])
print()
print("Data written to output.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment