diff options
author | Chris Schlaeger <chris@linux.com> | 2014-07-28 22:01:42 +0200 |
---|---|---|
committer | Chris Schlaeger <chris@linux.com> | 2014-07-28 22:01:42 +0200 |
commit | 0e6e0f98cd89f136fdb5f098ca4e829f5f8af97a (patch) | |
tree | 4880291bbb0d359a700f0f118101e9bdf3d88d97 /lib | |
parent | 4f742d6a95c4c936cff891dc5f64c177201a4139 (diff) | |
download | postrunner-0e6e0f98cd89f136fdb5f098ca4e829f5f8af97a.zip |
Adding summary report and more testing
Diffstat (limited to 'lib')
-rw-r--r-- | lib/postrunner/ActivitiesDB.rb | 12 | ||||
-rw-r--r-- | lib/postrunner/Activity.rb | 20 | ||||
-rw-r--r-- | lib/postrunner/ActivityReport.rb | 66 | ||||
-rw-r--r-- | lib/postrunner/FlexiTable.rb | 1 | ||||
-rw-r--r-- | lib/postrunner/Main.rb | 2 |
5 files changed, 91 insertions, 10 deletions
diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index 2bd7093..e0c61e4 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -36,6 +36,13 @@ module PostRunner unless @activities.is_a?(Array) Log.fatal "The archive file '#{@archive_file}' is corrupted" end + + # The reference to this object is needed inside Activity object but is + # not stored in the archive file. We have to retrofit the Activity + # instances with this data. + @activities.each do |a| + a.db = self + end end def add(fit_file) @@ -78,6 +85,11 @@ module PostRunner sync end + def rename(activity, name) + activity.rename(name) + sync + end + def check @activities.each { |a| a.check } end diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb index 838ac8e..aa50789 100644 --- a/lib/postrunner/Activity.rb +++ b/lib/postrunner/Activity.rb @@ -1,10 +1,13 @@ require 'fit4ruby' +require 'postrunner/ActivityReport' + module PostRunner class Activity attr_reader :fit_file, :name + attr_accessor :db # This is a list of variables that provide data from the fit file. To # speed up access to it, we cache the data in the activity database. @@ -28,6 +31,10 @@ module PostRunner Log.info "FIT file #{@fit_file} is OK" end + def dump + load_fit_file + end + def yaml_initialize(tag, value) # Create attr_readers for cached variables. @@CachedVariables.each { |v| self.class.send(:attr_reader, v.to_sym) } @@ -41,7 +48,7 @@ module PostRunner end def encode_with(coder) - attr_ignore = %w( @fit_activity ) + attr_ignore = %w( @db @fit_activity ) instance_variables.each do |v| v = v.to_s @@ -51,14 +58,9 @@ module PostRunner end end - #def method_missing(method_name, *args, &block) - # @fit_activity = load_fit_file unless @fit_activity - # @fit_activity.send(method_name, *args, &block) - #end - - def summary(fit_file) - load_fit_file - + def summary + @fit_activity = load_fit_file unless @fit_activity + puts ActivityReport.new(@fit_activity).to_s end def rename(name) diff --git a/lib/postrunner/ActivityReport.rb b/lib/postrunner/ActivityReport.rb new file mode 100644 index 0000000..971ed3c --- /dev/null +++ b/lib/postrunner/ActivityReport.rb @@ -0,0 +1,66 @@ +require 'fit4ruby' +require 'postrunner/FlexiTable' + +module PostRunner + + class ActivityReport + + include Fit4Ruby::Converters + + def initialize(activity) + @activity = activity + end + + def to_s + session = @activity.sessions[0] + + summary(session) + "\n" + laps + end + + private + + def summary(session) + t = FlexiTable.new + t.enable_frame(false) + t.body + t.row([ 'Date:', session.start_time ]) + t.row([ 'Distance:', "#{'%.2f' % (session.distance / 1000.0)} km" ]) + t.row([ 'Time:', secsToHMS(session.duration) ]) + t.row([ 'Avg. Pace:', + "#{speedToPace(session.avg_speed)} min/km" ]) + t.row([ 'Total Ascend:', "#{session.ascend} m" ]) + t.row([ 'Total Descend:', "#{session.descent} m" ]) + t.row([ 'Calories:', "#{session.calories} m" ]) + t.row([ 'Avg. HR:', "#{session.avg_heart_rate} bpm" ]) + t.row([ 'Max. HR:', "#{session.max_heart_rate} bpm" ]) + t.row([ 'Training Effect:', session.training_effect ]) + t.row([ 'Avg. Run Cadence:', "#{session.avg_running_cadence.round} spm" ]) + t.row([ 'Avg. Vertical Oscillation:', + "#{'%.1f' % (session.avg_vertical_oscillation / 10)} cm" ]) + t.row([ 'Avg. Ground Contact Time:', + "#{session.avg_stance_time.round} ms" ]) + t.row([ 'Avg. Stride Length:', + "#{'%.2f' % (session.avg_stride_length / 2)} m" ]) + + t.to_s + end + + def laps + t = FlexiTable.new + t.head + t.row([ 'Duration', 'Avg. Pace', 'Avg. HR', 'Max. HR' ]) + t.body + @activity.laps.each do |lap| + t.cell(secsToHMS(lap.total_timer_time)) + t.cell(speedToPace(lap.avg_speed)) + t.cell(lap.avg_heart_rate.to_s) + t.cell(lap.max_heart_rate.to_s) + t.new_row + end + t.to_s + end + + end + +end + diff --git a/lib/postrunner/FlexiTable.rb b/lib/postrunner/FlexiTable.rb index 041e414..9655f2e 100644 --- a/lib/postrunner/FlexiTable.rb +++ b/lib/postrunner/FlexiTable.rb @@ -259,6 +259,7 @@ module PostRunner end def frame_line_to_s + return '' unless @enable_frame s = '+' @column_attributes.each do |c| s += '-' * c.min_terminal_width + '+' diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 89823d4..96d6265 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -222,7 +222,7 @@ EOT when :dump activity.dump when :rename - activity.rename(@name) + @activities.rename(activity, @name) when :summary activity.summary else |