-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassessment_time.rb
More file actions
767 lines (611 loc) · 15.9 KB
/
assessment_time.rb
File metadata and controls
767 lines (611 loc) · 15.9 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
require 'csv'
require 'date'
require 'byebug'
# The number of minutes we can accept for a single student assessment
THRESHOLD = 15
class User
def initialize
@username = nil
@name = nil
@role = nil
@id = nil
end
def id
@id
end
def id= value
@id = value
end
def username
@username
end
def username= value
@username = value
end
def name
@name
end
def name= value
@name = value
end
def role
@role
end
def role= value
@role = value
end
end
class TaskDefinition
@id
def id
@id
end
def id= value
@id = value
end
end
class Task
def initialize
@task_def = nil
@id = nil
@project = nil
@activity = []
end
def task_def
@task_def
end
def task_def= value
@task_def = value
end
def id
@id
end
def id= value
@id = value
end
def project
@project
end
def project= value
@project = value
@project.add_task self
end
def activity
@activity
end
def add_activity value
@activity << value
end
def last_put_activity
@activity.length.times do |i|
result = @activity[@activity.length - i - 1]
return result if result.action == 'PUT'
end
nil
end
end
class Tutorial
@unit = nil
@tutor = nil
@code = nil
@student_count = 0
def initialize unit, tutor, code, count
@unit = unit
@tutor = tutor
@code = code
@student_count = count
end
def tutor
@tutor
end
def code
@code
end
def student_count
@student_count
end
end
class Unit
@id = nil
@name = nil
@projects = []
@code = nil
@student_count = 0
@task_defs = {}
@sessions = []
@markers = {}
@tutorials = []
def initialize
@projects = []
@sessions = []
@task_defs = {}
@markers = {}
@tutorials = []
end
def name
@name
end
def name= value
@name = value
end
def id
@id
end
def id= value
@id = value
end
def code
@code
end
def code= value
@code = value
end
def student_count
@student_count
end
def student_count= value
@student_count = value.to_i
end
def add_project project
@projects << project
end
def add_tutorial tutorial
@tutorials << tutorial
end
def task_def_for_id task_id
return @task_defs[task_id] if @task_defs.has_key? task_id
result = TaskDefinition.new
result.id = task_id
@task_defs[task_id] = result
result
end
def add_session session
@sessions << session
end
def report_summary(stream)
return if @sessions.length == 0
# puts "Sessions #{@name} length: #{@sessions.length}"
total_duration = @sessions.inject(0){|sum,e| sum + e.duration }
total_assessments = @sessions.inject(0){|sum,e| sum + e.number_assessments }
stream.write "#{@id},all,#{@code},\"#{@name}\",#{student_count},#{total_duration},#{total_assessments},#{ (total_duration / 60.0) / student_count}\n"
@sessions.group_by(&:marker).each do |k,v|
# Get tutorials for marker
marker_tutorials = @tutorials.select{|t| t.tutor == k }
total_students = marker_tutorials.inject(0){|sum,e| sum + e.student_count } unless marker_tutorials.length == 0
total_students = 1 if total_students.nil? || total_students <= 0
marker_duration = v.inject(0){|sum,e| sum + e.duration }
marker_assessments = v.inject(0){|sum,e| sum + e.number_assessments }
tutor_name = k.nil? ? "none" : k.name
stream.write "#{@id},#{tutor_name},#{@code},\"#{@name}\",#{total_students},#{marker_duration},#{marker_assessments},#{ (marker_duration / 60.0) / total_students}\n"
end
end
def report_session_details stream
return if @sessions.length == 0
stream.write("\nDetails for #{@code} #{@name} (#{@id})\n")
@sessions.group_by(&:marker).each do |k,v|
tutor_name = k.nil? ? "none" : k.name
stream.write("activity for #{tutor_name} in #{@code} #{@name}\n")
v.each do |session|
stream.write(" #{session.start_time} - #{session.end_time} (#{session.duration} minutes)\n")
session.activity_log.each do |activity|
stream.write(" #{activity.action} #{activity.task_def&.id} #{activity.project&.id} #{activity.time_stamp}\n")
end
end
end
end
end
class MarkingSession
@start_time = nil
@marker = nil
@unit = nil
@end_time = nil
@number_assessments = 0
@duration = nil
@activity_log = []
def initialize
@number_assessments = 0
@duration = 0
end
def number_assessments
@number_assessments
end
def marker
@marker
end
def duration
@duration
end
def activity_log
@activity_log
end
def start_time
@start_time
end
def end_time
@end_time
end
def set_details start_time, marker, unit, end_time, num_assessments, duration, activity_log
@start_time = start_time
@marker = marker
@unit = unit
@end_time = end_time
@number_assessments = num_assessments
@duration = duration
@activity_log = activity_log
@unit.add_session self
end
end
class IPAddressActivity
def initialize
@ip_address = nil
@activity = []
end
def add_activity value
@activity << value
end
def record_end_session start_time, marker, unit, end_time, num_assessments, activity_log
name = marker.name unless marker.nil?
duration = ((end_time - start_time) * 24 * 60).to_i
return if duration == 0
session = MarkingSession.new
session.set_details start_time, marker, unit, end_time, num_assessments, duration, activity_log
# puts "#{unit.id},#{unit.code},#{name},#{start_time},#{end_time},#{num_assessments},#{duration}" # if !marker.nil? && marker.id == "13883"
end
def record_assessment_activity_to_tasks
post_inbox = false
marker = nil
session_start = nil
unit = nil
prev = nil
num_assessments = 0
activity_log = []
# puts "-----------"
@activity.each do |activity|
session_start = activity.time_stamp if session_start.nil?
# look for end conditions first...
# is after inbox.... and either timeout, or assessed by different user, or change of unit
if post_inbox && (
(prev != nil && activity.time_diff(prev) > THRESHOLD) ||
(activity.action == "assessing" && (activity.user == activity.project.user || marker != nil && activity.user != marker)) ||
(activity.project != nil && activity.project.unit != unit) )
# end the session with what we have already recorded
# puts "NEW SESSION - #{num_assessments}"
record_end_session session_start, marker, unit, prev.time_stamp, num_assessments, activity_log
activity_log = []
post_inbox = false
marker = nil
session_start = nil
unit = nil
prev = nil
num_assessments = 0
end
activity_log << activity
# Now read this activity...
unless post_inbox # if it is not post inbox - we are looking for a start
# puts activity.action
# Looking for start or marker activity on this IP address
if activity.action == 'inbox'
# inbox request marks start... we wont know who yet
post_inbox = true
session_start = activity.time_stamp
unit = activity.unit
prev = activity
elsif (activity.action == "assessing") && activity.user != activity.project.user
# marker updated task... lets mark start of session here...
# TODO: Look back for start of session?
post_inbox = true
session_start = activity.time_stamp
unit = activity.project.unit
prev = activity
marker = activity.user
num_assessments = 1 # this was an assessment
end
else # this is part of the session...
# within marking timeframe... assume this is part of the sequence
prev = activity
# puts "#{activity.action} ... #{activity.action == "assessing"}"
if activity.action == "assessing"
# puts "IN ASSESSING"
# ASAP get the details of the tutor - from the first assessment post inbox
if marker.nil? # cant be user as that would end the post inbox
marker = activity.user
end
# we assessed a task -- count this
num_assessments += 1
# puts num_assessments
end
end
end
# All activities done... do we need to record end of this session?
if post_inbox
# puts "POST INBOX - #{num_assessments}"
record_end_session session_start, marker, unit, prev.time_stamp, num_assessments, activity_log
end
end
def activity
@activity
end
end
class AssessmentActivity
def initialize
@action = nil
@project = nil
@unit = nil
@user = nil
@time_stamp = nil
@ip_address = nil
@task_def = nil
@ip_self_idx = 0
@project_self_idx = 0
end
def task_def
@task_def
end
def task_def= value
@task_def = value
end
def unit
@unit
end
def unit= value
@unit = value
end
def project
@project
end
def project= value
@project = value
end
def action
@action
end
def action= value
@action = value
end
def time_stamp
@time_stamp
end
def time_stamp= value
@time_stamp = value
end
def user
@user
end
def user= value
@user = value
end
# def project_self_idx
# @project_self_idx
# end
# def project_self_idx= value
# @project_self_idx = value
# end
def ip_address
@ip_address
end
def ip_address= value
@ip_address = value
end
def load csv, projects, tasks, units, ip_addresses, users
puts csv.inspect
@action = csv[0]
if @action =~ /GET|PUT/
if csv[2] == 'inbox'
@unit = units[csv[1]]
@action = 'inbox'
throw csv if @unit.nil?
else
@project = projects[csv[1]]
@task_def = @project.unit.task_def_for_id csv[2]
end
@ip_address = csv[3]
@time_stamp = DateTime.parse csv[4]
elsif @action == 'assessing'
# byebug
task = tasks[csv[2]] # get task from id
return if task.nil? # Some old tasks deleted - cannot find matching data
@user = users[csv[1]]
@project = task.project
@task_def = task.task_def
last_put = task.last_put_activity
if last_put.nil?
puts "no last put - #{csv.inspect}"
@ip_address = ''
@time_stamp = ''
else
@ip_address = last_put.ip_address
@time_stamp = last_put.time_stamp
end
end
unless @project.nil?
@project_self_idx = @project.add_activity(self)
unless @task_def.nil?
# record action against task - enable find activity for task
td = @project.task_for_task_def(@task_def.id)
td.add_activity(self) unless td.nil? # Some old tasks deleted - link lost
end
end
ip_addresses[@ip_address] = IPAddressActivity.new unless ip_addresses.has_key? @ip_address
ip_addresses[@ip_address].add_activity(self)
@ip_self_idx = ip_addresses[@ip_address].activity.length - 1
end
def time_diff other
((@time_stamp - other.time_stamp) * 24 * 60).to_i
end
# def elapsed_minutes
# return 0 if @project_self_idx == 0 # no time for first activity
# prev_activity = @project.next_activity self, -1
# tmp = time_diff prev_activity
# return 0 unless tmp < THRESHOLD
# tmp
# end
end
class Project
def initialize
@id = nil
@unit = nil
@activity = {}
@user = nil
@tasks = {}
end
def unit
@unit
end
def unit=value
@unit = value
@unit.add_project(self)
end
def user
@user
end
def user=value
@user = value
end
def id
@id
end
def id=value
@id = value
end
def tasks
@tasks
end
def tasks=value
@tasks = value
end
def add_task value
@tasks[value.task_def.id] = value
end
def task_for_task_def task_def_id
@tasks[task_def_id]
end
def add_activity value
@activity[value.ip_address] = {} unless @activity.has_key? value.ip_address
@activity[value.ip_address][value.task_def.id] = [] unless @activity.has_key? value.task_def.id
@activity[value.ip_address][value.task_def.id] << value
@activity[value.ip_address][value.task_def.id].length - 1 # return index of assessment activity
end
# Return the activity relative to a given activity (based on offset)
def next_activity value, offset
arr = @activity[value.ip_address][value.task_def.id]
arr_idx = value.project_self_idx + offset
# Check valid index
return nil if arr_idx < 0 || arr_idx >= arr.length
# Return relative value
arr[arr_idx]
end
end
class LogFile
def initialize
@users_by_id = {}
@users_by_username = {}
@units = {}
@projects = {}
@ip_addresses = {}
end
def load_users
puts 'loading users'
CSV.foreach('users.csv', :headers => true) do |csv|
result = User.new()
result.id = csv[0]
result.username = csv[1]
result.name = csv[2]
result.role = csv[3]
@users_by_id[csv[0]] = result
@users_by_username[csv[1]] = result
# puts "Add user #{result.id} = #{result.name} #{result.role}"
end
end
def load_units
puts 'loading units'
CSV.foreach('units.csv', :headers => true) do |csv|
result = Unit.new()
result.id = csv[0]
result.name = csv[1]
result.code = csv[2]
result.student_count = csv[3]
@units[csv[0]] = result
# puts "Add unit #{result.id} = #{result.name}"
end
end
def load_tutorials
puts 'loading tutorials'
CSV.foreach('tutorials.csv', :headers => true) do |csv|
unit = @units[csv[0]]
tutor = @users_by_id[csv[1]]
result = Tutorial.new(unit, tutor, csv[2], csv[3].to_i)
unit.add_tutorial(result)
# puts "Add tutorial #{result.code} = #{result.tutor.name} #{result.student_count}"
end
end
def load_projects
puts 'loading projects'
CSV.foreach('projects.csv', :headers => true) do |csv|
result = Project.new()
result.id = csv[0]
result.unit = @units[csv[1]]
result.user = @users_by_id[csv[2]]
@projects[csv[0]] = result
# puts csv.inspect
# puts result.inspect
# puts "Add project #{result.id} to #{result.unit.code} for #{result.user.name}"
end
end
def load_tasks
puts 'loading tasks'
@tasks = {}
CSV.foreach('tasks.csv', :headers => true) do |csv|
project = @projects[csv[1]]
result = Task.new()
result.id = csv[0]
result.task_def = project.unit.task_def_for_id csv[2]
result.project = project # must be after loading task def as this links them together
@tasks[result.id] = result
# @tasks[csv[0]] = result
# puts "Add task #{result.id} to #{result.project.unit.code} for #{result.project.user.name}"
end
end
def process file_name
CSV.foreach(file_name) do |csv|
result = AssessmentActivity.new
result.load csv, @projects, @tasks, @units, @ip_addresses, @users_by_username
end
end
def report_time
# Build the time data from each ip address
@ip_addresses.values.each do |ip_addr|
ip_addr.record_assessment_activity_to_tasks
end
# Report unit times
date = DateTime.now
puts
puts "activity summay"
File.open("activity_summary_#{date}.log", "w") do |stream|
stream.write "id,assessor,unit code,unit name,students,minutes,assessments,hours/student\n"
@units.values.each do |unit|
print "."
unit.report_summary(stream)
end
puts " done"
end
puts
puts "activity details"
File.open("activity_details_#{date}.log", "w") do |stream|
# Report activity details
@units.values.each do |unit|
print "."
unit.report_session_details(stream)
end
puts " done"
end
end
end
# byebug
log = LogFile.new()
log.load_users()
log.load_units()
log.load_projects()
log.load_tasks()
log.load_tutorials()
log.process('ontrack-time-data-3.csv')
log.report_time()