Skip to content

Commit

Permalink
Merge pull request #131 from Freika/zones_and_visits
Browse files Browse the repository at this point in the history
Areas and visits
  • Loading branch information
Freika authored Jul 27, 2024
2 parents e34ab04 + 8fc24b6 commit 4da8313
Show file tree
Hide file tree
Showing 61 changed files with 1,556 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .app_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.4
0.9.5
1 change: 0 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
MAPBOX_ACCESS_TOKEN=MAPBOX_ACCESS_TOKEN
83 changes: 51 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,72 @@
# .github/workflows/ruby.yml
name: Ruby
name: CI
# Not functional at the moment

on:
push:
branches: [main]
pull_request:
push:
branches: [main]

env:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password

jobs:
tests:
name: Tests
test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:12
image: postgres
env:
POSTGRES_PASSWORD: password
ports: ['5432:5432']
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3

redis:
image: redis
ports:
- 6379:6379

steps:
- name: Install packages
run: sudo apt-get update && sudo apt-get install --no-install-recommends -y google-chrome-stable curl libjemalloc2 libvips postgresql-client libpq-dev

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup Ruby
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.2.3
ruby-version: '3.2.3'
bundler-cache: true

- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get -yqq install libpq-dev
bundle install --jobs 4 --retry 3
- name: Run Tests
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16' # Use the appropriate Node.js version

- name: Install Node.js dependencies
run: npm install

- name: Install Ruby dependencies
run: bundle install

- name: Run tests
env:
RAILS_ENV: test
PGHOST: localhost
DISABLE_SPRING: 1
DATABASE_URL: postgres://postgres:postgres@localhost:5432
REDIS_URL: redis://localhost:6379/1
run: |
cp config/database.ci.yml config/database.yml
bundle exec rails db:schema:load
bundle exec rspec --format progress
bin/rails db:setup
bin/rails spec || (cat log/test.log && exit 1)
- name: Keep screenshots from failed system tests
uses: actions/upload-artifact@v4
if: failure()
with:
name: screenshots
path: ${{ github.workspace }}/tmp/capybara
if-no-files-found: ignore

- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-3
3.2.3
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.9.5] — 2024-07-27

### Added

- A possibility to create areas. To create an area, click on the Areas checkbox in map controls (top right corner of the map), then in the top left corner of the map, click on a small circle icon. This will enable draw tool, allowing you to draw an area. When you finish drawing, release the mouse button, and the area will be created. Click on the area, set the name and click "Save" to save the area. You can also delete the area by clicking on the trash icon in the area popup.
- A background job to calculate your visits. This job will calculate your visits based on the areas you've created.
- Visits page. This page will show you all your visits, calculated based on the areas you've created. You can see the date and time of the visit, the area you've visited, and the duration of the visit.
- A possibility to confirm or decline a visit. When you create an area, the visit is not calculated immediately. You need to confirm or decline the visit. You can do this on the Visits page. Click on the visit, then click on the "Confirm" or "Decline" button. If you confirm the visit, it will be added to your timeline. If you decline the visit, it will be removed from your timeline.
- Settings for visit calculation. You can set the minimum time spent in the area to consider it as a visit. This setting can be found in the Settings page.
- POST `/api/v1/areas` and GET `/api/v1/areas` endpoints. You can now create and list your areas via the API.

⚠️ Visits functionality is still in beta. If you find any issues, please let me know. ⚠️

### Fixed

- A route popup now correctly shows distance made in the route, not the distance between first and last points in the route.

---

## [0.9.4] — 2024-07-21

### Added
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gem 'data_migrate'
gem 'devise'
gem 'geocoder'
gem 'importmap-rails'
gem 'kaminari'
gem 'lograge'
gem 'oj'
gem 'pg'
Expand All @@ -26,7 +27,6 @@ gem 'stimulus-rails'
gem 'tailwindcss-rails'
gem 'turbo-rails'
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
gem 'will_paginate', '~> 4.0'

group :development, :test do
gem 'debug', platforms: %i[mri mingw x64_mingw]
Expand Down
15 changes: 13 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,18 @@ GEM
json (2.7.2)
json-schema (4.3.0)
addressable (>= 2.8)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
kaminari-activerecord (= 1.2.2)
kaminari-core (= 1.2.2)
kaminari-actionview (1.2.2)
actionview
kaminari-core (= 1.2.2)
kaminari-activerecord (1.2.2)
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
language_server-protocol (3.17.0.3)
lograge (0.14.0)
actionpack (>= 4)
Expand Down Expand Up @@ -389,7 +401,6 @@ GEM
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
will_paginate (4.0.1)
zeitwerk (2.6.16)

PLATFORMS
Expand All @@ -413,6 +424,7 @@ DEPENDENCIES
foreman
geocoder
importmap-rails
kaminari
lograge
oj
pg
Expand All @@ -439,7 +451,6 @@ DEPENDENCIES
turbo-rails
tzinfo-data
webmock
will_paginate (~> 4.0)

RUBY VERSION
ruby 3.2.3p157
Expand Down
7 changes: 6 additions & 1 deletion app/assets/builds/tailwind.css

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@
text-align: center;
line-height: 36px; /* Same as font-size for perfect centering */
}

.timeline-box {
overflow: visible !important;
}
46 changes: 46 additions & 0 deletions app/controllers/api/v1/areas_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

class Api::V1::AreasController < ApplicationController
before_action :authenticate_api_key
before_action :set_area, only: %i[update destroy]

def index
@areas = current_api_user.areas

render json: @areas, status: :ok
end

def create
@area = current_api_user.areas.build(area_params)

if @area.save
render json: @area, status: :created
else
render json: { errors: @area.errors.full_messages }, status: :unprocessable_entity
end
end

def update
if @area.update(area_params)
render json: @area, status: :ok
else
render json: { errors: @area.errors.full_messages }, status: :unprocessable_entity
end
end

def destroy
@area.destroy!

render json: { message: 'Area was successfully deleted' }, status: :ok
end

private

def set_area
@area = current_api_user.areas.find(params[:id])
end

def area_params
params.require(:area).permit(:name, :latitude, :longitude, :radius)
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/points_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::V1::PointsController < ApplicationController
before_action :authenticate_user!

def destroy
point = current_user.points.find(params[:id])
point = current_user.tracked_points.find(params[:id])
point.destroy

render json: { message: 'Point deleted successfully' }
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class NotificationsController < ApplicationController

def index
@notifications =
current_user.notifications.order(created_at: :desc).paginate(page: params[:page], per_page: 20)
current_user.notifications.order(created_at: :desc).page(params[:page]).per(20)
end

def show
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/points_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def index
.without_raw_data
.where(timestamp: start_at..end_at)
.order(timestamp: :desc)
.paginate(page: params[:page], per_page: 50)
.page(params[:page])
.per(50)

@start_at = Time.zone.at(start_at)
@end_at = Time.zone.at(end_at)
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def generate_api_key

def settings_params
params.require(:settings).permit(
:meters_between_routes, :minutes_between_routes, :fog_of_war_meters
:meters_between_routes, :minutes_between_routes, :fog_of_war_meters,
:time_threshold_minutes, :merge_threshold_minutes
)
end
end
36 changes: 36 additions & 0 deletions app/controllers/visits_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

class VisitsController < ApplicationController
before_action
before_action :set_visit, only: %i[update]

def index
visits = current_user
.visits
.where(status: :pending)
.or(current_user.visits.where(status: :confirmed))
.order(started_at: :asc)
.group_by { |visit| visit.started_at.to_date }
.map { |k, v| { date: k, visits: v } }

@visits = Kaminari.paginate_array(visits).page(params[:page]).per(10)
end

def update
if @visit.update(visit_params)
redirect_to visits_url, notice: 'Visit was successfully updated.', status: :see_other
else
render :edit, status: :unprocessable_entity
end
end

private

def set_visit
@visit = current_user.visits.find(params[:id])
end

def visit_params
params.require(:visit).permit(:name, :started_at, :ended_at, :status)
end
end
12 changes: 2 additions & 10 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,9 @@ def classes_for_flash(flash_type)
end
end

def month_timespan(stat)
month = DateTime.new(stat.year, stat.month).in_time_zone(Time.zone)
start_at = month.beginning_of_month.to_time.strftime('%Y-%m-%dT%H:%M')
end_at = month.end_of_month.to_time.strftime('%Y-%m-%dT%H:%M')

{ start_at:, end_at: }
end

def year_timespan(year)
start_at = Time.utc(year).in_time_zone('Europe/Berlin').beginning_of_year.strftime('%Y-%m-%dT%H:%M')
end_at = Time.utc(year).in_time_zone('Europe/Berlin').end_of_year.strftime('%Y-%m-%dT%H:%M')
start_at = Time.utc(year).in_time_zone(Time.zone).beginning_of_year.strftime('%Y-%m-%dT%H:%M')
end_at = Time.utc(year).in_time_zone(Time.zone).end_of_year.strftime('%Y-%m-%dT%H:%M')

{ start_at:, end_at: }
end
Expand Down
Loading

0 comments on commit 4da8313

Please sign in to comment.