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

Production update #319

Merged
merged 122 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
e6e9a6d
Add extra field tests
juuso-j Sep 14, 2023
f7b8702
Import all fields in the GEOJSON file
juuso-j Sep 14, 2023
4ec1275
Add extra data to fixture
juuso-j Sep 14, 2023
94958ce
Fix Swedish name source field for major district
juuso-j Sep 22, 2023
781e2eb
Change id to mac
juuso-j Oct 3, 2023
34ca4d7
Store the WKT of location as comment to the CSV file
juuso-j Oct 3, 2023
6e50cbf
Move functionality into functions
juuso-j Oct 3, 2023
c8daf7a
Add function that returns Telraam data frames
juuso-j Oct 4, 2023
09ef2a9
Add Telraam stations initial WKT geometries
juuso-j Oct 4, 2023
cefeb3c
Add function to get or create Telraam station
juuso-j Oct 4, 2023
3fdced6
Fix bug, new element not created to data_frames dict
juuso-j Oct 4, 2023
5082bbd
Write geometry as WKT comment to CSV
juuso-j Oct 4, 2023
783c4a9
Handle Telraam data with multiple dataframes in stations
juuso-j Oct 4, 2023
66e2ced
Fix dataframe building bugs, remove get_or_create from telraam stations
juuso-j Oct 4, 2023
d82e183
Move functions from Command class
juuso-j Oct 5, 2023
a1a52db
Remove obsolete type_dirs_lower
juuso-j Oct 5, 2023
364dc47
Refactor code from handle function into separate functions
juuso-j Oct 5, 2023
bb52b53
Add function that generates fixture data for Telraam tests
juuso-j Oct 6, 2023
87044a0
Add COUNTERS_LIST and COUNTER_CHOICES_STR
juuso-j Oct 9, 2023
ecb5a73
Delete
juuso-j Oct 9, 2023
7673601
Add info about deleting data
juuso-j Oct 9, 2023
f6e487c
Add command to delete specific type of data
juuso-j Oct 9, 2023
7c673e1
Add task to delete specific type of data
juuso-j Oct 9, 2023
1afc6b5
Add check_counters_argument function
juuso-j Oct 9, 2023
e6b04d2
Refactor
juuso-j Oct 9, 2023
7d2863b
Fix keyerror bug in get_telraam_data_frame
juuso-j Oct 9, 2023
dd72bc9
Save hours for last day in data frame
juuso-j Oct 9, 2023
8070ae0
Add Telraam importing tests
juuso-j Oct 9, 2023
09c46ef
Add function to generate Telraam fixture data frames
juuso-j Oct 9, 2023
92018e6
Add TEST_COLUMN_NAMES
juuso-j Oct 9, 2023
0dcbfe1
Refactor
juuso-j Oct 9, 2023
285b72a
Fix missing hour bug
juuso-j Oct 9, 2023
06c51cf
Refactor
juuso-j Oct 10, 2023
0a1e724
Add is_active_fixtures
juuso-j Oct 10, 2023
55a83fc
Add is_active field tests
juuso-j Oct 10, 2023
b9ed23b
Serialize is_active field
juuso-j Oct 10, 2023
c635620
Add data_until_date field
juuso-j Oct 10, 2023
ff12ce1
Add data_from_date field
juuso-j Oct 11, 2023
e200f84
Remove data_from_year field
juuso-j Oct 11, 2023
8a922b5
Add value_at to day datas
juuso-j Oct 11, 2023
4f4d044
Test data_until_date and data_from_date fields
juuso-j Oct 11, 2023
8afa79c
Add ECO_COUNTER_STATION_URL to env
juuso-j Oct 11, 2023
d57fdd0
Add ECO_COUNTER_OBSERVATIONS_URL to env
juuso-j Oct 11, 2023
42abd74
Add TRAFFIC_COUNTER_OBSERVATIONS_BASE_URL to env
juuso-j Oct 11, 2023
b61b43c
Add Air monitoring constants
juuso-j Oct 12, 2023
18043e7
Add EnvironmentDataConfig AppConfig
juuso-j Oct 12, 2023
cfc4fca
Add translations
juuso-j Oct 12, 2023
217b03d
Add Air monitoring utility functions
juuso-j Oct 12, 2023
963eff3
Add initial version of Environment data importer
juuso-j Oct 12, 2023
e77b9b0
Add __init__.py
juuso-j Oct 12, 2023
63bf4d6
Add __init__.py
juuso-j Oct 12, 2023
c67b04c
Add EnvironmentData to installed apps
juuso-j Oct 12, 2023
f061ed6
Add general constants
juuso-j Oct 12, 2023
9da4c1c
Deleted, as renamed
juuso-j Oct 12, 2023
4ebcb66
Add air quality constants
juuso-j Oct 12, 2023
89c9e76
Add logging for Environment Data APP
juuso-j Oct 12, 2023
84e3a04
Add get and create functions from imported and generic get_stations f…
juuso-j Oct 12, 2023
67088b6
Add weather observations and refactor
juuso-j Oct 12, 2023
5cdff19
Add basic admin for models
juuso-j Oct 13, 2023
ce09125
Add API constants
juuso-j Oct 13, 2023
387a9a7
Add serializers
juuso-j Oct 13, 2023
0a091d7
Add urls
juuso-j Oct 13, 2023
50c3474
Add utility function for Air Quality
juuso-j Oct 13, 2023
e0222ee
Add API utility functions
juuso-j Oct 13, 2023
f78b8f7
Add API views
juuso-j Oct 13, 2023
97537da
Add environment_data/ to DOC_ENDPOINTS
juuso-j Oct 13, 2023
92f65af
Add environment_data urls
juuso-j Oct 13, 2023
31423da
Add common constants
juuso-j Oct 13, 2023
3c02b4a
Improve error message
juuso-j Oct 13, 2023
fbb85e5
Add utility function for weather observations
juuso-j Oct 13, 2023
d81f1da
Improve args handling and initial_import
juuso-j Oct 13, 2023
6ed51d4
Add constant for weather observations
juuso-j Oct 13, 2023
35c8249
Fix station filtering
juuso-j Oct 13, 2023
2c5b641
Insert constants from importer
juuso-j Oct 13, 2023
0b685c5
Add custom list view for Stations
juuso-j Oct 13, 2023
b401c04
Add station list view params
juuso-j Oct 13, 2023
8655d5b
Refactor
juuso-j Oct 13, 2023
d904082
Serialize params that have data
juuso-j Oct 16, 2023
d6e0cb9
Improve naming and add descriptions
juuso-j Oct 16, 2023
51d3f75
Change PERICIPITATIONAMOUNT to PRECIPITATION_AMOUNT
juuso-j Oct 16, 2023
1e49dd0
Improve naming and add descriptions
juuso-j Oct 16, 2023
ac782fd
Save parameter description and data_type
juuso-j Oct 16, 2023
fcf2902
Handle cumulative parameters
juuso-j Oct 16, 2023
979e532
Improve naming and add comment
juuso-j Oct 16, 2023
d56f602
Add data_type_verbose to Parameter and Station
juuso-j Oct 17, 2023
86e1af8
Delete, wrong name of file
juuso-j Oct 17, 2023
a1f09be
Add translations
juuso-j Oct 17, 2023
85f1ad2
Formating
juuso-j Oct 17, 2023
8dd97a5
Improve error handling for list data view
juuso-j Oct 17, 2023
5c19ee9
Add models for environment data APP
juuso-j Oct 17, 2023
30a22b2
Rename field params to parameters_in_use
juuso-j Oct 17, 2023
4064399
Add management command to delete all environment data
juuso-j Oct 17, 2023
6c85bf6
Use changed name of PARAMS constant
juuso-j Oct 17, 2023
02fd0d9
Formating
juuso-j Oct 17, 2023
e2b742a
Fix OBSERVABLE_PARAMETERS and AIR_PRESSURE
juuso-j Oct 17, 2023
f06064a
Fix bug start_date_time > current_date_time
juuso-j Oct 17, 2023
591e502
Add initial migration
juuso-j Oct 17, 2023
7df8db2
Add test fixtures
juuso-j Oct 17, 2023
246781d
Add API tests
juuso-j Oct 17, 2023
8602aa3
Add importer tests
juuso-j Oct 17, 2023
0cab70a
Replace requests with request session
juuso-j Oct 18, 2023
bc5c1b2
Use REQUEST_SESSION instead of requests
juuso-j Oct 18, 2023
33d3e0b
Add README.mk
juuso-j Oct 18, 2023
8a497b2
Add celery tasks
juuso-j Oct 18, 2023
664141f
Add REQUEST_SESSION and retry_strategy
juuso-j Oct 18, 2023
4d0e5a6
Delete, file renamed
juuso-j Oct 19, 2023
3345c07
Add README
juuso-j Oct 19, 2023
7c1da09
Handle cumulative <0 values. Delete datas in initial import
juuso-j Oct 19, 2023
6bc98f1
Add parameter info
juuso-j Oct 19, 2023
6ab3241
Fix timestamp bug in incremental import
juuso-j Oct 19, 2023
a1d81a5
Add negative numbers tests
juuso-j Oct 19, 2023
be473e3
Change retry strategy
juuso-j Oct 23, 2023
2f33748
Change duplicate MonthData to HourData
juuso-j Oct 23, 2023
07a446e
Change field voimassa_a to rajoitus as the source data has changed
juuso-j Oct 23, 2023
fcc3a82
Filter by station data tables to delete
juuso-j Oct 24, 2023
803b2cb
Bump urllib3 from 1.26.16 to 1.26.18
dependabot[bot] Oct 30, 2023
02675d2
Merge pull request #311 from City-of-Turku/feature/loading-and-unload…
juuso-j Oct 30, 2023
4561982
Merge pull request #313 from City-of-Turku/bugfix/major-district-swed…
juuso-j Oct 30, 2023
21fa5c9
Merge pull request #314 from City-of-Turku/feature/eco-counter-telraa…
juuso-j Oct 30, 2023
ad0e7d9
Merge pull request #315 from City-of-Turku/feature/environment-data-app
juuso-j Oct 30, 2023
1e66ea5
Merge pull request #317 from City-of-Turku/feature/speed-limit-zones-…
juuso-j Oct 30, 2023
ef05844
Merge pull request #318 from City-of-Turku/dependabot/pip/urllib3-1.2…
juuso-j Oct 30, 2023
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
3 changes: 3 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
ADDITIONAL_INSTALLED_APPS: smbackend_turku,ptv
PTV_ID_OFFSET: 10000000
LAM_COUNTER_API_BASE_URL: https://tie.digitraffic.fi/api/tms/v1/history
ECO_COUNTER_STATIONS_URL: https://dev.turku.fi/datasets/ecocounter/liikennelaskimet.geojson
ECO_COUNTER_OBSERVATIONS_URL: https://data.turku.fi/cjtv3brqr7gectdv7rfttc/counters-15min.csv
TRAFFIC_COUNTER_OBSERVATIONS_BASE_URL: https://data.turku.fi/2yxpk2imqi2mzxpa6e6knq/

steps:
- uses: actions/checkout@v3
Expand Down
7 changes: 7 additions & 0 deletions eco_counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ e.g. ./manage.py import_counter_data --counters EC TC
Counter names are: EC (Eco Counter), TC (Traffic Counter), LC (Lam Counter) and TR (Telraam Counter).
Note, Traffic Counter data is updated once a week and Lam Counter data once a day.

## Deleting data
To delete data use the delete_counter_data management command.
e.g. to delete all Lam Counter data type:
```
./manage.py delete_counter_data --counters LC
```

### Importing Telraam raw data
In order to import Telraam data into the database the raw data has to be imported. The raw data is imported with the _import_telraam_to_csv_ management command.
The imported should be set to be run once a hour (see: https://github.com/City-of-Turku/smbackend/wiki/Celery-Tasks#telraam-to-csv-eco_countertasksimport_telraam_to_csv )
Expand Down
51 changes: 38 additions & 13 deletions eco_counter/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import date, timedelta

from django.db.models import Q
from rest_framework import serializers

Expand Down Expand Up @@ -28,6 +30,7 @@
"value_bp",
"value_bt",
]
Q_EXP = Q(value_at__gt=0) | Q(value_pt__gt=0) | Q(value_jt__gt=0) | Q(value_bt__gt=0)


class StationSerializer(serializers.ModelSerializer):
Expand All @@ -36,7 +39,9 @@ class StationSerializer(serializers.ModelSerializer):
lon = serializers.SerializerMethodField()
lat = serializers.SerializerMethodField()
sensor_types = serializers.SerializerMethodField()
data_from_year = serializers.SerializerMethodField()
data_until_date = serializers.SerializerMethodField()
data_from_date = serializers.SerializerMethodField()
is_active = serializers.SerializerMethodField()

class Meta:
model = Station
Expand All @@ -54,7 +59,9 @@ class Meta:
"lon",
"lat",
"sensor_types",
"data_from_year",
"data_until_date",
"data_from_date",
"is_active",
]

def get_y(self, obj):
Expand Down Expand Up @@ -82,17 +89,35 @@ def get_sensor_types(self, obj):
result.append(type)
return result

def get_data_from_year(self, obj):
q_exp = (
Q(value_at__gt=0)
| Q(value_pt__gt=0)
| Q(value_jt__gt=0)
| Q(value_bt__gt=0)
)
qs = YearData.objects.filter(q_exp, station=obj).order_by("year__year_number")
if qs.count() > 0:
return qs[0].year.year_number
else:
def get_is_active(self, obj):
num_days = [1, 7, 30, 365]
res = {}
for days in num_days:
from_date = date.today() - timedelta(days=days - 1)
day_qs = Day.objects.filter(station=obj, date__gte=from_date)
day_data_qs = DayData.objects.filter(day__in=day_qs)
if day_data_qs.filter(Q_EXP).count() > 0:
res[days] = True
else:
res[days] = False
return res

def get_data_until_date(self, obj):
try:
return (
DayData.objects.filter(Q_EXP, station=obj).latest("day__date").day.date
)
except DayData.DoesNotExist:
return None

def get_data_from_date(self, obj):
try:
return (
DayData.objects.filter(Q_EXP, station=obj)
.earliest("day__date")
.day.date
)
except DayData.DoesNotExist:
return None


Expand Down
32 changes: 29 additions & 3 deletions eco_counter/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
COUNTERS.LAM_COUNTER = LAM_COUNTER
COUNTERS.TELRAAM_COUNTER = TELRAAM_COUNTER

COUNTERS_LIST = [ECO_COUNTER, TRAFFIC_COUNTER, LAM_COUNTER, TELRAAM_COUNTER]
COUNTER_CHOICES_STR = (
f"{ECO_COUNTER}, {TRAFFIC_COUNTER}, {TELRAAM_COUNTER} and {LAM_COUNTER}"
)
CSV_DATA_SOURCES = (
(TRAFFIC_COUNTER, "TrafficCounter"),
(ECO_COUNTER, "EcoCounter"),
Expand Down Expand Up @@ -118,12 +122,14 @@

TELRAAM_COUNTER_CSV_FILE_PATH = f"{settings.MEDIA_ROOT}/telraam_data/"
TELRAAM_COUNTER_CSV_FILE = (
TELRAAM_COUNTER_CSV_FILE_PATH + "telraam_data_{id}_{day}_{month}_{year}.csv"
TELRAAM_COUNTER_CSV_FILE_PATH + "telraam_data_{mac}_{day}_{month}_{year}.csv"
)
TELRAAM_STATION_350457790598039 = 350457790598039
TELRAAM_STATION_350457790600975 = 350457790600975
TELRAAM_COUNTER_CAMERAS = {
# Mac id: Direction flag (True=rgt prefix will be keskustaan päin)
350457790598039: False, # Kristiinanankatu, Joelle katsottaessa vasemmalle
350457790600975: True, # Kristiinanankatu, Joelle katsottaessa oikealle
TELRAAM_STATION_350457790598039: False, # Kristiinanankatu, Joelle katsottaessa vasemmalle
TELRAAM_STATION_350457790600975: True, # Kristiinanankatu, Joelle katsottaessa oikealle
}
# For 429 (too many request) TELRAAM need a retry strategy
retry_strategy = Retry(
Expand All @@ -136,3 +142,23 @@
TELRAAM_HTTP = requests.Session()
TELRAAM_HTTP.mount("https://", adapter)
TELRAAM_HTTP.mount("http://", adapter)


# Telraam stations initial geometries in WKT format
# These coordinates are used if CSV files do not include any geometries.
TELRAAM_STATIONS_INITIAL_WKT_GEOMETRIES = {
TELRAAM_STATION_350457790598039: {
"location": "POINT (239628.47846388057 6710757.471557152)",
"geometry": "MULTILINESTRING ((239565.80107971327 6710861.8209667895, 239572.58901459936 6710850.524818219,"
" 239574.73294378238 6710846.950531884, 239628.47846388057 6710757.471557152,"
" 239630.0339247121 6710754.923177836, 239635.52748551324 6710745.732077925))",
},
TELRAAM_STATION_350457790600975: {
"location": "POINT (239523.2288977413 6710932.715108742)",
"geometry": "MULTILINESTRING ((239490.42663459244 6710989.092283992, 239493.45037993207 6710983.7110835295,"
" 239495.88642941663 6710979.3668986475, 239517.9904128411 6710941.530425406,"
" 239520.0691194288 6710937.971973339, 239523.2288977413 6710932.715108742,"
" 239529.37000273907 6710922.482472116, 239558.08254550528 6710874.681753734,"
" 239559.97438753376 6710871.516775628, 239565.80107971327 6710861.8209667895))",
},
}
17 changes: 0 additions & 17 deletions eco_counter/management/commands/delete_all_counter_data.py

This file was deleted.

36 changes: 36 additions & 0 deletions eco_counter/management/commands/delete_counter_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import logging

from django import db
from django.core.management.base import BaseCommand

from eco_counter.constants import COUNTER_CHOICES_STR
from eco_counter.management.commands.utils import check_counters_argument
from eco_counter.models import ImportState, Station

logger = logging.getLogger("eco_counter")


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--counters",
type=str,
nargs="+",
default=False,
help=f"Delete given counter data, choices are: {COUNTER_CHOICES_STR}.",
)

@db.transaction.atomic
def handle(self, *args, **options):
counters = options.get("counters", None)
check_counters_argument(counters)
if counters:
for counter in counters:
logger.info(f"Deleting counter data for {counter}")
logger.info(
f"{Station.objects.filter(csv_data_source=counter).delete()}"
)
logger.info(
f"{ImportState.objects.filter(csv_data_source=counter).delete()}"
)
logger.info("Deleted counter data.")
Loading
Loading