-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b53e85f
commit 21ad183
Showing
4 changed files
with
142 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from abc import ABC, abstractmethod | ||
from datetime import datetime | ||
from typing import Any, List | ||
from pydantic import BaseModel | ||
from sqlalchemy import Engine, Table, delete, insert | ||
from sqlalchemy.schema import CreateTable | ||
from autosubmit_api.database import tables | ||
from autosubmit_api.database.common import create_as_times_db_engine | ||
|
||
|
||
class ExperimentStatusModel(BaseModel): | ||
exp_id: int | ||
name: str | ||
status: str | ||
seconds_diff: Any | ||
modified: Any | ||
|
||
|
||
class ExperimentStatusRepository(ABC): | ||
@abstractmethod | ||
def get_all(self) -> List[ExperimentStatusModel]: | ||
""" | ||
Get all experiments status | ||
""" | ||
|
||
@abstractmethod | ||
def upsert_status(self, exp_id: int, expid: str, status: str) -> int: | ||
""" | ||
Delete and insert experiment status by expid | ||
""" | ||
|
||
|
||
class ExperimentStatusSQLRepository(ExperimentStatusRepository): | ||
def __init__(self, engine: Engine, table: Table): | ||
self.engine = engine | ||
self.table = table | ||
|
||
with self.engine.connect() as conn: | ||
conn.execute(CreateTable(self.table, if_not_exists=True)) | ||
conn.commit() | ||
|
||
def get_all(self): | ||
with self.engine.connect() as conn: | ||
statement = self.table.select() | ||
result = conn.execute(statement).all() | ||
return [ | ||
ExperimentStatusModel.model_validate(row, from_attributes=True) | ||
for row in result | ||
] | ||
|
||
def upsert_status(self, exp_id: int, expid: str, status: str): | ||
with self.engine.connect() as conn: | ||
with conn.begin(): | ||
try: | ||
del_stmnt = delete(self.table).where(self.table.c.id == exp_id) | ||
ins_stmnt = insert(self.table).values( | ||
exp_id=exp_id, | ||
name=expid, | ||
status=status, | ||
seconds_diff=0, | ||
modified=datetime.now().isoformat(sep="-", timespec="seconds"), | ||
) | ||
conn.execute(del_stmnt) | ||
result = conn.execute(ins_stmnt) | ||
conn.commit() | ||
except Exception as exc: | ||
conn.rollback() | ||
raise exc | ||
|
||
return result.rowcount | ||
|
||
|
||
def create_experiment_status_repository() -> ExperimentStatusRepository: | ||
engine = create_as_times_db_engine() | ||
return ExperimentStatusSQLRepository(engine, tables.experiment_status_table) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from abc import ABC, abstractmethod | ||
from sqlalchemy import select | ||
from autosubmit_api.database import tables | ||
from autosubmit_api.database.common import create_main_db_conn | ||
|
||
|
||
class ExperimentJoinRepository(ABC): | ||
@abstractmethod | ||
def drop_status_from_deleted_experiments(self) -> int: | ||
""" | ||
Drop status records from experiments that are not in the experiments table | ||
""" | ||
|
||
|
||
class ExperimentJoinSQLRepository(ExperimentJoinRepository): | ||
def _get_connection(self): | ||
return create_main_db_conn() | ||
|
||
def drop_status_from_deleted_experiments(self) -> int: | ||
with self._get_connection() as conn: | ||
del_stmnt = tables.experiment_status_table.delete().where( | ||
tables.experiment_status_table.c.exp_id.not_in( | ||
select(tables.experiment_table.c.id) | ||
) | ||
) | ||
result = conn.execute(del_stmnt) | ||
conn.commit() | ||
|
||
return result.rowcount | ||
|
||
|
||
def create_experiment_join_repository() -> ExperimentJoinRepository: | ||
return ExperimentJoinSQLRepository() |