Skip to content

Commit

Permalink
Fix track duration calc
Browse files Browse the repository at this point in the history
  • Loading branch information
yamax2 committed Feb 22, 2021
1 parent 76897ec commit f771686
Show file tree
Hide file tree
Showing 5 changed files with 44,070 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
GIT
remote: https://github.com/rspec/rspec-core.git
revision: ecb65d2d4ea7b381472e3084f45e2da94e00ce91
revision: a5bf456bfb999adb180da13178013884a6f8a736
specs:
rspec-core (3.11.0.pre)
rspec-support (= 3.11.0.pre)

GIT
remote: https://github.com/rspec/rspec-expectations.git
revision: 9efd205e008661b88eddf21c8b6cf52f564aa140
revision: 4e7011fe8ac68b74621336b07f894879c8da202d
specs:
rspec-expectations (3.11.0.pre)
diff-lcs (>= 1.2.0, < 2.0)
Expand All @@ -23,7 +23,7 @@ GIT

GIT
remote: https://github.com/rspec/rspec-rails.git
revision: 9883300cebb9f1a6c3891aa0baa0e61034adf40a
revision: 5874755a3e6043a0dada6d24c7414728900f9401
branch: rails-6-1-dev
specs:
rspec-rails (4.1.0.pre)
Expand Down
2 changes: 1 addition & 1 deletion app/services/tracks/load_info_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def call

track.update!(
distance: gpx.distance,
duration: gpx.duration,
duration: gpx.moving_duration,
started_at: calc_time(:min),
finished_at: calc_time(:max),
bounds: bounds
Expand Down
27 changes: 27 additions & 0 deletions lib/tasks/migrations.rake
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,31 @@ namespace :migrations do
end
end
end

desc 'fix duration for all tracks'
task fix_tracks_duration: :environment do
host = Rails.application.routes.default_url_options[:host]
protocol = Rails.application.routes.default_url_options[:protocol]

Track.uploaded.order(:id).each_instance(with_hold: true, block_size: 10) do |track|
url = "#{protocol}://#{host}#{::ProxyUrls::Track.new(track).generate}"

# rubocop:disable Security/Open
tempfile = URI.open(url)
# rubocop:enable Security/Open
begin
gpx = GPX::GPXFile.new(gpx_file: tempfile.path)

old_value = track.duration
track.update!(duration: gpx.moving_duration)

Rails.logger.info "track #{track.id} updated from #{old_value} to #{track.duration}"
rescue => e
Rails.logger.error("#{track.id}: #{e.message}")
ensure
tempfile.try(:close)
tempfile.try(:unlink)
end
end
end
end
Loading

0 comments on commit f771686

Please sign in to comment.