2024-05-23 14:12:23 -04:00
|
|
|
# frozen_string_literal: true
|
2024-05-30 05:50:12 -04:00
|
|
|
|
2024-05-23 14:12:23 -04:00
|
|
|
class MapController < ApplicationController
|
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
|
|
def index
|
2024-09-08 11:25:35 -04:00
|
|
|
@points = points
|
2024-10-02 15:29:56 -04:00
|
|
|
.without_raw_data
|
|
|
|
|
.where('timestamp >= ? AND timestamp <= ?', start_at, end_at)
|
|
|
|
|
.order(timestamp: :asc)
|
2024-05-23 14:12:23 -04:00
|
|
|
|
2024-06-25 16:42:05 -04:00
|
|
|
@countries_and_cities = CountriesAndCities.new(@points).call
|
2024-05-23 14:12:23 -04:00
|
|
|
@coordinates =
|
|
|
|
|
@points.pluck(:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id)
|
2024-07-21 10:45:29 -04:00
|
|
|
.map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s] }
|
2024-05-23 14:12:23 -04:00
|
|
|
@distance = distance
|
|
|
|
|
@start_at = Time.zone.at(start_at)
|
|
|
|
|
@end_at = Time.zone.at(end_at)
|
|
|
|
|
@years = (@start_at.year..@end_at.year).to_a
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def start_at
|
2024-07-23 10:54:44 -04:00
|
|
|
return Time.zone.today.beginning_of_day.to_i if params[:start_at].nil?
|
2024-05-23 14:12:23 -04:00
|
|
|
|
|
|
|
|
Time.zone.parse(params[:start_at]).to_i
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def end_at
|
|
|
|
|
return Time.zone.today.end_of_day.to_i if params[:end_at].nil?
|
|
|
|
|
|
|
|
|
|
Time.zone.parse(params[:end_at]).to_i
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def distance
|
|
|
|
|
@distance ||= 0
|
|
|
|
|
|
|
|
|
|
@coordinates.each_cons(2) do
|
2024-08-28 17:54:00 -04:00
|
|
|
@distance += Geocoder::Calculations.distance_between(
|
2024-10-02 15:29:56 -04:00
|
|
|
[_1[0], _1[1]], [_2[0], _2[1]], units: DISTANCE_UNIT
|
2024-08-28 17:54:00 -04:00
|
|
|
)
|
2024-05-23 14:12:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@distance.round(1)
|
|
|
|
|
end
|
2024-09-08 11:25:35 -04:00
|
|
|
|
|
|
|
|
def points
|
|
|
|
|
params[:import_id] ? points_from_import : points_from_user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def points_from_import
|
|
|
|
|
current_user.imports.find(params[:import_id]).points
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def points_from_user
|
|
|
|
|
current_user.tracked_points
|
|
|
|
|
end
|
2024-05-23 14:12:23 -04:00
|
|
|
end
|