fix: CRITICAL - Do not re-sort rows by NodeNum after MySQL ORDER BY consolidation key
Bug: After fetching rows ordered by consolidation key (UnitName, ToolNameID,
EventDate, EventTime) from MySQL, code was re-sorting by NodeNum. This breaks
the grouping because rows with different consolidation keys get intermixed.
Example of what was happening:
- MySQL returns: (Unit1, Tool1, Date1, Time1, Node1),
(Unit1, Tool1, Date1, Time1, Node12),
(Unit2, Tool2, Date2, Time2, Node1)
- Re-sorting by NodeNum gives: (Unit1, Tool1, Date1, Time1, Node1),
(Unit2, Tool2, Date2, Time2, Node1),
(Unit1, Tool1, Date1, Time1, Node12)
- Result: Different consolidation keys are now mixed, each node becomes separate group!
Fix: Remove the re-sort. Trust MySQL's ORDER BY to keep rows of same key together.
The clustering nature of InnoDB ensures rows with same consolidation key are
physically adjacent.
This was causing 1 row per node instead of consolidating all nodes of same
measurement into 1 row.
🤖 Generated with Claude Code
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -275,16 +275,16 @@ class MySQLConnector:
|
|||||||
if not rows:
|
if not rows:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Sort rows by NodeNum within the batch
|
# DO NOT re-sort by NodeNum! The database has already ordered by consolidation key,
|
||||||
# (rows already grouped by consolidation key from ORDER BY)
|
# and re-sorting breaks the grouping. Rows with same key will not be consecutive anymore.
|
||||||
sorted_rows = sorted(rows, key=lambda r: int(r.get("NodeNum") or 0))
|
# We trust the MySQL ORDER BY to keep all rows of same key together.
|
||||||
|
|
||||||
# Group rows by consolidation key
|
# Group rows by consolidation key
|
||||||
# Since rows are already ordered by key, all rows with same key are consecutive
|
# Since rows are already ordered by key from MySQL ORDER BY, all rows with same key are consecutive
|
||||||
current_group = []
|
current_group = []
|
||||||
current_key = None
|
current_key = None
|
||||||
|
|
||||||
for row in sorted_rows:
|
for row in rows:
|
||||||
key = (
|
key = (
|
||||||
row.get("UnitName"),
|
row.get("UnitName"),
|
||||||
row.get("ToolNameID"),
|
row.get("ToolNameID"),
|
||||||
|
|||||||
Reference in New Issue
Block a user