Skip to content

Commit e049282

Browse files
committed
fix: change map_status and clean up schema
1 parent d37244d commit e049282

File tree

9 files changed

+2401
-1951
lines changed

9 files changed

+2401
-1951
lines changed

diracx-core/src/diracx/core/models/rss.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class AllowedStatus(BaseModel):
1010
allowed: Literal[True]
11-
warnings: list[str] = []
11+
warnings: str | None = None
1212

1313
def __bool__(self) -> bool:
1414
return True
@@ -55,19 +55,3 @@ class SiteStatus(BaseModel):
5555

5656
ALLOWED = {"Active", "Degraded"}
5757
BANNED = {"Banned", "Probing", "Error", "Unknown"}
58-
59-
60-
def map_status(db_status: str, reason: str | None = None) -> ResourceStatus:
61-
if db_status in ALLOWED:
62-
return AllowedStatus(allowed=True)
63-
64-
if db_status in BANNED:
65-
return BannedStatus(
66-
allowed=False,
67-
reason=reason or db_status,
68-
)
69-
70-
return BannedStatus(
71-
allowed=False,
72-
reason=f"Unknown status: {db_status}",
73-
)

diracx-db/src/diracx/db/sql/rss/db.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ResourceStatusDB(BaseSQLDB):
2121
async def get_site_status(self, name: str, vo: str = "all") -> tuple[str, str]:
2222
stmt = select(SiteStatus.status, SiteStatus.reason).where(
2323
SiteStatus.name == name,
24-
SiteStatus.statustype == "all",
24+
SiteStatus.status_type == "all",
2525
SiteStatus.vo == vo,
2626
)
2727
result = await self.conn.execute(stmt)
@@ -34,14 +34,16 @@ async def get_site_status(self, name: str, vo: str = "all") -> tuple[str, str]:
3434
async def get_resource_status(
3535
self,
3636
name: str,
37-
statustypes: list[str] = ["all"],
37+
status_types: list[str] | None = None,
3838
vo: str = "all",
3939
) -> dict[str, Row]:
40+
if not status_types:
41+
status_types = ["all"]
4042
stmt = select(
41-
ResourceStatus.status, ResourceStatus.reason, ResourceStatus.statustype
43+
ResourceStatus.status, ResourceStatus.reason, ResourceStatus.status_type
4244
).where(
4345
ResourceStatus.name == name,
44-
ResourceStatus.statustype.in_(statustypes),
46+
ResourceStatus.status_type.in_(status_types),
4547
ResourceStatus.vo == vo,
4648
)
4749
result = await self.conn.execute(stmt)

diracx-db/src/diracx/db/sql/rss/schema.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
from datetime import datetime
44

5-
from sqlalchemy import BigInteger, DateTime, String
5+
from sqlalchemy import BigInteger, String
66
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
77

8+
from diracx.db.sql.utils.types import SmarterDateTime
9+
810
from ..utils import str32, str64, str128, str512
911

1012
# Defining the tables
@@ -20,24 +22,22 @@ class RSSBase(DeclarativeBase):
2022

2123

2224
class ElementStatusBase:
23-
__table_args__ = {"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"}
24-
2525
name: Mapped[str64] = mapped_column("Name", primary_key=True)
26-
statustype: Mapped[str128] = mapped_column(
26+
status_type: Mapped[str128] = mapped_column(
2727
"StatusType", server_default="all", primary_key=True
2828
)
2929
vo: Mapped[str64] = mapped_column("VO", primary_key=True, server_default="all")
3030
status: Mapped[str] = mapped_column("Status", String(8), server_default="")
3131
reason: Mapped[str512] = mapped_column("Reason", server_default="Unspecified")
32-
dateeffective: Mapped[datetime] = mapped_column("DateEffective", DateTime)
33-
tokenexpiration: Mapped[datetime] = mapped_column(
34-
"TokenExpiration", DateTime, server_default="9999-12-31 23:59:59"
32+
date_effective: Mapped[datetime] = mapped_column("DateEffective", SmarterDateTime())
33+
token_expiration: Mapped[datetime] = mapped_column(
34+
"TokenExpiration", SmarterDateTime(), server_default="9999-12-31 23:59:59"
3535
)
36-
elementtype: Mapped[str32] = mapped_column("ElementType", server_default="")
37-
lastchecktime: Mapped[datetime] = mapped_column(
38-
"LastCheckTime", DateTime, server_default="1000-01-01 00:00:00"
36+
element_type: Mapped[str32] = mapped_column("ElementType", server_default="")
37+
last_check_time: Mapped[datetime] = mapped_column(
38+
"LastCheckTime", SmarterDateTime(), server_default="1000-01-01 00:00:00"
3939
)
40-
tokenowner: Mapped[str] = mapped_column(
40+
token_owner: Mapped[str] = mapped_column(
4141
"TokenOwner", String(16), server_default="rs_svc"
4242
)
4343

@@ -54,19 +54,19 @@ class ElementStatusBaseWithID(ElementStatusBase):
5454
"ID", BigInteger, autoincrement=True, primary_key=True
5555
)
5656
name: Mapped[str64] = mapped_column("Name")
57-
statustype: Mapped[str128] = mapped_column("StatusType", server_default="all")
57+
status_type: Mapped[str128] = mapped_column("StatusType", server_default="all")
5858
vo: Mapped[str64] = mapped_column("VO", server_default="all")
5959
status: Mapped[str] = mapped_column("Status", String(8), server_default="")
6060
reason: Mapped[str512] = mapped_column("Reason", server_default="Unspecified")
61-
dateeffective: Mapped[datetime] = mapped_column("DateEffective", DateTime)
62-
tokenexpiration: Mapped[datetime] = mapped_column(
63-
"TokenExpiration", DateTime, server_default="9999-12-31 23:59:59"
61+
date_effective: Mapped[datetime] = mapped_column("DateEffective", SmarterDateTime())
62+
token_expiration: Mapped[datetime] = mapped_column(
63+
"TokenExpiration", SmarterDateTime(), server_default="9999-12-31 23:59:59"
6464
)
65-
elementtype: Mapped[str32] = mapped_column("ElementType", server_default="")
66-
lastchecktime: Mapped[datetime] = mapped_column(
67-
"LastCheckTime", DateTime, server_default="1000-01-01 00:00:00"
65+
element_type: Mapped[str32] = mapped_column("ElementType", server_default="")
66+
last_check_time: Mapped[datetime] = mapped_column(
67+
"LastCheckTime", SmarterDateTime(), server_default="1000-01-01 00:00:00"
6868
)
69-
tokenowner: Mapped[str] = mapped_column(
69+
token_owner: Mapped[str] = mapped_column(
7070
"TokenOwner", String(16), server_default="rs_svc"
7171
)
7272

diracx-db/tests/rss/__init__.py

Whitespace-only changes.

diracx-db/tests/rss/test_rss_db.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ async def test_site_status(rss_db: ResourceStatusDB):
4040
)
4141

4242
# Test with the test Site (should be found)
43-
async with rss_db as rss_db:
44-
status, reason = await rss_db.get_site_status("TestSite")
43+
async with rss_db as db:
44+
status, reason = await db.get_site_status("TestSite")
4545
assert status == "Active"
4646
assert reason == "All good"
4747

4848
# Test with an unknow Site (should not be found)
4949
with pytest.raises(ResourceNotFoundError):
50-
async with rss_db as rss_db:
51-
await rss_db.get_site_status("Unknown")
50+
async with rss_db as db:
51+
await db.get_site_status("Unknown")
5252

5353

5454
async def test_resource_status(rss_db: ResourceStatusDB):
@@ -101,22 +101,22 @@ async def test_resource_status(rss_db: ResourceStatusDB):
101101
)
102102

103103
# Test with the test Compute Element (should be found)
104-
async with rss_db as rss_db:
105-
result = await rss_db.get_resource_status("TestCompute")
104+
async with rss_db as db:
105+
result = await db.get_resource_status("TestCompute")
106106
assert "all" in result
107107
assert result["all"].Status == "Active"
108108
assert result["all"].Reason == "All good"
109109

110110
# Test with the test FTS (should be found)
111-
async with rss_db as rss_db:
112-
result = await rss_db.get_resource_status("TestFTS")
111+
async with rss_db as db:
112+
result = await db.get_resource_status("TestFTS")
113113
assert "all" in result
114114
assert result["all"].Status == "Active"
115115
assert result["all"].Reason == "All good"
116116

117117
# Test with the test Storage Element (should be found)
118-
async with rss_db as rss_db:
119-
result = await rss_db.get_resource_status(
118+
async with rss_db as db:
119+
result = await db.get_resource_status(
120120
"TestStorage", ["ReadAccess", "WriteAccess", "CheckAccess", "RemoveAccess"]
121121
)
122122
assert set(result.keys()) == {
@@ -131,5 +131,5 @@ async def test_resource_status(rss_db: ResourceStatusDB):
131131

132132
# Test with an unknow Resource (should not be found)
133133
with pytest.raises(ResourceNotFoundError):
134-
async with rss_db as rss_db:
135-
await rss_db.get_resource_status("Unknown")
134+
async with rss_db as db:
135+
await db.get_resource_status("Unknown")

diracx-logic/src/diracx/logic/rss/rss.py renamed to diracx-logic/src/diracx/logic/rss/query.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
from __future__ import annotations
22

33
from diracx.core.models.rss import (
4+
ALLOWED,
5+
BANNED,
6+
AllowedStatus,
7+
BannedStatus,
48
ComputeElementStatus,
59
FTSStatus,
10+
ResourceStatus,
611
StorageElementStatus,
7-
map_status,
812
)
913
from diracx.core.models.rss import (
1014
SiteStatus as SiteStatusModel,
1115
)
1216
from diracx.db.sql import ResourceStatusDB
1317

1418

19+
def map_status(db_status: str, reason: str | None = None) -> ResourceStatus:
20+
if db_status in ALLOWED:
21+
return AllowedStatus(
22+
allowed=True,
23+
warnings=reason or db_status if db_status == "Degraded" else None,
24+
)
25+
26+
if db_status in BANNED:
27+
return BannedStatus(
28+
allowed=False,
29+
reason=reason or db_status,
30+
)
31+
32+
return BannedStatus(
33+
allowed=False,
34+
reason=f"Unknown status: {db_status}",
35+
)
36+
37+
1538
async def get_site_status(
1639
resource_status_db: ResourceStatusDB, name: str, vo: str
1740
) -> SiteStatusModel:

diracx-logic/tests/rss/__init__.py

Whitespace-only changes.
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@
55
from diracx.core.models.rss import (
66
AllowedStatus,
77
BannedStatus,
8-
map_status,
98
)
9+
from diracx.logic.rss.query import map_status
1010

1111

12-
@pytest.mark.parametrize("status", ["Active", "Degraded"])
13-
async def test_map_status_allowed(status):
14-
assert bool(map_status(status, "")) is True
15-
assert isinstance(map_status(status, ""), AllowedStatus)
12+
async def test_map_status_allowed():
13+
result = map_status("Active", "Resource active")
14+
assert bool(result) is True
15+
assert isinstance(result, AllowedStatus)
16+
assert result.warnings is None
17+
18+
result = map_status("Degraded", "Resource degraded")
19+
assert bool(result) is True
20+
assert isinstance(result, AllowedStatus)
21+
assert result.warnings == "Resource degraded"
1622

1723

1824
@pytest.mark.parametrize("status", ["Banned", "Probing", "Error", "Unknown"])
1925
async def test_map_status_banned(status):
20-
result = map_status(status, "CE banned")
26+
result = map_status(status, "Resource banned")
2127
assert bool(result) is False
2228
assert isinstance(result, BannedStatus)
23-
assert result.reason == "CE banned"
29+
assert result.reason == "Resource banned"
2430

2531

2632
async def test_map_status_unknown_banned():

0 commit comments

Comments
 (0)