Skip to content

Commit

Permalink
merge GL4
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaesousa committed May 8, 2024
1 parent 7fb06e6 commit c626f44
Show file tree
Hide file tree
Showing 12 changed files with 554 additions and 294 deletions.
79 changes: 52 additions & 27 deletions comptages/comptages.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,7 @@ def import_file(self, file_path, count_id=None):
return

QgsMessageLog.logMessage(
"{} - Prepare import file {}".format(
datetime.now(), os.path.basename(file_path)
),
"Comptages",
Qgis.Info,
)

QgsMessageLog.logMessage(
"{} - Import file {} started".format(
"{} - Prepare import file {} ended".format(
datetime.now(), os.path.basename(file_path)
),
"Comptages",
Expand Down Expand Up @@ -369,14 +361,14 @@ def do_filter_action(self):

def do_yearly_report_action(self):
QgsMessageLog.logMessage(
"{} - Generate yearly report action started".format(datetime.now()),
"{} - Generate task for yearly report action started".format(datetime.now()),
"Comptages",
Qgis.Info,
)

if self.tm.countActiveTasks() > 0:
push_info(
("Veuillez patienter jusqu'à ce que l'importation " "soit terminée.")
("Veuillez patienter jusqu'à ce que l'importation soit terminée.")
)
return

Expand All @@ -394,11 +386,15 @@ def do_yearly_report_action(self):

section_id = selected_feature.attribute("id")
section = models.Section.objects.get(id=section_id)

classes = self.layers.get_classes_of_section(section_id)
years = self.layers.get_years_of_counts_on_section(section_id)

dlg = YearlyReportDialog(self.iface)
dlg.section.insert(section_id)
dlg.classi.addItems(classes)
if years is not None:
years.sort()
dlg.year.setValue(years[-1])

if dlg.exec_():
year = dlg.year.value()
Expand All @@ -411,15 +407,15 @@ def do_yearly_report_action(self):

if not file_path:
QgsMessageLog.logMessage(
"{} - Generate yearly report action ended: No file_path given".format(
"{} - Generate task for yearly report action cancelled: No file_path given".format(
datetime.now()
),
"Comptages",
Qgis.Info,
)
return
QgsMessageLog.logMessage(
"{} - Generate yearly report action can really begin now for count {} with file_path: {}".format(
"{} - Generate task for yearly report action can really begin now for count {} with file_path: {}".format(
datetime.now(), selected_count, file_path
),
"Comptages",
Expand All @@ -435,7 +431,7 @@ def do_yearly_report_action(self):
return

if clazz.startswith("SPCH-MD"):
yrb = YearlyReportBike(file_path, year, section_id)
yrb = YearlyReportBike(file_path, year, section_id, clazz)
yrb.run()
else:
self.tm.allTasksFinished.connect(
Expand All @@ -454,7 +450,7 @@ def do_yearly_report_action(self):
# TODO: check if there are comptages for this section and year

QgsMessageLog.logMessage(
"{} - Generate yearly report action ended".format(datetime.now()),
"{} - Generate task for yearly report action ended".format(datetime.now()),
"Comptages",
Qgis.Info,
)
Expand Down Expand Up @@ -505,7 +501,7 @@ def do_import_single_file_action(self, count_id):

def do_generate_report_action(self, count_id):
QgsMessageLog.logMessage(
"{} - Generate report action started".format(datetime.now()),
"{} - Generate report preparation started for count_id {}".format(datetime.now(), count_id),
"Comptages",
Qgis.Info,
)
Expand All @@ -514,7 +510,9 @@ def do_generate_report_action(self, count_id):

if self.tm.countActiveTasks() > 0:
push_info(
("Veuillez patienter jusqu'à ce que l'importation " "soit terminée.")
("Veuillez patienter jusqu'à ce que l'importation soit terminée,"
" puis relancer la génération du rapport."
)
)
return

Expand All @@ -525,7 +523,7 @@ def do_generate_report_action(self, count_id):
"le comptage {}".format(count.id_installation.name, count.id)
)
QgsMessageLog.logMessage(
"{} - Generate report action ended: No data for count {}".format(
"{} - Generate report preparation ended: No data for count {}".format(
datetime.now(), count.id
),
"Comptages",
Expand All @@ -534,39 +532,50 @@ def do_generate_report_action(self, count_id):
return

file_dialog = QFileDialog()
mondays = list(report._mondays_of_count(count))
mondays = report._mondays_of_count(count)
sections_ids = (
models.Section.objects.filter(lane__id_installation__count=count)
.distinct()
.values_list("id", flat=True)
)
report_selection_dialog = SelectSectionsToReport(
sections_ids=list(sections_ids), mondays=mondays
sections_ids=list(sections_ids), mondays=list(mondays)
)

if report_selection_dialog.exec_():
selected_sections_dates: dict[str, list[date]] = (
report_selection_dialog.get_inputs()
)
title = "Exporter un rapport"
date_choosen=list()
for selsec in selected_sections_dates:
date_choosen.extend(selected_sections_dates[selsec])
if len(date_choosen)==0:
QgsMessageLog.logMessage(
"{} - Generate report preparation ended: Nothing choosen to report on".format(
datetime.now()
),
"Comptages",
Qgis.Info,
)
return

title = "Exporter un rapport"
path = self.settings.value("report_export_directory")
file_path = QFileDialog.getExistingDirectory(file_dialog, title, path)

if not file_path:
QgsMessageLog.logMessage(
"{} - Generate report action ended: No file_path given".format(
"{} - Generate report preparation ended: No file_path given".format(
datetime.now()
),
"Comptages",
Qgis.Info,
)
return

QgsMessageLog.logMessage(
f"""
{datetime.now()} - Generate report action can really begin now for count {count.id} with file_path: {file_path}.
Selected sections and dates: {selected_sections_dates}
""",
f"""{datetime.now()} - Generate report action can really begin now for count {count.id} with file_path: {file_path}.
Selected sections and dates: {selected_sections_dates}""",
"Comptages",
Qgis.Info,
)
Expand All @@ -579,6 +588,22 @@ def do_generate_report_action(self, count_id):
selected_sections_dates=selected_sections_dates,
)
)
else:
QgsMessageLog.logMessage(
"{} - Generate report preparation ended: Cancel buton clicked...".format(
datetime.now()
),
"Comptages",
Qgis.Info,
)
return


QgsMessageLog.logMessage(
"{} - Generate report preparation ended for count_id {}".format(datetime.now(), count_id),
"Comptages",
Qgis.Info,
)

def do_export_plan_action(self, count_id):
count = models.Count.objects.get(id=count_id)
Expand Down
31 changes: 14 additions & 17 deletions comptages/core/importer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import Callable, Iterator, Optional
import pytz
import os
from pytz import timezone
from os import path
from datetime import datetime, timedelta
from django.db.models import Q

from comptages.core import definitions
from comptages.datamodel import models
from comptages.core.bulk_create_manager import BulkCreateManager

TZ = timezone("Europe/Zurich")

def simple_print_callback(progress: int):
if progress % 10 == 0:
Expand Down Expand Up @@ -51,7 +52,7 @@ def _parse_and_write(
from_aggregate: bool = False,
**kwargs,
):
basename = os.path.basename(file_path)
basename = path.basename(file_path)
bulk_mgr = BulkCreateManager(chunk_size=1000)
lanes = _populate_lane_dict(count)
directions = _populate_direction_dict(count)
Expand Down Expand Up @@ -112,10 +113,9 @@ def _parse_line_vbv1(line: str, **kwargs) -> Optional[list[dict]]:
return None

parsed_line = {}
tz = pytz.timezone("Europe/Zurich")
try:
parsed_line["numbering"] = line[0:6]
parsed_line["timestamp"] = tz.localize(
parsed_line["timestamp"] = TZ.localize(
datetime.strptime("{}0000".format(line[7:24]), "%d%m%y %H%M %S %f")
)
parsed_line["reserve_code"] = line[25:31]
Expand Down Expand Up @@ -162,11 +162,10 @@ def _parse_line_mc(line: str, **kwargs) -> Optional[list[dict]]:

parsed_line = {}
try:
tz = pytz.timezone("Europe/Zurich")
# TODO: numbering
numbering = 1
parsed_line["numbering"] = numbering
parsed_line["timestamp"] = tz.localize(
parsed_line["timestamp"] = TZ.localize(
datetime.strptime(line[0:19], "%Y-%m-%d %H:%M:%S")
)
# On MetroCount files, the direction is 0-1 instead of 1-2
Expand Down Expand Up @@ -198,18 +197,17 @@ def _parse_line_int2(line, **kwargs) -> Iterator[Optional[dict]]:
return None

parsed_line = {}
tz = pytz.timezone("Europe/Zurich")
# TODO: numbering
numbering = 1
parsed_line["numbering"] = numbering
# In the data files midnight is 2400 of the current day
# instead of 0000 of the next day
if line[7:9] == "24":
line = line[:7] + "00" + line[9:]
end = tz.localize(datetime.strptime("{}".format(line[0:11]), "%d%m%y %H%M"))
end = TZ.localize(datetime.strptime("{}".format(line[0:11]), "%d%m%y %H%M"))
end += timedelta(days=1)
else:
end = tz.localize(datetime.strptime("{}".format(line[0:11]), "%d%m%y %H%M"))
end = TZ.localize(datetime.strptime("{}".format(line[0:11]), "%d%m%y %H%M"))

parsed_line["end"] = end
parsed_line["start"] = parsed_line["end"] - timedelta(minutes=kwargs["interval"])
Expand Down Expand Up @@ -294,7 +292,6 @@ def _get_int_bins(

def _parse_file_header(file_path: str):
file_header = dict()
tz = pytz.timezone("Europe/Zurich")

with open(file_path, encoding=get_file_encoding(file_path)) as f:
for line in f:
Expand All @@ -308,12 +305,12 @@ def _parse_file_header(file_path: str):
if key == "CLASS" and value == "SPECIAL10":
value = "SWISS10"
if key in ["STARTREC", "STOPREC"]:
value = tz.localize(datetime.strptime(value, "%H:%M %d/%m/%y"))
value = TZ.localize(datetime.strptime(value, "%H:%M %d/%m/%y"))
file_header[key] = value
# MetroCount
elif line.startswith("MetroCount"):
file_header["FORMAT"] = "MC"
elif line.startswith("Place"):
elif line.startswith("Place") and "SITE" not in file_header:
file_header["SITE"] = line[line.find("[") + 1 : line.find("]")].replace(
"-", ""
)
Expand All @@ -322,11 +319,11 @@ def _parse_file_header(file_path: str):
and file_header["FORMAT"] == "MC"
and "STARTREC" not in file_header
):
file_header["STARTREC"] = tz.localize(
file_header["STARTREC"] = TZ.localize(
datetime.strptime(line[:19], "%Y-%m-%d %H:%M:%S")
)
elif line.startswith("20") and file_header["FORMAT"] == "MC":
file_header["STOPREC"] = tz.localize(
file_header["STOPREC"] = TZ.localize(
datetime.strptime(line[:19], "%Y-%m-%d %H:%M:%S")
)
elif line.startswith("Type de Cat") and file_header["FORMAT"] == "MC":
Expand All @@ -337,8 +334,8 @@ def _parse_file_header(file_path: str):
file_header["CLASS"] = "NZ13"
elif file_header["CLASS"][:5] == "FHWA ":
file_header["CLASS"] = "FHWA13"
elif file_header["CLASS"] == "CAT-Cycle_dist-empat":
file_header["CLASS"] = "SPCH-MD 5C"
elif file_header["CLASS"] in ("CAT-Cycle_dist-empat", "SPCH-MD5C", "SPCH-MD 5C"):
file_header["CLASS"] = "SPCH-MD_5C"

return file_header

Expand Down
4 changes: 2 additions & 2 deletions comptages/core/importer_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def run(self):
def finished(self, result: Any):
if result:
QgsMessageLog.logMessage(
"{} - Import file {} ended".format(datetime.now(), self.basename),
"{} - Task import for file {} created".format(datetime.now(), self.basename),
"Comptages",
Qgis.Info,
)

else:
QgsMessageLog.logMessage(
"{} - Import file {} ended with errors: {}".format(
"{} - Task import creation for file {} ended with errors: {}".format(
datetime.now(), self.basename, self.exception
),
"Comptages",
Expand Down
11 changes: 10 additions & 1 deletion comptages/core/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def get_counts_of_section_by_year(self, section_id, year):
except StopIteration:
return []

print(counts)
print(f"get_counts_of_section_by_year - counts:{counts}")
return counts

def get_lanes_of_section(self, section_id):
Expand Down Expand Up @@ -981,6 +981,15 @@ def get_classes_of_section(self, section_id: str):
result.add(self.get_class_name_of_count(count.attribute("id")))

return result

def get_years_of_counts_on_section(self, section_id: str):
result = list()
counts = self.get_counts_of_section(section_id)

for count in counts:
result.append((count.attribute("end_process_date")).year())

return result

def check_sensor_of_lane(self, lane_id: str):
"""Check id a lane is registered in the sensor table"""
Expand Down
Loading

0 comments on commit c626f44

Please sign in to comment.