@@ -4,20 +4,39 @@ def duration_minutes
44 return ( duration_seconds / 60.0 ) . round
55 end
66
7+ # this function doesnt belong here. where does it belong?
8+ def smooth ( indata , window_size )
9+ window = [ ]
10+ result = [ ]
11+ runningavg = 0
12+ for elm in indata
13+ elm = 0 if elm . nil?
14+ runningavg += elm
15+ window . push ( elm )
16+ if window . length > window_size
17+ removedelm = window . shift
18+ runningavg -= removedelm
19+ end
20+ windowavg = runningavg . to_f / window . length
21+ result . push ( windowavg . round ( 2 ) )
22+ end
23+ return result
24+ end
25+
726 # where does this function belong?
827 #
928 # MR says "[if it] deals directly with data on the model, it goes in the model"
1029 #
1130 # and yet this feels suspiciously like view logic.
1231 #
13- def chart_data ( measure )
32+ def chart_data ( measure , smoothing_window )
1433 entities . collect { |entity |
1534 extracted_measures = [ ]
1635 entity [ "minutes" ] . each { |minute , measures |
17- extracted_measures [ minute . to_i ] = measures [ measure ] . to_f
36+ extracted_measures [ minute . to_i - 1 ] = measures [ measure ] . to_f unless minute == "0"
1837 }
1938 { :name => entity [ "identity" ] [ "name" ] ,
20- :data => extracted_measures ,
39+ :data => smooth ( extracted_measures , smoothing_window ) ,
2140 :color => "#" + entity [ "color" ] ,
2241 }
2342 } . to_json
0 commit comments