-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.rb
386 lines (291 loc) · 9.9 KB
/
cron.rb
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
#! /usr/bin/env ruby
#
# This file is to be run via crontab
#
#
# What is cached
# Three requests
# 1. spritRotut by tripId
# 2. tutorTrips by year+month
# 3. tutorTrips by workflowId
# The union of the latter two will give a list of
require 'base64'
require_relative 'settings.rb'
require_relative 'helpers/Couch'
require_relative 'helpers/CouchIterator'
require_relative 'utilities/cloneDeep'
require_relative 'utilities/countyTranslate'
require_relative 'utilities/pushUniq'
require_relative 'utilities/timestamp'
require_relative 'utilities/zoneTranslate'
require_relative 'cronSupport/ntpReports'
header = <<END
Brockman presents
| ,---. |
,---.,---.,---.|---.,---. ,---.,---.|__. ,---.,---.,---.|---.
| ,---|| | ||---' | |---'| | |---'`---.| |
`---'`---^`---'` '`---' ` `---'` ` `---'`---'` '
END
puts header
groups = []
groups.push({ 'db' => 'group-national_tablet_program', 'helper' => NtpReports, 'startYear' => 2018, 'endYear' => 2018 })
#
# Time Variables for tracking processing time
#
cronStart = Time.now()
dbStart = nil
workflowStart = nil
taskStart = nil
subTaskStart = nil
CHUNK_SIZE = 1000
groups.each { |group|
#
# Prep for preprocessing the group
#
# Determine DB and init Couch Connection
db = group["db"] || ""
helper = group["helper"] || nil
puts "\nStarting DB: #{db}"
dbStart = Time.now()
couch = Couch.new({
:host => $settings[:dbHost],
:login => $settings[:login],
:designDoc => $settings[:designDoc],
:db => db
})
#
# get Group settings - for time zone calculation
#
groupSettings = couch.getRequest({ :doc => 'settings', :parseJson => true })
groupTimeZone = groupSettings['timeZone']
#
# get report aggregate settings
#
begin
reportSettings = couch.getRequest({ :doc => "report-aggregate-settings", :parseJson => true })
rescue => e
# the doc doesn't already exist
reportSettings ||= {}
reportSettings['fluency'] ||= {}
reportSettings['fluency']['subjects'] ||= []
end
# Determine if there is a helper class and init it
if group["helper"]
helper = group["helper"].new(:couch => couch, :timezone => groupTimeZone, :reportSettings => reportSettings)
end
#
# Identify Workflows to Process
#
puts "\n- Retrieving Workflows: "
taskStart = Time.now()
workflowsRequest = couch.postRequest({
:view => "byCollection",
:params => {
"reduce" => false,
"include_docs" => true
},
:data => {"keys" => ["workflow"]},
:parseJson => true
})
workflows = {}
workflowsRequest['rows'].each{ |e|
workflows[e['doc']['_id']] = e['doc']
}
puts " #{workflows.length} Workflows Retrieved - (#{time_diff(Time.now(), taskStart)})"
workflowIds = []
workflows.each{ |workflowId, workflow|
if workflow['reporting'].nil?
next
end
if not workflow['reporting']['preProcess']
next
end
workflowIds.push workflowId
}
#puts "\n- Caching Workflow Trips:"
#taskStart = Time.now()
# workflowIds = []
# workflows.each { |workflow|
# workflowId = workflow['_id'] || ""
# workflowName = workflow['name'] || ""
# puts "\n Workflow -> #{workflowName}"
# puts " Id: #{workflowId}"
# subTaskStart = Time.now()
# if not workflow['reporting']['preProcess']
# puts " [SKIP] Pre-Processing Disabled - (#{time_diff(Time.now(), subTaskStart)})"
# next
# end
# add workflow ID to the list of those processed
# workflowIds.push workflowId
# tripsRequest = JSON.parse(couch.postRequest({
# :view => "tutorTrips",
# :params => {"reduce" => false},
# :data => {"keys" => ["workflow-#{workflowId}"]}
# }))
# hTripIds = {}
# tripsRequest['rows'].each { |row| hTripIds[row['value']] = true }
# aTripIds = hTripIds.keys
# totalTripIds = aTripIds.length
# puts " # Trips: #{totalTripIds}"
# print " "
# (0..totalTripIds).step(CHUNK_SIZE).each { | chunkIndex |
# idChunk = aTripIds.slice(chunkIndex, chunkIndex + CHUNK_SIZE)
# couch.postRequest({
# :view => "spirtRotut",
# :params => { "group" => true },
# :cache => true,
# :data => { "keys" => idChunk }
# })
# print "*"
# }
# puts "\n [COMPLETE] Workflow Processed - (#{time_diff(Time.now(), subTaskStart)})"
# }
puts "\n [COMPLETE] Caching Workflow Trips - (#{time_diff(Time.now(), taskStart)})"
#
#
# => BEGIN Pre-processing data for reports
#
#
#
# Process locations and setup data structure
#
templates ||= {}
templates['result'] ||= {}
templates['geoJSON'] ||= {}
templates['locationBySchool'] ||= {}
templates['users'] ||= {}
puts "\n- Processing Locations"
taskStart = Time.now()
templates = helper.processLocations(templates) if helper
puts " [COMPLETE] Processing Schools - (#{time_diff(Time.now(), taskStart)})"
#
# Retrieve and Filter All Users
#
puts "\n- Processing Users"
taskStart = Time.now()
templates = helper.processUsers(templates) if helper
puts " [COMPLETE] Processing Users - (#{time_diff(Time.now(), taskStart)})"
#
# Processing Trips By Month
#
puts "\n- Processing Tutor Trips By Month"
taskStart = Time.now()
(group["startYear"]..group["endYear"]).each { |year|
#(1..12).each { |month|
(1..3).each { |month|
helper.resetSkippedCount() if helper
puts " * #{month}/#{year}"
subTaskStart = Time.now()
aggregateDocId = "report-aggregate-year#{year}month#{month}"
aggregateGeoDocId = "report-aggregate-geo-year#{year}month#{month}"
#duplicate the resultTemplate to store this months data
monthData = {}
monthData['result'] = cloneDeep(templates['result'])
monthData['geoJSON'] = cloneDeep(templates['geoJSON'])
# Check to see if the aggregate doc already exists - need for doc update
begin
aggDoc = couch.getRequest({
:doc => "#{aggregateDocId}",
:parseJson => true
})
rescue => e
# the doc doesn't already exist
aggDoc = {}
end
if aggDoc.has_key?('_rev')
monthData['result']['_rev'] = aggDoc['_rev']
end
# Check to see if the aggregate geo doc already exists for each county - needed for doc update
monthData['geoJSON']['byCounty'].map { | countyId, county |
begin
aggGeoDoc = couch.getRequest({
:doc => "#{aggregateGeoDocId}-#{countyId}",
:parseJson => true
})
rescue => e
# the doc doesn't already exist
aggGeoDoc = {}
end
if aggGeoDoc.has_key?('_rev')
monthData['geoJSON']['byCounty'][countyId]['_rev'] = aggGeoDoc['_rev']
end
}
workflowIds.each{ |workflowId|
puts " Workflow - #{workflowId}"
monthKeys = ["year#{year}month#{month}workflowId#{workflowId}"]
tripsFromMonth = couch.postRequest({
:view => "tutorTrips",
:data => { "keys" => monthKeys },
:params => { "reduce" => false },
:categoryCache => true,
:parseJson => true
})
tripIds = tripsFromMonth['rows'].map{ |e| e['value'] }
# puts "trips #{tripIds}"
# remove duplicates
tripKeys = tripIds.uniq
puts " # Trips: #{tripKeys.size}"
# break trip keys into chunks
tripKeyChunks = tripKeys.each_slice(CHUNK_SIZE).to_a
# hash for optimization
subjectsExists = {}
zoneCountyExists = {
'all' => {}
}
#
# Get chunks of trips and work on the result
#
#print " Filtering Valid Visits... "
tripKeyChunks.each { | tripKeys |
# get the real data
tripsResponse = couch.postRequest({
:view => "spirtRotut",
:params => { "group" => true },
:data => { "keys" => tripKeys },
:parseJson => true,
:cache => true
} )
tripRows = tripsResponse['rows']
#puts "Processing Chunk: #{tripRows.length}"
# Process each Trip result record in chunk
for trip in tripRows
helper.processTrip(trip, monthData, templates, workflows) if helper
#staff trips
helper.processStaffTrip(trip, monthData, templates, workflows) if helper
end
}
}
#post processing
puts "Processing Compensation"
helper.postProcessTrips(monthData, templates) if helper
#end
puts " # Skipped: #{helper.getSkippedCount()}"
#
# Saving the generated result back to the server
puts "Putting result Doc: #{aggregateDocId}"
couch.putRequest({
:doc => "#{aggregateDocId}",
:data => monthData['result']
})
monthData['geoJSON']['byCounty'].map { | countyId, countyData |
#Saving the generated Geo result back to the server
couch.putRequest({
:doc => "#{aggregateGeoDocId}-#{countyId}",
:data => countyData
})
}
puts " Month Completed - (#{time_diff(Time.now(), subTaskStart)})"
}
}
puts "\n Processing Tutor Trips by Month Completed - (#{time_diff(Time.now(), taskStart)})"
# ensure that the lisyt of subjects doens't contain duplicates
#reportSettings['fluency']['subjects'] = reportSettings['fluency']['subjects'].uniq
#
# Saving the aggregate report settings in case of modification
# couch.putRequest({
# :doc => "report-aggregate-settings",
# :data => reportSettings
# })
puts "\n DB Processing Completed - (#{time_diff(Time.now, dbStart)})"
}
puts "\nCron Job Completed - (#{time_diff(Time.now(), cronStart)})"