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

fix #145 and improve data behaviour #147

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/), and this

<!-- ## Unreleased [{version_tag}](https://github.com/opengisch/qgis-plugin-ci/releases/tag/{version_tag}) - YYYY-MM-DD -->

## 3.3.5 - 2024-09-25

* Time interval script (fix #145 from PR #147) is now fixed and usable as expected

## 3.3.4 - 2024-09-24

* Plugin menu is now correctly removed from bar when plugin is uninstalled or reloaded

## 3.3.3 - 2024-08-26

* update documentation

## 3.3.2 - 2024-08-26
Expand Down
122 changes: 39 additions & 83 deletions plugin_qgis_lpo/commons/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@

def sql_timeinterval_cols_builder( # noqa C901
self: QgsProcessingAlgorithm,
time_interval_param,
start_year_param: int,
end_year_param: int,
period_type_filter: str,
time_interval_param: str,
aggregation_type_param: str,
parameters: Dict,
context: QgsProcessingContext,
Expand All @@ -210,41 +209,41 @@
"*" if aggregation_type_param == "Nombre de données" else "DISTINCT t.cd_ref"
)
if time_interval_param == "Par année":
add_five_years = self.parameterAsEnums(
parameters, self.ADD_FIVE_YEARS, context # type: ignore
)
if len(add_five_years) > 0:
if (end_year_param - start_year_param + 1) % 5 != 0:
timestamp = datetime.now()

Check warning on line 212 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L212

Added line #L212 was not covered by tests
if period_type_filter in ("5 dernières années", "10 dernières années", "Pas de filtre temporel"):
end_year = int(timestamp.strftime("%Y"))
period = period_type_filter.split()[0]
start_year = end_year - int(period if period.isdigit() else 10)

Check warning on line 216 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L214-L216

Added lines #L214 - L216 were not covered by tests
years = [str(year) for year in range(start_year,end_year)]

elif period_type_filter == "Cette année":
end_year = int(timestamp.strftime("%Y"))
start_year = end_year
years = [timestamp.strftime("%Y"),]

Check warning on line 222 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L220-L222

Added lines #L220 - L222 were not covered by tests

elif period_type_filter == "Date de début - Date de fin (à définir ci-dessous)":
# Retrieve the start and end dates
start_date = datetime.fromisoformat(self.parameterAsString(parameters, self.START_DATE, context))
end_date = datetime.fromisoformat(self.parameterAsString(parameters, self.END_DATE, context))

Check warning on line 227 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L226-L227

Added lines #L226 - L227 were not covered by tests

if end_date < start_date:
raise QgsProcessingException(
"Veuillez renseigner une période en année qui soit "
"divisible par 5 ! Exemple : 2011 - 2020."
"Veuillez renseigner une date de fin postérieure ou égale à la date de début !"
)
else:
counter = start_year_param
step_limit = start_year_param
while counter <= end_year_param:
select_data.append(
f"""COUNT({count_param}) filter (WHERE date_an={counter}) AS \"{counter}\" """
)
x_var.append(str(counter))
if counter == step_limit + 4:
select_data.append(
f"""COUNT({count_param}) filter (WHERE date_an>={counter-4} and date_an<={counter}) AS \"{counter-4} - {counter}\" """
)
step_limit += 5
counter += 1
else:
for year in range(start_year_param, end_year_param + 1):
end_year = int(end_date.strftime("%Y"))
start_year = int(start_date.strftime("%Y"))

Check warning on line 235 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L234-L235

Added lines #L234 - L235 were not covered by tests
years = [str(year) for year in range(start_year,end_year)]

for year in years:
select_data.append(
f"""COUNT({count_param}) filter (WHERE date_an={year}) AS \"{year}\""""
)
x_var.append(str(year))
select_data.append(
f"""COUNT({count_param}) filter (WHERE date_an>={start_year_param} and date_an<={end_year_param}) AS \"TOTAL\""""
)

else:
start_month = self.parameterAsEnum(parameters, self.START_MONTH, context)
end_month = self.parameterAsEnum(parameters, self.END_MONTH, context)
monthes = self.parameterAsEnums(parameters, self.MONTHES, context)
self.log(message=f'MONTHES {monthes}')

Check warning on line 246 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L245-L246

Added lines #L245 - L246 were not covered by tests
months_numbers_variables = [
"01",
"02",
Expand All @@ -259,60 +258,17 @@
"11",
"12",
]
if start_year_param == end_year_param:
if end_month < start_month:
raise QgsProcessingException(
"Veuillez renseigner un mois de fin postérieur ou égal au mois de début !"
)
else:
for month in range(start_month, end_month + 1):
select_data.append(
f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{start_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {start_year_param}\""""
)
x_var.append(
self._months_names_variables[month] # type: ignore
+ " "
+ str(start_year_param)
)
elif end_year_param == start_year_param + 1:
for month in range(start_month, 12):
select_data.append(
f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{start_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {start_year_param}\""""
)
x_var.append(
self._months_names_variables[month] + " " + str(start_year_param) # type: ignore
for month in monthes:
select_data.append(

Check warning on line 262 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L262

Added line #L262 was not covered by tests
f"""COUNT({count_param}) filter (WHERE extract(month from date)={month+1}) AS \"{self._months_names_variables[month]}\""""
)
for month in range(0, end_month + 1):
select_data.append(
f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{end_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {end_year_param}\""""
)
x_var.append(
self._months_names_variables[month] + " " + str(end_year_param)
)
else:
for month in range(start_month, 12):
select_data.append(
f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{start_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {start_year_param}\""""
)
x_var.append(
self._months_names_variables[month] + " " + str(start_year_param)
)
for year in range(start_year_param + 1, end_year_param):
for month in range(0, 12):
select_data.append(
f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{year}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {year}\""""
)
x_var.append(self._months_names_variables[month] + " " + str(year))
for month in range(0, end_month + 1):
select_data.append(
f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')='{end_year_param}-{months_numbers_variables[month]}') AS \"{self._months_names_variables[month]} {end_year_param}\""""
)
x_var.append(
self._months_names_variables[month] + " " + str(end_year_param)
)
select_data.append(
f"""COUNT({count_param}) filter (WHERE to_char(date, 'YYYY-MM')>='{start_year_param}-{months_numbers_variables[start_month]}' and to_char(date, 'YYYY-MM')<='{end_year_param}-{months_numbers_variables[end_month]}') AS \"TOTAL\""""
)
x_var.append(

Check warning on line 265 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L265

Added line #L265 was not covered by tests
self._months_names_variables[month] # type: ignore
)
# Adding total count
select_data.append(

Check warning on line 269 in plugin_qgis_lpo/commons/helpers.py

View check run for this annotation

Codecov / codecov/patch

plugin_qgis_lpo/commons/helpers.py#L269

Added line #L269 was not covered by tests
f"""COUNT({count_param}) AS \"TOTAL\""""
)
final_select_data = ", ".join(select_data)
feedback.pushDebugInfo(final_select_data)
return final_select_data, x_var
Expand Down
2 changes: 1 addition & 1 deletion plugin_qgis_lpo/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ qgisMinimumVersion=3.16
qgisMaximumVersion=3.99

# versioning
version=3.3.3
version=3.3.5
changelog=https://github.com/lpoaura/PluginQGis-LPOData/blob/master/CHANGELOG.md
Loading