Module: Fit4Ruby::RecordAggregator

Included in:
HeartRateZones, Lap, Length, Session
Defined in:
lib/fit4ruby/RecordAggregator.rb

Instance Method Summary collapse

Instance Method Details

#aggregateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fit4ruby/RecordAggregator.rb', line 17

def aggregate
  return if @records.empty?

  # TODO: Add support for pause events.
  @total_timer_time = @total_elapsed_time

  aggregate_geo_region
  aggregate_ascent_descent
  aggregate_speed_distance
  aggregate_heart_rate
  aggregate_strides
  aggregate_vertical_oscillation
  aggregate_stance_time
end

#aggregate_ascent_descentObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fit4ruby/RecordAggregator.rb', line 61

def aggregate_ascent_descent
  @total_ascent = @total_descent = 0
  altitude = nil

  @records.each do |r|
    if altitude
      if r.altitude < altitude
        @total_descent += (altitude - r.altitude)
      else
        @total_ascent += (r.altitude - altitude)
      end
    end
    altitude = r.altitude
  end
end

#aggregate_geo_regionObject



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
# File 'lib/fit4ruby/RecordAggregator.rb', line 32

def aggregate_geo_region
  r = @records.first
  @start_position_lat = r.position_lat
  @start_position_long = r.position_long

  r = @records.last
  @end_position_lat = r.position_lat
  @end_position_long = r.position_long

  @records.each do |r|
    if r.position_lat
      if (@swc_lat.nil? || r.position_lat < @swc_lat)
        @swc_lat = r.position_lat
      end
      if (@nec_lat.nil? || r.position_lat > @nec_lat)
        @nec_lat = r.position_lat
      end
    end
    if r.position_long
      if (@swc_long.nil? || r.position_long < @swc_long)
        @swc_long = r.position_long
      end
      if (@nec_long.nil? || r.position_long > @nec_long)
        @nec_long = r.position_long
      end
    end
  end
end

#aggregate_heart_rateObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fit4ruby/RecordAggregator.rb', line 93

def aggregate_heart_rate
  total_heart_beats = 0
  @max_heart_rate = 0
  last_timestamp = @start_time

  @records.each do |r|
    if r.heart_rate
      delta_t = last_timestamp ? r.timestamp - last_timestamp : nil
      total_heart_beats += (r.heart_rate / 60.0) * delta_t if delta_t
      if r.heart_rate > @max_heart_rate
        @max_heart_rate = r.heart_rate
      end
    end
    last_timestamp = r.timestamp
  end
  @avg_heart_rate = (total_heart_beats.to_f / @total_elapsed_time * 60).to_i
end

#aggregate_speed_distanceObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fit4ruby/RecordAggregator.rb', line 77

def aggregate_speed_distance
  @max_speed = 0
  first_distance, last_distance = nil, nil

  @records.each do |r|
    @max_speed = r.speed if r.speed && @max_speed < r.speed

    first_distance = r.distance if first_distance.nil? && r.distance
    last_distance = r.distance if r.distance
  end

  @total_distance = last_distance && first_distance ?
    last_distance - first_distance : 0
  @avg_speed = @total_distance.to_f / @total_elapsed_time
end

#aggregate_stance_timeObject



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fit4ruby/RecordAggregator.rb', line 148

def aggregate_stance_time
  total_stance_time = 0.0
  total_stance_time_percent = 0.0
  stance_time_count = 0
  stance_time_percent_count = 0

  @records.each do |r|
    if r.stance_time
      total_stance_time += r.stance_time
      stance_time_count += 1
    end
    if r.stance_time_percent
      total_stance_time_percent += r.stance_time_percent
      stance_time_percent_count += 1
    end
  end

  @avg_stance_time = stance_time_count > 0 ?
    total_stance_time / stance_time_count : 0
  @avg_stance_time_percent = stance_time_percent_count > 0 ?
    total_stance_time_percent / stance_time_percent_count : 0
end

#aggregate_stridesObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fit4ruby/RecordAggregator.rb', line 111

def aggregate_strides
  @total_strides = 0
  @max_running_cadence = 0
  last_timestamp = @start_time

  @records.each do |r|
    delta_t = last_timestamp ? r.timestamp - last_timestamp : nil

    if delta_t && (run_cadence = r.run_cadence)
      @total_strides += (run_cadence / 60.0) * delta_t
    end
    if run_cadence && run_cadence > @max_running_cadence
      @max_running_cadence = run_cadence.to_i
    end

    last_timestamp = r.timestamp
  end
  @avg_running_cadence, @avg_fractional_cadence =
    (@total_strides.to_f / @total_timer_time * (60 / 2)).divmod(1)
  @total_strides = @total_strides.to_i
end

#aggregate_vertical_oscillationObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/fit4ruby/RecordAggregator.rb', line 133

def aggregate_vertical_oscillation
  total_vertical_oscillation = 0.0
  vertical_oscillation_count = 0

  @records.each do |r|
    if r.vertical_oscillation
      total_vertical_oscillation += r.vertical_oscillation
      vertical_oscillation_count += 1
    end
  end

  @avg_vertical_oscillation = total_vertical_oscillation /
    vertical_oscillation_count
end