-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
load_dataframe.py
66 lines (52 loc) · 1.6 KB
/
load_dataframe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Load ActivityWatch data into a dataframe, and export as CSV.
"""
import os
import socket
from datetime import datetime, timedelta, timezone
import iso8601
import pandas as pd
from aw_client import ActivityWatchClient
from aw_client.classes import default_classes
from aw_client.queries import DesktopQueryParams, canonicalEvents
def build_query() -> str:
hostname = "fakedata" if os.getenv("CI") else socket.gethostname()
canonicalQuery = canonicalEvents(
DesktopQueryParams(
bid_window=f"aw-watcher-window_{hostname}",
bid_afk=f"aw-watcher-afk_{hostname}",
classes=default_classes,
)
)
return f"""
{canonicalQuery}
RETURN = {{"events": events}};
"""
def main() -> None:
now = datetime.now(tz=timezone.utc)
td30d = timedelta(days=30)
aw = ActivityWatchClient()
print("Querying...")
query = build_query()
data = aw.query(query, [(now - td30d, now)])
events = [
{
"timestamp": e["timestamp"],
"duration": timedelta(seconds=e["duration"]),
**e["data"],
}
for e in data[0]["events"]
]
for e in events:
e["$category"] = " > ".join(e["$category"])
df = pd.json_normalize(events)
df["timestamp"] = pd.to_datetime(df["timestamp"].apply(iso8601.parse_date))
df.set_index("timestamp", inplace=True)
print(df)
answer = input("Do you want to export to CSV? (y/N): ")
if answer == "y":
filename = "output.csv"
df.to_csv(filename)
print(f"Wrote to {filename}")
if __name__ == "__main__":
main()