Add path to trips

This commit is contained in:
Eugene Burmakin 2025-01-24 12:01:54 +01:00
parent 774de9991b
commit 7db7fb17d5
13 changed files with 89 additions and 14 deletions

View file

@ -23,6 +23,7 @@ gem 'activerecord-postgis-adapter', github: 'StoneGod/activerecord-postgis-adapt
gem 'puma' gem 'puma'
gem 'pundit' gem 'pundit'
gem 'rails', '~> 8.0' gem 'rails', '~> 8.0'
gem 'rgeo'
gem 'rswag-api' gem 'rswag-api'
gem 'rswag-ui' gem 'rswag-ui'
gem 'shrine', '~> 3.6' gem 'shrine', '~> 3.6'

View file

@ -484,6 +484,7 @@ DEPENDENCIES
pundit pundit
rails (~> 8.0) rails (~> 8.0)
redis redis
rgeo
rspec-rails rspec-rails
rswag-api rswag-api
rswag-specs rswag-specs

View file

@ -1,17 +1,21 @@
import { Controller } from "@hotwired/stimulus" import { Controller } from "@hotwired/stimulus"
import L from "leaflet" import L from "leaflet"
import { osmMapLayer } from "../maps/layers" import {
osmMapLayer,
osmHotMapLayer,
OPNVMapLayer,
openTopoMapLayer,
cyclOsmMapLayer,
esriWorldStreetMapLayer,
esriWorldTopoMapLayer,
esriWorldImageryMapLayer,
esriWorldGrayCanvasMapLayer
} from "../maps/layers"
import { createPopupContent } from "../maps/popups" import { createPopupContent } from "../maps/popups"
import { osmHotMapLayer } from "../maps/layers" import {
import { OPNVMapLayer } from "../maps/layers" fetchAndDisplayPhotos,
import { openTopoMapLayer } from "../maps/layers" showFlashMessage
import { cyclOsmMapLayer } from "../maps/layers" } from '../maps/helpers';
import { esriWorldStreetMapLayer } from "../maps/layers"
import { esriWorldTopoMapLayer } from "../maps/layers"
import { esriWorldImageryMapLayer } from "../maps/layers"
import { esriWorldGrayCanvasMapLayer } from "../maps/layers"
import { fetchAndDisplayPhotos } from '../maps/helpers';
import { showFlashMessage } from "../maps/helpers";
export default class extends Controller { export default class extends Controller {
static targets = ["container", "startedAt", "endedAt"] static targets = ["container", "startedAt", "endedAt"]

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
class Trips::CreatePathJob < ApplicationJob
queue_as :default
def perform(trip_id)
trip = Trip.find(trip_id)
trip.create_path!
end
end

View file

@ -9,6 +9,12 @@ class Trip < ApplicationRecord
before_save :calculate_distance before_save :calculate_distance
def create_path!
self.path = Tracks::BuildPath.new(points.pluck(:latitude, :longitude)).call
save!
end
def points def points
user.tracked_points.where(timestamp: started_at.to_i..ended_at.to_i).order(:timestamp) user.tracked_points.where(timestamp: started_at.to_i..ended_at.to_i).order(:timestamp)
end end

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
class Tracks::BuildPath
def initialize(coordinates)
@coordinates = coordinates
end
def call
factory.line_string(
coordinates.map { |point| factory.point(point[1].to_f.round(5), point[0].to_f.round(5)) }
)
end
private
attr_reader :coordinates
def factory
@factory ||= RGeo::Geographic.spherical_factory(srid: 3857)
end
end

View file

@ -25,6 +25,7 @@
data-api_key="<%= current_user.api_key %>" data-api_key="<%= current_user.api_key %>"
data-user_settings="<%= current_user.settings.to_json %>" data-user_settings="<%= current_user.settings.to_json %>"
data-coordinates="<%= @coordinates.to_json %>" data-coordinates="<%= @coordinates.to_json %>"
data-path="<%= @trip.path.to_json %>"
data-timezone="<%= Rails.configuration.time_zone %>"> data-timezone="<%= Rails.configuration.time_zone %>">
<div data-trips-target="container" class="h-[25rem] w-full min-h-screen"> <div data-trips-target="container" class="h-[25rem] w-full min-h-screen">
</div> </div>

View file

@ -17,5 +17,13 @@ class DawarichSettings
def geoapify_enabled? def geoapify_enabled?
@geoapify_enabled ||= GEOAPIFY_API_KEY.present? @geoapify_enabled ||= GEOAPIFY_API_KEY.present?
end end
def meters_between_tracks
@meters_between_tracks ||= 300
end
def minutes_between_tracks
@minutes_between_tracks ||= 20
end
end end
end end

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
class CreatePathsForTrips < ActiveRecord::Migration[8.0]
def up
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -6,7 +6,7 @@ class CreateTracks < ActiveRecord::Migration[8.0]
t.datetime :started_at, null: false t.datetime :started_at, null: false
t.datetime :ended_at, null: false t.datetime :ended_at, null: false
t.references :user, null: false, foreign_key: true t.references :user, null: false, foreign_key: true
t.line_string :path, srid: 3785, null: false t.line_string :path, srid: 3857, null: false
t.timestamps t.timestamps
end end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddPathToTrips < ActiveRecord::Migration[8.0]
def change
add_column :trips, :path, :line_string, srid: 3857
end
end

5
db/schema.rb generated
View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_01_23_145954) do ActiveRecord::Schema[8.0].define(version: 2025_01_23_151657) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql" enable_extension "pg_catalog.plpgsql"
enable_extension "postgis" enable_extension "postgis"
@ -197,7 +197,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_23_145954) do
t.datetime "started_at", null: false t.datetime "started_at", null: false
t.datetime "ended_at", null: false t.datetime "ended_at", null: false
t.bigint "user_id", null: false t.bigint "user_id", null: false
t.geometry "path", limit: {:srid=>3785, :type=>"line_string"}, null: false t.geometry "path", limit: {:srid=>3857, :type=>"line_string"}, null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_tracks_on_user_id" t.index ["user_id"], name: "index_tracks_on_user_id"
@ -211,6 +211,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_23_145954) do
t.bigint "user_id", null: false t.bigint "user_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.geometry "path", limit: {:srid=>3857, :type=>"line_string"}
t.index ["user_id"], name: "index_trips_on_user_id" t.index ["user_id"], name: "index_trips_on_user_id"
end end

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Trips::CreatePathJob, type: :job do
pending "add some examples to (or delete) #{__FILE__}"
end