Skip to content

RSS data models (SQLAlchemy + Pydantic) #837

@aldbr

Description

@aldbr

Description

Create the data layer for RSS in diracx:

  1. SQLAlchemy 2.0 models in diracx-db that map the existing ResourceStatusDB tables — no schema changes, DB is shared with legacy DIRAC.
  2. Pydantic models in diracx-core for the consumer-facing view — a simplified binary allowed/banned model.

SQLAlchemy models

Note: The ResourceStatusCache table from the legacy schema is not needed — caching is handled by CacheableSource.

Technical details:

  • Create diracx-db/src/diracx/db/sql/rss/ package with schema.py and db.py
  • Follow diracx SQLAlchemy 2.0 patterns (DeclarativeBase, Mapped[T], mapped_column()) — see diracx-db/src/diracx/db/sql/auth/schema.py and job/schema.py for reference
  • Create ResourceStatusDB(BaseSQLDB) with read methods for status queries
  • Register via entry point in pyproject.toml under diracx.dbs.sql
  • Must not create or alter tables — schema is owned by legacy DIRAC

Pydantic models

Implement the simplified binary consumer model proposed in the #790 discussion:

from pydantic import BaseModel
from typing import Literal

class AllowedStatus(BaseModel):
    allowed: Literal[True]
    warnings: list[str] = []

    def __bool__(self) -> bool:
        return True

class BannedStatus(BaseModel):
    allowed: Literal[False]
    reason: str = "Unknown"

    def __bool__(self) -> bool:
        return False

ResourceStatus = AllowedStatus | BannedStatus  # Discriminated union on `allowed`

class StorageElementStatus(BaseModel):
    read: ResourceStatus
    write: ResourceStatus
    check: ResourceStatus
    remove: ResourceStatus

class ComputeElementStatus(BaseModel):
    all: ResourceStatus

class SiteStatus(BaseModel):
    all: ResourceStatus

Mapping from DB status to binary model:

  • Allowed: Active, Degraded
  • Banned: Banned, Probing, Error, Unknown

This matches the existing getUsableSites logic in DIRAC (SiteStatus.py).

Reference

Acceptance criteria

  • SQLAlchemy 2.0 models for the tables, matching the existing schema exactly
  • ResourceStatusDB(BaseSQLDB) class with read methods for SiteStatus and ResourceStatus
  • Entry point registered in pyproject.toml
  • Pydantic models for AllowedStatus, BannedStatus, StorageElementStatus, ComputeElementStatus, SiteStatus
  • ResourceType enum
  • Unit tests for DB reads and model serialization/deserialization
  • No DB migrations or schema changes

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions