Skip to content

Commit

Permalink
refactor: move asset result classes
Browse files Browse the repository at this point in the history
Move asset result mapping and classes to presentation. Makes it more
clear that this is a presentation layer, which exist to provide a json
result to the user.
  • Loading branch information
jsolaas committed Jul 4, 2024
1 parent 1725d53 commit 590d076
Show file tree
Hide file tree
Showing 20 changed files with 93 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/ecalc_cli/io/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from libecalc.application.graph_result import EnergyCalculatorResult, GraphResult
from libecalc.common.run_info import RunInfo
from libecalc.dto.base import EcalcBaseModel
from libecalc.dto.result import EcalcModelResult
from libecalc.presentation.json_result.mapper import get_asset_result
from libecalc.presentation.json_result.result import EcalcModelResult

from ecalc_cli.logger import logger

Expand Down
4 changes: 3 additions & 1 deletion src/ecalc_cli/io/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
from libecalc.application.graph_result import GraphResult
from libecalc.common.run_info import RunInfo
from libecalc.common.time_utils import resample_time_steps
from libecalc.dto.result import EcalcModelResult as EcalcModelResultDTO
from libecalc.infrastructure.file_utils import OutputFormat, get_result_output
from libecalc.presentation.exporter.configs.configs import LTPConfig, STPConfig
from libecalc.presentation.exporter.exporter import Exporter
from libecalc.presentation.exporter.formatters.formatter import CSVFormatter
from libecalc.presentation.exporter.handlers.handler import MultiFileHandler
from libecalc.presentation.flow_diagram.EcalcModelMapper import EcalcModelMapper
from libecalc.presentation.json_result.result import (
EcalcModelResult as EcalcModelResultDTO,
)

from ecalc_cli.errors import EcalcCLIError

Expand Down
11 changes: 9 additions & 2 deletions src/libecalc/core/result/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

from enum import Enum

from pydantic import BaseModel, ConfigDict
from typing_extensions import Self

from libecalc.common.logger import logger
from libecalc.common.string.string_utils import to_camel_case
from libecalc.common.utils.rates import TimeSeries
from libecalc.dto import SingleSpeedChart, VariableSpeedChart
from libecalc.dto.base import EcalcBaseModel


class EcalcResultBaseModel(EcalcBaseModel):
class EcalcResultBaseModel(BaseModel):
model_config = ConfigDict(
extra="forbid",
alias_generator=to_camel_case,
populate_by_name=True,
)

def extend(self, other: Self) -> Self:
"""This is used when merging different time slots when the energy function of a consumer changes over time.
Append method covering all the basics. All additional extend methods needs to be covered in
Expand Down
7 changes: 0 additions & 7 deletions src/libecalc/dto/result/base.py

This file was deleted.

16 changes: 0 additions & 16 deletions src/libecalc/dto/result/types.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/libecalc/infrastructure/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from libecalc.common.datetime.utils import DateTimeFormats
from libecalc.common.logger import logger
from libecalc.dto.result import ComponentResult, EcalcModelResult
from libecalc.presentation.json_result.result import ComponentResult, EcalcModelResult
from libecalc.presentation.simple_result import SimpleResultData


Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/libecalc/presentation/json_result/aggregators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from libecalc.common.utils.rates import (
TimeSeriesBoolean,
)
from libecalc.dto.result.emission import PartialEmissionResult
from libecalc.presentation.json_result.result.emission import PartialEmissionResult


class HasIsValid(Protocol):
Expand Down
66 changes: 36 additions & 30 deletions src/libecalc/presentation/json_result/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from functools import reduce
from typing import Any, Dict, List, Optional, Union

from pydantic import TypeAdapter

import libecalc
from libecalc import dto
from libecalc.application.graph_result import GraphResult
from libecalc.common.component_info.component_level import ComponentLevel
from libecalc.common.component_info.compressor import CompressorPressureType
Expand All @@ -29,20 +30,23 @@
from libecalc.core.result.emission import EmissionResult
from libecalc.dto import CompressorSystemConsumerFunction
from libecalc.dto.base import ComponentType
from libecalc.dto.result.emission import EmissionIntensityResult, PartialEmissionResult
from libecalc.dto.result.results import (
from libecalc.expression import Expression
from libecalc.presentation.json_result.aggregators import (
aggregate_emissions,
aggregate_is_valid,
)
from libecalc.presentation.json_result.result.emission import (
EmissionIntensityResult,
PartialEmissionResult,
)
from libecalc.presentation.json_result.result.results import (
AssetResult,
CompressorModelResult,
CompressorModelStageResult,
CompressorStreamConditionResult,
PumpModelResult,
TurbineModelResult,
)
from libecalc.expression import Expression
from libecalc.presentation.json_result.aggregators import (
aggregate_emissions,
aggregate_is_valid,
)
from pydantic import TypeAdapter


def get_operational_setting_used_id(timestep: datetime, operational_settings_used: TimeSeriesInt) -> int:
Expand Down Expand Up @@ -160,7 +164,9 @@ def _compute_intensity(
return emission_intensities


def _to_full_result(emissions: Dict[str, PartialEmissionResult]) -> Dict[str, libecalc.dto.result.EmissionResult]:
def _to_full_result(
emissions: Dict[str, PartialEmissionResult]
) -> Dict[str, libecalc.presentation.json_result.result.EmissionResult]:
"""
From the partial result, generate cumulatives for the full emissions result per installation
Args:
Expand All @@ -170,7 +176,7 @@ def _to_full_result(emissions: Dict[str, PartialEmissionResult]) -> Dict[str, li
"""
return {
key: libecalc.dto.result.EmissionResult(
key: libecalc.presentation.json_result.result.EmissionResult(
name=key,
timesteps=emissions[key].timesteps,
rate=emissions[key].rate,
Expand Down Expand Up @@ -216,7 +222,7 @@ def _convert_to_timeseries(

def _parse_emissions(
emissions: Dict[str, EmissionResult], regularity: TimeSeriesFloat
) -> Dict[str, libecalc.dto.result.EmissionResult]:
) -> Dict[str, libecalc.presentation.json_result.result.EmissionResult]:
"""
Convert emissions from core result format to dto result format.
Expand All @@ -229,7 +235,7 @@ def _parse_emissions(
"""
return {
key: libecalc.dto.result.EmissionResult(
key: libecalc.presentation.json_result.result.EmissionResult(
name=key,
timesteps=emissions[key].timesteps,
rate=TimeSeriesRate.from_timeseries_stream_day_rate(
Expand Down Expand Up @@ -266,8 +272,8 @@ def _compute_aggregated_power(


def _evaluate_installations(
graph_result: GraphResult, variables_map: dto.VariablesMap
) -> List[libecalc.dto.result.InstallationResult]:
graph_result: GraphResult, variables_map: libecalc.dto.VariablesMap
) -> List[libecalc.presentation.json_result.result.InstallationResult]:
"""
All subcomponents have already been evaluated, here we basically collect and aggregate the results
Expand Down Expand Up @@ -364,7 +370,7 @@ def _evaluate_installations(
)

installation_results.append(
libecalc.dto.result.InstallationResult(
libecalc.presentation.json_result.result.InstallationResult(
id=installation.id,
name=installation_node_info.name,
parent=asset.id,
Expand Down Expand Up @@ -402,11 +408,11 @@ def _evaluate_installations(
return installation_results


def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
def get_asset_result(graph_result: GraphResult) -> libecalc.presentation.json_result.result.results.EcalcModelResult:
asset_id = graph_result.graph.root
asset = graph_result.graph.get_node(asset_id)

if not isinstance(asset, dto.Asset):
if not isinstance(asset, libecalc.dto.Asset):
raise ProgrammingError("Need an asset graph to get asset result")

installation_results = _evaluate_installations(
Expand Down Expand Up @@ -956,7 +962,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
else:
models.extend(
[
TypeAdapter(libecalc.dto.result.ConsumerModelResult).validate_python(
TypeAdapter(libecalc.presentation.json_result.result.ConsumerModelResult).validate_python(
{
**model.model_dump(),
"parent": consumer_id,
Expand Down Expand Up @@ -987,9 +993,9 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
]
)

obj: libecalc.dto.result.ComponentResult
obj: libecalc.presentation.json_result.result.ComponentResult
if consumer_node_info.component_type == ComponentType.GENERATOR_SET:
obj = dto.result.results.GeneratorSetResult(
obj = libecalc.presentation.json_result.result.results.GeneratorSetResult(
name=consumer_node_info.name,
parent=graph_result.graph.get_predecessor(consumer_id),
component_level=consumer_node_info.component_level,
Expand Down Expand Up @@ -1028,7 +1034,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
is_valid=consumer_result.component_result.is_valid,
)
elif consumer_node_info.component_type == ComponentType.PUMP:
obj = dto.result.results.PumpResult(
obj = libecalc.presentation.json_result.result.results.PumpResult(
name=consumer_node_info.name,
parent=graph_result.graph.get_predecessor(consumer_id),
component_level=consumer_node_info.component_level,
Expand Down Expand Up @@ -1071,7 +1077,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
streams=consumer_result.component_result.streams,
)
elif consumer_node_info.component_type == ComponentType.COMPRESSOR:
obj = dto.result.results.CompressorResult(
obj = libecalc.presentation.json_result.result.results.CompressorResult(
name=consumer_node_info.name,
parent=graph_result.graph.get_predecessor(consumer_id),
component_level=consumer_node_info.component_level,
Expand Down Expand Up @@ -1113,7 +1119,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
streams=consumer_result.component_result.streams,
)
elif consumer_node_info.component_type == ComponentType.ASSET:
obj = dto.result.results.AssetResult(
obj = libecalc.presentation.json_result.result.results.AssetResult(
name=consumer_node_info.name,
parent=graph_result.graph.get_predecessor(consumer_id),
component_level=consumer_node_info.component_level,
Expand Down Expand Up @@ -1153,7 +1159,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
is_valid=consumer_result.component_result.is_valid,
)
elif consumer_node_info.component_type == ComponentType.INSTALLATION:
obj = dto.result.results.InstallationResult(
obj = libecalc.presentation.json_result.result.results.InstallationResult(
name=consumer_node_info.name,
parent=graph_result.graph.get_predecessor(consumer_id),
component_level=consumer_node_info.component_level,
Expand Down Expand Up @@ -1190,7 +1196,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
is_valid=consumer_result.component_result.is_valid,
)
elif consumer_node_info.component_type == ComponentType.CONSUMER_SYSTEM_V2:
obj = dto.result.ConsumerSystemResult(
obj = libecalc.presentation.json_result.result.ConsumerSystemResult(
id=consumer_result.component_result.id,
is_valid=consumer_result.component_result.is_valid,
timesteps=consumer_result.component_result.timesteps,
Expand Down Expand Up @@ -1236,7 +1242,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
if consumer_id in graph_result.emission_results
else {}
)
obj = TypeAdapter(libecalc.dto.result.ComponentResult).validate_python(
obj = TypeAdapter(libecalc.presentation.json_result.result.ComponentResult).validate_python(
{
**consumer_result.component_result.model_dump(exclude={"typ"}),
"name": consumer_node_info.name,
Expand Down Expand Up @@ -1301,7 +1307,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
for venting_emitter in installation.venting_emitters:
energy_usage = time_series_zero
sub_components.append(
libecalc.dto.result.VentingEmitterResult(
libecalc.presentation.json_result.result.VentingEmitterResult(
id=venting_emitter.id,
name=venting_emitter.name,
componentType=venting_emitter.component_type,
Expand Down Expand Up @@ -1401,7 +1407,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
asset_energy_usage_cumulative = asset_energy_usage_core.to_volumes().cumulative()

asset_node_info = graph_result.graph.get_node_info(asset_id)
asset_result_dto = libecalc.dto.result.AssetResult(
asset_result_dto = AssetResult(
id=asset.id,
name=asset_node_info.name,
component_level=asset_node_info.component_level,
Expand Down Expand Up @@ -1433,7 +1439,7 @@ def get_asset_result(graph_result: GraphResult) -> dto.result.EcalcModelResult:
)

return Numbers.format_results_to_precision(
libecalc.dto.result.EcalcModelResult(
libecalc.presentation.json_result.result.results.EcalcModelResult(
component_result=asset_result_dto,
sub_components=sub_components,
models=models,
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions src/libecalc/presentation/json_result/result/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations

from pydantic import BaseModel, ConfigDict

from libecalc.common.string.string_utils import to_camel_case


class EcalcResultBaseModel(BaseModel):
model_config = ConfigDict(
extra="forbid",
alias_generator=to_camel_case,
populate_by_name=True,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
TimeSeriesVolumesCumulative,
)
from libecalc.core.result.emission import EmissionResult as EmissionCoreResult
from libecalc.dto.result.tabular_time_series import TabularTimeSeries
from libecalc.presentation.json_result.result.tabular_time_series import (
TabularTimeSeries,
)


class EmissionResult(TabularTimeSeries):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from operator import attrgetter
from typing import Any, Dict, List, Literal, Optional, Union

from _operator import attrgetter
from pydantic import Field, field_validator
from pydantic_core.core_schema import ValidationInfo
from typing_extensions import Annotated
Expand All @@ -25,9 +25,14 @@
)
from libecalc.dto.base import ComponentType
from libecalc.dto.models import SingleSpeedChart, VariableSpeedChart
from libecalc.dto.result.base import EcalcResultBaseModel
from libecalc.dto.result.emission import EmissionIntensityResult, EmissionResult
from libecalc.dto.result.tabular_time_series import TabularTimeSeries
from libecalc.presentation.json_result.result.base import EcalcResultBaseModel
from libecalc.presentation.json_result.result.emission import (
EmissionIntensityResult,
EmissionResult,
)
from libecalc.presentation.json_result.result.tabular_time_series import (
TabularTimeSeries,
)


class NodeInfo(EcalcResultBaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
TimeSeriesRate,
TimeSeriesVolumesCumulative,
)
from libecalc.dto.result.base import EcalcResultBaseModel
from libecalc.presentation.json_result.result.base import EcalcResultBaseModel


class TabularTimeSeries(ABC, EcalcResultBaseModel):
Expand Down
Loading

0 comments on commit 590d076

Please sign in to comment.