Skip to content

Commit 21a33ef

Browse files
committed
Display sankey from eesyplan method instead of openplan
1 parent 4cf40be commit 21a33ef

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

app/dashboard/views.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
import tempfile
2+
from pathlib import Path
3+
14
import numpy as np
5+
import pandas as pd
26
from django.core.exceptions import PermissionDenied
37
from django.template.loader import get_template
48
from django.db.models import Count, Value, F, Q, Case, When
59
from django.db.models.functions import Concat, Replace
610
from django.http.response import Http404, HttpResponse
11+
from oemof.datapackage import datapackage
12+
from oemof.eesyplan.datapackage.energy_system import create_energy_system_from_dp
13+
714
from dashboard.helpers import *
815
from dashboard.models import (
916
AssetsResults,
@@ -64,7 +71,9 @@
6471
import datetime
6572
import logging
6673
import traceback
67-
from projects.helpers import parameters_helper
74+
from projects.helpers import parameters_helper, add_timeseries_to_database_datapackage
75+
76+
import oemof.eesyplan.postprocessing.graphs as eesyplan_graphs
6877

6978
logger = logging.getLogger(__name__)
7079

@@ -1223,21 +1232,30 @@ def scenario_visualize_sankey(request, scen_id, ts=None):
12231232
is False
12241233
):
12251234
raise PermissionDenied
1226-
if ts is not None:
1227-
ts = int(ts)
1228-
results_json = report_item_render_to_json(
1229-
report_item_id="sankey",
1230-
data=REPORT_GRAPHS[GRAPH_SANKEY](
1231-
simulation=scenario.simulation,
1232-
energy_vector=scenario.energy_vectors,
1233-
timestep=ts,
1234-
),
1235-
title="Sankey",
1236-
report_item_type=GRAPH_SANKEY,
1237-
)
12381235

1236+
# TODO this sankey implementation does not allow for single timesteps
1237+
# if ts is not None:
1238+
# ts = int(ts)
1239+
rebuilt_dp = add_timeseries_to_database_datapackage(scenario)
1240+
1241+
# TODO these flows do not generate the correct sankey result yet
1242+
with tempfile.TemporaryDirectory(prefix="dp_") as td:
1243+
temp_path = Path(td)
1244+
dp_path = datapackage.rebuild_dp_from_json(rebuilt_dp, temp_path)
1245+
es = create_energy_system_from_dp(dp_path)
1246+
1247+
qs = FancyResults.objects.filter(simulation=scenario.simulation)
1248+
1249+
if qs.exists():
1250+
flows = {
1251+
(asset, bus): json.loads(flow_data)
1252+
for asset, bus, flow_data in qs.values_list("asset", "bus", "flow_data")
1253+
}
1254+
flows_df = pd.DataFrame(flows)
1255+
flows_df.index = scenario.get_timestamps()[: len(flows_df)]
1256+
fig, links_df = eesyplan_graphs.sankey(flows=flows_df, es=es)
12391257
return JsonResponse(
1240-
results_json, status=200, content_type="application/json", safe=False
1258+
fig.to_dict(), status=200, content_type="application/json", safe=False
12411259
)
12421260

12431261

app/static/js/update_results_page.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ function scenario_visualize_sankey(scen_id, ts=null){
227227
$.ajax({
228228
url: urlVisualizeSankey + urlParams,
229229
type: "GET",
230-
success: async (parameters) => {
231-
await graph_type_mapping[parameters.type](parameters.id, parameters);
230+
success: async (eesyplan_fig) => {
231+
await Plotly.newPlot("sankey", eesyplan_fig.data, eesyplan_fig.layout);
232232
},
233233
});
234234
}

0 commit comments

Comments
 (0)