-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheileen-score.py
executable file
·90 lines (80 loc) · 2.98 KB
/
eileen-score.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
#!/usr/bin/python
import requests
from os.path import expanduser
import yaml
import json
import sys
import re
from datetime import datetime, timedelta
from monthdelta import monthdelta
import glob, os
from collections import Counter
today_date = datetime.now().strftime("%Y-%m-%d")
print "Running on", today_date
config_file = expanduser("~") + '/.bigtime/eileen-config'
config = yaml.safe_load(open(config_file))
logdir = expanduser("~") + '/.bigtime/log/'
start_date_obj = datetime.now() - monthdelta(1)
days_of_week = ['Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday','Sunday']
employee_score = Counter()
employee_days = Counter()
for logfilename in glob.glob(logdir + "timecard-data-*.json"):
#print(logfilename)
with open(logfilename, 'r') as logfile:
timecard_data = json.load(logfile)
logdate = re.sub(logdir + "timecard-data-", '', logfilename)
logdate = re.sub("\.json",'', logdate)
logdate_obj = datetime.strptime(logdate, "%Y-%m-%d")
if logdate_obj > start_date_obj:
print logdate, ">", start_date_obj.strftime("%Y-%m-%d")
#print timecard_data
employees = {}
try:
tc_data = timecard_data['Data']
for record in tc_data:
name = record[4]
date = record[9]
if name in employees.keys():
if date not in employees[name]:
employees[name].append(date)
else:
employees[name] = [date]
dayobj = datetime.strptime(logdate, "%Y-%m-%d") - timedelta(days=1)
day = dayobj.strftime("%Y-%m-%d")
dow = datetime.strptime(day, "%Y-%m-%d").weekday()
if dow > 4: # don't show results for weekend days
continue
for name in employees.keys():
if name not in config['ignore']:
employee_days[name] += 1
if day in employees[name]:
employee_score[name] += 1
except KeyError:
print "Bad data in ", logfilename
pass
report = ""
report_dow = datetime.strptime(today_date, "%Y-%m-%d").weekday()
report += "Timecard Compliance Scores since "
report += start_date_obj.strftime("%Y-%m-%d") + "\n"
employee_names_to_report = employee_days.keys()
employee_names_to_report.sort()
for employee in employee_names_to_report:
employee_percent = (employee_score[employee] / \
float(employee_days[employee])) * 100
employee_percent = "{0:.2f}".format(employee_percent)
report += employee + " " + str(employee_percent) + "%\n"
print "Slack report: \n" + report
slack_payload = {
"text":report,
"channel": config['channel'],
"icon_emoji": config['emoji'],
"username": config['username']
}
if report != "":
print "sending to slack...", slack_payload
r = requests.post(config['slack_webhook_url'],
data = json.dumps(slack_payload))
print "slack response:", r.text
else:
print "Nothing to report"