Skip to content

Commit cbdeff4

Browse files
committed
fix: jobparameters and jobattributes pydantic models
1 parent 69494d8 commit cbdeff4

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from enum import StrEnum
1010
from typing import Literal
1111

12-
from pydantic import BaseModel, Field
12+
from pydantic import BaseModel, Field, field_validator
1313
from typing_extensions import TypedDict
1414

1515

@@ -75,7 +75,7 @@ class SearchParams(BaseModel):
7575
# TODO: Add more validation
7676

7777

78-
class JobParameters(BaseModel, extra="forbid"):
78+
class JobParameters(BaseModel):
7979
"""All the parameters that can be set for a job."""
8080

8181
timestamp: datetime | None = None
@@ -95,8 +95,25 @@ class JobParameters(BaseModel, extra="forbid"):
9595
job_type: str | None = Field(None, alias="JobType")
9696
job_status: str | None = Field(None, alias="JobStatus")
9797

98-
99-
class JobAttributes(BaseModel, extra="forbid"):
98+
@field_validator(
99+
"cpu_normalization_factor", "norm_cpu_time_s", "total_cpu_time_s", mode="before"
100+
)
101+
@classmethod
102+
def convert_cpu_fields_to_int(cls, v):
103+
"""Convert string representation of float to integer for CPU-related fields."""
104+
if v is None:
105+
return v
106+
if isinstance(v, str):
107+
try:
108+
return int(float(v))
109+
except (ValueError, TypeError) as e:
110+
raise ValueError(f"Cannot convert '{v}' to integer") from e
111+
if isinstance(v, (int, float)):
112+
return int(v)
113+
return v
114+
115+
116+
class JobAttributes(BaseModel):
100117
"""All the attributes that can be set for a job."""
101118

102119
job_type: str | None = Field(None, alias="JobType")
@@ -121,7 +138,7 @@ class JobAttributes(BaseModel, extra="forbid"):
121138
accounted_flag: bool | str | None = Field(None, alias="AccountedFlag")
122139

123140

124-
class JobMetaData(JobAttributes, JobParameters, extra="forbid"):
141+
class JobMetaData(JobAttributes, JobParameters):
125142
"""A model that combines both JobAttributes and JobParameters."""
126143

127144

diracx-logic/src/diracx/logic/jobs/status.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,14 @@ async def set_job_parameters_or_attributes(
541541
if pname in JOB_PARAMETERS_ALIASES:
542542
param_updates[job_id][pname] = pvalue
543543

544+
# If the field is not in either known aliases, default to treating it as a parameter
545+
# This allows for more flexible metadata handling
546+
# WARNING: This is a fragile fallback that can lead to unexpected behavior
547+
# as attributes and parameters are not strictly defined...
548+
# See https://github.com/DIRACGrid/diracx/pull/626 for further details
549+
elif pname not in JOB_ATTRIBUTES_ALIASES:
550+
param_updates[job_id][pname] = pvalue
551+
544552
# Bulk set job attributes if required
545553
attr_updates = {k: v for k, v in attr_updates.items() if v}
546554
if attr_updates:

0 commit comments

Comments
 (0)