Repository navigation
Improve Monthly/Yearly Log #3829
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
Merged
LKuemmel
merged 10 commits into
openWB:master
from
AlexanderHa98:feature_improve_monthly_log
Sep 28, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d97f8c
Improve Log
AlexanderHa98 6e76e6f
Move generate_totals-Logger in logger.py
AlexanderHa98 50bb1ca
Fix
AlexanderHa98 8b2d651
Fix Circular Import
AlexanderHa98 ca79e8b
Update .gitignore
AlexanderHa98 76142ee
fix
AlexanderHa98 0552a87
Ensure ramdisk folder exists
AlexanderHa98 07fc783
retrigger Copilot
AlexanderHa98 85ae6ed
Add Copilot suggestions
AlexanderHa98 59b1ccb
fix
AlexanderHa98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/helpermodules/measurement_logging/generate_totals_subprocess.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import fcntl | ||
| import json | ||
| from pathlib import Path | ||
| from datetime import date | ||
|
|
||
| from typing import List | ||
| from helpermodules import logger, pub | ||
| from helpermodules.measurement_logging.process_log import (save_daily_source_totals, | ||
| save_monthly_source_totals) | ||
|
|
||
| import logging | ||
|
|
||
|
|
||
| BASE_PATH = Path(__file__).resolve().parents[3] | ||
| logger.setup_generate_totals_logging(include_process_log=True) | ||
| log = logging.getLogger("generate_totals") | ||
|
|
||
| LOCK_FILE = BASE_PATH / "data" / "generate_totals.lock" | ||
|
|
||
|
|
||
| def get_all_days_to_calc(): | ||
| try: | ||
| daily_log_dir = BASE_PATH / "data" / "daily_log" | ||
| if not daily_log_dir.is_dir(): | ||
| return None | ||
|
|
||
| today_stem = date.today().strftime("%Y%m%d") | ||
| daily_log_files = sorted( | ||
| [p for p in daily_log_dir.glob("*.json") | ||
| if p.stem.isdigit() and p.stem < today_stem], | ||
| key=lambda p: p.stem, | ||
| ) | ||
|
|
||
| log.debug(f"Anzahl zu berechnender Tageslogs: {len(daily_log_files)}") | ||
| return daily_log_files | ||
| except Exception: | ||
| log.debug("Fehler beim Abrufen der Tageslogs. Es werden keine Tageslogs berechnet.") | ||
| return None | ||
|
|
||
|
|
||
| def get_all_months_to_calc(days: List[str] = None): | ||
| if days is None: | ||
| return None | ||
| current_month = date.today().strftime("%Y%m") | ||
| months = sorted({p.stem[:6] for p in days if len(p.stem) == 8 and p.stem[:6] < current_month}) | ||
| log.debug(f"Anzahl zu berechnender Monatslogs: {len(months)}") | ||
| return months | ||
|
|
||
|
|
||
| def _generate_totals(): | ||
|
|
||
| errors = 0 | ||
| days_to_calc = get_all_days_to_calc() or [] | ||
| months_to_calc = get_all_months_to_calc(days_to_calc) or [] | ||
|
|
||
| for day in days_to_calc: | ||
| try: | ||
| save_daily_source_totals(day.stem, saving=True) | ||
| except Exception: | ||
| log.exception(f"Fehler beim Generieren der Tageserträge für {day.stem}, Tag wird übersprungen.") | ||
| errors += 1 | ||
| continue | ||
|
|
||
| for month in months_to_calc: | ||
| try: | ||
| save_monthly_source_totals(month, saving=True) | ||
| except Exception: | ||
| log.exception(f"Fehler beim Generieren der Monatswerte für {month}, Monat wird übersprungen.") | ||
| errors += 1 | ||
| continue | ||
|
|
||
| log.info(f"Totals-Migration abgeschlossen. Fehlerhafte Logs: {errors}.") | ||
| publisher = pub.Pub() | ||
| publish_info = publisher.publisher.client.publish( | ||
| "openWB/set/system/log_data_ready", json.dumps(True), qos=1, retain=True) | ||
| publish_info.wait_for_publish() | ||
|
|
||
|
|
||
| def generate_totals(): | ||
| LOCK_FILE.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| with LOCK_FILE.open("w") as lock_file: | ||
| try: | ||
| fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) | ||
| except Exception: | ||
| log.exception("generate_totals_subprocess läuft bereits. " | ||
| "Der zweite Prozess wird beendet.") | ||
| return | ||
| _generate_totals() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| generate_totals() |
43 changes: 43 additions & 0 deletions
43
packages/helpermodules/measurement_logging/generate_totals_subprocess_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| from helpermodules.measurement_logging import generate_totals_subprocess | ||
|
|
||
|
|
||
| def test_generate_totals_calls_generator_when_lock_is_acquired(monkeypatch, tmp_path): | ||
| # setup | ||
| lock_file = tmp_path / "locks" / "generate_totals.lock" | ||
| monkeypatch.setattr(generate_totals_subprocess, "LOCK_FILE", lock_file) | ||
|
|
||
| flock_mock = Mock() | ||
| generate_mock = Mock() | ||
|
|
||
| monkeypatch.setattr(generate_totals_subprocess.fcntl, "flock", flock_mock) | ||
| monkeypatch.setattr(generate_totals_subprocess, "_generate_totals", generate_mock) | ||
|
|
||
| # execution | ||
| generate_totals_subprocess.generate_totals() | ||
|
|
||
| # evaluation | ||
| flock_mock.assert_called_once() | ||
| generate_mock.assert_called_once() | ||
| assert lock_file.parent.is_dir() | ||
|
|
||
|
|
||
| def test_generate_totals_skips_generator_when_lock_is_not_acquired(monkeypatch, tmp_path): | ||
| # setup | ||
| lock_file = tmp_path / "locks" / "generate_totals.lock" | ||
| monkeypatch.setattr(generate_totals_subprocess, "LOCK_FILE", lock_file) | ||
|
|
||
| flock_mock = Mock(side_effect=RuntimeError("already locked")) | ||
| generate_mock = Mock() | ||
|
|
||
| monkeypatch.setattr(generate_totals_subprocess.fcntl, "flock", flock_mock) | ||
| monkeypatch.setattr(generate_totals_subprocess, "_generate_totals", generate_mock) | ||
|
|
||
| # execution | ||
| generate_totals_subprocess.generate_totals() | ||
|
|
||
| # evaluation | ||
| flock_mock.assert_called_once() | ||
| generate_mock.assert_not_called() | ||
| assert lock_file.parent.is_dir() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.