-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
512 lines (412 loc) · 15.9 KB
/
main.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import contextlib
import subprocess
from collections import defaultdict
from datetime import date, datetime, time, timedelta
from enum import Enum, auto
from pathlib import Path
from typing import DefaultDict, Dict, List, Optional
from dotenv import dotenv_values
from pydantic import BaseModel, Field
from rich.console import Console
from rich.table import Table
DEFAULT_LUNCH_DURATION = 30
console = Console()
class Config:
mode: str
datafile_dir: Path
datafile: str
workhours_one_day: int
def __init__(self) -> None:
self.reload()
def reload(self):
# Set default values
self.mode = ""
self.datafile_dir = Path(Path.home(), ".worktimer")
self.datafile = (
f"{datetime.now().year}-{datetime.now().month:02d}-timesheet.json"
)
self.workhours_one_day = 8
# Override default values
config = dotenv_values("config.env")
if config.get("mode", "") == "dev":
self.mode = "dev"
self.datafile_dir = Path(".worktimer")
cfg = Config()
def _today_iso_format() -> str:
return date.today().isoformat()
def time_diff(t1: Optional[time], t2: Optional[time]) -> int:
if t1 is None or t2 is None:
return 0
some_date = date(1, 1, 1)
datetime1 = datetime.combine(some_date, t1)
datetime2 = datetime.combine(some_date, t2)
return int((datetime1 - datetime2).total_seconds()) // 60
class WorkBlock(BaseModel):
start: Optional[time] = None
stop: Optional[time] = None
comment: Optional[str] = None
@property
def worked_time(self) -> int:
if not self.started() or not self.stopped():
return 0
return time_diff(self.stop, self.start)
def started(self) -> bool:
return self.start is not None
def stopped(self) -> bool:
return self.stop is not None
class Day(BaseModel):
this_date: date
lunch: int = 0
flex_minutes: int = 0
work_blocks: List[WorkBlock] = Field(default_factory=lambda: [])
time_off_minutes: int = 0
@property
def last_work_block(self) -> WorkBlock:
if len(self.work_blocks) == 0:
self.work_blocks.append(WorkBlock())
return self.work_blocks[-1]
@property
def worked_time(self) -> int:
worked_mins = sum(wt.worked_time for wt in self.work_blocks)
return 0 if worked_mins == 0 else worked_mins - self.lunch
def recalc_flex(self) -> None:
expected_worktime_in_mins = cfg.workhours_one_day * 60
time_off_minutes = self.time_off_minutes
weekday = self.this_date.isoweekday()
# Check if weekend
if weekday in (6, 7):
expected_worktime_in_mins = 0
time_off_minutes = 0
if len(self.work_blocks) == 1 and not self.last_work_block.stopped():
self.flex_minutes = 0
else:
self.flex_minutes = (
self.worked_time - expected_worktime_in_mins + time_off_minutes
)
class Timesheet(BaseModel):
days: Dict[str, Day] = Field(default_factory=lambda: {})
target_hours: int = 167
@property
def monthly_flex(self) -> int:
return sum(d.flex_minutes for d in self.days.values())
@property
def today(self) -> Day:
today = _today_iso_format()
if today not in self.days:
self.days[today] = Day(this_date=datetime.now().date())
return self.days[today]
def get_day(self, key: str) -> Day:
if key not in self.days:
self.days[key] = Day(this_date=date.fromisoformat(key))
return self.days[key]
def get_days(self, start_date: str, end_date: str) -> List[Day]:
days: List[Day] = []
from_date = date.fromisoformat(start_date)
to_date = date.fromisoformat(end_date)
while from_date <= to_date:
days.append(self.get_day(from_date.isoformat()))
from_date += timedelta(days=1)
return days
class ViewSpans(Enum):
TODAY = auto()
WEEK = auto()
PREV_WEEK = auto()
MONTH = auto()
class RecalcAction(Enum):
FLEX = 1
def load_timesheet(datafile: Optional[str] = None) -> Timesheet:
if datafile is None:
datafile = cfg.datafile
if not cfg.datafile_dir.joinpath(datafile).is_file():
empty_ts = Timesheet()
save_timesheet(empty_ts)
with open(cfg.datafile_dir.joinpath(datafile), "r") as f:
json_content = f.read()
return Timesheet.model_validate_json(json_content)
def save_timesheet(ts: Timesheet, datafile: Optional[str] = None) -> None:
if datafile is None:
datafile = cfg.datafile
with open(cfg.datafile_dir.joinpath(datafile), "w+", encoding="utf-8") as f:
f.write(ts.model_dump_json(indent=4))
def get_time_and_comment(params):
try:
if params:
h, m = map(int, params[0].split(":"))
time = datetime.now().replace(hour=h, minute=m, second=0, microsecond=0)
comment = " ".join(params[1:]) if len(params) > 1 else None
else:
time = datetime.now().replace(second=0, microsecond=0)
comment = None
return time, comment
except ValueError:
print("Invalid time format. Expeted format is hh:mm")
return None, None
def handle_command(cmd: str) -> None:
command_map = {
"start": lambda params: start(get_time_and_comment(params)[0]),
"stop": lambda params: stop(*get_time_and_comment(params)),
"lunch": lambda params: lunch(int(params[0]))
if params
else lunch(DEFAULT_LUNCH_DURATION),
"edit": lambda _: edit(),
"view": lambda params: view(ViewSpans[params[0].upper()]) if params else view(),
"summary": lambda _: summary(),
"recalc": lambda params: recalc(RecalcAction[params[0].upper()])
if params
else recalc(),
"timeoff": lambda params: set_time_off(int(params[0]) * 60),
"target_hours": lambda params: set_target_hours(int(params[0])),
"comment": lambda params: set_comment(" ".join(params) if params else None),
}
cmd, *params = cmd.split()
if cmd in command_map:
command_map[cmd](params)
else:
print(f"Unknown command: {cmd}")
def _print_estimated_endtime_for_today(
work_blocks: List[WorkBlock], lunch: int = 30, timeoff: int = 0
) -> None:
mins_left_to_work = (
(cfg.workhours_one_day * 60)
+ lunch
- timeoff
- sum(wt.worked_time for wt in work_blocks)
)
if not work_blocks[-1].start:
return
work_end_with_lunch = (
(
datetime.combine(date.today(), work_blocks[-1].start)
+ timedelta(minutes=mins_left_to_work)
)
.time()
.replace(second=0, microsecond=0)
)
print(
f"Estimated end time for today with {lunch} min lunch is {work_end_with_lunch}"
)
def start(start_time: datetime) -> None:
ts = load_timesheet()
last_wb = ts.today.last_work_block
if last_wb.started() and not last_wb.stopped():
print("Workblock already started, stop it before starting another one")
return
if last_wb.stopped():
ts.today.work_blocks.append(WorkBlock(start=start_time.time()))
else:
last_wb.start = start_time.time()
print(f"Starting at {start_time}")
save_timesheet(ts)
if ts.today.lunch > 0:
_print_estimated_endtime_for_today(
work_blocks=ts.today.work_blocks,
lunch=ts.today.lunch,
timeoff=ts.today.time_off_minutes,
)
else:
_print_estimated_endtime_for_today(
work_blocks=ts.today.work_blocks, timeoff=ts.today.time_off_minutes
)
def stop(stop_time: datetime, comment: Optional[str] = None) -> None:
ts = load_timesheet()
if not ts.today.last_work_block.started():
print("Could not stop workblock, is your last workblock started?")
return
if ts.today.last_work_block.stopped():
return
print(f"Stopping at {stop_time}")
ts.today.last_work_block.stop = stop_time.time()
ts.today.last_work_block.comment = comment
ts.today.recalc_flex()
flex_hours = abs(ts.today.flex_minutes) // 60
flex_mins = abs(ts.today.flex_minutes) % 60
if ts.today.flex_minutes >= 0:
print(f"Flex for today: {flex_hours} hours {flex_mins} mins")
else:
print(f"Flex for today is negative: {flex_hours} hours {flex_mins} mins")
save_timesheet(ts)
def lunch(lunch_mins: int) -> None:
ts = load_timesheet()
if len(ts.today.work_blocks) == 0 or not ts.today.last_work_block.started():
print("Could not find today in timesheet, did you start the day?")
return
if ts.today.lunch != 0:
return
_print_estimated_endtime_for_today(ts.today.work_blocks, lunch_mins)
ts.today.lunch = lunch_mins
ts.today.recalc_flex()
save_timesheet(ts)
print(f"Added {lunch_mins} mins as lunch")
def edit() -> None:
subprocess.call(["vim", cfg.datafile_dir.joinpath(cfg.datafile)])
def view(viewSpan: ViewSpans = ViewSpans.TODAY) -> None:
today = datetime.now().date()
if viewSpan == ViewSpans.TODAY:
start_date = end_date = date.today()
elif viewSpan == ViewSpans.WEEK:
start_date = today - timedelta(days=today.isoweekday() - 1)
end_date = today
elif viewSpan == ViewSpans.PREV_WEEK:
start_date = today - timedelta(days=today.isoweekday() + 6)
end_date = start_date + timedelta(days=6)
entries = load_timesheet().get_days(start_date.isoformat(), end_date.isoformat())
print_days(entries)
if viewSpan in [ViewSpans.WEEK, ViewSpans.PREV_WEEK]:
_print_footer(entries)
def summary(viewSpan: ViewSpans = ViewSpans.MONTH) -> None:
ts = load_timesheet()
days: List[Day] = []
for n in range(date.today().day - 1, -1, -1):
days.append(ts.get_day((date.today() - timedelta(days=n)).isoformat()))
table = Table()
table.add_column("week")
table.add_column("date")
table.add_column("worked time")
table.add_column("daily flex")
table.add_column("time off")
for d in days:
the_date = d.this_date.isoformat()
worked_time = fmt_mins(d.worked_time, expand=True) if d.worked_time > 0 else ""
daily_flex = fmt_mins(d.flex_minutes) if d.worked_time > 0 else ""
timeoff = fmt_mins(d.time_off_minutes) if d.time_off_minutes > 0 else ""
week = d.this_date.isocalendar()[1] if d.this_date.isoweekday() == 1 else ""
if d.this_date.isoweekday() == 1:
table.add_section()
table.add_row(str(week), the_date, worked_time, daily_flex, timeoff)
console.print(table)
print("")
# summarize weeks
weekly_summary: DefaultDict[int, int] = defaultdict(int)
for d in days:
if d.worked_time > 0:
weekly_summary[d.this_date.isocalendar()[1]] += d.worked_time
for week, weekly_time in weekly_summary.items():
print(f"week {week}: {fmt_mins(weekly_time)}")
# summarize month
expected_worked_hours_sum = (
sum(
(cfg.workhours_one_day * 60 - d.time_off_minutes)
for d in days
if d.worked_time > 0
)
// 60
)
print(
(
f"Worked {fmt_mins(sum(d.worked_time for d in days))} "
f"of {expected_worked_hours_sum} hour(s) => "
f"monthly flex: {fmt_mins(sum(d.flex_minutes for d in days))}"
)
)
print(f"Target hours for month: {ts.target_hours}")
def recalc(action: RecalcAction = RecalcAction.FLEX) -> None:
if action == RecalcAction.FLEX:
for f in cfg.datafile_dir.glob("*-timesheet.json"):
ts = load_timesheet(f.name)
for _, v in ts.days.items():
v.recalc_flex()
save_timesheet(ts, f.name)
def set_time_off(time_off_mins: int) -> None:
if time_off_mins < 0 or time_off_mins > 8 * 60:
raise ValueError(
"Invalid timeoff value, must be an int between 0 and 8 inclusive."
)
ts = load_timesheet()
ts.today.time_off_minutes = time_off_mins
ts.today.recalc_flex()
save_timesheet(ts)
print(f"Setting timeoff to {fmt_mins(time_off_mins)}")
def set_target_hours(target_hours: int) -> None:
if target_hours < 0:
raise ValueError("Invalid target_hours value, must be an int greater than 0.")
ts = load_timesheet()
ts.target_hours = target_hours
save_timesheet(ts)
print(f"Setting target hours to {target_hours}")
def set_comment(text: Optional[str]) -> None:
ts = load_timesheet()
if ts.today.last_work_block.started() and not ts.today.last_work_block.stopped():
ts.today.last_work_block.comment = text
else:
print("Cannot set comment for workblock not started")
save_timesheet(ts)
def calc_total_flex() -> int:
return sum(
load_timesheet(f.name).monthly_flex
for f in cfg.datafile_dir.glob("*-timesheet.json")
)
def total_flex_as_str() -> str:
return fmt_mins(calc_total_flex())
def print_menu():
print("-- commands --")
print("start [hh:mm]")
print("stop [hh:mm]")
print("lunch [n]")
print("edit")
print("view [TODAY|WEEK]")
print("summary [MONTH]")
print("recalc [FLEX]")
print("timeoff [hours]")
print("target_hours [hours]")
def print_days(days: List[Day]) -> None:
if len(days) == 1:
_print_day(days[0])
return
for day in days[:-1]:
_print_day(day)
print("")
_print_day(days[-1])
def _print_day(day: Day) -> None:
header = " | ".join(
[
day.this_date.isoformat(),
f"worked time: {fmt_mins(day.worked_time, expand=True)}",
f"lunch: {fmt_mins(day.lunch)}",
f"daily flex: {fmt_mins(day.flex_minutes)}",
]
)
print(header)
_print_work_blocks(day.work_blocks)
def _print_footer(days: List[Day]) -> None:
weekly_flex = sum(d.flex_minutes for d in days)
print("---")
print(f"Weekly flex: {fmt_mins(weekly_flex)}")
def _print_work_blocks(blocks: List[WorkBlock]) -> None:
for block in blocks:
block_start = block.start.isoformat()[:5] if block.start is not None else ""
block_stop = block.stop.isoformat()[:5] if block.stop is not None else ""
if not block.stopped():
print(f" {block_start}-")
else:
print(f" {block_start}-{block_stop} => {fmt_mins(block.worked_time)}")
if block.comment:
print(f" {block.comment}")
def fmt_mins(mins: int, expand: bool = False) -> str:
sign = "" if mins >= 0 else "-"
mins = abs(mins)
if mins < 60 and not expand:
return f"{sign}{mins}min"
return f"{sign}{mins // 60}h {mins % 60}min"
def run():
cfg.datafile_dir.mkdir(exist_ok=True)
if cfg.mode == "dev":
print("Running in dev mode.")
ts = load_timesheet()
print(
f"Worked {fmt_mins(sum(d.worked_time for d in ts.days.values()), expand=True)}"
f" of your {ts.target_hours} target hours for this month"
)
print(f"Monthly flex: {fmt_mins(sum(d.flex_minutes for d in ts.days.values()))} \n")
if len(ts.today.work_blocks) > 0:
print(f"You started last work block @ {ts.today.last_work_block.start}")
print_menu()
done = False
while not done:
cmd = input("> ")
if cmd in ("quit", "exit", "q"):
done = True
else:
with contextlib.suppress(KeyError):
handle_command(cmd.lower())
if __name__ == "__main__":
run()