diff --git a/Gemfile b/Gemfile index 3c1ab6d..05b3b30 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ gem "rake" gem "slim" gem "builder" gem "georuby" +gem "gpx" gem "gnuplot" gem "matrix" diff --git a/Gemfile.lock b/Gemfile.lock index 7db3323..5a8def6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -139,6 +139,9 @@ GEM formatador (1.1.0) georuby (2.5.2) gnuplot (2.6.2) + gpx (0.8.3) + nokogiri + rake guard (2.18.0) formatador (>= 0.2.4) listen (>= 2.7, < 4.0) @@ -416,6 +419,7 @@ DEPENDENCIES dry-types georuby gnuplot + gpx guard-puma (~> 0.8) hanami (~> 2.0.0) hanami-controller (~> 2.0.0) diff --git a/app/commands/workouts/create.rb b/app/commands/workouts/create.rb index 0363577..1d7ac3e 100644 --- a/app/commands/workouts/create.rb +++ b/app/commands/workouts/create.rb @@ -10,8 +10,8 @@ module Adamantium include Deps["repos.workout_repo"] include Dry::Monads[:result] - def call(svg:, distance:) - workout_repo.create(path: svg, distance: distance, published_at: Time.now) + def call(svg:, distance:, duration:) + workout_repo.create(path: svg, distance: distance, duration: duration, published_at: Time.now) end end end diff --git a/app/templates/workouts/index.html.slim b/app/templates/workouts/index.html.slim index e832b72..65035e9 100644 --- a/app/templates/workouts/index.html.slim +++ b/app/templates/workouts/index.html.slim @@ -4,7 +4,7 @@ div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark: div class="max-w-prose mx-auto" - workouts_by_year.each do |year, workouts| h3= year - h1= "#{(workouts.map{|w| w.distance }.sum / 1000).round} km" + h1= "#{workouts.map{|w| w.distance }.sum.round} km — #{workouts.map{|w| w.duration / 60 / 60 }.sum.round} hours" div class="grid grid-cols-3 gap-4 mb-4 max-w-prose mx-auto" - workouts.each do |workout| == workout.path diff --git a/db/migrate/20230425062935_add_duration_to_workouts.rb b/db/migrate/20230425062935_add_duration_to_workouts.rb new file mode 100644 index 0000000..831943d --- /dev/null +++ b/db/migrate/20230425062935_add_duration_to_workouts.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +ROM::SQL.migration do + change do + alter_table :workouts do + add_column :duration, :float, null: false + end + end +end diff --git a/lib/adamantium/geo/gpx_parser.rb b/lib/adamantium/geo/gpx_parser.rb index 28fe2b9..538d82a 100644 --- a/lib/adamantium/geo/gpx_parser.rb +++ b/lib/adamantium/geo/gpx_parser.rb @@ -2,6 +2,7 @@ require "geo_ruby" require "geo_ruby/gpx" require "gnuplot" require "dry/monads" +require "gpx" module Adamantium module Geo @@ -10,6 +11,7 @@ module Adamantium def call(file:) gpxfile = GeoRuby::Gpx4r::GpxFile.open(file.path) + gpx = GPX::GPXFile.new(gpx_file: file.path) x = gpxfile.as_line_string.points.flat_map { |p| p.x } y = gpxfile.as_line_string.points.flat_map { |p| p.y } @@ -42,9 +44,8 @@ module Adamantium end svg.gsub!('width="600" height="480"', 'width="100%" height="100%"') - # svg.gsub!('viewBox="0 0 600 480"', 'viewBox="0 0 100% 100%"') - Success({svg: svg, distance: gpxfile.as_line_string.spherical_distance}) + Success({svg: svg, distance: gpx.distance(units: "kilometers"), duration: gpx.duration}) end end end