|
| 1 | +import logging |
| 2 | +from datetime import datetime |
1 | 3 | from typing import Union
|
2 |
| -from pymongo.results import InsertOneResult |
3 |
| -from pymongo.results import DeleteResult |
| 4 | +from lib.repositories import parse_error |
4 | 5 | from lib.models.environment import Env
|
5 | 6 | from lib.repositories.repo import Repository
|
6 | 7 |
|
7 | 8 |
|
8 | 9 | class EnvRepository(Repository):
|
9 | 10 | """
|
10 |
| - Environment repository |
| 11 | + Enables database CRUD operations with models.Env |
11 | 12 |
|
12 | 13 | Init Attributes:
|
13 |
| - environment: Env object |
14 |
| - env_id: Environment id |
| 14 | + environment: models.Env |
| 15 | + env_id: str |
15 | 16 |
|
16 |
| - Enables CRUD operations on environment objects |
17 | 17 | """
|
18 | 18 |
|
19 | 19 | def __init__(self, environment: Env = None, env_id: str = None):
|
20 | 20 | super().__init__("environments")
|
21 |
| - self.environment = environment |
| 21 | + self._env = environment |
22 | 22 | if env_id:
|
23 |
| - self.env_id = env_id |
| 23 | + self._env_id = env_id |
24 | 24 | else:
|
25 |
| - self.env_id = self.environment.__hash__() |
| 25 | + self._env_id = hash(self._env) |
26 | 26 |
|
27 |
| - def __del__(self): |
28 |
| - super().__del__() |
| 27 | + def close_connection(self) -> None: |
| 28 | + logging.info(f"[{datetime.now()}] Closing connection to database") |
| 29 | + self.client.close() |
29 | 30 |
|
30 |
| - async def create_env(self) -> "InsertOneResult": |
31 |
| - """ |
32 |
| - Creates a environment in the database |
| 31 | + @property |
| 32 | + def env(self) -> "Env": |
| 33 | + return self._env |
| 34 | + |
| 35 | + @env.setter |
| 36 | + def env(self, environment: "Env"): |
| 37 | + self._env = environment |
| 38 | + |
| 39 | + @property |
| 40 | + def env_id(self) -> "str": |
| 41 | + return self._env_id |
33 | 42 |
|
34 |
| - Args: |
35 |
| - rocketpy_env: rocketpy environment object |
| 43 | + @env_id.setter |
| 44 | + def env_id(self, env_id: "str"): |
| 45 | + self._env_id = env_id |
| 46 | + |
| 47 | + async def create_env(self): |
| 48 | + """ |
| 49 | + Creates a non-existing models.Env in the database |
36 | 50 |
|
37 | 51 | Returns:
|
38 |
| - InsertOneResult: result of the insert operation |
| 52 | + None |
39 | 53 | """
|
40 |
| - if not await self.get_env(): |
41 |
| - try: |
42 |
| - environment_to_dict = self.environment.dict() |
43 |
| - environment_to_dict["env_id"] = self.env_id |
44 |
| - return await self.collection.insert_one(environment_to_dict) |
45 |
| - except Exception as e: |
46 |
| - raise Exception(f"Error creating environment: {str(e)}") from e |
47 |
| - finally: |
48 |
| - self.__del__() |
49 |
| - else: |
50 |
| - return InsertOneResult(acknowledged=True, inserted_id=None) |
| 54 | + env_exists = await self.get_env() |
| 55 | + if env_exists: |
| 56 | + return |
| 57 | + |
| 58 | + try: |
| 59 | + environment_to_dict = self.env.dict() |
| 60 | + environment_to_dict["env_id"] = self.env_id |
| 61 | + await self.collection.insert_one(environment_to_dict) |
| 62 | + except Exception as e: |
| 63 | + exc_str = parse_error(e) |
| 64 | + logging.error( |
| 65 | + f"[{datetime.now()}] repositories.environment.create_env: {exc_str}" |
| 66 | + ) |
| 67 | + raise Exception(f"Error creating environment: {str(e)}") from e |
| 68 | + finally: |
| 69 | + logging.info( |
| 70 | + f"[{datetime.now()}] Call to repositories.environment.create_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}" |
| 71 | + ) |
| 72 | + self.close_connection() |
51 | 73 |
|
52 |
| - async def update_env(self) -> "Union[int, None]": |
| 74 | + async def update_env(self): |
53 | 75 | """
|
54 |
| - Updates a environment in the database |
| 76 | + Updates a models.Env in the database |
55 | 77 |
|
56 | 78 | Returns:
|
57 |
| - int: environment id |
| 79 | + None |
58 | 80 | """
|
59 | 81 | try:
|
60 |
| - environment_to_dict = self.environment.dict() |
61 |
| - environment_to_dict["env_id"] = self.environment.__hash__() |
62 |
| - |
| 82 | + environment_to_dict = self.env.dict() |
| 83 | + environment_to_dict["env_id"] = hash(self.env) |
63 | 84 | await self.collection.update_one(
|
64 | 85 | {"env_id": self.env_id}, {"$set": environment_to_dict}
|
65 | 86 | )
|
66 |
| - |
67 | 87 | self.env_id = environment_to_dict["env_id"]
|
68 |
| - return self.env_id |
69 | 88 | except Exception as e:
|
| 89 | + exc_str = parse_error(e) |
| 90 | + logging.error( |
| 91 | + f"[{datetime.now()}] repositories.environment.update_env: {exc_str}" |
| 92 | + ) |
70 | 93 | raise Exception(f"Error updating environment: {str(e)}") from e
|
| 94 | + else: |
| 95 | + return |
71 | 96 | finally:
|
72 |
| - self.__del__() |
| 97 | + logging.info( |
| 98 | + f"[{datetime.now()}] Call to repositories.environment.update_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}" |
| 99 | + ) |
| 100 | + self.close_connection() |
73 | 101 |
|
74 | 102 | async def get_env(self) -> "Union[Env, None]":
|
75 | 103 | """
|
76 |
| - Gets a environment from the database |
| 104 | + Gets a models.Env from the database |
77 | 105 |
|
78 | 106 | Returns:
|
79 |
| - models.Env: Model environment object |
| 107 | + models.Env |
80 | 108 | """
|
81 | 109 | try:
|
82 |
| - environment = await self.collection.find_one({"env_id": self.env_id}) |
83 |
| - if environment is not None: |
84 |
| - return Env.parse_obj(environment) |
85 |
| - return None |
| 110 | + read_env = await self.collection.find_one({"env_id": self.env_id}) |
86 | 111 | except Exception as e:
|
| 112 | + logging.error( |
| 113 | + f"[{datetime.now()}] repositories.environment.get_env: {str(e)}" |
| 114 | + ) |
87 | 115 | raise Exception(f"Error getting environment: {str(e)}") from e
|
| 116 | + else: |
| 117 | + return Env.parse_obj(read_env) if read_env else None |
| 118 | + finally: |
| 119 | + logging.info( |
| 120 | + f"[{datetime.now()}] Call to repositories.environment.get_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}" |
| 121 | + ) |
| 122 | + self.close_connection() |
88 | 123 |
|
89 |
| - async def delete_env(self) -> "DeleteResult": |
| 124 | + async def delete_env(self): |
90 | 125 | """
|
91 |
| - Deletes a environment from the database |
| 126 | + Deletes a models.Env from the database |
92 | 127 |
|
93 | 128 | Returns:
|
94 |
| - DeleteResult: result of the delete operation |
| 129 | + None |
95 | 130 | """
|
96 | 131 | try:
|
97 |
| - return await self.collection.delete_one({"env_id": self.env_id}) |
| 132 | + await self.collection.delete_one({"env_id": self.env_id}) |
98 | 133 | except Exception as e:
|
| 134 | + logging.error( |
| 135 | + f"[{datetime.now()}] repositories.environment.delete_env: {str(e)}" |
| 136 | + ) |
99 | 137 | raise Exception(f"Error deleting environment: {str(e)}") from e
|
100 | 138 | finally:
|
101 |
| - self.__del__() |
| 139 | + logging.info( |
| 140 | + f"[{datetime.now()}] Call to repositories.environment.delete_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}" |
| 141 | + ) |
| 142 | + self.close_connection() |
0 commit comments