flip_api.model_services.services.model_service

Attributes

_STATUS_AUDIT_MAP

Functions

edit_model(→ None)

Edit the model details

update_model_status(...)

Update the status of a model.

add_log(→ None)

Add a log entry to the database

delete_model(→ None)

Delete a model by setting its 'deleted' attribute to True.

delete_models(→ int)

Delete all models associated with a given project by setting their 'deleted' attribute to True.

get_model_status(...)

Get the status of a model.

resolve_trust_from_fl_client_name(...)

Resolve an FL client name (its FL kit slot) to its Trust.

validate_trust_ids(→ bool)

Validate that every trust id is associated with the model via ModelTrustIntersect.

get_metrics(...)

Get the metrics for a given model.

_run_trusts_by_model(→ dict[uuid.UUID, ...)

Group each model's dispatched (run) trusts, keyed by model id.

queued_positions_by_model(→ dict[uuid.UUID, int])

Map each queued model to its 1-based place in the FL training queue.

get_all_models_service(...)

Estate-wide, access-scoped model list joined with project name, owner and run trusts (#726).

Module Contents

flip_api.model_services.services.model_service.edit_model(model_id: uuid.UUID, model_details: flip_api.domain.interfaces.model.IModelDetails, user_id: uuid.UUID, session: sqlmodel.Session) None

Edit the model details

Parameters:
  • model_id (UUID) – The ID of the model to be edited

  • model_details (IModelDetails) – The new details for the model

  • user_id (UUID) – The ID of the user making the changes

  • session (Session) – The database session

Raises:

ValueError – If no model exists with the given model_id.

flip_api.model_services.services.model_service._STATUS_AUDIT_MAP: dict[flip_api.domain.schemas.status.ModelStatus, flip_api.domain.schemas.actions.ModelAuditAction]
flip_api.model_services.services.model_service.update_model_status(model_id: uuid.UUID, status: flip_api.domain.schemas.status.ModelStatus | None, session: sqlmodel.Session, user_id: uuid.UUID | None = None) flip_api.domain.schemas.status.ModelStatus | None

Update the status of a model.

Parameters:
  • model_id (UUID) – The ID of the model.

  • status (ModelStatus | None) – The new status to be set.

  • session (Session) – The database session.

  • user_id (UUID | None) – Optional user who triggered the change; recorded on the audit row when present. Background transitions pass None.

Returns:

The updated status of the model, or None if the model does not exist.

Return type:

ModelStatus | None

flip_api.model_services.services.model_service.add_log(model_id: uuid.UUID, log: str | None, session: sqlmodel.Session, transaction: Any | None = None, success: bool = True, trust: flip_api.db.models.main_models.Trust | None = None, fl_client_name: str | None = None, event_type: str | None = None, global_round: int | None = None, details: dict[str, Any] | None = None) None

Add a log entry to the database

Parameters:
  • model_id (UUID) – The ID of the model.

  • log (str | None) – The log message to be added. None for typed event rows, whose display text is composed at serve time.

  • session (Session) – The database session.

  • transaction (Any | None) – Optional transaction to control commit behavior.

  • success (bool) – Indicates if the log entry is a success or failure.

  • trust (Trust | None) – The trust the log is attributed to, when the log was reported by an FL client. None for model-level (hub) logs.

  • fl_client_name (str | None) – The FL client name as reported by the FL server. None for model-level logs.

  • event_type (str | None) – The typed event this row records, when a structured fact rather than free text was reported — round events from the FL server, QUEUE_POSITION from the hub’s own FL scheduler. Senders use the FLLogEvent vocabulary but the value is stored as plain text.

  • global_round (int | None) – 1-based federated round the event belongs to.

  • details (dict[str, Any] | None) – Event-specific facts (e.g. total_rounds, size_bytes, returned/expected counts).

Returns:

None

Raises:

Exception – Re-raises any error encountered when persisting the log entry, after rolling back the session.

flip_api.model_services.services.model_service.delete_model(model_id: uuid.UUID, user_id: uuid.UUID, session: sqlmodel.Session) None

Delete a model by setting its ‘deleted’ attribute to True.

Parameters:
  • model_id (UUID) – The ID of the model to be deleted.

  • user_id (UUID) – The ID of the user performing the deletion.

  • session (Session) – The database session.

Raises:

ValueError – If the model with the given ID does not exist.

flip_api.model_services.services.model_service.delete_models(project_id: uuid.UUID, user_id: str, session: sqlmodel.Session, ensure_deletion: bool = True) int

Delete all models associated with a given project by setting their ‘deleted’ attribute to True.

Parameters:
  • project_id (UUID) – The ID of the project whose models are to be deleted.

  • user_id (str) – The ID of the user performing the deletion.

  • session (Session) – The database session.

  • ensure_deletion (bool) – If True, raises an error if no models are found for deletion.

Returns:

The number of models deleted.

Return type:

int

Raises:

ValueError – If ensure_deletion is True and no non-deleted models exist for the given project.

flip_api.model_services.services.model_service.get_model_status(model_id: uuid.UUID, session: sqlmodel.Session) flip_api.domain.interfaces.model.IDetailedModelStatus | None

Get the status of a model.

Parameters:
  • model_id (UUID) – The ID of the model.

  • session (Session) – The database session.

Returns:

The model’s status and deleted flag, or None if the model does not exist.

Return type:

IDetailedModelStatus | None

flip_api.model_services.services.model_service.resolve_trust_from_fl_client_name(fl_client_name: str, session: sqlmodel.Session) flip_api.db.models.main_models.Trust | None

Resolve an FL client name (its FL kit slot) to its Trust.

Both backends report the FL kit slot as the client name, so resolution is uniform — the slot is mapped to its assigned trust via the FLKitSlot table, independent of the operator-chosen trust display name (closes the #538 drift from Flower’s previous free-form name lookup):

  • NVFLARE: the slot is the certificate CN baked in at provisioning time (e.g. Trust_2).

  • Flower: the slot is the SUPERNODE_NAME env var, set to FL_KIT_SLOT in the trust compose so the FL identity is the slot, not the trust name.

Parameters:
  • fl_client_name (str) – The FL kit slot name reported by the FL server.

  • session (Session) – The database session.

Returns:

The resolved trust, or None when the slot name matches no

assigned slot (an unknown / unassigned FL kit slot).

Return type:

Trust | None

flip_api.model_services.services.model_service.validate_trust_ids(model_id: uuid.UUID, trust_ids: list[uuid.UUID], session: sqlmodel.Session) bool

Validate that every trust id is associated with the model via ModelTrustIntersect.

Parameters:
  • model_id (UUID) – The ID of the model.

  • trust_ids (list[UUID]) – Trust ids to validate.

  • session (Session) – The database session.

Returns:

True if every id is associated with the model, False otherwise.

Return type:

bool

flip_api.model_services.services.model_service.get_metrics(model_id: uuid.UUID, session: sqlmodel.Session) list[flip_api.domain.interfaces.model.IModelMetrics]

Get the metrics for a given model.

Parameters:
  • model_id (UUID) – The ID of the model.

  • session (Session) – The database session.

Returns:

A list of metrics associated with the model.

Return type:

list[IModelMetrics]

flip_api.model_services.services.model_service._run_trusts_by_model(model_ids: list[uuid.UUID], session: sqlmodel.Session) dict[uuid.UUID, list[flip_api.domain.interfaces.model.ITrustSummary]]

Group each model’s dispatched (run) trusts, keyed by model id.

Reads each model’s latest FL job’s fl_job_trust roster — the trusts actually selected at initiate-training. Deliberately NOT ModelTrustIntersect: that table gets a row per approved trust at model creation, so it lists the approved pool, not the selected subset. Models awaiting dispatch have no job and simply map to an empty list.

Parameters:
  • model_ids (list[UUID]) – Model ids to fetch trusts for. Empty input short-circuits.

  • session (Session) – The database session.

Returns:

model_id -> [trust, ...]. Missing keys mean no trusts.

Return type:

dict[UUID, list[ITrustSummary]]

flip_api.model_services.services.model_service.queued_positions_by_model(session: sqlmodel.Session) dict[uuid.UUID, int]

Map each queued model to its 1-based place in the FL training queue.

The rank mirrors the scheduler’s pickup order exactly — QUEUED FLJob rows by created ascending, id as tiebreak (check_for_queued_jobs) — so position 1 is the next model to start when a net frees up. Models with no queued job are simply absent. A model queued twice (FLJob has no uniqueness on model_id) maps to its earliest position, so the status pill shows the position the model will actually be picked at, while the activity feed logs each job’s own position.

Parameters:

session (Session) – The database session.

Returns:

Model id to 1-based queue position.

Return type:

dict[UUID, int]

flip_api.model_services.services.model_service.get_all_models_service(session: sqlmodel.Session, user_id: uuid.UUID | None, query_params: dict, status_filter: list[flip_api.domain.schemas.status.ModelStatus] | None = None) tuple[flip_api.utils.paging_utils.IPagedResponse[flip_api.domain.interfaces.model.IAllModelsResponse], dict[str, int], flip_api.utils.paging_utils.PagingInfo]

Estate-wide, access-scoped model list joined with project name, owner and run trusts (#726).

Mirrors the projects-list access rule: when user_id is provided the query is restricted to models whose project the user owns or has a ProjectUserAccess row for; when user_id is None (a manager with CAN_MANAGE_PROJECTS) no access filter is applied. Soft-deleted models and projects are always excluded. Search matches the model name or the owning project name; results are newest-first and paginated.

Alongside the page it returns per-status counts over the same access-scoped, search-filtered set but without the status filter — so the filter tiles keep their numbers while one is selected.

Parameters:
  • session (Session) – The database session.

  • user_id (UUID | None) – The requesting user, or None to see every model (manager bypass).

  • query_params (dict) – Raw query params (pageNumber / pageSize / search).

  • status_filter (list[ModelStatus] | None) – Optional set of statuses to filter the list to.

Returns:

the page of rows, the status value -> count map for the tiles, and the paging details.

Return type:

tuple[IPagedResponse[IAllModelsResponse], dict[str, int], PagingInfo]