Skip to content

Commit

Permalink
refactor: remove need for id on components etc (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeeeJay authored Jan 15, 2024
1 parent b5d3451 commit f9e32ae
Show file tree
Hide file tree
Showing 14 changed files with 37,070 additions and 37,066 deletions.
2 changes: 1 addition & 1 deletion src/libecalc/common/stream_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def mix(self, *other_streams: "TimeSeriesStreamConditions") -> "TimeSeriesStream

return TimeSeriesStreamConditions(
id=generate_id(*[stream.id for stream in streams]),
name=f"Mixed-{'-'.join(stream.name for stream in streams)}",
name=f"{'-'.join(stream.name for stream in streams)}",
rate=reduce(operator.add, [stream.rate for stream in streams]),
pressure=target_pressure,
fluid_density=self.fluid_density, # TODO: Check that they are equal? Or handle it?
Expand Down
12 changes: 8 additions & 4 deletions src/libecalc/common/string/string_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import hashlib
from typing import Iterable, Set


Expand All @@ -14,13 +13,18 @@ def get_duplicates(names: Iterable[str]) -> Set[str]:


def generate_id(*args: str) -> str:
"""Generate an id from one or more strings. The string is encoded to avoid it being used to get other info than
"""
Deprecated: When names were not unique, this was necessary in order to make names unique based on context/hierarchy. Now names should
be unique for any part of the eCalc model that supports names, and it should therefore not be needed any more.
TODO: First step is to make the function return the string as normal, next step is to remove it altogether.
Generate an id from one or more strings. The string is encoded to avoid it being used to get other info than
the id, i.e. it should not be used to get the name of a consumer, even if the name might be used to create the id.
If there are many strings they are joined together.
"""
full_string = "-".join(args)
return hashlib.md5(full_string.encode()).hexdigest() # noqa: S324 - insecure hash for ids
return "-".join(args)


def to_camel_case(string: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/libecalc/domain/stream_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def mix(self, *other_stream_conditions: StreamConditions) -> StreamConditions:

return StreamConditions(
id=generate_id(*[stream.id for stream in streams]),
name=f"Mixed-{'-'.join(stream.name for stream in streams)}",
name=f"{'-'.join(stream.name for stream in streams)}",
timestep=self.timestep,
rate=reduce(operator.add, [stream.rate for stream in streams]),
pressure=target_pressure,
Expand Down
4 changes: 2 additions & 2 deletions src/libecalc/dto/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def evaluate_stream_conditions(
parsed_priorities[priority_name][generate_id(consumer_name)] = [
TimeSeriesStreamConditions(
id=generate_id(consumer_name, stream_name),
name=stream_name,
name="-".join([consumer_name, stream_name]),
rate=TimeSeriesStreamDayRate(
timesteps=variables_map.time_vector,
values=list(
Expand Down Expand Up @@ -371,7 +371,7 @@ def get_graph(self) -> ComponentGraph:
class Asset(Component):
@property
def id(self):
return generate_id(self.component_type, self.name)
return generate_id(self.name)

component_type: Literal[ComponentType.ASSET] = ComponentType.ASSET

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
33178.86
]
},
"id": "2c369e853d9f77089ab26d579918b199",
"id": "Sea water injection pump",
"is_valid": {
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -112,7 +112,7 @@
]
},
"name": "Sea water injection pump",
"parent": "df82601448d0dfff409ca827eb200bf7",
"parent": "Generator set A",
"power": {
"rate_type": "STREAM_DAY",
"regularity": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
33178.86
]
},
"id": "2c369e853d9f77089ab26d579918b199",
"id": "Sea water injection pump",
"is_valid": {
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -112,7 +112,7 @@
]
},
"name": "Sea water injection pump",
"parent": "df82601448d0dfff409ca827eb200bf7",
"parent": "Generator set A",
"power": {
"rate_type": "STREAM_DAY",
"regularity": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
{
"componentType": "INSTALLATION",
"component_level": "INSTALLATION",
"parent": "c0cdc34263d4b970f2f3e6cd5c68324d",
"parent": "model",
"name": "Installation A",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -180,7 +180,7 @@
{
"componentType": "GENERATOR_SET",
"component_level": "GENERATOR_SET",
"parent": "e852d5d4b9101f154db921ece82a697c",
"parent": "Installation A",
"name": "Generator set A",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -262,7 +262,7 @@
{
"componentType": "COMPRESSOR",
"component_level": "CONSUMER",
"parent": "e852d5d4b9101f154db921ece82a697c",
"parent": "Installation A",
"name": "Gas export compressor",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -330,7 +330,7 @@
{
"componentType": "COMPRESSOR",
"component_level": "CONSUMER",
"parent": "df82601448d0dfff409ca827eb200bf7",
"parent": "Generator set A",
"name": "Gas injection compressor",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -394,7 +394,7 @@
{
"componentType": "PUMP",
"component_level": "CONSUMER",
"parent": "df82601448d0dfff409ca827eb200bf7",
"parent": "Generator set A",
"name": "Produced water reinjection pump",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -458,7 +458,7 @@
{
"componentType": "GENERIC",
"component_level": "CONSUMER",
"parent": "df82601448d0dfff409ca827eb200bf7",
"parent": "Generator set A",
"name": "Base production load",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -522,7 +522,7 @@
{
"componentType": "GENERIC",
"component_level": "CONSUMER",
"parent": "e852d5d4b9101f154db921ece82a697c",
"parent": "Installation A",
"name": "Flare",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down Expand Up @@ -590,7 +590,7 @@
{
"componentType": "GENERIC",
"component_level": "CONSUMER",
"parent": "df82601448d0dfff409ca827eb200bf7",
"parent": "Generator set A",
"name": "Sea water injection pump",
"timesteps": [
"2020-01-01 00:00:00",
Expand Down
Loading

0 comments on commit f9e32ae

Please sign in to comment.