-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworklife_check.py
96 lines (80 loc) · 2.96 KB
/
worklife_check.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
import yaml
from pathlib import Path
from db_util import sum_times_range, get_user_mail_alias
from datetime import datetime, timezone, timedelta
#from tabulate import tabulate
from mail import send_mail
from time import sleep
def load_users():
pf = Path(__file__).parent.parent / "worklife.yaml"
with open(str(pf), "r") as infi:
# yaml file example:
# [email protected]: whatever! can specify multiple receipents
# db_user_id: hours per week to trigger allert
return yaml.load(infi, yaml.Loader)
def get_alert_mails():
receipents = []
for k, v in load_users().items():
if type(k) == str and "@" in k:
receipents.append(k)
return receipents
def get_scan_users():
users = []
for k, v in load_users().items():
if type(k) == int:
users.append((k, v))
return users
def get_week_by_idx(idx):
monday = get_current_monday() + timedelta(days=idx * 7)
sunday = monday + timedelta(days=6)
return monday.date(), sunday.date()
def get_current_monday():
dt = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
wd = dt.weekday()
#timezone.
return dt - timedelta(days=wd)
def generate_report_user(args):
id, hourperweek = args
email, alias = get_user_mail_alias(id)
tbl = []
alert = False
for i in range(-5, 1):
mon, sun = get_week_by_idx(i)
seconds = sum_times_range(id, mon, sun)
#print(f"Got {alias} for {mon} - {sun} -> {seconds} hours -> {seconds / 3600} hours")
hours = seconds / 3600
overtime = hours - hourperweek
if overtime >= 10.0:
overtime = f"!!! Overtime {overtime:.1f} !!!"
if i == 0:
alert = True
elif overtime > 0.0:
overtime = f" Overtime {overtime:.1f}"
else:
overtime = ""
tbl.append((f"{mon} - {sun}", f"{hours:.1f}", overtime))
return email, alias, hourperweek, alert, tbl
if __name__ == "__main__":
users = get_scan_users()
supervisor = get_alert_mails()
print("Checking users: ", users)
for email, alias, hpw, alert, table in map(generate_report_user, users):
if alert:
alert = " ALERT"
receiver = supervisor.append(email)
msg = f"Hi {alias} and {', '.join(supervisor)}!\n\n"
else:
alert = ""
receiver = [email]
msg = f"Hi {alias}!\n\n"
msg += f"Here is your worktime report for the last 5 weeks.\nYour nominal worktime is set to {hpw:.1f} hours\n\n"
for dt, hr, ov in table:
if ov:
msg += f"{dt}\n {hr}h -> {ov} hours\n\n "
else:
msg += f"{dt}\n {hr}h\n\n"
msg += "I hope you have a great week!"
send_mail(receiver, "Worktime Report" + alert, msg)
# take a break between sending to avoid ionos rate limit
# dont know exactly what the limit is :(
sleep(10)