Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Limmen committed Aug 8, 2023
1 parent 13e7af2 commit e34a127
Show file tree
Hide file tree
Showing 9 changed files with 454 additions and 202 deletions.
8 changes: 4 additions & 4 deletions simulation-system/libs/csle-cli/src/csle_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from csle_common.util.cluster_util import ClusterUtil
from csle_common.util.general_util import GeneralUtil
from csle_cluster.cluster_manager.cluster_controller import ClusterController
from csle_cluster.cluster_manager.cluster_manager_pb2 import DockerContainerDTO, ContainerImageDTO
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from csle_common.dao.emulation_config.emulation_env_state import EmulationEnvState
from csle_common.dao.emulation_config.emulation_env_config import EmulationEnvConfig
Expand Down Expand Up @@ -643,7 +643,7 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
emulations=MetastoreFacade.list_emulations())
emulations: List[str] = running_emulations
running_containers = ContainerController.list_all_running_containers()
containers: List[Tuple[str, str ,str]] = running_containers
containers: List[Tuple[str, str, str]] = running_containers
container_names: List[str] = list(map(lambda x: x[0], containers))
return ["prometheus", "node_exporter", "cadvisor", "pgadmin", "grafana", "flask",
"statsmanager", "all", "emulation_executions"] + emulations + container_names
Expand Down Expand Up @@ -1039,7 +1039,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
emulations=MetastoreFacade.list_emulations())
emulations = stopped_emulations
stopped_containers = ContainerController.list_all_stopped_containers()
containers: List[Tuple[str, str ,str]] = stopped_containers
containers: List[Tuple[str, str, str]] = stopped_containers
container_names: List[str] = list(map(lambda x: x[0], containers))
images: List[Tuple[str, str, str, str, str]] = ContainerController.list_all_images()
image_names: List[str] = list(map(lambda x: x[0], images))
Expand Down Expand Up @@ -1313,7 +1313,7 @@ def rm_shell_complete(ctx, param, incomplete) -> List[str]:
emulations = list(map(lambda x: x.name, MetastoreFacade.list_emulations()))
running_containers = ContainerController.list_all_running_containers()
stopped_containers = ContainerController.list_all_stopped_containers()
containers: List[Tuple[str, str ,str]] = running_containers + stopped_containers
containers: List[Tuple[str, str, str]] = running_containers + stopped_containers
container_names: List[str] = list(map(lambda x: x[0], containers))
images: List[Tuple[str, str, str, str, str]] = ContainerController.list_all_images()
image_names: List[str] = list(map(lambda x: x[0], images))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Dict, Any
import numpy as np
from csle_base.json_serializable import JSONSerializable


Expand All @@ -23,10 +24,8 @@ def from_dict(d: Dict[str, Any]) -> "ObservationFunctionConfig":
:param d: the dict to convert
:return: the created instance
"""
obj = ObservationFunctionConfig(
observation_tensor=d["observation_tensor"],
component_observation_tensors=d["component_observation_tensors"]
)
obj = ObservationFunctionConfig(observation_tensor=d["observation_tensor"],
component_observation_tensors=d["component_observation_tensors"])
return obj

def to_dict(self) -> Dict[str, Any]:
Expand All @@ -36,8 +35,21 @@ def to_dict(self) -> Dict[str, Any]:
:return: a dict representation of the object
"""
d = {}
d["observation_tensor"] = self.observation_tensor
if isinstance(self.observation_tensor, np.ndarray):
tensor = self.observation_tensor.tolist()
else:
tensor = self.observation_tensor
for i in range(len(tensor)):
if isinstance(tensor[i], np.ndarray):
tensor[i] = tensor[i].tolist()
d["observation_tensor"] = list(tensor)
d["component_observation_tensors"] = self.component_observation_tensors
for k, v in d["component_observation_tensors"].items():
if isinstance(v, np.ndarray):
d["component_observation_tensors"][k] = v.tolist()
for i in range(len(d["component_observation_tensors"][k])):
if isinstance(d["component_observation_tensors"][k][i], np.ndarray):
d["component_observation_tensors"][k][i] = d["component_observation_tensors"][k][i].tolist()
return d

def __str__(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Dict, Any, List

import numpy as np

from csle_base.json_serializable import JSONSerializable


Expand All @@ -23,17 +26,22 @@ def from_dict(d: Dict[str, Any]) -> "RewardFunctionConfig":
:param d: the dict to convert
:return: the created instance
"""
obj = RewardFunctionConfig(
reward_tensor=d["reward_tensor"]
)
obj = RewardFunctionConfig(reward_tensor=d["reward_tensor"])
return obj

def to_dict(self) -> Dict[str, Any]:
"""
:return: a dict representation of the object
"""
d = {}
d["reward_tensor"] = self.reward_tensor
if isinstance(self.reward_tensor, np.ndarray):
tensor = self.reward_tensor.tolist()
else:
tensor = self.reward_tensor
for i in range(len(tensor)):
if isinstance(tensor[i], np.ndarray):
tensor[i] = tensor[i].tolist()
d["reward_tensor"] = list(tensor)
return d

def __str__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ def __init__(self, name: str, descr: str, version: str,
observation_function_config: ObservationFunctionConfig,
initial_state_distribution_config: InitialStateDistributionConfig,
env_parameters_config: EnvParametersConfig, plot_transition_probabilities: bool = False,
plot_observation_function: bool = False, plot_reward_function: bool = False
):
plot_observation_function: bool = False, plot_reward_function: bool = False) -> None:
"""
Initializes the DTO
Expand Down Expand Up @@ -145,7 +144,7 @@ def to_dict(self) -> Dict[str, Any]:
d["state_space_config"] = self.state_space_config.to_dict()
d["joint_action_space_config"] = self.joint_action_space_config.to_dict()
d["joint_observation_space_config"] = self.joint_observation_space_config.to_dict()
d["time_step_type"] = self.time_step_type
d["time_step_type"] = self.time_step_type.value
d["reward_function_config"] = self.reward_function_config.to_dict()
d["transition_operator_config"] = self.transition_operator_config.to_dict()
d["observation_function_config"] = self.observation_function_config.to_dict()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def to_dict(self) -> Dict[str, Any]:
d["id"] = self.id
d["name"] = self.name
d["descr"] = self.descr
d["state_type"] = self.state_type
d["state_type"] = self.state_type.value
return d

def __str__(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Dict, Any
import numpy as np
from csle_base.json_serializable import JSONSerializable


Expand All @@ -23,9 +24,7 @@ def from_dict(d: Dict[str, Any]) -> "TransitionOperatorConfig":
:param d: the dict to convert
:return: the created instance
"""
obj = TransitionOperatorConfig(
transition_tensor=d["transition_tensor"]
)
obj = TransitionOperatorConfig(transition_tensor=d["transition_tensor"])
return obj

def to_dict(self) -> Dict[str, Any]:
Expand All @@ -35,7 +34,14 @@ def to_dict(self) -> Dict[str, Any]:
:return: a dict representation of the object
"""
d = {}
d["transition_tensor"] = self.transition_tensor
if isinstance(self.transition_tensor, np.ndarray):
tensor = self.transition_tensor.tolist()
else:
tensor = self.transition_tensor
for i in range(len(tensor)):
if isinstance(tensor[i], np.ndarray):
tensor[i] = tensor[i].tolist()
d["transition_tensor"] = list(tensor)
return d

def __str__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"""
import base64
from typing import Tuple

from flask import Blueprint, Response, jsonify, request
import json
import csle_common.constants.constants as constants
from csle_common.controllers.simulation_env_controller import SimulationEnvController
from csle_common.metastore.metastore_facade import MetastoreFacade
from flask import Blueprint, Response, jsonify, request

from csle_common.dao.encoding.np_encoder import NpEncoder
import csle_rest_api.constants.constants as api_constants
import csle_rest_api.util.rest_api_util as rest_api_util

Expand Down Expand Up @@ -49,7 +49,8 @@ def simulations() -> Tuple[Response, int]:
SimulationEnvController.uninstall_simulation(sim)

if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET:
simulations_dicts = list(map(lambda x: x.to_dict(), all_simulations))
simulations_dicts = list(map(lambda x: json.loads(json.dumps(x.to_dict(), indent=4, sort_keys=True,
cls=NpEncoder)), all_simulations))
elif request.method == api_constants.MGMT_WEBAPP.HTTP_REST_DELETE:
simulations_dicts = []
response = jsonify(simulations_dicts)
Expand Down Expand Up @@ -98,7 +99,7 @@ def get_simulation(simulation_id: int) -> Tuple[Response, int]:
if simulation is not None:

if request.method == api_constants.MGMT_WEBAPP.HTTP_REST_GET:
response = jsonify(simulation.to_dict())
response = jsonify(json.loads(json.dumps(simulation.to_dict(), indent=4, sort_keys=True, cls=NpEncoder)))
elif request.method == api_constants.MGMT_WEBAPP.HTTP_REST_DELETE:
SimulationEnvController.uninstall_simulation(simulation)
response.headers.add(api_constants.MGMT_WEBAPP.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*")
Expand Down
Loading

0 comments on commit e34a127

Please sign in to comment.