@@ -4,20 +4,39 @@ def duration_minutes
4
4
return ( duration_seconds / 60.0 ) . round
5
5
end
6
6
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
+
7
26
# where does this function belong?
8
27
#
9
28
# MR says "[if it] deals directly with data on the model, it goes in the model"
10
29
#
11
30
# and yet this feels suspiciously like view logic.
12
31
#
13
- def chart_data ( measure )
32
+ def chart_data ( measure , smoothing_window )
14
33
entities . collect { |entity |
15
34
extracted_measures = [ ]
16
35
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"
18
37
}
19
38
{ :name => entity [ "identity" ] [ "name" ] ,
20
- :data => extracted_measures ,
39
+ :data => smooth ( extracted_measures , smoothing_window ) ,
21
40
:color => "#" + entity [ "color" ] ,
22
41
}
23
42
} . to_json
0 commit comments