-
Notifications
You must be signed in to change notification settings - Fork 1
/
t-metric.js
140 lines (123 loc) · 4.16 KB
/
t-metric.js
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
"use strict"
const tmetricToken = process.env.HUBOT_TMETRIC_TOKEN
const tmetricAccountId = process.env.HUBOT_TMETRIC_ACCOUNT_ID
const tmetricClientList = process.env.HUBOT_TMETRIC_CLIENT_LIST
const room = 'developers'
var cron = require('node-cron');
module.exports = (robot) => {
robot.error(function (err, res) {
robot.logger.error(err)
robot.send({room: room}, `there is an issue with the t-metric bot '${err}'`)
})
robot.hear(/tmetric\smonthly/gi, (res) => {
tmetricMonthlyReport(
robot,
{
res: res
}
)
})
robot.hear(/tmetric\sweekly/gi, (res) => {
tmetricWeeklyReport(
robot,
{
res: res
}
)
})
cron.schedule('30 12 * * 5', () => {
tmetricWeeklyReport(
robot,
{
outputPrefix: '@all ',
outputPostfix: 'Aaphno :tea: -metric :thumbsup: -sanga :straight_ruler: -bhayo?'
},
)
})
cron.schedule('00 16 28-31 * *', () => {
const tomorrow = new Date()
tomorrow.setDate(new Date().getDate() + 1)
if (tomorrow.getDate() === 1) {
tmetricMonthlyReport(
robot,
{
outputPrefix: '@all ',
outputPostfix: 'Aaphno :tea: -metric :thumbsup: -sanga :straight_ruler: -bhayo?'
},
)
}
})
}
function tmetricMonthlyReport(robot, {outputPrefix = '', outputPostfix = '', res = null}) {
const today = new Date()
const startDate = `${today.getFullYear()}-${today.getMonth() + 1}-1`
outputPrefix += `This month (since ${startDate}) we have these chargable hours logged in :tmetric:\n`
getTmetricreport(robot, {outputPrefix, outputPostfix, startDate, res})
}
function tmetricWeeklyReport(robot, {outputPrefix = '', outputPostfix = '', res = null}) {
const mondayOfThisWeek = getMonday(new Date())
const startDate = `${mondayOfThisWeek.getFullYear()}-${mondayOfThisWeek.getMonth() + 1}-${mondayOfThisWeek.getDate()}`
outputPrefix += `This week (since ${startDate}) we have these chargable hours logged in :tmetric:\n`
getTmetricreport(robot, {outputPrefix, outputPostfix, startDate, res})
}
function getTmetricreport(robot, {outputPrefix = '', startDate, outputPostfix = '', res = null}) {
let clientList = []
if (tmetricToken === '' || tmetricToken === undefined) {
robot.emit('error', `env. variable HUBOT_TMETRIC_TOKEN is not set`)
return
}
if (tmetricAccountId === '' || tmetricAccountId === undefined) {
robot.emit('error', `env. variable HUBOT_TMETRIC_ACCOUNT_ID is not set`)
return
}
if (typeof tmetricClientList === 'string') {
clientList = tmetricClientList.split(',')
} else {
robot.emit('error', `env. variable HUBOT_TMETRIC_CLIENT_LIST is not set or not valid`)
return
}
let query = {AccountId: tmetricAccountId, StartDate: startDate, ClientList: clientList}
robot.http(`https://app.tmetric.com/api/reports/detailed`)
.query(query)
.headers({Accept: 'application/json', Authorization: `Bearer ${tmetricToken}`})
.get()((err, response, body) => {
let parsedData = {}
let durations = []
let output = {}
output.text = outputPrefix
if (err) {
robot.emit('error', `problem getting t-metric report: '${err}'`)
return
}
try {
parsedData = JSON.parse(body)
} catch (e) {
robot.emit('error', `problem parsing '${body}' as JSON`)
return
}
for (const key in parsedData) {
if (typeof durations[parsedData[key]['user']] === 'undefined') {
durations[parsedData[key]['user']] = 0
}
durations[parsedData[key]['user']] += parseInt(parsedData[key]['duration'])
}
for (const user in durations) {
durations[user] = parseFloat((durations[user] / 3600000).toFixed(2))
}
for (const user in durations) {
output.text += user + ' ' + durations[user] + 'h\n'
}
output.text += outputPostfix
if (res !== null) {
res.send(output.text)
} else {
robot.send({room: room}, output.text)
}
})
}
function getMonday(d) {
d = new Date(d);
var day = d.getDay(),
diff = d.getDate() - day + (day === 0 ? -6 : 1); // adjust when day is sunday
return new Date(d.setDate(diff));
}