Skip to content

Commit

Permalink
feat: add endpoint to list all models across catalog sources
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirajsb committed Jan 24, 2025
1 parent 7f2ebdb commit 098e54e
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 2 deletions.
30 changes: 28 additions & 2 deletions api/openapi/model-registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,34 @@ paths:
type: string
in: path
required: true
/api/model_catalog/v1alpha3/catalog_sources/models:
summary: Path used to get the list of catalog models from all catalog sources.
description: >-
The REST endpoint/path used to list zero or more `CatalogModel` entities from all `CatalogSources`.
get:
tags:
- ModelCatalogService
parameters:
- $ref: "#/components/parameters/name"
- $ref: "#/components/parameters/externalId"
- $ref: "#/components/parameters/pageSize"
- $ref: "#/components/parameters/orderBy"
- $ref: "#/components/parameters/sortOrder"
- $ref: "#/components/parameters/offset"
responses:
"200":
$ref: "#/components/responses/CatalogModelListResponse"
"400":
$ref: "#/components/responses/BadRequest"
"401":
$ref: "#/components/responses/Unauthorized"
"404":
$ref: "#/components/responses/NotFound"
"500":
$ref: "#/components/responses/InternalServerError"
operationId: getAllCatalogModels
summary: List All CatalogModels from CatalogSources
description: Gets a list of all `CatalogModel` entities.
/api/model_catalog/v1alpha3/catalog_sources/{id}/models/{model_id}:
summary: Path used to get a single CatalogModel.
description: >-
Expand Down Expand Up @@ -1249,8 +1277,6 @@ components:
type within a Model Registry instance and cannot be changed once set.
type: string
artifact:
description: |-
The catalog model artifact.
$ref: "#/components/schemas/ModelArtifact"
ArtifactState:
description: |2-
Expand Down
1 change: 1 addition & 0 deletions internal/server/openapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ModelCatalogServiceAPIServicer interface {
GetCatalogModels(context.Context, string, string, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetCatalogSource(context.Context, string) (ImplResponse, error)
GetCatalogSources(context.Context, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetAllCatalogModels(context.Context, string, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
}

// ModelRegistryServiceAPIRouter defines the required methods for binding the api requests to a responses for the ModelRegistryServiceAPI
Expand Down
24 changes: 24 additions & 0 deletions internal/server/openapi/api_model_catalog_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func NewModelCatalogServiceAPIController(s ModelCatalogServiceAPIServicer, opts
// Routes returns all the api routes for the ModelCatalogServiceAPIController
func (c *ModelCatalogServiceAPIController) Routes() Routes {
return Routes{
"GetAllCatalogModels": Route{
strings.ToUpper("Get"),
"/api/model_catalog/v1alpha3/catalog_sources/models",
c.GetAllCatalogModels,
},
"GetCatalogModel": Route{
strings.ToUpper("Get"),
"/api/model_catalog/v1alpha3/catalog_sources/{id}/models/{model_id}",
Expand Down Expand Up @@ -84,6 +89,25 @@ func (c *ModelCatalogServiceAPIController) Routes() Routes {
}
}

// GetAllCatalogModels - List All CatalogModels from CatalogSources
func (c *ModelCatalogServiceAPIController) GetAllCatalogModels(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
nameParam := query.Get("name")
externalIdParam := query.Get("externalId")
pageSizeParam := query.Get("pageSize")
orderByParam := query.Get("orderBy")
sortOrderParam := query.Get("sortOrder")
offsetParam := query.Get("offset")
result, err := c.service.GetAllCatalogModels(r.Context(), nameParam, externalIdParam, pageSizeParam, model.OrderByField(orderByParam), model.SortOrder(sortOrderParam), offsetParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}

// GetCatalogModel - Get a CatalogModel
func (c *ModelCatalogServiceAPIController) GetCatalogModel(w http.ResponseWriter, r *http.Request) {
idParam := chi.URLParam(r, "id")
Expand Down
22 changes: 22 additions & 0 deletions internal/server/openapi/api_model_catalog_service_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ type ModelCatalogServiceAPIService struct {
modelCatalogs map[string]catalog.ModelCatalogApi
}

func (m ModelCatalogServiceAPIService) GetAllCatalogModels(ctx context.Context, name string, externalId string, pageSize string, orderBy openapi.OrderByField, sortOrder openapi.SortOrder, offset string) (ImplResponse, error) {
var lastError error
var allModels openapi.CatalogModelList
for _, modelCatalog := range m.modelCatalogs {
models, err := modelCatalog.GetCatalogModels(ctx, name, externalId, pageSize, orderBy, sortOrder, offset)
if err != nil {
lastError = err
}
allModels.Items = append(allModels.Items, models.Items...)
allModels.PageSize = models.PageSize
allModels.Size += models.Size
}
if lastError != nil && allModels.Size == 0 {
// only return an error if there are no models from any catalogs
// NOTE: catalog access errors are silently ignored if at least one catalog is functioning
return Response(http.StatusInternalServerError, lastError), lastError
}
return Response(http.StatusOK, allModels), lastError
}

func (m ModelCatalogServiceAPIService) GetCatalogModel(ctx context.Context, id string, modelId string) (ImplResponse, error) {
catalog, ok := m.modelCatalogs[id]
if !ok {
Expand Down Expand Up @@ -104,6 +124,8 @@ func missingCatalogError(id string) (ImplResponse, error) {
return ErrorResponse(http.StatusNotFound, err), err
}

var _ ModelCatalogServiceAPIServicer = &ModelCatalogServiceAPIService{}

// NewModelCatalogServiceAPIService creates a default api service
func NewModelCatalogServiceAPIService(modelCatalogs map[string]catalog.ModelCatalogApi) ModelCatalogServiceAPIServicer {
return &ModelCatalogServiceAPIService{
Expand Down
203 changes: 203 additions & 0 deletions pkg/openapi/api_model_catalog_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 098e54e

Please sign in to comment.