diff options
author | Chris Schlaeger <chris@linux.com> | 2014-08-02 22:32:04 +0200 |
---|---|---|
committer | Chris Schlaeger <chris@linux.com> | 2014-08-02 22:32:04 +0200 |
commit | b9eaf5d4bc366e2f13921bac49017801ce4d6449 (patch) | |
tree | 3d16b29f63c14d8def9f0da9706081a9eeda9eee | |
parent | 70f54be360a7d0f55ceec1636e0fea9447c2b83c (diff) | |
download | postrunner-b9eaf5d4bc366e2f13921bac49017801ce4d6449.zip |
Adding personal record management
-rw-r--r-- | Rakefile | 6 | ||||
-rw-r--r-- | lib/postrunner/ActivitiesDB.rb | 65 | ||||
-rw-r--r-- | lib/postrunner/Activity.rb | 21 | ||||
-rw-r--r-- | lib/postrunner/ActivityReport.rb | 2 | ||||
-rw-r--r-- | lib/postrunner/Main.rb | 6 | ||||
-rw-r--r-- | postrunner.gemspec | 2 | ||||
-rw-r--r-- | spec/FlexiTable_spec.rb (renamed from test/FlexiTable_spec.rb) | 0 | ||||
-rw-r--r-- | spec/PostRunner_spec.rb (renamed from test/PostRunner_spec.rb) | 16 |
8 files changed, 91 insertions, 27 deletions
@@ -1,2 +1,8 @@ require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new + +task :default => :spec +task :test => :spec diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index e0c61e4..a5a4b8d 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -3,6 +3,7 @@ require 'yaml' require 'fit4ruby' require 'postrunner/Activity' +require 'postrunner/PersonalRecords' require 'postrunner/FlexiTable' module PostRunner @@ -19,18 +20,14 @@ module PostRunner @archive_file = File.join(@db_dir, 'archive.yml') create_directories - if Dir.exists?(@db_dir) - begin - if File.exists?(@archive_file) - @activities = YAML.load_file(@archive_file) - else - @activities = [] - end - rescue - Log.fatal "Cannot load archive file '#{@archive_file}': #{$!}" + begin + if File.exists?(@archive_file) + @activities = YAML.load_file(@archive_file) + else + @activities = [] end - else - @activities = [] + rescue StandardError + Log.fatal "Cannot load archive file '#{@archive_file}': #{$!}" end unless @activities.is_a?(Array) @@ -43,6 +40,8 @@ module PostRunner @activities.each do |a| a.db = self end + + @records = PersonalRecords.new(self) end def add(fit_file) @@ -66,14 +65,18 @@ module PostRunner begin FileUtils.cp(fit_file, @fit_dir) - rescue + rescue StandardError Log.fatal "Cannot copy #{fit_file} into #{@fit_dir}: #{$!}" end - @activities << Activity.new(self, base_fit_file, fit_activity) + @activities << (activity = Activity.new(self, base_fit_file, + fit_activity)) @activities.sort! do |a1, a2| - a2.start_time <=> a1.start_time + a2.timestamp <=> a1.timestamp end + + activity.register_records(@records) + sync Log.info "#{fit_file} successfully added to archive" @@ -94,6 +97,20 @@ module PostRunner @activities.each { |a| a.check } end + def ref_by_fit_file(fit_file) + i = 1 + @activities.each do |activity| + return i if activity.fit_file == fit_file + i += 1 + end + + nil + end + + def activity_by_fit_file(fit_file) + @activities.find { |a| a.fit_file == fit_file } + end + def find(query) case query when /\A-?\d+$\z/ @@ -172,18 +189,28 @@ module PostRunner t.row([ i += 1, a.name[0..19], - a.start_time.strftime("%a, %Y %b %d %H:%M"), - "%.2f" % (a.distance / 1000), - secsToHMS(a.duration), + a.timestamp.strftime("%a, %Y %b %d %H:%M"), + "%.2f" % (a.total_distance / 1000), + secsToHMS(a.total_timer_time), speedToPace(a.avg_speed) ]) end puts t.to_s end + def show_records + puts @records.to_s + end + private def sync - File.open(@archive_file, 'w') { |f| f.write(@activities.to_yaml) } + begin + File.open(@archive_file, 'w') { |f| f.write(@activities.to_yaml) } + rescue StandardError + Log.fatal "Cannot write archive file '#{@archive_file}': #{$!}" + end + + @records.sync end def create_directories @@ -197,7 +224,7 @@ module PostRunner Log.info "Creating #{name} directory #{dir}" begin Dir.mkdir(dir) - rescue + rescue StandardError Log.fatal "Cannot create #{name} directory #{dir}: #{$!}" end end diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb index a37c2de..0ded82b 100644 --- a/lib/postrunner/Activity.rb +++ b/lib/postrunner/Activity.rb @@ -6,12 +6,13 @@ module PostRunner class Activity - attr_reader :fit_file, :name + attr_reader :fit_file, :name, :fit_activity 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. - @@CachedVariables = %w( start_time distance duration avg_speed ) + @@CachedVariables = %w( timestamp total_distance total_timer_time + avg_speed ) def initialize(db, fit_file, fit_activity, name = nil) @db = db @@ -27,12 +28,12 @@ module PostRunner end def check - load_fit_file + @fit_activity = load_fit_file Log.info "FIT file #{@fit_file} is OK" end def dump(filter) - load_fit_file(filter) + @fit_activity = load_fit_file(filter) end def yaml_initialize(tag, value) @@ -67,6 +68,18 @@ module PostRunner @name = name end + def register_records(db) + @fit_activity.personal_records.each do |r| + if r.longest_distance == 1 + # In case longest_distance is 1 the distance is stored in the + # duration field in 10-th of meters. + db.register_result(r.duration * 10.0 , 0, r.start_time, @fit_file) + else + db.register_result(r.distance, r.duration, r.start_time, @fit_file) + end + end + end + private def load_fit_file(filter = nil) diff --git a/lib/postrunner/ActivityReport.rb b/lib/postrunner/ActivityReport.rb index 60ceef1..4c328c9 100644 --- a/lib/postrunner/ActivityReport.rb +++ b/lib/postrunner/ActivityReport.rb @@ -23,7 +23,7 @@ module PostRunner t = FlexiTable.new t.enable_frame(false) t.body - t.row([ 'Date:', session.start_time]) + t.row([ 'Date:', session.timestamp]) t.row([ 'Distance:', "#{'%.2f' % (session.total_distance / 1000.0)} km" ]) t.row([ 'Time:', secsToHMS(session.total_timer_time) ]) t.row([ 'Avg. Pace:', diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 3ec3c70..fadb89a 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -3,7 +3,6 @@ require 'logger' require 'fit4ruby' require 'postrunner/version' -require 'postrunner/RuntimeConfig' require 'postrunner/ActivitiesDB' module PostRunner @@ -115,6 +114,9 @@ delete <ref> list List all FIT files stored in the data base. +records + List all personal records. + rename <ref> Replace the FIT file name with a more meaningful name that describes the activity. @@ -147,6 +149,8 @@ EOT process_files(args, :import) when 'list' @activities.list + when 'records' + @activities.show_records when 'rename' process_activities(args, :rename) when 'summary' diff --git a/postrunner.gemspec b/postrunner.gemspec index 2554491..ab4f3e2 100644 --- a/postrunner.gemspec +++ b/postrunner.gemspec @@ -21,4 +21,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.6" spec.add_development_dependency "rake" + spec.add_development_dependency "rspec" + spec.add_development_dependency "yard" end diff --git a/test/FlexiTable_spec.rb b/spec/FlexiTable_spec.rb index dedc9cb..dedc9cb 100644 --- a/test/FlexiTable_spec.rb +++ b/spec/FlexiTable_spec.rb diff --git a/test/PostRunner_spec.rb b/spec/PostRunner_spec.rb index 1c803e9..cd21f7c 100644 --- a/test/PostRunner_spec.rb +++ b/spec/PostRunner_spec.rb @@ -15,8 +15,20 @@ describe PostRunner::Main do def create_fit_file(name, date) a = Fit4Ruby::Activity.new - a.start_time = Time.parse(date) - a.duration = 30 * 60 + a.timestamp = Time.parse(date) + a.total_timer_time = 30 * 60 + 0.upto(30) do |mins| + r = a.new_record + r.timestamp = a.timestamp + mins * 60 + r.distance = 200.0 * mins + r.cadence = 75 + + if mins > 0 && mins % 5 == 0 + s = a.new_lap + end + end + a.new_session + a.aggregate Fit4Ruby.write(name, a) end |