-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaw_fakedata.py
executable file
·343 lines (281 loc) · 9.86 KB
/
aw_fakedata.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
"""
File should be self-contained, as it's run by simple `wget .../fakedata.py; python3 fakedata.py` from actions like ActivityWatch/setup-action and integration tests.
"""
import os
import sys
import random
import logging
from copy import copy
from datetime import datetime, timezone, timedelta, date, time
from collections import defaultdict
from typing import List, Iterator, Dict
import click
from aw_core.models import Event
from aw_client import ActivityWatchClient
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
hostname = "fakedata"
client_name = "aw-fakedata"
bucket_window = "aw-watcher-window_" + hostname
bucket_afk = "aw-watcher-afk_" + hostname
bucket_browser_chrome = "aw-watcher-web-chrome_" + hostname
bucket_browser_firefox = "aw-watcher-web-firefox_" + hostname
now = datetime.now(tz=timezone.utc)
@click.command("aw-fakedata")
@click.option("--since", type=click.DateTime(formats=["%Y-%m-%d"]))
@click.option("--until", type=click.DateTime(formats=["%Y-%m-%d"]))
def main(since: datetime = None, until: datetime = None):
"""
Generates fake data for used in testing of ActivityWatch.
Will run in testing mode by default, can be overridden with the env var AW_TESTING.
"""
client = setup_client()
if not until:
until = now
else:
until = until.replace(tzinfo=timezone.utc)
if not since:
since = until - timedelta(days=14)
else:
since = since.replace(tzinfo=timezone.utc)
print(f"Range: {since} to {until}")
generate(client, since, until)
def setup_client() -> ActivityWatchClient:
logger.info("Setting up client")
# Default is to run in testing mode, can be run in prod mode if set to exactly 'false'
testing = os.getenv("AW_TESTING", "true").lower() not in ["false"]
if testing:
logger.info(
"Using testing parameters (set the env var AW_TESTING to false to run in prod mode)"
)
client = ActivityWatchClient(client_name, testing=testing)
client.client_hostname = hostname
buckets = client.get_buckets()
logger.info("Deleting old buckets")
buckets_all = [
bucket_afk,
bucket_window,
bucket_browser_chrome,
bucket_browser_firefox,
]
if not testing and sys.stdin.isatty():
ans = input(
f"Running in prod, are you sure you want to delete the existing buckets?\n{buckets_all}\nAre you sure? (y/N) "
)
if ans != "y":
print("Exiting")
sys.exit(0)
for bucket in buckets_all:
if bucket in buckets:
client.delete_bucket(bucket, force=True)
client.create_bucket(bucket_window, "currentwindow")
client.create_bucket(bucket_afk, "afkstatus")
client.create_bucket(bucket_browser_chrome, "web.tab.current")
client.create_bucket(bucket_browser_firefox, "web.tab.current")
client.connect()
return client
# Sample window event data with weights
sample_data_afk: List[dict] = [
{"status": "not-afk", "$weight": 1, "$duration": 120},
{"status": "afk", "$weight": 1, "$duration": 10},
]
# $weight controls the likelihood of being picked.
# $duration can be optionally set to the expected number of minutes per event.
sample_data_window: List[dict] = [
# Meetings
# Should be ~30min every other day
{
"app": "zoom",
"title": "Zoom Meeting",
"$weight": 3,
"$duration": 20,
},
# Games
# Should be ~60min every week
{"app": "Minecraft", "title": "Minecraft", "$weight": 2, "$duration": 200},
# ActivityWatch-related
# Should be ~60% of time
{
"app": "Firefox",
"title": "ActivityWatch/activitywatch: Track how you spend your time - github.com/",
"$weight": 20,
"$duration": 5,
},
{
"app": "Terminal",
"title": "vim ~/code/activitywatch/other/aw-fakedata",
"$weight": 10,
},
{
"app": "Terminal",
"title": "vim ~/code/activitywatch/README.md",
"$weight": 3,
"$duration": 5,
},
{"app": "Terminal", "title": "vim ~/code/activitywatch/aw-server", "$weight": 5},
{"app": "Terminal", "title": "bash ~/code/activitywatch", "$weight": 5},
# Misc work
# Should be ~20% of work
{
"app": "Firefox",
"title": "Gmail - mail.google.com/",
"$weight": 5,
"$duration": 10,
},
{
"app": "Firefox",
"title": "Stack Overflow - stackoverflow.com/",
"$weight": 10,
"$duration": 5,
},
{
"app": "Firefox",
"title": "Google Calendar - calendar.google.com/",
"$weight": 5,
"$duration": 2,
},
# Social media
# Should be ~30min/day
{
"app": "Firefox",
"title": "reddit: the front page of the internet - reddit.com/",
"$weight": 10,
"$duration": 10,
},
{
"app": "Firefox",
"title": "Home / Twitter - twitter.com/",
"$weight": 10,
"$duration": 8,
},
{
"app": "Firefox",
"title": "Facebook - facebook.com/",
"$weight": 10,
"$duration": 3,
},
{"app": "Chrome", "title": "Unknown site", "$weight": 2},
# Media
# Should be ~1h/month
{"app": "Spotify", "title": "Spotify", "$weight": 8, "$duration": 3},
{
"app": "Chrome",
"title": "YouTube - youtube.com/",
"$weight": 4,
"$duration": 25,
},
]
sample_data_browser: List[dict] = [
{"title": "GitHub", "url": "https://github.com", "$weight": 10, "$duration": 10},
{"title": "Twitter", "url": "https://twitter.com", "$weight": 3, "$duration": 5},
{"title": "YouTube", "url": "https://youtube.com", "$weight": 5, "$duration": 20},
]
def random_events(
start: datetime,
stop: datetime,
sample_data: List[dict],
duration_max_default: float = 120 * 60,
) -> List[Event]:
"""Randomly samples events from sample data"""
events = []
ts = start
while ts < stop:
data = copy(
random.choices(sample_data, weights=[d["$weight"] for d in sample_data])[0]
)
if "$duration" in data:
duration = timedelta(minutes=random.uniform(0.5, 2) * data["$duration"])
else:
duration = timedelta(seconds=random.uniform(5, duration_max_default))
# Ensure event doesn't spill over
end = min(stop, ts + duration)
e = Event(
timestamp=ts,
duration=end - ts,
data=data,
)
events += [e]
ts += e.duration
return events
def daterange(d1: datetime, d2: datetime, inclusive=False) -> Iterator[date]:
ts = d1
while ts < d2:
yield ts.date()
ts += timedelta(days=1)
if inclusive:
yield ts.date()
def generate(client, start: datetime, end: datetime):
print("Generating fake window events")
# Seed the rng to get replicable results.
# Identical input parameters should get identical outputs.
random.seed(start.timestamp() + end.timestamp())
buckets = generate_days(start, end)
for bucketid, events in buckets.items():
client.insert_events(bucketid, events)
print(f"Sent {len(events)} to bucket {bucketid}")
def generate_days(start: datetime, stop: datetime) -> Dict[str, List[Event]]:
buckets: Dict[str, List[Event]] = defaultdict(list)
for day in daterange(start, stop, inclusive=True):
for bucket, events in generate_day(day).items():
buckets[bucket] += events
return buckets
def generate_day(day: date) -> Dict[str, List[Event]]:
# Select a random day start and stop
day_offset = timedelta(hours=8)
start = datetime.combine(day, time()).replace(tzinfo=timezone.utc) + day_offset
is_workday = day.isoweekday() in range(1, 6)
if is_workday:
day_duration = timedelta(hours=5 + 5 * random.random())
else:
# Weekend
day_duration = timedelta(hours=1 + 4 * random.random())
# print(day_duration)
stop = start + day_duration
# TODO: Add lunchbreak by splitting generate_activity into thw
break_start = start + (stop - start) / 2
break_duration = timedelta(minutes=random.uniform(60, 120))
break_stop = break_start + break_duration
return merge_activity(
generate_activity(start, break_start),
generate_activity(break_stop, stop + break_duration),
)
def merge_activity(d1, d2):
dres = defaultdict(list)
dres.update(d1)
for k in d2:
dres[k].extend(d2[k])
return dres
def generate_afk(start: datetime, stop: datetime) -> List[Event]:
# FIXME: Randomly generates non-afk events in sequence, should alternate
return random_events(start, stop, sample_data_afk)
def generate_browser(start: datetime, stop: datetime) -> List[Event]:
return random_events(start, stop, sample_data_browser)
def generate_activity(start, end) -> Dict[str, List[Event]]:
# Generate an AFK event and containing window events
events_afk = generate_afk(start, end)
events_window = []
events_browser_chrome = []
events_browser_firefox = []
for event in events_afk:
if event.data["status"] == "not-afk":
events_window += random_events(
event.timestamp, event.timestamp + event.duration, sample_data_window
)
for event in events_window:
if event.data["app"].lower() == "firefox":
events_browser_firefox += generate_browser(
event.timestamp, event.timestamp + event.duration
)
if event.data["app"].lower() == "chrome":
events_browser_chrome += generate_browser(
event.timestamp, event.timestamp + event.duration
)
return {
bucket_window: events_window,
bucket_afk: events_afk,
bucket_browser_chrome: events_browser_chrome,
bucket_browser_firefox: events_browser_firefox,
}
if __name__ == "__main__":
main()