'''

1) Create a new collection in the mongo shell:

db.tracking_collection.aggregate([{$match :{$or : [{"event_type" : "seek_video"}, {"event_type" : "play_video"},{"event_type":"pause_video"}]}}, {$out : "video_watch_duration_collection"}])

2) Run: python video_watch_duration.py


desired final output.csv

new row for every unique load_video event for a username

username, video associated with load_video event, chapter, vertical, sequential, watch duration

get the event_types : load_video, play_video, pause_video, seek_video

for each load_video watch duration should include ONLY:

- time between play_video -> another video event (pause_video or seek_video)
- time between seek_video : {'new_time' : Time}  -> pause video (only with new_time > old_time, this is to avoid including rewinds)

watch periods:
    event_type : pause_video - "event_type":"play_video" {"event":{"currentTime":TIME}} = add to watch duration
    if seek_video : {'old_time' : TIME} > seek_video : {'new_time' : TIME} = rewind (exclude from watch duration)
    if seek_video : {'old_time' : TIME} < seek_video : {'new_time' : TIME} 
      "pause_video" {"event":{"currentTime":TIME}} - seek_video : {'new_time': TIME } = add to watch duration

'''

from base_edx import EdXConnection
from generate_csv_report import CSV

connection = EdXConnection('video_watch_duration_collection')
collection = connection.get_access_to_collection()
cursor = collection['video_watch_duration_collection'].find()

result = []

output = CSV(result,['Username',], output_file='video_watch_duration.csv', row_limit=200000) 
output.generate_csv()

@@ -264,24 +264,24 @@
 o_watch_
-duration
+segments
 .py%0A%0A%0Ade
@@ -443,30 +443,36 @@
 ential, 
-watch duration
+video watch segments
 %0A%0Aget th
@@ -556,30 +556,39 @@
 d_video 
-watch duration
+new video watch segment
  should 
@@ -921,143 +921,31 @@
 %7D = 
-add to watch duration%0A    if seek_video : %7B'old_time' : TIME%7D %3E seek_video : %7B'new_time' : TIME%7D = rewind (exclude from watch duration)
+new video watch segment
 %0A   
@@ -1109,30 +1109,146 @@
 %7D = 
-add to watch duration%0A
+new video watch segment%0A%0A    if seek_video : %7B'old_time' : TIME%7D %3E seek_video : %7B'new_time' : TIME%7D = rewind (exclude from watch segments)
 %0A'''

Update video_watch_duration.py