Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P3 (all in 1) #320

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6dfb83d
improvements
why-not-try-calmer Dec 21, 2023
cbc5203
no settings
why-not-try-calmer Dec 21, 2023
196d3f3
application does not allow to import django from core.utils
why-not-try-calmer Dec 21, 2023
c79cdbb
to_time_aware removed from utils
why-not-try-calmer Dec 22, 2023
eee8d9b
format
why-not-try-calmer Dec 22, 2023
a73f052
wrong import
why-not-try-calmer Jan 7, 2024
6da2fd6
circular import
why-not-try-calmer Jan 7, 2024
818866e
-> utils
why-not-try-calmer Jan 7, 2024
eedca66
settings has no need to be imported globally as it creates uninitaliz…
why-not-try-calmer Jan 7, 2024
0767bc1
call "tjmreset" upon deleting count data
why-not-try-calmer Jan 8, 2024
e73cc18
message for user
why-not-try-calmer Jan 8, 2024
e295c15
let's see if the tests agree with this
why-not-try-calmer Dec 19, 2023
2e5c585
versions
why-not-try-calmer Jan 10, 2024
62a1f0c
matrix settings
why-not-try-calmer Jan 10, 2024
6f19009
docs
why-not-try-calmer Jan 10, 2024
5ee4cf2
Trafic by season
why-not-try-calmer Jan 8, 2024
a7281d3
linter
why-not-try-calmer Jan 8, 2024
7dbc525
settings locally imported
why-not-try-calmer Jan 8, 2024
1ad9ca2
adjusted on new specs
why-not-try-calmer Jan 8, 2024
564ba5a
updated template for MD; updated test data for MD; implemented season…
why-not-try-calmer Jan 9, 2024
3de6cce
...
why-not-try-calmer Jan 22, 2024
3a43743
count details by various criteria
why-not-try-calmer Jan 22, 2024
f10e205
Merge branch '301-upgrade-python-dependencies' into p3
why-not-try-calmer Jan 23, 2024
2305e7d
Merge branch '220-time-awareness-in-stats' into p3
why-not-try-calmer Jan 23, 2024
d525755
Merge branch '295-busiest-by-season' into p3
why-not-try-calmer Jan 23, 2024
b494dbd
Merge branch '282-update-tjm-on-deletion' into p3
why-not-try-calmer Jan 23, 2024
2172e88
missing envar from ci
why-not-try-calmer Jan 23, 2024
96e8edb
matrix in uploads
why-not-try-calmer Jan 23, 2024
0ab261f
miscellaneous stats
why-not-try-calmer Jan 30, 2024
59404a8
---
why-not-try-calmer Jan 30, 2024
66801a6
implementation of yearly bike report miscellaneous statistics
why-not-try-calmer Jan 31, 2024
36c888b
---
why-not-try-calmer Jan 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: "3.10"
python-version: "3.9"
cache: "pip"
- name: Setup, build, install
run: |
Expand All @@ -31,17 +31,23 @@ jobs:
run: black . --check

test:
name: Test
runs-on: ubuntu-22.04
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
postgis_versions:
- 12-3.4
- 15-3.4
name: Test with PostGIS v. ${{ matrix.postgis_versions }}
steps:
- uses: actions/checkout@v4
- name: Start docker stack
run: docker compose up db -d
run: POSTGIS_VERSION=${{ matrix.postgis_versions }} docker compose up db -d
- name: Running tests
run: docker compose run qgis_tester
run: POSTGIS_VERSION=${{ matrix.postgis_versions }} docker compose run qgis_tester
- name: Upload test coverage report
uses: actions/upload-artifact@v4
with:
name: coverage_report.txt
path: testoutputs/coverage_report.txt
name: coverage_report-${{ matrix.postgis_versions }}.txt
path: test_outputs/coverage_report.txt
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.vscode/
.docker/tests/
.docker/tests/screenshot.png
/test_outputs
.env
.idea
.coverage
2 changes: 1 addition & 1 deletion comptages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def prepare_django(default_db=None, **additional_settings):
USE_TZ=True,
TIME_ZONE="Europe/Zurich",
SECRET_KEY="09n+dhzh+02+_#$!1+8h-&(s-wbda#0*2mrv@lx*y#&fzlv&l)",
**additional_settings
**additional_settings,
)
django.setup()

Expand Down
8 changes: 8 additions & 0 deletions comptages/comptages.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from comptages.ui.resources import *
from comptages.core import definitions

from django.core.management import call_command


class Comptages(QObject):
def __init__(self, iface):
Expand Down Expand Up @@ -640,6 +642,12 @@ def do_delete_data_action(self, count_id):
qs = qs.filter(status=definitions.IMPORT_STATUS_DEFINITIVE)

qs.delete()
call_command("tjmreset")
QgsMessageLog.logMessage(
"Les TJMs ont été réinitialisés; rouvrir cette fenêtre pour afficher les données actualisées!",
"Comptages",
Qgis.Info,
)

def enable_actions_if_needed(self):
"""Enable actions if the plugin is connected to the db
Expand Down
12 changes: 11 additions & 1 deletion comptages/core/statistics.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pandas as pd

from datetime import timedelta, datetime

from django.db.models import F, CharField, Value, Q
from django.db.models import Sum
from django.db.models.functions import ExtractHour, Trunc, Concat

from comptages.core import definitions
from comptages.core import utils
from comptages.datamodel import models


Expand All @@ -23,6 +23,7 @@ def get_time_data(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

# By lane/direction grouped per hour

Expand Down Expand Up @@ -64,6 +65,7 @@ def get_time_data_yearly(year, section, lane=None, direction=None):
"""Vehicles by hour and day of the week"""
start = datetime(year, 1, 1)
end = datetime(year + 1, 1, 1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

# By lane/direction grouped per hour

Expand Down Expand Up @@ -111,6 +113,7 @@ def get_day_data(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_count=count,
Expand Down Expand Up @@ -164,6 +167,7 @@ def get_category_data(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_count=count,
Expand Down Expand Up @@ -207,6 +211,7 @@ def get_speed_data(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_count=count,
Expand Down Expand Up @@ -270,6 +275,7 @@ def get_light_numbers(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_count=count,
Expand Down Expand Up @@ -336,6 +342,7 @@ def get_speed_data_by_hour(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_lane__id_section=section,
Expand Down Expand Up @@ -378,6 +385,7 @@ def get_characteristic_speed_by_hour(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_lane__id_section=section,
Expand Down Expand Up @@ -421,6 +429,7 @@ def get_average_speed_by_hour(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_lane__id_section=section,
Expand Down Expand Up @@ -465,6 +474,7 @@ def get_category_data_by_hour(
start = count.start_process_date
if not end:
end = count.end_process_date + timedelta(days=1)
start, end = tuple([utils.to_time_aware_utc(d) for d in (start, end)])

qs = models.CountDetail.objects.filter(
id_lane__id_section=section,
Expand Down
17 changes: 14 additions & 3 deletions comptages/core/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os

from datetime import datetime
from datetime import date, datetime, time

import pytz

from qgis.core import Qgis
from qgis.PyQt.uic import loadUiType
Expand All @@ -9,8 +11,6 @@
from qgis.PyQt.QtSql import QSqlDatabase
from qgis.utils import iface

from comptages.core.settings import Settings


def get_ui_class(ui_file):
"""Get UI Python class from .ui file.
Expand Down Expand Up @@ -55,6 +55,8 @@ def clear_widgets():


def connect_to_db():
from comptages.core.settings import Settings

settings = Settings()
db = QSqlDatabase.addDatabase("QPSQL", str(datetime.now()))
db.setHostName(settings.value("db_host"))
Expand All @@ -65,3 +67,12 @@ def connect_to_db():
db.open()

return db


def to_time_aware_utc(d: datetime | date) -> datetime:
"""Time aware datetimes"""
if isinstance(d, datetime):
return d.astimezone(pytz.timezone("UTC"))
if isinstance(d, date):
return to_time_aware_utc(datetime.combine(d, time()))
raise ValueError(f"Expected datetime or date, got {type(d)}")
Binary file modified comptages/report/template_yearly_bike.xlsx
Binary file not shown.
Loading
Loading