-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall.coffee
242 lines (175 loc) · 5.87 KB
/
all.coffee
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
command: 'bash capstone.widget/get-pages.sh'
refreshFrequency: 900000
style: '''
top: 120px
left: 3px
column-padding = 0.85em
column-border = solid 1px rgba(black, 0.15)
-webkit-font-smoothing: antialiased
font-family: Source Code Pro
font-size: 10px
line-height: 1
box-shadow: 0 0 10px rgba(black, 0.25)
padding: column-padding
background-color: rgba(white, 0.65)
-webkit-backdrop-filter: blur(10px) saturate(150%)
table
border-collapse: collapse
tbody tr:nth-child(odd)
background-color: rgba(white, 0.35)
th
border-bottom: column-border
font-weight: normal
td, th
padding-left: column-padding
padding-right: column-padding
border-right: column-border
td:last-child, th:last-child
border-right: none
.block
padding: 4px 6px
color: rgba(black, 0.85)
.active
font-weight: bold
.active strong
font-weight: normal
.danger
color: rgba(red, 0.85)
.name
text-align: right
.data
text-align: center
'''
parseTime: (timestring) ->
negative = timestring && timestring[0] == '-'
hours = /(\d+)h/.exec(timestring)
hours = if hours then Number(hours[1]) else 0
minutes = /(\d+)m/.exec(timestring)
minutes = if minutes then Number(minutes[1]) else 0
total = minutes + (hours * 60)
if negative
total *= -1
return total
prettifyMinutes: (minutes) ->
negative = minutes < 0
hourSlot = if negative then Math.ceil(minutes / 60) else Math.floor(minutes / 60)
minuteSlot = minutes % 60
result = if hourSlot then "#{hourSlot}h #{minuteSlot}m" else "#{minuteSlot}m"
return result.trim()
sumListOfTimestrings: (timestrings) ->
minutes = 0
for amount in (@parseTime(timestring) for timestring in timestrings)
minutes += amount
return minutes
getHoursWorked: (html) ->
table = html.querySelectorAll('.table')[1]
cells = table.querySelectorAll('tbody tr td:last-child')
timestrings = (cell.textContent for cell in cells)
minutes = @sumListOfTimestrings(timestrings)
return @prettifyMinutes(minutes)
getMarginRaw: (html) ->
timestring = html.querySelector('h5 > span').textContent
return @parseTime(timestring)
getMargin: (html) ->
margin = @getMarginRaw(html)
return "<span class='#{if margin < 0 then 'danger' else ''}'>#{@prettifyMinutes(margin)}</span>"
getWorkedThisWeekRaw: (html) ->
table = html.querySelectorAll('.table')[1]
cells = Array.prototype.slice.call(table.querySelectorAll('tbody tr:last-child td'))[1...-1]
timestrings = (cell.innerText.split('\n')[0] for cell in cells)
minutes = @sumListOfTimestrings(timestrings)
return minutes
getWorkedThisWeek: (html) ->
return @prettifyMinutes(@getWorkedThisWeekRaw(html))
getTimeNeededForQuota: (html) ->
quota = 77 * 7
workedThisWeek = @getWorkedThisWeekRaw(html)
margin = @getMarginRaw(html)
remainingTime = quota - workedThisWeek - margin
if (remainingTime < 0) then (remainingTime = 0)
return @prettifyMinutes(remainingTime)
isTimerActive: (html) ->
cell = html.querySelector('.table tbody tr:first-child td:first-child')
inProgressRegex = /in progress/i
isInProgress = inProgressRegex.test(cell.textContent)
return isInProgress
getTimerTimeRaw: (html) ->
since = html.querySelector('.table tbody tr:first-child td:first-child').innerText.split('\n')[1]
# Dec 09 06:03PM
regex = /([a-z]{3}) +(\d{2}) +(\d{2}):(\d{2})(AM|PM)/i
[_, month, day, hour, minute, ampm] = since.match(regex)
date = new Date()
date.setDate(Number(day))
date.setHours(if ampm == 'PM' and hour != '12' then Number(hour) + 12 else Number(hour))
date.setMinutes(Number(minute))
return date
getTimerTimeWellDone: (html) ->
time = @getTimerTimeRaw(html)
now = Date.now()
diff = time - now
minutes = Math.round((diff / 1000 / 60) * -1)
return minutes
getTimerTime: (html) ->
return @prettifyMinutes(@getTimerTimeWellDone(html))
countBadGradeDays: (html) ->
return html.querySelectorAll('.table .danger').length
calculateGrade: (html) ->
numberDown = @countBadGradeDays(html)
start = 100
per = 1/15
step = 10
return Number(start - (per * numberDown * step)).toFixed(2)
letterGrade: (gradeString) ->
grade = Number(gradeString)
if grade < 60
return 'F'
if grade < 70
return 'D'
if grade < 80
return 'C'
if grade < 90
return 'B'
if grade <= 100
return 'A'
getDataForWidget: (name, htmlText) ->
parser = new DOMParser()
html = parser.parseFromString(htmlText, 'text/html')
isInProgress = @isTimerActive(html)
grade = @calculateGrade(html)
return {
isActive: isInProgress,
timer: @getTimerTime(html),
name: name,
hoursWorked: @getHoursWorked(html),
margin: @getMargin(html),
workedThisWeek: @getWorkedThisWeek(html),
getTimeNeededForQuota: @getTimeNeededForQuota(html),
badGradeDays: @countBadGradeDays(html),
maxGrade: grade,
letterGrade: @letterGrade(grade)
}
makeRowFromData: (info) ->
classname = if info.isActive then 'active' else ''
timer = if info.isActive then "今 #{info.timer}" else ''
content = [
"<td class='name'>#{info.name}</td>",
"<td class='data'>#{info.hoursWorked} #{timer}</td>",
"<td class='data'>#{info.margin}</td>",
"<td class='data'>#{info.workedThisWeek}</td>",
"<td class='data'>#{info.getTimeNeededForQuota}</td>",
"<td class='data'>#{info.badGradeDays} days</td>",
"<td class='data'>#{info.maxGrade}%</td>",
"<td class='data'>#{info.letterGrade}</td>"
].join('')
return "<tr class='block #{classname}'>#{content}</tr>"
render: (result) ->
text = result.split(/__USER:/)
text = ([t.match(/\w+/)[0], t] for t in text when t)
# sortBy = 'badGradeDays'
sortBy = 'name'
infos = (@getDataForWidget(n, t) for [n, t] in text)
infos.sort(`function(a, b){if (a[sortBy] < b[sortBy]) return -1; if (a[sortBy] > b[sortBy]) return 1; return 0}`)
widgets = infos.map(@makeRowFromData)
columns = ["Name", "Total", "Margin", "Week", "Pre-Quota", "日 < Quota", "Max Grade", ""]
headings = "<tr>#{("<th>#{text}</th>" for text in columns).join('')}"
return "<table><thead>#{headings}</thead><tbody>#{widgets.join('\n')}</tbody></table>"