Skip to content

Commit def0eba

Browse files
authored
Merge pull request #6 from Freika/basic_tests
Basic tests
2 parents b676967 + 904fdaf commit def0eba

30 files changed

+543
-75
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
require: rubocop-rails
2+
3+
Style/Documentation:
4+
Enabled: false

Gemfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ gem 'stimulus-rails'
1515
gem 'tailwindcss-rails'
1616
gem 'turbo-rails'
1717
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
18-
gem "importmap-rails"
19-
gem "chartkick"
18+
gem 'importmap-rails'
19+
gem 'chartkick'
2020
gem 'geocoder'
2121
gem 'sidekiq'
22+
gem 'sidekiq-cron'
2223

2324

2425
group :development, :test do
@@ -42,4 +43,4 @@ group :development do
4243
end
4344

4445
# Use Redis for Action Cable
45-
gem "redis"
46+
gem 'redis'

Gemfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,18 @@ GEM
106106
railties (>= 6.1)
107107
drb (2.2.1)
108108
erubi (1.12.0)
109+
et-orbi (1.2.11)
110+
tzinfo
109111
factory_bot (6.4.6)
110112
activesupport (>= 5.0.0)
111113
factory_bot_rails (6.4.3)
112114
factory_bot (~> 6.4)
113115
railties (>= 5.0.0)
114116
ffaker (2.23.0)
115117
foreman (0.87.2)
118+
fugit (1.10.1)
119+
et-orbi (~> 1, >= 1.2.7)
120+
raabro (~> 1.4)
116121
geocoder (1.8.2)
117122
globalid (1.2.1)
118123
activesupport (>= 6.1)
@@ -178,6 +183,7 @@ GEM
178183
nio4r (~> 2.0)
179184
pundit (2.3.1)
180185
activesupport (>= 3.0.0)
186+
raabro (1.4.0)
181187
racc (1.7.3)
182188
rack (3.0.10)
183189
rack-session (2.0.0)
@@ -274,6 +280,10 @@ GEM
274280
connection_pool (>= 2.3.0)
275281
rack (>= 2.2.4)
276282
redis-client (>= 0.19.0)
283+
sidekiq-cron (1.12.0)
284+
fugit (~> 1.8)
285+
globalid (>= 1.0.1)
286+
sidekiq (>= 6)
277287
simplecov (0.22.0)
278288
docile (~> 1.1)
279289
simplecov-html (~> 0.11)
@@ -343,6 +353,7 @@ DEPENDENCIES
343353
rubocop-rails
344354
shoulda-matchers
345355
sidekiq
356+
sidekiq-cron
346357
simplecov
347358
sprockets-rails
348359
stimulus-rails

Procfile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
web: bin/rails server -p 3000 -b 0.0.0.0
22
css: bin/rails tailwindcss:watch
3-
worker: bundle exec sidekiq
3+
worker: bundle exec sidekiq -C config/sidekiq.yml

app/controllers/api/v1/points_controller.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ class Api::V1::PointsController < ApplicationController
22
skip_forgery_protection
33

44
def create
5-
parsed_params = OwnTracks::Params.new(point_params).call
5+
PointCreatingJob.perform_later(point_params)
66

7-
@point = Point.create(parsed_params)
8-
9-
if @point.valid?
10-
render json: @point, status: :ok
11-
else
12-
render json: @point.errors, status: :unprocessable_entity
13-
end
7+
render json: {}, status: :ok
148
end
159

1610
def destroy

app/controllers/points_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ def index
1111
@distance = distance
1212
@start_at = Time.zone.at(start_at)
1313
@end_at = Time.zone.at(end_at)
14+
@years = (@start_at.year..@end_at.year).to_a
1415
end
1516

1617
private
1718

1819
def start_at
1920
return 1.month.ago.beginning_of_day.to_i if params[:start_at].nil?
2021

21-
params[:start_at].to_datetime.to_i
22+
Time.parse(params[:start_at]).to_i
2223
end
2324

2425
def end_at
2526
return Time.zone.today.end_of_day.to_i if params[:end_at].nil?
2627

27-
params[:end_at].to_datetime.to_i
28+
Time.parse(params[:end_at]).to_i
2829
end
2930

3031
def distance

app/helpers/application_helper.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ def month_timespan(stat)
1717
end
1818

1919
def year_timespan(year)
20-
start_at = DateTime.new(year).beginning_of_year.to_time.strftime('%Y-%m-%dT%H:%M')
21-
end_at = DateTime.new(year).end_of_year.to_time.strftime('%Y-%m-%dT%H:%M')
20+
start_at = Time.utc(year).in_time_zone('Europe/Berlin').beginning_of_year.strftime('%Y-%m-%dT%H:%M')
21+
end_at = Time.utc(year).in_time_zone('Europe/Berlin').end_of_year.strftime('%Y-%m-%dT%H:%M')
2222

23-
{ start_at: start_at, end_at: end_at }
23+
{ start_at:, end_at: }
24+
end
25+
26+
def timespan(month, year)
27+
month = DateTime.new(year, month).in_time_zone(Time.zone)
28+
start_at = month.beginning_of_month.to_time.strftime('%Y-%m-%dT%H:%M')
29+
end_at = month.end_of_month.to_time.strftime('%Y-%m-%dT%H:%M')
30+
31+
{ start_at:, end_at: }
2432
end
2533

2634
def header_colors
@@ -38,4 +46,14 @@ def countries_and_cities_stat(year)
3846
def year_distance_stat_in_km(year)
3947
Stat.year_distance(year).sum { _1[1] }
4048
end
49+
50+
def is_past?(year, month)
51+
DateTime.new(year, month).past?
52+
end
53+
54+
def points_exist?(year, month)
55+
Point.where(
56+
timestamp: DateTime.new(year, month).beginning_of_month..DateTime.new(year, month).end_of_month
57+
).exists?
58+
end
4159
end

app/jobs/point_creating_job.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class PointCreatingJob < ApplicationJob
2+
queue_as :default
3+
4+
def perform(point_params)
5+
parsed_params = OwnTracks::Params.new(point_params).call
6+
7+
point = Point.create(parsed_params)
8+
end
9+
end

app/jobs/stat_creating_job.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# frozen_string_literal: true
2+
13
class StatCreatingJob < ApplicationJob
24
queue_as :default
35

4-
def perform(user_id)
5-
CreateStats.new(user_id).call
6+
def perform(user_ids = nil)
7+
CreateStats.new(user_ids).call
68
end
79
end

app/models/stat.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ class Stat < ApplicationRecord
55

66
belongs_to :user
77

8-
def timespan
9-
DateTime.new(year, month).beginning_of_month..DateTime.new(year, month).end_of_month
10-
end
11-
128
def distance_by_day
139
timespan.to_a.map.with_index(1) do |day, index|
1410
beginning_of_day = day.beginning_of_day.to_i
1511
end_of_day = day.end_of_day.to_i
1612

17-
data = { day: index, distance: 0 }
18-
1913
# We have to filter by user as well
2014
points = Point.where(timestamp: beginning_of_day..end_of_day)
2115

16+
data = { day: index, distance: 0 }
17+
2218
points.each_cons(2) do |point1, point2|
2319
distance = Geocoder::Calculations.distance_between(
2420
[point1.latitude, point1.longitude], [point2.latitude, point2.longitude]
@@ -49,6 +45,18 @@ def self.year_cities_and_countries(year)
4945

5046
data = CountriesAndCities.new(points).call
5147

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
5361
end
5462
end

0 commit comments

Comments
 (0)