Skip to content

Conversation

@BelhsanHmida
Copy link
Contributor

@BelhsanHmida BelhsanHmida commented Sep 2, 2024

Description

This PR introduces changes to prevent string length exceeding column size in the AssetAuditLog by implementing updating audit_log for each asset or attribute change individually and truncation for long strings (exceeding 255).

  • Added the truncate_string function to handle string truncation, ensuring compliance with the 255-character limit on the event field in the AssetAuditLog table by adding ... in middle of string.

  • Updated the AssetAuditLog class to ensure each asset update or feature change is logged as a separate entry. This prevents exceeding the column size limit by breaking up large events into individual records.

Look & Feel

changing an asset attribute From:

{  "capacity_in_mw": 0.5, 
   "min_soc_in_mwh": 0.05, 
   "max_soc_in_mwh": 0.45, 
   "sensors_to_show": [{"title": "Test Title 1", "sensors": [1]}, {"title": "Test Title 2", "sensors": [3,2]}, {"title": "Test Title 3", "sensors": [2,5]}, {"title": "Test Title 4", "sensors": [1]} ,{"title": "Test Title 5", "sensors": [1]}]}

To:

{  "capacity_in_mw": 0.05, 
   "min_soc_in_mwh": 0.005, 
   "max_soc_in_mwh": 0.045, 
   "sensors_to_show": [{"title": "New Test Title 1", "sensors": [1]}, {"title": "Test Title 2", "sensors": [3,2]}, {"title": "Test Title 3", "sensors": [2,5]}, {"title": "Test Title 4", "sensors": [1]} ,{"title": "Test Title 5", "sensors": [1]}]}

would result in 5 audit_log instead of 1 long one that would exceed column length:

  • Updated asset 'toy-battery': 3 fields: Attr: capacity_in_mw, From: 0.5, To: 0.05
  • Updated asset 'toy-battery': 3 fields: Attr: max_soc_in_mwh, From: 0.45, To: 0.045
  • Updated asset 'toy-battery': 3 fields: Attr: min_soc_in_mwh, From: 0.05, To: 5
  • Updated asset 'toy-battery-1': 3 fields: Attr: sensors_to_show, From: [{'title': 'Test Title 1', 'sensors': [1]}, {'title': ' ... e': 'Test Title 3', 'sensors': [2, 5]}, {'title': 'Test Title 4', 'sensors': [1]}, {'title': 'Test Title 5', 'sensors': [1]}] (This event was truncated by adding ' ... ' in middle because event was over 255 limit)

How to test

  1. Reproduce the Issue:

    • Modify the sensors_to_show field with JSON data such as:
      {
        "sensors_to_show": [[129, 125, 119], [129, 125, 119], [129, 125, 119], [129, 125, 119], [129, 125, 119], [129, 125, 119], [129, 125, 119]]
      }
    • Alternatively, use a long title in sensors_to_show to trigger the error.
  2. Verify Fix:

    • Test adding or updating asset pages with lengthy data or titles and confirm that no DataError occurs.
    • Ensure that each update is logged in the audit log properly and are truncated appropriately and do not exceed the column constraints.

Further Improvements

  • Review and potentially adjust truncation logic if further edge cases are identified.
  • Increase audit_log column size to minimize truncation and information loss.

Related Items

closes #1136


  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures.

…dit logs and asset attribute changes

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@BelhsanHmida BelhsanHmida changed the title Fix DataError Due to String Length Exceeding Column Constraint in AssetAuditLog #1136 Fix DataError Due to String Length Exceeding Column Constraint in AssetAuditLog Sep 2, 2024
Copy link
Contributor

@nhoening nhoening left a comment

Choose a reason for hiding this comment

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

It tested well!
I only have documentation improvements, let's do this.

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
BelhsanHmida added a commit that referenced this pull request Sep 6, 2024
…event From PR #1162

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Copy link
Contributor

@nhoening nhoening left a comment

Choose a reason for hiding this comment

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

This is adding lots of code that is hard to read, with the goal to make one message for potentially many changes at once. One more complexity problem can be seen in my one comment.

Idea: We do it simpler. Each updated field or attribute gets its own audit log, by AssetAuditLog.add_record_for_attribute_update() or AssetAuditLog.add_record().

We buy simplicity with sometimes more than one log entry. That will not happen too often I believe. And the asset audit log will be more readable.

Each message is probably short enough, but these two helper methods can still make sure by truncating if needed.

@nhoening nhoening added this to the 0.23.0 milestone Sep 10, 2024
BelhsanHmida added a commit that referenced this pull request Sep 11, 2024
…sts before PR #1162 resolves file changes

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
nhoening added a commit that referenced this pull request Sep 12, 2024
* Add support for adding titles to asset page graphs

* Add support for adding titles to asset page graphs #1119

* Update belief_charts.py

Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>

* Update belief_charts.py

Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>

* Update belief_charts.py

Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>

* Update Files

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Refactor: Move sensor_to_show to SensorToShowSchema from GenericAssetSchema and Able to handle both 'sensor' and 'sensors' inputs

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Update: Modify sensors_to_show method in GenericAsset to handle both 'sensor' and 'sensors' inputs

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Update: chart_for_multiple_sensors to handle both 'sensor' and 'sensors' inputs

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Update: flatten_unique to handle both 'sensor' a
nd 'sensors' inputs

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Resolve audit_log by truncating event string

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* update assets.py and audit_log with new truncate string and truncate event From PR #1162

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* refactor chart_for_multiple_sensors to show titles as graph titles not axis titles

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Refactor SensorsToShowSchema to handle true deserialization and validation

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* refactor sensors_to_show property to utilize SensorsToShowSchema

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* refactor flatten_unique

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Add Tests for SensorsToShowSchema

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* run pre-commit

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* update test_alter_an_asset_with_bad_json_attributes params

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Improve validation error messages for 'sensors_to_show'

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Update tests to handle new event string and validation error message for 'sensors_to_show'

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Temporarily refactor audit_log.py and assets.py to address failing tests before PR #1162 resolves file changes

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* Add changelog entry and note in docs asset page

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>

* focus changelog entry more on the end result (what users care about)

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

* give the sensors_to_show attribute a more meaningful place in the docs

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

---------

Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Nicolas Höning <nicolas@seita.nl>
Co-authored-by: Nicolas Höning <nicolas@seita.nl>
BelhsanHmida and others added 4 commits September 13, 2024 17:32
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <149331360+BelhsanHmida@users.noreply.github.com>
… update get's it's own entry

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
@nhoening
Copy link
Contributor

Can you bring the PR description up to date?

Copy link
Contributor

@nhoening nhoening left a comment

Choose a reason for hiding this comment

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

Thanks, it (still) works.
Can you quickly add a changelog entry and add one semi-colon for me?

Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Copy link
Contributor

@nhoening nhoening left a comment

Choose a reason for hiding this comment

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

Thanks!

@nhoening nhoening merged commit db758a0 into main Sep 16, 2024
@nhoening nhoening deleted the fix/AuditLog-String-Truncation branch September 16, 2024 14:18
@Flix6x Flix6x added the UI label Feb 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataError Due to String Length Exceeding Column Constraint in AssetAuditLog

4 participants