move "userdata" views into own database
[mygpo.git] / couchdb / userdata / _design / heatmap / views / by_episode / map.js
blob6717a7c9cdae4787092ecc9275965d756cb559d7
1 function(doc)
3     if(doc.doc_type == "EpisodeUserState")
4     {
5         if(doc.actions == null || doc.actions.length == 0)
6         {
7             return;
8         }
10         function sortByStarted(a, b)
11         {
12             var x = a.started;
13             var y = b.started;
14             return ((x < y) ? -1 : ((x > y) ? 1 : 0));
15         }
17         function hasTimeValues(action)
18         {
19             return ((action != null) && (action.started != null) && (action.playmark != null));
20         }
22         var actions = doc.actions.slice(0); // creates a copy
23         actions = actions.filter(hasTimeValues);
24         actions.sort(sortByStarted);
26         var played_parts = [];
27         var flat_date = null;
29         for(var n in actions)
30         {
31             var action = actions[n];
33             if(flat_date == null)
34             {
35                 flat_date = {start: action.started, end: action.playmark};
36                 played_parts.push(flat_date);
37                 continue;
38             }
40             if(action.started <= flat_date.end && action.playmark >= flat_date.end)
41             {
42                 flat_date.end = action.playmark;
43             }
44             else if(action.started >= flat_date.start && action.playmark <= flat_date.end)
45             {
46                 // part already contained
47                 continue;
48             }
49             else
50             {
51                 flat_date = {start: action.started, end: action.playmark};
52                 played_parts.push(flat_date);
53             }
54         }
56         if (played_parts.length == 0)
57         {
58             return;
59         }
61         var sections = [];
62         for(var n in played_parts)
63         {
64             var part = played_parts[n];
65             sections.push(part.start);
66             sections.push(part.end);
67         }
69         emit([doc.podcast, doc.episode, doc.user], sections);
70     }