forked from isichos/epa
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/implement-timeseries-model' into test_timeserie…
…s_cap_rover
- Loading branch information
Showing
11 changed files
with
220 additions
and
43 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
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
16 changes: 16 additions & 0 deletions
16
app/projects/migrations/0020_rename_input_timeseries_asset_input_timeseries_old.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,16 @@ | ||
# Generated by Django 4.2.4 on 2024-10-10 14:37 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("projects", "0019_timeseries")] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="asset", | ||
old_name="input_timeseries", | ||
new_name="input_timeseries_old", | ||
) | ||
] |
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,23 @@ | ||
# Generated by Django 4.2.4 on 2024-10-10 14:38 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("projects", "0020_rename_input_timeseries_asset_input_timeseries_old") | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="asset", | ||
name="input_timeseries", | ||
field=models.ForeignKey( | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to="projects.timeseries", | ||
), | ||
) | ||
] |
17 changes: 17 additions & 0 deletions
17
app/projects/migrations/0022_rename_end_time_timeseries_end_date_and_more.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,17 @@ | ||
# Generated by Django 4.2.4 on 2024-10-14 15:19 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("projects", "0021_asset_input_timeseries")] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="timeseries", old_name="end_time", new_name="end_date" | ||
), | ||
migrations.RenameField( | ||
model_name="timeseries", old_name="start_time", new_name="start_date" | ||
), | ||
] |
82 changes: 82 additions & 0 deletions
82
app/projects/migrations/0023_migrate_timeseries_to_model.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,82 @@ | ||
from django.db import migrations | ||
from django.db.models import Q | ||
import json | ||
from datetime import timedelta | ||
|
||
|
||
def convert_timeseries_to_model(apps, schema_editor): | ||
""" | ||
Forward migration: Convert timeseries_old to Timeseries instance | ||
""" | ||
# Get historical models | ||
Asset = apps.get_model('projects', 'Asset') | ||
Timeseries = apps.get_model('projects', 'Timeseries') | ||
db_alias = schema_editor.connection.alias | ||
|
||
# Iterate through all assets with timeseries_old data | ||
for asset in Asset.objects.using(db_alias).exclude(Q(input_timeseries_old__isnull=True) | Q(input_timeseries_old=[])): | ||
try: | ||
# Calculate end time from asset start date and duration | ||
duration = asset.scenario.evaluated_period | ||
total_duration = timedelta(hours=asset.scenario.time_step) * duration | ||
end_date = asset.scenario.start_date + total_duration | ||
|
||
# Create new Timeseries instance | ||
timeseries = Timeseries.objects.using(db_alias).create( | ||
name=f"{asset.name}_migration", | ||
user=asset.scenario.project.user, | ||
scenario=asset.scenario, | ||
values=json.loads(asset.input_timeseries_old), | ||
ts_type=asset.asset_type.mvs_type, | ||
open_source=False, | ||
start_date=asset.scenario.start_date, | ||
time_step=asset.scenario.time_step, | ||
end_date=end_date | ||
) | ||
|
||
# Update asset to point to new timeseries | ||
asset.input_timeseries = timeseries | ||
asset.save() | ||
|
||
except Exception as e: | ||
print(f"Error migrating asset {asset.id} timeseries: {str(e)}") | ||
raise e | ||
|
||
|
||
def reverse_timeseries_conversion(apps, schema_editor): | ||
""" | ||
Reverse migration: Delete created Timeseries instances and restore old data | ||
""" | ||
Asset = apps.get_model('projects', 'Asset') | ||
Timeseries = apps.get_model('projects', 'Timeseries') | ||
db_alias = schema_editor.connection.alias | ||
|
||
try: | ||
# Find all timeseries created by this migration | ||
migration_timeseries = Timeseries.objects.using(db_alias).filter(name__contains="_migration") | ||
|
||
# Update assets to remove reference to timeseries | ||
Asset.objects.using(db_alias).filter(input_timeseries__in=migration_timeseries).update( | ||
input_timeseries=None | ||
) | ||
|
||
# Delete the timeseries instances | ||
migration_timeseries.delete() | ||
|
||
except Exception as e: | ||
print(f"Error deleting migrated timeseries: {str(e)}") | ||
raise e | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("projects", "0022_rename_end_time_timeseries_end_date_and_more")] | ||
|
||
operations = [ | ||
# Run the timeseries migration | ||
migrations.RunPython( | ||
convert_timeseries_to_model, | ||
reverse_timeseries_conversion | ||
), | ||
] | ||
|
Oops, something went wrong.