Skip to content

Conversation

@cyiallou
Copy link
Contributor

This PR introduces a new state_analysis module for analysing microgrid component state transitions using the Reporting API. It includes:

  • A StateRecord named tuple for structured representation of state changes.
  • Logic to fetch, extract, and group state/warning/error transitions.
  • Filtering support for alert detection based on ComponentStateCode values.
  • A private _resolve_enum_name() helper for translating raw integer states to descriptive enum names.
  • Added # type: ignore[arg-type] temporarily to ignore mypy errors raised for the deprecated from_proto method. This will be replaced in the neear future when the reporting and weather clients are updated to the latest client-common and common-api versions - currently, it will raise all sorts of errors when one tries to use enum_from_proto from client-common v0.3.3.
  • Tests.

Copilot AI review requested due to automatic review settings July 16, 2025 15:14
@cyiallou cyiallou requested a review from a team as a code owner July 16, 2025 15:14
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests labels Jul 16, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new state_analysis module for analyzing microgrid component state transitions using the Reporting API. The module provides functionality to extract component state changes, detect alerts, and convert raw state values into human-readable enum names.

Key changes include:

  • A new StateRecord named tuple for structured representation of state transitions
  • Logic to fetch, process, and filter component state data for alert detection
  • Support for warnings and error states with configurable filtering

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/frequenz/lib/notebooks/reporting/_state_records.py Defines the StateRecord named tuple data structure
src/frequenz/lib/notebooks/reporting/state_analysis.py Core implementation with state extraction, filtering, and API client integration
tests/test_state_analysis.py Comprehensive test suite covering state extraction and filtering scenarios
RELEASE_NOTES.md Documents the new state analysis functionality

@@ -0,0 +1,34 @@
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
Copy link

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright year should be 2025 to match the current year mentioned in the PR description and other files in the codebase.

Suggested change
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update.

@@ -0,0 +1,302 @@
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
Copy link

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright year should be 2025 to match the current year mentioned in the PR description and other files in the codebase.

Suggested change
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update.

- Added consistent logger setup across all modules for structured logging and improved observability. Example notebooks updated to demonstrate logger usage.
- The signature for passing config files MicrogridConfig.load_config() has been changed to accept a path a list of paths and a directory containing the config files.
- `MicrogridData` class needs to be initialized with a `MicrogridConfig` object instead of a path to config file(s).
- Added a new `state_analysis` module for detecting and analysing component state transitions and alerts from reporting data.
Copy link

Copilot AI Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The word 'analysing' should be spelled 'analyzing' to match American English spelling conventions used elsewhere in the codebase.

Suggested change
- Added a new `state_analysis` module for detecting and analysing component state transitions and alerts from reporting data.
- Added a new `state_analysis` module for detecting and analyzing component state transitions and alerts from reporting data.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually lean towards British english, thank you.

@cyiallou
Copy link
Contributor Author

  • The first commit moves the code from here as is.
  • The second commit fixes a bug in tracking error/warning state transitions which required some refactoring.

@cyiallou cyiallou force-pushed the feat/add-component-state branch from 8a8f3a7 to 8905696 Compare July 16, 2025 15:19
cwasicki
cwasicki previously approved these changes Jul 22, 2025
cyiallou added 2 commits July 22, 2025 23:02
…and alerts

This introduces a new `state_analysis` module for analysing microgrid
component state transitions using the Reporting API. It includes:
- A `StateRecord` named tuple for structured representation of state
  changes.
- Logic to fetch, extract, and group state/warning/error transitions.
- Filtering support for alert detection based on `ComponentStateCode`
  values.
- A private `_resolve_enum_name()` helper for translating raw integer
  states to descriptive enum names.
- Added `# type: ignore[arg-type]` temporarily to ignore `mypy` errors
  raised for the deprecated `from_proto` method. This will be replaced
  in the neear future when the reporting and weather clients are updated
  to the latest `client-common` and `common-api` versions - currently, it
  will raise all sorts of errors when one tries to use `enum_from_proto`
  from `client-common` `v0.3.3`.
- Tests.

Signed-off-by: cyiallou - Costas <[email protected]>
Previously, error and warning metric samples were tracked independently
of the component's primary `state` metric. This led to incorrect or
unbounded durations when the component transitioned into or out of
`ERROR` state.

This patch changes the extraction logic to emit error and warning
`StateRecords` only while the component is in `ERROR` state, ensuring
accurate segmentation.

Key changes:
- `_extract_state_records()` now groups samples by (microgrid_id,
  component_id) and collects state, error, and optional warning metrics
  together.
- `_process_sample_group()` was rewritten to iterate over state samples
  and conditionally emit error and warning transitions aligned with the
  `ERROR` window.
- Introduced an `emit()` helper to simplify record generation.

Fixes:
- ERROR segments with repeated values never closed
- ERROR durations overshot actual ERROR state window

Test suite has been updated to reflect the stricter state coordination.

Signed-off-by: cyiallou - Costas <[email protected]>
@cyiallou
Copy link
Contributor Author

rebased to latest main to resolve conflict in release notes. Requires reapproval

@cyiallou cyiallou added this pull request to the merge queue Jul 22, 2025
Merged via the queue into frequenz-floss:v0.x.x with commit 364ca41 Jul 22, 2025
5 checks passed
@cyiallou cyiallou deleted the feat/add-component-state branch July 22, 2025 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants