Skip to content

Commit

Permalink
feat: optimize variable set and variable apis
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 committed Feb 13, 2025
1 parent 1d7ed52 commit 17d3134
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 62 deletions.
16 changes: 8 additions & 8 deletions api/openapispec/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3391,8 +3391,8 @@ const docTemplate = `{
},
{
"type": "boolean",
"description": "Whether to sort the list in ascending order. Default to false",
"name": "ascending",
"description": "Whether to sort the list in descending order. Default to false",
"name": "descending",
"in": "query"
},
{
Expand Down Expand Up @@ -3762,8 +3762,8 @@ const docTemplate = `{
},
{
"type": "boolean",
"description": "Whether to sort the list in ascending order. Default to false",
"name": "ascending",
"description": "Whether to sort the list in descending order. Default to false",
"name": "descending",
"in": "query"
},
{
Expand Down Expand Up @@ -4763,18 +4763,18 @@ const docTemplate = `{
"constant.SourceProviderType": {
"type": "string",
"enum": [
"git",
"git",
"github",
"oci",
"local"
"local",
"git"
],
"x-enum-varnames": [
"DefaultSourceType",
"SourceProviderTypeGit",
"SourceProviderTypeGithub",
"SourceProviderTypeOCI",
"SourceProviderTypeLocal"
"SourceProviderTypeLocal",
"DefaultSourceType"
]
},
"constant.StackState": {
Expand Down
16 changes: 8 additions & 8 deletions api/openapispec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3380,8 +3380,8 @@
},
{
"type": "boolean",
"description": "Whether to sort the list in ascending order. Default to false",
"name": "ascending",
"description": "Whether to sort the list in descending order. Default to false",
"name": "descending",
"in": "query"
},
{
Expand Down Expand Up @@ -3751,8 +3751,8 @@
},
{
"type": "boolean",
"description": "Whether to sort the list in ascending order. Default to false",
"name": "ascending",
"description": "Whether to sort the list in descending order. Default to false",
"name": "descending",
"in": "query"
},
{
Expand Down Expand Up @@ -4752,18 +4752,18 @@
"constant.SourceProviderType": {
"type": "string",
"enum": [
"git",
"git",
"github",
"oci",
"local"
"local",
"git"
],
"x-enum-varnames": [
"DefaultSourceType",
"SourceProviderTypeGit",
"SourceProviderTypeGithub",
"SourceProviderTypeOCI",
"SourceProviderTypeLocal"
"SourceProviderTypeLocal",
"DefaultSourceType"
]
},
"constant.StackState": {
Expand Down
12 changes: 6 additions & 6 deletions api/openapispec/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ definitions:
constant.SourceProviderType:
enum:
- git
- git
- github
- oci
- local
- git
type: string
x-enum-varnames:
- DefaultSourceType
- SourceProviderTypeGit
- SourceProviderTypeGithub
- SourceProviderTypeOCI
- SourceProviderTypeLocal
- DefaultSourceType
constant.StackState:
enum:
- UnSynced
Expand Down Expand Up @@ -3754,9 +3754,9 @@ paths:
in: query
name: sortBy
type: string
- description: Whether to sort the list in ascending order. Default to false
- description: Whether to sort the list in descending order. Default to false
in: query
name: ascending
name: descending
type: boolean
- description: Whether to list all the variables
in: query
Expand Down Expand Up @@ -3999,9 +3999,9 @@ paths:
in: query
name: sortBy
type: string
- description: Whether to sort the list in ascending order. Default to false
- description: Whether to sort the list in descending order. Default to false
in: query
name: ascending
name: descending
type: boolean
- description: Whether to list all the variable sets
in: query
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/constant/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
ResourcePageSizeLarge = 1000
CommonPageDefault = 1
CommonPageSizeDefault = 10
CommonMaxResultLimit = 1000
CommonMaxResultLimit = 10000
SortByCreateTimestamp = "createTimestamp"
SortByModifiedTimestamp = "modifiedTimestamp"
SortByName = "name"
Expand Down
5 changes: 3 additions & 2 deletions pkg/domain/entity/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Pagination struct {
}

type SortOptions struct {
Field string
Ascending bool
Field string
Ascending bool
Descending bool
}
2 changes: 1 addition & 1 deletion pkg/infra/persistence/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func GetVariableQuery(filter *entity.VariableFilter) (string, []interface{}) {
args = append(args, filter.Name)
}
if filter.VariableSet != "" {
pattern = append(pattern, "variable.variable_set LIKE ?")
pattern = append(pattern, "variable.variable_set = ?")
args = append(args, filter.VariableSet)
}
return CombineQueryParts(pattern), args
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/persistence/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (v *variableRepository) List(ctx context.Context,
pattern, args := GetVariableQuery(filter)

sortArgs := sortOptions.Field
if !sortOptions.Ascending {
if sortOptions.Descending {
sortArgs += " DESC"
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/infra/persistence/variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (vs *variableSetRepository) List(ctx context.Context,
pattern, args := GetVariableSetQuery(filter)

sortArgs := sortOptions.Field
if !sortOptions.Ascending {
if sortOptions.Descending {
sortArgs += " DESC"
}

Expand Down
14 changes: 12 additions & 2 deletions pkg/server/handler/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ func GenerateResponse(ctx context.Context, data any, err error) render.Renderer
// Set the Success and Message fields based on the error parameter.
if err == nil {
resp.Success = true
resp.Message = SuccessMessage
// Get the response message from the context.
if rspMsg := appmiddleware.GetResponseMessage(ctx); rspMsg != "" {
resp.Message = rspMsg
} else {
resp.Message = SuccessMessage
}
resp.Data = data
} else {
resp.Success = false
resp.Message = err.Error()
// Get the response message from the context.
if rspMsg := appmiddleware.GetResponseMessage(ctx); rspMsg != "" {
resp.Message = rspMsg
} else {
resp.Message = err.Error()
}
}

// Include the request trace ID if available.
Expand Down
13 changes: 10 additions & 3 deletions pkg/server/handler/variable/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"kusionstack.io/kusion/pkg/server/handler"
"kusionstack.io/kusion/pkg/server/manager/variable"
"kusionstack.io/kusion/pkg/server/manager/variableset"
"kusionstack.io/kusion/pkg/server/middleware"
logutil "kusionstack.io/kusion/pkg/server/util/logging"
)

Expand Down Expand Up @@ -100,7 +101,7 @@ func (h *Handler) DeleteVariable() http.HandlerFunc {
// @Failure 429 {object} error "Too Many Requests"
// @Failure 404 {object} error "Not Found"
// @Failure 500 {object} error "Internal Server Error"
// @Router /api/v1/variables/{variableSetName}/{variableName} [put]
// @Router /api/v1/variables/{variableSetName}/{variableName} [put]
func (h *Handler) UpdateVariable() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Getting stuff from context.
Expand Down Expand Up @@ -154,7 +155,7 @@ func (h *Handler) UpdateVariable() http.HandlerFunc {
// @Failure 429 {object} error "Too Many Requests"
// @Failure 404 {object} error "Not Found"
// @Failure 500 {object} error "Internal Server Error"
// @Router /api/v1/variables/{variableSetName}/{variableName} [get]
// @Router /api/v1/variables/{variableSetName}/{variableName} [get]
func (h *Handler) GetVariable() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Getting stuff from context.
Expand All @@ -181,7 +182,7 @@ func (h *Handler) GetVariable() http.HandlerFunc {
// @Param page query uint false "The current page to fetch. Default to 1"
// @Param pageSize query uint false "The size of the page. Default to 10"
// @Param sortBy query string false "Which field to sort the list by. Default to id"
// @Param ascending query bool false "Whether to sort the list in ascending order. Default to false"
// @Param descending query bool false "Whether to sort the list in descending order. Default to false"
// @Param fetchAll query bool false "Whether to list all the variables"
// @Success 200 {object} handler.Response{data=response.PaginatedVariableResponse} "Success"
// @Failure 400 {object} error "Bad Request"
Expand Down Expand Up @@ -213,6 +214,12 @@ func (h *Handler) ListVariables() http.HandlerFunc {
return
}

// If the amount of variable sets exceeds the maximum result limit,
// then indicate in the response message.
if len(variableEntities.Variables) < variableEntities.Total {
ctx = context.WithValue(ctx, middleware.ResponseMessageKey, "the result exceeds the maximum amount limit")
}

paginatedResponse := response.PaginatedVariableResponse{
Variables: variableEntities.Variables,
Total: variableEntities.Total,
Expand Down
37 changes: 16 additions & 21 deletions pkg/server/handler/variableset/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"kusionstack.io/kusion/pkg/domain/response"
"kusionstack.io/kusion/pkg/server/handler"
"kusionstack.io/kusion/pkg/server/manager/variableset"
"kusionstack.io/kusion/pkg/server/middleware"
logutil "kusionstack.io/kusion/pkg/server/util/logging"
)

Expand Down Expand Up @@ -100,7 +101,7 @@ func (h *Handler) DeleteVariableSet() http.HandlerFunc {
// @Failure 429 {object} error "Too Many Requests"
// @Failure 404 {object} error "Not Found"
// @Failure 500 {object} error "Internal Server Error"
// @Router /api/v1/variablesets/{variableSetName} [put]
// @Router /api/v1/variablesets/{variableSetName} [put]
func (h *Handler) UpdateVariableSet() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Getting stuff from context.
Expand Down Expand Up @@ -147,7 +148,7 @@ func (h *Handler) UpdateVariableSet() http.HandlerFunc {
// @Failure 429 {object} error "Too Many Requests"
// @Failure 404 {object} error "Not Found"
// @Failure 500 {object} error "Internal Server Error"
// @Router /api/v1/variablesets/{variableSetName} [get]
// @Router /api/v1/variablesets/{variableSetName} [get]
func (h *Handler) GetVariableSet() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Getting stuff from context.
Expand All @@ -172,7 +173,7 @@ func (h *Handler) GetVariableSet() http.HandlerFunc {
// @Param page query uint false "The current page to fetch. Default to 1"
// @Param pageSize query uint false "The size of the page. Default to 10"
// @Param sortBy query string false "Which field to sort the list by. Default to id"
// @Param ascending query bool false "Whether to sort the list in ascending order. Default to false"
// @Param descending query bool false "Whether to sort the list in descending order. Default to false"
// @Param fetchAll query bool false "Whether to list all the variable sets"
// @Success 200 {object} handler.Response{data=response.PaginatedVariableSetResponse} "Success"
// @Failure 400 {object} error "Bad Request"
Expand Down Expand Up @@ -204,6 +205,12 @@ func (h *Handler) ListVariableSets() http.HandlerFunc {
return
}

// If the amount of variable sets exceeds the maximum result limit,
// then indicate in the response message.
if len(variableSetEntities.VariableSets) < variableSetEntities.Total {
ctx = context.WithValue(ctx, middleware.ResponseMessageKey, "the result exceeds the maximum amount limit")
}

paginatedResponse := response.PaginatedVariableSetResponse{
VariableSets: variableSetEntities.VariableSets,
Total: variableSetEntities.Total,
Expand Down Expand Up @@ -247,13 +254,13 @@ func (h *Handler) ListVariableSetsByLabels() http.HandlerFunc {
filter := &entity.VariableSetFilter{
Pagination: &entity.Pagination{
Page: constant.CommonPageDefault,
PageSize: constant.CommonPageSizeDefault,
PageSize: constant.CommonMaxResultLimit,
},
FetchAll: true,
}
sortOptions := &entity.SortOptions{
Field: constant.SortByID,
Ascending: false,
Field: constant.SortByID,
Descending: false,
}

variableSetEntities, err := h.variableSetManager.ListVariableSets(ctx, filter, sortOptions)
Expand All @@ -263,21 +270,9 @@ func (h *Handler) ListVariableSetsByLabels() http.HandlerFunc {
}

// If the amount of variable sets exceeds the maximum result limit,
// then retrieve the complete result through a looped query.
for len(variableSetEntities.VariableSets) < variableSetEntities.Total {
filter = &entity.VariableSetFilter{
Pagination: &entity.Pagination{
Page: 2,
PageSize: len(variableSetEntities.VariableSets),
},
FetchAll: true,
}
tmpVariableSetEntities, err := h.variableSetManager.ListVariableSets(ctx, filter, sortOptions)
if err != nil {
render.Render(w, r, handler.FailureResponse(ctx, err))
return
}
variableSetEntities.VariableSets = append(variableSetEntities.VariableSets, tmpVariableSetEntities.VariableSets...)
// then indicate in the response message.
if len(variableSetEntities.VariableSets) < variableSetEntities.Total {
ctx = context.WithValue(ctx, middleware.ResponseMessageKey, "the result exceeds the maximum amount limit")
}

var matchedVariableSets []*entity.VariableSet
Expand Down
10 changes: 5 additions & 5 deletions pkg/server/manager/variable/variable_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ func (v *VariableManager) BuildVariableFilterAndSortOptions(ctx context.Context,
if err != nil {
return nil, nil, err
}
SortOrderAscending, _ := strconv.ParseBool(query.Get("ascending"))
variableSetSortOptions := &entity.SortOptions{
Field: sortBy,
Ascending: SortOrderAscending,
sortOrderDescending, _ := strconv.ParseBool(query.Get("descending"))
variableSortOptions := &entity.SortOptions{
Field: sortBy,
Descending: sortOrderDescending,
}

return &filter, variableSetSortOptions, nil
return &filter, variableSortOptions, nil
}
6 changes: 3 additions & 3 deletions pkg/server/manager/variableset/variable_set_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ func (vs *VariableSetManager) BuildVariableSetFilterAndSortOptions(ctx context.C
if err != nil {
return nil, nil, err
}
SortOrderAscending, _ := strconv.ParseBool(query.Get("ascending"))
sortOrderDescending, _ := strconv.ParseBool(query.Get("descending"))
variableSetSortOptions := &entity.SortOptions{
Field: sortBy,
Ascending: SortOrderAscending,
Field: sortBy,
Descending: sortOrderDescending,
}

return &filter, variableSetSortOptions, nil
Expand Down
25 changes: 25 additions & 0 deletions pkg/server/middleware/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package middleware

import (
"context"
)

// Key to use when setting the response message.
type (
ctxKeyResponseMessage string
)

// ResponseMessageKey is a context key used for associating a message with a response.
const ResponseMessageKey ctxKeyResponseMessage = "response_message"

// GetResponseMessage returns a response message from the given context.
func GetResponseMessage(ctx context.Context) string {
if ctx == nil {
return ""
}
if responseMessage, ok := ctx.Value(ResponseMessageKey).(string); ok {
return responseMessage
}

return ""
}

0 comments on commit 17d3134

Please sign in to comment.