@@ -5,20 +5,16 @@ class Stat < ApplicationRecord
5
5
6
6
belongs_to :user
7
7
8
- def timespan
9
- DateTime . new ( year , month ) . beginning_of_month ..DateTime . new ( year , month ) . end_of_month
10
- end
11
-
12
8
def distance_by_day
13
9
timespan . to_a . map . with_index ( 1 ) do |day , index |
14
10
beginning_of_day = day . beginning_of_day . to_i
15
11
end_of_day = day . end_of_day . to_i
16
12
17
- data = { day : index , distance : 0 }
18
-
19
13
# We have to filter by user as well
20
14
points = Point . where ( timestamp : beginning_of_day ..end_of_day )
21
15
16
+ data = { day : index , distance : 0 }
17
+
22
18
points . each_cons ( 2 ) do |point1 , point2 |
23
19
distance = Geocoder ::Calculations . distance_between (
24
20
[ point1 . latitude , point1 . longitude ] , [ point2 . latitude , point2 . longitude ]
@@ -49,6 +45,18 @@ def self.year_cities_and_countries(year)
49
45
50
46
data = CountriesAndCities . new ( points ) . call
51
47
52
- { countries : data . count , cities : data . sum { |country | country [ :cities ] . count } }
48
+ { countries : data . map { _1 [ :country ] } . uniq . count , cities : data . sum { |country | country [ :cities ] . count } }
49
+ end
50
+
51
+ def self . years
52
+ starting_year = select ( :year ) . min &.year || Time . current . year
53
+
54
+ ( starting_year ..Time . current . year ) . to_a . reverse
55
+ end
56
+
57
+ private
58
+
59
+ def timespan
60
+ DateTime . new ( year , month ) . beginning_of_month ..DateTime . new ( year , month ) . end_of_month
53
61
end
54
62
end
0 commit comments