Skip to content

Commit

Permalink
refactor JobPackageReader
Browse files Browse the repository at this point in the history
  • Loading branch information
LuiggiTenorioK committed Dec 10, 2024
1 parent 3215aa5 commit 078dd4a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
33 changes: 12 additions & 21 deletions autosubmit_api/persistance/job_package_reader.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from typing import Dict, List
from sqlalchemy import select
from autosubmit_api.logger import logger
from autosubmit_api.database import tables
from autosubmit_api.database.common import AttachedDatabaseConnBuilder
from autosubmit_api.persistance.experiment import ExperimentPaths
from autosubmit_api.repositories.job_packages import create_job_packages_repository


class JobPackageReader:
Expand All @@ -17,23 +14,17 @@ def __init__(self, expid: str) -> None:
self._package_to_symbol: Dict[str, str] = {}

def read(self):
conn_builder = AttachedDatabaseConnBuilder()
conn_builder.attach_db(
ExperimentPaths(self.expid).job_packages_db, "job_packages"
)

with conn_builder.product as conn:
try:
statement = select(tables.JobPackageTable)
self._content = [x._mapping for x in conn.execute(statement).all()]
if len(self._content) == 0:
raise Warning(
"job_packages table empty, trying wrapper_job_packages"
)
except Exception as exc:
logger.warning(exc)
statement = select(tables.WrapperJobPackageTable)
self._content = [x._mapping for x in conn.execute(statement).all()]
try:
raw_content = create_job_packages_repository(self.expid).get_all()
self._content = [x.model_dump() for x in raw_content]
if len(self._content) == 0:
raise Warning("job_packages table empty, trying wrapper_job_packages")
except Exception as exc:
logger.warning(exc)
raw_content = create_job_packages_repository(
self.expid, wrapper=True
).get_all()
self._content = [x.model_dump() for x in raw_content]

self._build_job_to_package()
self._build_package_to_jobs()
Expand Down
53 changes: 53 additions & 0 deletions autosubmit_api/repositories/job_packages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from abc import ABC, abstractmethod
from typing import List, Any
from pydantic import BaseModel
from sqlalchemy import Engine, Table
from autosubmit_api.database import tables
from autosubmit_api.database.common import (
create_sqlite_db_engine,
)
from autosubmit_api.persistance.experiment import ExperimentPaths


class JobPackageModel(BaseModel):
exp_id: Any
package_name: Any
job_name: Any


class JobPackagesRepository(ABC):
@abstractmethod
def get_all(self) -> List[JobPackageModel]:
"""
Get all job packages.
"""


class JobPackagesSQLRepository(JobPackagesRepository):
def __init__(self, engine: Engine, table: Table):
self.engine = engine
self.table = table

def get_all(self):
with self.engine.connect() as conn:
statement = self.table.select()
result = conn.execute(statement).all()
return [
JobPackageModel(
exp_id=row.exp_id,
package_name=row.package_name,
job_name=row.job_name,
)
for row in result
]


def create_job_packages_repository(expid: str, wrapper=False) -> JobPackagesRepository:
"""
Create a job packages repository.
:param wrapper: Whether to use the alternative wrapper job packages table.
"""
engine = create_sqlite_db_engine(ExperimentPaths(expid).job_packages_db)
table = tables.wrapper_job_package_table if wrapper else tables.job_package_table
return JobPackagesSQLRepository(engine, table)

0 comments on commit 078dd4a

Please sign in to comment.