agent-usage-tracker 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agent_usage_tracker/__init__.py +8 -0
- agent_usage_tracker/backup.py +225 -0
- agent_usage_tracker/cli.py +479 -0
- agent_usage_tracker/collectors/__init__.py +115 -0
- agent_usage_tracker/collectors/_util.py +310 -0
- agent_usage_tracker/collectors/activity_common.py +294 -0
- agent_usage_tracker/collectors/activitywatch.py +394 -0
- agent_usage_tracker/collectors/claude_code.py +484 -0
- agent_usage_tracker/collectors/claude_code_activity.py +317 -0
- agent_usage_tracker/collectors/codex.py +612 -0
- agent_usage_tracker/collectors/commits.py +154 -0
- agent_usage_tracker/collectors/cursor.py +193 -0
- agent_usage_tracker/collectors/cursor_api.py +247 -0
- agent_usage_tracker/collectors/cursor_cli.py +592 -0
- agent_usage_tracker/collectors/cursor_sdk.py +95 -0
- agent_usage_tracker/collectors/generic_sdk.py +100 -0
- agent_usage_tracker/collectors/pi.py +397 -0
- agent_usage_tracker/collectors/session_digest.py +379 -0
- agent_usage_tracker/collectors/t3.py +490 -0
- agent_usage_tracker/config.py +392 -0
- agent_usage_tracker/coverage.py +615 -0
- agent_usage_tracker/coverage_catalog.json +588 -0
- agent_usage_tracker/cursor_run.py +62 -0
- agent_usage_tracker/diagnostics.py +74 -0
- agent_usage_tracker/episode_store.py +78 -0
- agent_usage_tracker/event.py +132 -0
- agent_usage_tracker/git_day_cache.py +128 -0
- agent_usage_tracker/git_days.py +499 -0
- agent_usage_tracker/git_safe.py +58 -0
- agent_usage_tracker/harness_inventory.py +208 -0
- agent_usage_tracker/history_steers.py +88 -0
- agent_usage_tracker/improve/__init__.py +1 -0
- agent_usage_tracker/improve/case_view.py +112 -0
- agent_usage_tracker/improve/cli.py +138 -0
- agent_usage_tracker/improve/desktop.py +347 -0
- agent_usage_tracker/improve/evidence.py +322 -0
- agent_usage_tracker/improve/judge.py +671 -0
- agent_usage_tracker/improve/proposals.py +72 -0
- agent_usage_tracker/improve/rubric.py +360 -0
- agent_usage_tracker/improve/rubrics/avoidable_recovery.md +20 -0
- agent_usage_tracker/improve/rubrics/code_quality.md +22 -0
- agent_usage_tracker/improve/rubrics/completion_supported.md +20 -0
- agent_usage_tracker/improve/rubrics/redundant_tests.md +21 -0
- agent_usage_tracker/improve/rubrics/verbosity.md +21 -0
- agent_usage_tracker/improve/runtime.py +117 -0
- agent_usage_tracker/improve/schedule.py +291 -0
- agent_usage_tracker/improve/search_check.py +563 -0
- agent_usage_tracker/improve/store.py +244 -0
- agent_usage_tracker/improve/studies.py +285 -0
- agent_usage_tracker/improve/study_cli.py +46 -0
- agent_usage_tracker/improve/study_decision.py +121 -0
- agent_usage_tracker/improve/study_followup.py +174 -0
- agent_usage_tracker/improve/t3.py +269 -0
- agent_usage_tracker/improve/trial.py +318 -0
- agent_usage_tracker/improve/validation.py +148 -0
- agent_usage_tracker/inspector.py +350 -0
- agent_usage_tracker/label/__init__.py +22 -0
- agent_usage_tracker/label/backends.py +742 -0
- agent_usage_tracker/label/bakeoff.py +661 -0
- agent_usage_tracker/label/baseline.py +173 -0
- agent_usage_tracker/label/clef.py +258 -0
- agent_usage_tracker/label/context.py +382 -0
- agent_usage_tracker/label/core.py +355 -0
- agent_usage_tracker/label/covered_run.py +92 -0
- agent_usage_tracker/label/evalset.py +154 -0
- agent_usage_tracker/label/gold.py +209 -0
- agent_usage_tracker/label/local.py +326 -0
- agent_usage_tracker/label/local_parallel.py +232 -0
- agent_usage_tracker/label/merge.py +57 -0
- agent_usage_tracker/label/outcome_coverage.py +57 -0
- agent_usage_tracker/label/replay.py +194 -0
- agent_usage_tracker/label/run.py +339 -0
- agent_usage_tracker/label/tasks.py +579 -0
- agent_usage_tracker/label/teach.py +145 -0
- agent_usage_tracker/local_page.py +128 -0
- agent_usage_tracker/manual_time.py +69 -0
- agent_usage_tracker/net.py +23 -0
- agent_usage_tracker/observation_cli.py +65 -0
- agent_usage_tracker/observation_report.py +116 -0
- agent_usage_tracker/observation_server.py +110 -0
- agent_usage_tracker/observations.py +365 -0
- agent_usage_tracker/outcome_git.py +383 -0
- agent_usage_tracker/outcomes.py +464 -0
- agent_usage_tracker/pricing.py +248 -0
- agent_usage_tracker/push.py +530 -0
- agent_usage_tracker/redact.py +45 -0
- agent_usage_tracker/repo_keys.py +165 -0
- agent_usage_tracker/review.py +252 -0
- agent_usage_tracker/scan.py +472 -0
- agent_usage_tracker/scheduled.py +164 -0
- agent_usage_tracker/sdk.py +79 -0
- agent_usage_tracker/sink.py +82 -0
- agent_usage_tracker/sink_stack/README.md +71 -0
- agent_usage_tracker/sink_stack/apply.sh +63 -0
- agent_usage_tracker/sink_stack/compose.yaml +94 -0
- agent_usage_tracker/sink_stack/env.example +20 -0
- agent_usage_tracker/sink_stack/grafana/dashboards/agents-effort.json +634 -0
- agent_usage_tracker/sink_stack/grafana/dashboards/agents-usage.json +1279 -0
- agent_usage_tracker/sink_stack/grafana/provisioning/dashboards/aut.yaml +9 -0
- agent_usage_tracker/sink_stack/grafana/provisioning/datasources/agents.yaml +16 -0
- agent_usage_tracker/sink_stack/sql/01_schema.sql +1186 -0
- agent_usage_tracker/sink_stack/sql/02_price.sql +60 -0
- agent_usage_tracker/sink_stack/sql/03_effort.sql +1180 -0
- agent_usage_tracker/sink_stack/sql/04_grants.sql +149 -0
- agent_usage_tracker/sink_stack/sql/refresh.sql +66 -0
- agent_usage_tracker/sources.py +180 -0
- agent_usage_tracker/state.py +191 -0
- agent_usage_tracker/telemetry.py +165 -0
- agent_usage_tracker-0.1.0.dist-info/METADATA +366 -0
- agent_usage_tracker-0.1.0.dist-info/RECORD +113 -0
- agent_usage_tracker-0.1.0.dist-info/WHEEL +4 -0
- agent_usage_tracker-0.1.0.dist-info/entry_points.txt +3 -0
- agent_usage_tracker-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
"""ActivityWatch collector (OPT-IN, local HTTP): not-AFK window time as agents.time_interval rows.
|
|
2
|
+
|
|
3
|
+
Reads THIS host's window and AFK buckets through aw-server's own query API, keeps only window
|
|
4
|
+
time while the AFK watcher says not-afk, and emits one TimeInterval per (clipped) window event.
|
|
5
|
+
Nothing leaves the machine before these guards run, in this order:
|
|
6
|
+
|
|
7
|
+
1. login window / screen saver rows are dropped (not active time);
|
|
8
|
+
2. deny-list: password managers, private-browsing titles, and whatever the config adds
|
|
9
|
+
(matched on the title and the URL) are dropped entirely (no interval, no app, no title);
|
|
10
|
+
3. titles are redacted (keys, tokens, JWTs, long hex/base64 runs, password=..., URL
|
|
11
|
+
credentials, emails) and cut to 300 chars; `activitywatch_titles = false` sends none, and
|
|
12
|
+
when activitywatch_title_days is set, a title older than that is never sent (default 0:
|
|
13
|
+
titles are kept for good).
|
|
14
|
+
|
|
15
|
+
Category and work relevance come from an app mapping (config over code defaults, then the app's
|
|
16
|
+
declared macOS category); a browser row is 'web-work' when its title or URL matches a work
|
|
17
|
+
pattern, else 'web'. A scan never fails because ActivityWatch is closed: no server or no buckets
|
|
18
|
+
means no rows and a 'degraded' heartbeat.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import functools
|
|
24
|
+
import json
|
|
25
|
+
import os
|
|
26
|
+
import plistlib
|
|
27
|
+
import re
|
|
28
|
+
import subprocess # nosec B404 # one mdfind argv below, no shell
|
|
29
|
+
import sys
|
|
30
|
+
import time
|
|
31
|
+
import urllib.error
|
|
32
|
+
import urllib.parse
|
|
33
|
+
import urllib.request
|
|
34
|
+
from collections import Counter
|
|
35
|
+
from datetime import datetime, timedelta, timezone
|
|
36
|
+
from typing import Any, Callable, Iterator
|
|
37
|
+
|
|
38
|
+
from ..config import Config
|
|
39
|
+
from ..event import CollectorUnavailable, TimeInterval, make_event_id
|
|
40
|
+
from ..net import HTTP_ERRORS, LOCAL_HOSTS
|
|
41
|
+
from ..redact import clean_text
|
|
42
|
+
from ._util import MALFORMED_RECORD, dict_or_empty, warn_skipped
|
|
43
|
+
|
|
44
|
+
NAME = "activitywatch"
|
|
45
|
+
_LAG = timedelta(minutes=5) # aw-watcher-window extends its last event; leave it settle
|
|
46
|
+
_CHUNK = timedelta(days=1) # one query per pass; the scan's drain loop runs the next day
|
|
47
|
+
_MIN_RANGE = timedelta(minutes=1) # below this, wait for the next scan (lets the drain loop end)
|
|
48
|
+
_PURGE_BATCH = 500
|
|
49
|
+
_PURGE_BUDGET = 20.0 # seconds of GET+DELETE per scan; a backlog drains over scans
|
|
50
|
+
_TIMEOUT = 10
|
|
51
|
+
|
|
52
|
+
_QUERY = """window = flood(query_bucket({win}));
|
|
53
|
+
afk = flood(query_bucket({afk}));
|
|
54
|
+
not_afk = filter_keyvals(afk, "status", ["not-afk"]);
|
|
55
|
+
RETURN = filter_period_intersect(window, not_afk);"""
|
|
56
|
+
|
|
57
|
+
_IDLE_APPS = ("loginwindow", "screensaverengine", "screen saver")
|
|
58
|
+
_DENY_APPS = ("1password", "bitwarden", "keepassxc", "keychain access", "passwords", "dashlane",
|
|
59
|
+
"lastpass", "proton pass")
|
|
60
|
+
_DENY_TITLES = (r"private browsing", r"\bincognito\b", r"\binprivate\b")
|
|
61
|
+
_CTRL = re.compile(r"[\x00-\x1f\x7f]") # Postgres text refuses NUL; the rest is noise
|
|
62
|
+
|
|
63
|
+
_EDITORS = ("code", "visual studio code", "cursor", "zed", "sublime text", "xcode",
|
|
64
|
+
"intellij idea", "pycharm", "webstorm", "goland", "clion", "rider", "rubymine",
|
|
65
|
+
"phpstorm", "datagrip", "rustrover", "android studio", "fleet")
|
|
66
|
+
_DEFAULT_APPS = {
|
|
67
|
+
**{a: "editor" for a in _EDITORS},
|
|
68
|
+
**{a: "terminal" for a in ("terminal", "iterm2", "iterm", "ghostty", "warp", "supacode")},
|
|
69
|
+
**{a: "agent" for a in ("claude", "chatgpt", "codex")},
|
|
70
|
+
**{a: "comms" for a in ("slack", "microsoft teams", "zoom", "zoom.us")},
|
|
71
|
+
**{a: "docs" for a in ("notion", "obsidian", "typora", "notes")},
|
|
72
|
+
"figma": "design",
|
|
73
|
+
**{a: "media" for a in ("spotify", "music", "vlc", "iina", "quicktime player")},
|
|
74
|
+
**{a: "browser" for a in ("safari", "google chrome", "chrome", "chromium", "brave browser",
|
|
75
|
+
"arc", "firefox", "helium", "microsoft edge", "dia", "vivaldi",
|
|
76
|
+
"opera", "zen", "orion", "comet", "duckduckgo", "tor browser",
|
|
77
|
+
"librewolf", "sigmaos", "ladybird")},
|
|
78
|
+
}
|
|
79
|
+
_WORK_TITLES = (
|
|
80
|
+
r"github|gitlab|bitbucket|codeberg|sourcegraph|pull request|merge request",
|
|
81
|
+
r"\bjira\b|linear|youtrack|sentry|confluence",
|
|
82
|
+
r"\bdocs?\b|documentation|readthedocs|stack ?overflow|\bmdn\b|pypi|npmjs|api reference",
|
|
83
|
+
r"\baws\b|console\.cloud\.google|portal\.azure|cloudflare|vercel|grafana|kubernetes",
|
|
84
|
+
r"localhost|127\.0\.0\.1",
|
|
85
|
+
r"chatgpt|claude|gemini|perplexity",
|
|
86
|
+
)
|
|
87
|
+
_RELEVANT = frozenset(("editor", "terminal", "agent", "dev", "comms", "docs", "design",
|
|
88
|
+
"web-work"))
|
|
89
|
+
|
|
90
|
+
_DARWIN = sys.platform == "darwin"
|
|
91
|
+
_MAC_TIMEOUT = 2
|
|
92
|
+
# local app folders only (and ~/Applications): a bundle on a network or external volume could
|
|
93
|
+
# block the plist read
|
|
94
|
+
_MAC_APP_DIRS = ("/Applications/", "/System/")
|
|
95
|
+
# LSApplicationCategoryType (after 'public.app-category.') -> category; games-* are media too.
|
|
96
|
+
# 'browser' is what _MacCategories reports for a bundle that opens http(s) links.
|
|
97
|
+
_MAC_CATEGORIES = {
|
|
98
|
+
"developer-tools": "dev", "business": "comms", "productivity": "docs",
|
|
99
|
+
"graphics-design": "design", "browser": "browser",
|
|
100
|
+
**dict.fromkeys(("music", "video", "entertainment", "games", "social-networking", "sports",
|
|
101
|
+
"lifestyle", "news", "photography"), "media"),
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@functools.lru_cache(maxsize=None) # once per URL per run: a scan makes many calls
|
|
106
|
+
def _warn_if_plaintext(url: str) -> None:
|
|
107
|
+
"""Window titles travel this link, and purge deletes over it. Warn, never refuse: an aw-server
|
|
108
|
+
on a private network works today."""
|
|
109
|
+
parsed = urllib.parse.urlparse(url)
|
|
110
|
+
if parsed.scheme == "http" and (parsed.hostname or "").lower() not in LOCAL_HOSTS:
|
|
111
|
+
print(f"aut: warning: activitywatch_url reaches {parsed.hostname} over plain http, so window "
|
|
112
|
+
"titles travel unencrypted. Use https:// unless the link is private (for example "
|
|
113
|
+
"Tailscale).", file=sys.stderr)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _http(cfg: Config, method: str, path: str, body: Any = None) -> Any:
|
|
117
|
+
"""The one HTTP call to aw-server (tests stub this). Raises on transport or HTTP failure."""
|
|
118
|
+
url = cfg.activitywatch_url.rstrip("/") + path
|
|
119
|
+
_warn_if_plaintext(cfg.activitywatch_url)
|
|
120
|
+
req = urllib.request.Request(url, method=method,
|
|
121
|
+
data=None if body is None else json.dumps(body).encode(),
|
|
122
|
+
headers={"Content-Type": "application/json"})
|
|
123
|
+
with urllib.request.urlopen(req, timeout=_TIMEOUT) as resp: # nosec B310 # load_config admits http(s) only
|
|
124
|
+
raw = resp.read()
|
|
125
|
+
return json.loads(raw) if raw else None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _now() -> datetime:
|
|
129
|
+
return datetime.now(timezone.utc)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _parse_aw_ts(ts: str) -> datetime:
|
|
133
|
+
dt = datetime.fromisoformat(ts.replace("Z", "+00:00"))
|
|
134
|
+
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _event_span(ev: dict[str, Any]) -> tuple[datetime, datetime]:
|
|
138
|
+
start = _parse_aw_ts(ev["timestamp"])
|
|
139
|
+
return start, start + timedelta(seconds=float(ev.get("duration") or 0))
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _buckets(cfg: Config) -> tuple[str, str, datetime] | None:
|
|
143
|
+
"""(window bucket, afk bucket, window bucket's first moment) of THIS host, or None."""
|
|
144
|
+
host = _http(cfg, "GET", "/api/0/info").get("hostname")
|
|
145
|
+
buckets = _http(cfg, "GET", "/api/0/buckets/")
|
|
146
|
+
|
|
147
|
+
def pick(kind: str) -> str | None:
|
|
148
|
+
ids = sorted(b for b, v in buckets.items()
|
|
149
|
+
if isinstance(v, dict) and v.get("type") == kind and v.get("hostname") == host)
|
|
150
|
+
return ids[0] if ids else None
|
|
151
|
+
|
|
152
|
+
win, afk = pick("currentwindow"), pick("afkstatus")
|
|
153
|
+
if not (host and win and afk):
|
|
154
|
+
return None
|
|
155
|
+
meta = buckets[win].get("metadata") or {}
|
|
156
|
+
if meta.get("start"):
|
|
157
|
+
return win, afk, _parse_aw_ts(meta["start"])
|
|
158
|
+
# aw-server-rust writes 'created' in local time labelled UTC, up to 14 h late: start a day
|
|
159
|
+
# earlier (empty chunks cost one query each)
|
|
160
|
+
return win, afk, _parse_aw_ts(buckets[win]["created"]) - timedelta(days=1)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _norm(app: str) -> str:
|
|
164
|
+
return app.strip().lower()
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _lookup(table: dict[str, Any], name: str) -> Any:
|
|
168
|
+
"""Exact name, else a known name followed by a space ('pycharm professional')."""
|
|
169
|
+
if name in table:
|
|
170
|
+
return table[name]
|
|
171
|
+
return next((v for k, v in table.items() if name.startswith(k + " ")), None)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class _MacCategories:
|
|
175
|
+
"""macOS lookups: once per app, and the first timeout (a stalled Spotlight) ends them. Each
|
|
176
|
+
returns 'browser' when the app bundle opens http(s) links (browsers declare
|
|
177
|
+
productivity), else its declared LSApplicationCategoryType, or None: off macOS, no local
|
|
178
|
+
bundle, no key, or any error or timeout. The app name only goes to the local Spotlight index."""
|
|
179
|
+
|
|
180
|
+
def __init__(self) -> None:
|
|
181
|
+
self.seen: dict[str, str | None] = {}
|
|
182
|
+
self.off = False
|
|
183
|
+
|
|
184
|
+
def __call__(self, app: str) -> str | None:
|
|
185
|
+
if app not in self.seen:
|
|
186
|
+
self.seen[app] = None if self.off else self._lookup(app)
|
|
187
|
+
return self.seen[app]
|
|
188
|
+
|
|
189
|
+
def _lookup(self, app: str) -> str | None:
|
|
190
|
+
if not _DARWIN or not app or any(c in app for c in "'\\*"): # no quoting
|
|
191
|
+
return None
|
|
192
|
+
query = ("kMDItemContentType == 'com.apple.application-bundle' && "
|
|
193
|
+
f"kMDItemDisplayName == '{app}'")
|
|
194
|
+
dirs = (*_MAC_APP_DIRS, os.path.expanduser("~/Applications/"))
|
|
195
|
+
try:
|
|
196
|
+
out = subprocess.run( # nosec B603, B607 # one query argument, quotes refused, no shell
|
|
197
|
+
["mdfind", query], capture_output=True, text=True, check=True,
|
|
198
|
+
timeout=_MAC_TIMEOUT).stdout.splitlines()
|
|
199
|
+
hit = next((p for p in out if p.startswith(dirs)), None)
|
|
200
|
+
if hit is None:
|
|
201
|
+
return None
|
|
202
|
+
cat = _bundle_category(hit)
|
|
203
|
+
except subprocess.TimeoutExpired:
|
|
204
|
+
self.off = True
|
|
205
|
+
return None
|
|
206
|
+
except Exception: # a lookup must never fail a scan
|
|
207
|
+
return None
|
|
208
|
+
return cat if isinstance(cat, str) and cat else None
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _bundle_category(bundle: str) -> object:
|
|
212
|
+
"""'browser' when the bundle opens http(s) links, else its declared category, if any."""
|
|
213
|
+
with open(f"{bundle}/Contents/Info.plist", "rb") as f:
|
|
214
|
+
info = plistlib.load(f)
|
|
215
|
+
schemes = {str(x).lower() for t in info.get("CFBundleURLTypes") or ()
|
|
216
|
+
for x in t.get("CFBundleURLSchemes") or ()}
|
|
217
|
+
return "browser" if schemes & {"http", "https"} else info.get("LSApplicationCategoryType")
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# One per process, not per collect(): a pass covers one day, and a catch-up runs many.
|
|
221
|
+
_MAC = _MacCategories()
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _category_from_declared(declared: str) -> str:
|
|
225
|
+
kind = declared.removeprefix("public.app-category.")
|
|
226
|
+
return "media" if kind.endswith("games") else _MAC_CATEGORIES.get(kind, "other")
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def denied(cfg: Config, app: str, title: str, data: dict[str, Any] | None = None) -> bool:
|
|
230
|
+
"""Password managers, private browsing, and config deny patterns, matched on title + URL.
|
|
231
|
+
A Chromium private window (Brave, Chrome, Helium) has neither a title marker nor an
|
|
232
|
+
incognito flag from today's watcher, so it is not detectable here."""
|
|
233
|
+
name = _norm(app)
|
|
234
|
+
if _lookup(dict.fromkeys((*_DENY_APPS, *cfg.activitywatch_deny_apps), True), name):
|
|
235
|
+
return True
|
|
236
|
+
if (data or {}).get("incognito") is True:
|
|
237
|
+
return True
|
|
238
|
+
text = f"{title} {(data or {}).get('url') or ''}"
|
|
239
|
+
return any(re.search(p, text, re.I) for p in (*_DENY_TITLES, *cfg.activitywatch_deny_titles))
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def browser_category(cfg: Config, title: str, url: str = "") -> tuple[str, str]:
|
|
243
|
+
"""Browser row -> (web-work | web, rule). Seam: phase 4 replaces this rule with a model.
|
|
244
|
+
The URL is matched here, on the machine, and is never sent."""
|
|
245
|
+
text = f"{title} {url}"
|
|
246
|
+
if any(re.search(p, text, re.I) for p in (*_WORK_TITLES, *cfg.activitywatch_work_titles)):
|
|
247
|
+
return "web-work", "browser_work_title"
|
|
248
|
+
return "web", "browser_other"
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def classify(cfg: Config, app: str, title: str, url: str = "") -> tuple[str, bool, str]:
|
|
252
|
+
"""(category, work_relevant, rule) for one window row."""
|
|
253
|
+
name = _norm(app)
|
|
254
|
+
cat, rule = _lookup(cfg.activitywatch_categories, name), "config_app"
|
|
255
|
+
if cat is None:
|
|
256
|
+
cat, rule = _lookup(_DEFAULT_APPS, name), "default_app"
|
|
257
|
+
if cat is None and (declared := _MAC(app.strip())):
|
|
258
|
+
cat, rule = _category_from_declared(declared), "macos_category"
|
|
259
|
+
if cat is None:
|
|
260
|
+
cat, rule = "other", "unknown"
|
|
261
|
+
if cat == "browser":
|
|
262
|
+
cat, rule = browser_category(cfg, title, url)
|
|
263
|
+
return cat, cat in _RELEVANT, rule
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def describe(cfg: Config, wm: dict[str, Any]) -> str:
|
|
267
|
+
return f"read until {wm['end']}" if "end" in wm else "never scanned"
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _read_range(cfg: Config, wm: dict[str, Any]) -> tuple[list[dict[str, Any]], datetime, datetime] | None:
|
|
271
|
+
"""(raw events, start, end) of this pass; None while the range is too short to read yet."""
|
|
272
|
+
try:
|
|
273
|
+
found = _buckets(cfg)
|
|
274
|
+
if found is None:
|
|
275
|
+
raise CollectorUnavailable("activitywatch: aw-server has no window and AFK buckets for "
|
|
276
|
+
"this host; skipping")
|
|
277
|
+
win, afk, first = found
|
|
278
|
+
start = _parse_aw_ts(wm["end"]) if wm.get("end") else first
|
|
279
|
+
end = min(_now() - _LAG, start + _CHUNK)
|
|
280
|
+
if end - start < _MIN_RANGE:
|
|
281
|
+
return None
|
|
282
|
+
query = _QUERY.format(win=json.dumps(win), afk=json.dumps(afk)).splitlines()
|
|
283
|
+
res = _http(cfg, "POST", "/api/0/query/",
|
|
284
|
+
{"query": query, "timeperiods": [f"{start.isoformat()}/{end.isoformat()}"]})
|
|
285
|
+
return (res[0] if isinstance(res, list) and res and isinstance(res[0], list) else []), \
|
|
286
|
+
start, end
|
|
287
|
+
except (*HTTP_ERRORS, ValueError, TypeError, AttributeError, KeyError) as exc:
|
|
288
|
+
raise CollectorUnavailable(f"activitywatch: aw-server not usable ({type(exc).__name__}); "
|
|
289
|
+
"skipping") from exc
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _interval(cfg: Config, s: datetime, e: datetime, app: str, title: str, data: dict[str, Any],
|
|
293
|
+
title_from: datetime | None) -> TimeInterval:
|
|
294
|
+
category, relevant, rule = classify(cfg, app, title, str(data.get("url") or ""))
|
|
295
|
+
keep = cfg.activitywatch_titles and (title_from is None or s >= title_from)
|
|
296
|
+
sent = (clean_text(title) or None) if keep else None
|
|
297
|
+
s_iso, e_iso = s.astimezone(timezone.utc).isoformat(), e.astimezone(timezone.utc).isoformat()
|
|
298
|
+
# No title in the id: only one window has focus at a time, and an id must not let anyone
|
|
299
|
+
# confirm a guessed title.
|
|
300
|
+
return TimeInterval(
|
|
301
|
+
event_id=make_event_id(NAME, cfg.machine, app, s_iso, e_iso),
|
|
302
|
+
ts=s_iso, end_ts=e_iso, source="aw", app=app or None, title=sent,
|
|
303
|
+
category=category, work_relevant=relevant, attrs={"rule": rule})
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def collect(cfg: Config, wm: dict[str, Any]) -> Iterator[tuple[list[TimeInterval], Callable[[], None]]]:
|
|
307
|
+
read = _read_range(cfg, wm)
|
|
308
|
+
if read is None:
|
|
309
|
+
return
|
|
310
|
+
raw, start, end = read
|
|
311
|
+
rows: list[TimeInterval] = []
|
|
312
|
+
days = cfg.activitywatch_title_days
|
|
313
|
+
title_from = _now() - timedelta(days=days) if days else None
|
|
314
|
+
denied_n, denied_s, idle_s = 0, 0.0, 0.0
|
|
315
|
+
skipped: Counter[str] = Counter()
|
|
316
|
+
for ev in raw:
|
|
317
|
+
try:
|
|
318
|
+
s, e = _event_span(ev)
|
|
319
|
+
data = dict_or_empty(ev.get("data"))
|
|
320
|
+
except MALFORMED_RECORD:
|
|
321
|
+
skipped["malformed"] += 1
|
|
322
|
+
continue
|
|
323
|
+
s, e = max(s, start), min(e, end)
|
|
324
|
+
if e <= s:
|
|
325
|
+
continue
|
|
326
|
+
secs = (e - s).total_seconds()
|
|
327
|
+
app = _CTRL.sub("", str(data.get("app") or ""))
|
|
328
|
+
title = _CTRL.sub(" ", str(data.get("title") or ""))
|
|
329
|
+
if _norm(app) in _IDLE_APPS:
|
|
330
|
+
idle_s += secs
|
|
331
|
+
continue
|
|
332
|
+
if denied(cfg, app, title, data):
|
|
333
|
+
denied_n, denied_s = denied_n + 1, denied_s + secs
|
|
334
|
+
continue
|
|
335
|
+
rows.append(_interval(cfg, s, e, app, title, data, title_from))
|
|
336
|
+
warn_skipped(NAME, skipped)
|
|
337
|
+
if denied_n or idle_s:
|
|
338
|
+
print(f"aut: activitywatch: dropped {denied_n} intervals ({denied_s / 60:.1f} min) by the "
|
|
339
|
+
f"deny-list, {idle_s / 60:.1f} min idle (login window / screen saver)",
|
|
340
|
+
file=sys.stderr)
|
|
341
|
+
|
|
342
|
+
def advance_watermark(w: dict[str, Any] = wm, end_iso: str = end.isoformat()) -> None:
|
|
343
|
+
w["end"] = end_iso
|
|
344
|
+
|
|
345
|
+
yield rows, advance_watermark
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def intervals(cfg: Config, since: datetime, until: datetime) -> list[TimeInterval]:
|
|
349
|
+
"""The rows from `since` until `until` is covered, through the same guards as a scan, one day
|
|
350
|
+
per query: the last row can run past `until`. Raises CollectorUnavailable."""
|
|
351
|
+
wm = {"end": since.astimezone(timezone.utc).isoformat()}
|
|
352
|
+
out: list[TimeInterval] = []
|
|
353
|
+
while _parse_aw_ts(wm["end"]) < until:
|
|
354
|
+
before = wm["end"]
|
|
355
|
+
for rows, advance_watermark in collect(cfg, wm):
|
|
356
|
+
out += rows
|
|
357
|
+
advance_watermark()
|
|
358
|
+
if wm["end"] == before: # the range is too short to read yet
|
|
359
|
+
break
|
|
360
|
+
return out
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def purge(cfg: Config) -> int:
|
|
364
|
+
"""Delete aw-server's own window events older than activitywatch_title_days, in batches of
|
|
365
|
+
_PURGE_BATCH until none is left or _PURGE_BUDGET seconds pass. Best effort: it never raises,
|
|
366
|
+
and logs a count only. activitywatch_title_days = 0 (the default) keeps everything."""
|
|
367
|
+
n = 0
|
|
368
|
+
if cfg.activitywatch_title_days <= 0:
|
|
369
|
+
return 0
|
|
370
|
+
deadline = time.monotonic() + _PURGE_BUDGET
|
|
371
|
+
try:
|
|
372
|
+
found = _buckets(cfg)
|
|
373
|
+
if found is None:
|
|
374
|
+
return 0
|
|
375
|
+
win = urllib.parse.quote(found[0], safe="")
|
|
376
|
+
cutoff = _now() - timedelta(days=cfg.activitywatch_title_days)
|
|
377
|
+
query = urllib.parse.urlencode({"end": cutoff.isoformat(), "limit": _PURGE_BATCH})
|
|
378
|
+
# the server answers newest first, so each batch is the next older slice: loop to the end
|
|
379
|
+
while time.monotonic() < deadline:
|
|
380
|
+
batch = _http(cfg, "GET", f"/api/0/buckets/{win}/events?{query}") or []
|
|
381
|
+
# aw-server-rust clips the event spanning `end` to it, so one that reaches the cutoff
|
|
382
|
+
# is partly inside the retention window and stays
|
|
383
|
+
gone = [int(ev["id"]) for ev in batch if _event_span(ev)[1] < cutoff]
|
|
384
|
+
if not gone:
|
|
385
|
+
break
|
|
386
|
+
for i in gone:
|
|
387
|
+
_http(cfg, "DELETE", f"/api/0/buckets/{win}/events/{i}")
|
|
388
|
+
n += 1
|
|
389
|
+
except Exception as exc: # purge must never fail a scan
|
|
390
|
+
print(f"aut: activitywatch: purge stopped ({type(exc).__name__})", file=sys.stderr)
|
|
391
|
+
if n:
|
|
392
|
+
print(f"aut: activitywatch: purged {n} window events older than "
|
|
393
|
+
f"{cfg.activitywatch_title_days} days", file=sys.stderr)
|
|
394
|
+
return n
|