Skip to content

Commit

Permalink
feat: Model schemas API and migrate client to OpenAPI 3 (#518)
Browse files Browse the repository at this point in the history
<!--  Thanks for sending a pull request!  Here are some tips for you:

1. Run unit tests and ensure that they are passing
2. If your change introduces any API changes, make sure to update the
e2e tests
3. Make sure documentation is updated for your PR!

-->
# Description
<!-- Briefly describe the motivation for the change. Please include
illustrations where appropriate. -->
A model to onboard to model monitoring must define the schema that
indicate the contract of the data that is required to do prediction also
what is the actual or ground truth for that model. During the
implementation it needs to upgrade to OpenAPI 3 due to lack of `oneOf`
in OpenAPI 2.x

# Modifications
<!-- Summarize the key code changes. -->
* Regenerate python client
* More strict check in SDK
* Model schema

# Tests
<!-- Besides the existing / updated automated tests, what specific
scenarios should be tested? Consider the backward compatibility of the
changes, whether corner cases are covered, etc. Please describe the
tests and check the ones that have been completed. Eg:
- [x] Deploying new and existing standard models
- [ ] Deploying PyFunc models
-->

# Checklist
- [ ] Added PR label
- [x] Added unit test, integration, and/or e2e tests
- [ ] Tested locally
- [ ] Updated documentation
- [ ] Update Swagger spec if the PR introduce API changes
- [ ] Regenerated Golang and Python client if the PR introduces API
changes

# Release Notes
<!--
Does this PR introduce a user-facing change?
If no, just write "NONE" in the release-note block below.
If yes, a release note is required. Enter your extended release note in
the block below.
If the PR requires additional action from users switching to the new
release, include the string "action required".

For more information about release notes, see kubernetes' guide here:
http://git.k8s.io/community/contributors/guide/release-notes.md
-->

```release-note

```
  • Loading branch information
tiopramayudi authored Jan 23, 2024
1 parent 904a894 commit efbe83f
Show file tree
Hide file tree
Showing 206 changed files with 44,360 additions and 20,506 deletions.
19 changes: 13 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,29 @@ generate-client-go:
@echo "Generating Go client from swagger.yaml"
@mv ${CLIENT_GO_EXAMPLES_DIR} ${TEMP_CLIENT_GO_EXAMPLES_DIR}
@rm -rf ${CLIENT_GO_OUTPUT_DIR}
@swagger-codegen generate -i swagger.yaml -l go -o ${TEMP_CLIENT_GO_OUTPUT_DIR} -DpackageName=client
@docker run --rm -v ${PWD}/:/local openapitools/openapi-generator-cli:v7.1.0 generate \
--input-spec /local/swagger.yaml \
--generator-name go \
--output /local/api/client_tmp/. \
--config /local/openapi-api-codegen.yaml \
--openapi-normalizer KEEP_ONLY_FIRST_TAG_IN_OPERATION=true
@mkdir ${CLIENT_GO_OUTPUT_DIR}
@mv ${TEMP_CLIENT_GO_OUTPUT_DIR}/*.go ${CLIENT_GO_OUTPUT_DIR}
@rm -rf ${TEMP_CLIENT_GO_OUTPUT_DIR}
@mv ${TEMP_CLIENT_GO_EXAMPLES_DIR} ${CLIENT_GO_EXAMPLES_DIR}
@goimports -w ${CLIENT_GO_OUTPUT_DIR}


CLIENT_PYTHON_OUTPUT_DIR = ./python/sdk/client
TEMP_CLIENT_PYTHON_OUTPUT_DIR = ./python/sdk/client_tmp
.PHONY: generate-client-python
generate-client-python:
@echo "Generating Python client from swagger.yaml"
@rm -rf ${CLIENT_PYTHON_OUTPUT_DIR}
@swagger-codegen generate -i swagger.yaml -l python -o ${TEMP_CLIENT_PYTHON_OUTPUT_DIR} -DpackageName=client
@mv ${TEMP_CLIENT_PYTHON_OUTPUT_DIR}/client ${CLIENT_PYTHON_OUTPUT_DIR}
@rm -rf ${TEMP_CLIENT_PYTHON_OUTPUT_DIR}
rm -rf ${CLIENT_PYTHON_OUTPUT_DIR}
@docker run --rm -v ${PWD}/:/local openapitools/openapi-generator-cli:v7.1.0 generate \
--input-spec /local/swagger.yaml \
--generator-name python \
--output /local/python/sdk/. \
--config /local/openapi-sdk-codegen.yaml


.PHONY: generate-proto
Expand Down
69 changes: 69 additions & 0 deletions api/api/model_schema_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package api

import (
"errors"
"fmt"
"net/http"

"github.com/caraml-dev/merlin/models"
mErrors "github.com/caraml-dev/merlin/pkg/errors"
)

type ModelSchemaController struct {
*AppContext
}

func (m *ModelSchemaController) GetAllSchemas(r *http.Request, vars map[string]string, _ interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])
modelSchemas, err := m.ModelSchemaService.List(ctx, modelID)
if err != nil {
if errors.Is(err, mErrors.ErrNotFound) {
return NotFound(fmt.Sprintf("Model schemas not found: %v", err))
}
return InternalServerError(fmt.Sprintf("Error get All schemas with model id: %d with error: %v", modelID, err))
}
return Ok(modelSchemas)
}

func (m *ModelSchemaController) GetSchema(r *http.Request, vars map[string]string, _ interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])
modelSchemaID, _ := models.ParseID(vars["schema_id"])
modelSchema, err := m.ModelSchemaService.FindByID(ctx, modelSchemaID, modelID)
if err != nil {
if errors.Is(err, mErrors.ErrNotFound) {
return NotFound(fmt.Sprintf("Model schema with id: %d not found: %v", modelSchemaID, err))
}
return InternalServerError(fmt.Sprintf("Error get schema with id: %d, model id: %d and error: %v", modelSchemaID, modelID, err))
}

return Ok(modelSchema)
}

func (m *ModelSchemaController) CreateOrUpdateSchema(r *http.Request, vars map[string]string, body interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])

modelSchema, ok := body.(*models.ModelSchema)
if !ok {
return BadRequest("Unable to parse request body")
}
modelSchema.ModelID = modelID
schema, err := m.ModelSchemaService.Save(ctx, modelSchema)
if err != nil {
return InternalServerError(fmt.Sprintf("Error save model schema: %v", err))
}
return Ok(schema)
}

func (m *ModelSchemaController) DeleteSchema(r *http.Request, vars map[string]string, _ interface{}) *Response {
ctx := r.Context()
modelID, _ := models.ParseID(vars["model_id"])
modelSchemaID, _ := models.ParseID(vars["schema_id"])
modelSchema := &models.ModelSchema{ID: modelSchemaID, ModelID: modelID}
if err := m.ModelSchemaService.Delete(ctx, modelSchema); err != nil {
return InternalServerError(fmt.Sprintf("Error delete model schema: %v", err))
}
return NoContent()
}
Loading

0 comments on commit efbe83f

Please sign in to comment.