-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix] Consumption were not calculated on resource deletion
- Loading branch information
Showing
7 changed files
with
126 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
39 changes: 39 additions & 0 deletions
39
resource_tracker_v2/migrations/0006_force_calculate_consumption.py
This file contains 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,39 @@ | ||
# Generated by Django 4.2.5 on 2024-04-24 13:49 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def update_all_resource_consumption(apps, schema_editor): | ||
Transformer = apps.get_model('resource_tracker_v2', 'Transformer') | ||
ResourceAttribute = apps.get_model('resource_tracker_v2', 'ResourceAttribute') | ||
|
||
for transformer in Transformer.objects.all(): | ||
# update total produced | ||
total_produced = 0 | ||
all_resource_att = ResourceAttribute.objects.filter( | ||
resource_id__in=transformer.resource_group.resources.values("id"), | ||
attribute_definition=transformer.attribute_definition) | ||
for attribute in all_resource_att: | ||
total_produced += attribute.value | ||
transformer.total_produced = total_produced | ||
transformer.save() | ||
|
||
# update total consumed | ||
total_consumed = 0 | ||
child_transformers = Transformer.objects.filter(consume_from_attribute_definition=transformer.attribute_definition, | ||
consume_from_resource_group=transformer.resource_group) | ||
for child_transformer in child_transformers: | ||
total_consumed += child_transformer.total_produced / child_transformer.factor | ||
transformer.total_consumed = total_consumed | ||
transformer.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("resource_tracker_v2", "0005_auto_20230803_1126"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_all_resource_consumption), | ||
] |
This file contains 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 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 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 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 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