Skip to content

Commit 17d3134

Browse files
committed
feat: optimize variable set and variable apis
1 parent 1d7ed52 commit 17d3134

File tree

14 files changed

+100
-62
lines changed

14 files changed

+100
-62
lines changed

api/openapispec/docs.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -3391,8 +3391,8 @@ const docTemplate = `{
33913391
},
33923392
{
33933393
"type": "boolean",
3394-
"description": "Whether to sort the list in ascending order. Default to false",
3395-
"name": "ascending",
3394+
"description": "Whether to sort the list in descending order. Default to false",
3395+
"name": "descending",
33963396
"in": "query"
33973397
},
33983398
{
@@ -3762,8 +3762,8 @@ const docTemplate = `{
37623762
},
37633763
{
37643764
"type": "boolean",
3765-
"description": "Whether to sort the list in ascending order. Default to false",
3766-
"name": "ascending",
3765+
"description": "Whether to sort the list in descending order. Default to false",
3766+
"name": "descending",
37673767
"in": "query"
37683768
},
37693769
{
@@ -4763,18 +4763,18 @@ const docTemplate = `{
47634763
"constant.SourceProviderType": {
47644764
"type": "string",
47654765
"enum": [
4766-
"git",
47674766
"git",
47684767
"github",
47694768
"oci",
4770-
"local"
4769+
"local",
4770+
"git"
47714771
],
47724772
"x-enum-varnames": [
4773-
"DefaultSourceType",
47744773
"SourceProviderTypeGit",
47754774
"SourceProviderTypeGithub",
47764775
"SourceProviderTypeOCI",
4777-
"SourceProviderTypeLocal"
4776+
"SourceProviderTypeLocal",
4777+
"DefaultSourceType"
47784778
]
47794779
},
47804780
"constant.StackState": {

api/openapispec/swagger.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -3380,8 +3380,8 @@
33803380
},
33813381
{
33823382
"type": "boolean",
3383-
"description": "Whether to sort the list in ascending order. Default to false",
3384-
"name": "ascending",
3383+
"description": "Whether to sort the list in descending order. Default to false",
3384+
"name": "descending",
33853385
"in": "query"
33863386
},
33873387
{
@@ -3751,8 +3751,8 @@
37513751
},
37523752
{
37533753
"type": "boolean",
3754-
"description": "Whether to sort the list in ascending order. Default to false",
3755-
"name": "ascending",
3754+
"description": "Whether to sort the list in descending order. Default to false",
3755+
"name": "descending",
37563756
"in": "query"
37573757
},
37583758
{
@@ -4752,18 +4752,18 @@
47524752
"constant.SourceProviderType": {
47534753
"type": "string",
47544754
"enum": [
4755-
"git",
47564755
"git",
47574756
"github",
47584757
"oci",
4759-
"local"
4758+
"local",
4759+
"git"
47604760
],
47614761
"x-enum-varnames": [
4762-
"DefaultSourceType",
47634762
"SourceProviderTypeGit",
47644763
"SourceProviderTypeGithub",
47654764
"SourceProviderTypeOCI",
4766-
"SourceProviderTypeLocal"
4765+
"SourceProviderTypeLocal",
4766+
"DefaultSourceType"
47674767
]
47684768
},
47694769
"constant.StackState": {

api/openapispec/swagger.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ definitions:
3030
constant.SourceProviderType:
3131
enum:
3232
- git
33-
- git
3433
- github
3534
- oci
3635
- local
36+
- git
3737
type: string
3838
x-enum-varnames:
39-
- DefaultSourceType
4039
- SourceProviderTypeGit
4140
- SourceProviderTypeGithub
4241
- SourceProviderTypeOCI
4342
- SourceProviderTypeLocal
43+
- DefaultSourceType
4444
constant.StackState:
4545
enum:
4646
- UnSynced
@@ -3754,9 +3754,9 @@ paths:
37543754
in: query
37553755
name: sortBy
37563756
type: string
3757-
- description: Whether to sort the list in ascending order. Default to false
3757+
- description: Whether to sort the list in descending order. Default to false
37583758
in: query
3759-
name: ascending
3759+
name: descending
37603760
type: boolean
37613761
- description: Whether to list all the variables
37623762
in: query
@@ -3999,9 +3999,9 @@ paths:
39993999
in: query
40004000
name: sortBy
40014001
type: string
4002-
- description: Whether to sort the list in ascending order. Default to false
4002+
- description: Whether to sort the list in descending order. Default to false
40034003
in: query
4004-
name: ascending
4004+
name: descending
40054005
type: boolean
40064006
- description: Whether to list all the variable sets
40074007
in: query

pkg/domain/constant/global.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
ResourcePageSizeLarge = 1000
2828
CommonPageDefault = 1
2929
CommonPageSizeDefault = 10
30-
CommonMaxResultLimit = 1000
30+
CommonMaxResultLimit = 10000
3131
SortByCreateTimestamp = "createTimestamp"
3232
SortByModifiedTimestamp = "modifiedTimestamp"
3333
SortByName = "name"

pkg/domain/entity/types.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Pagination struct {
66
}
77

88
type SortOptions struct {
9-
Field string
10-
Ascending bool
9+
Field string
10+
Ascending bool
11+
Descending bool
1112
}

pkg/infra/persistence/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func GetVariableQuery(filter *entity.VariableFilter) (string, []interface{}) {
249249
args = append(args, filter.Name)
250250
}
251251
if filter.VariableSet != "" {
252-
pattern = append(pattern, "variable.variable_set LIKE ?")
252+
pattern = append(pattern, "variable.variable_set = ?")
253253
args = append(args, filter.VariableSet)
254254
}
255255
return CombineQueryParts(pattern), args

pkg/infra/persistence/variable.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (v *variableRepository) List(ctx context.Context,
105105
pattern, args := GetVariableQuery(filter)
106106

107107
sortArgs := sortOptions.Field
108-
if !sortOptions.Ascending {
108+
if sortOptions.Descending {
109109
sortArgs += " DESC"
110110
}
111111

pkg/infra/persistence/variable_set.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (vs *variableSetRepository) List(ctx context.Context,
104104
pattern, args := GetVariableSetQuery(filter)
105105

106106
sortArgs := sortOptions.Field
107-
if !sortOptions.Ascending {
107+
if sortOptions.Descending {
108108
sortArgs += " DESC"
109109
}
110110

pkg/server/handler/render.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ func GenerateResponse(ctx context.Context, data any, err error) render.Renderer
1818
// Set the Success and Message fields based on the error parameter.
1919
if err == nil {
2020
resp.Success = true
21-
resp.Message = SuccessMessage
21+
// Get the response message from the context.
22+
if rspMsg := appmiddleware.GetResponseMessage(ctx); rspMsg != "" {
23+
resp.Message = rspMsg
24+
} else {
25+
resp.Message = SuccessMessage
26+
}
2227
resp.Data = data
2328
} else {
2429
resp.Success = false
25-
resp.Message = err.Error()
30+
// Get the response message from the context.
31+
if rspMsg := appmiddleware.GetResponseMessage(ctx); rspMsg != "" {
32+
resp.Message = rspMsg
33+
} else {
34+
resp.Message = err.Error()
35+
}
2636
}
2737

2838
// Include the request trace ID if available.

pkg/server/handler/variable/handler.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"kusionstack.io/kusion/pkg/server/handler"
1414
"kusionstack.io/kusion/pkg/server/manager/variable"
1515
"kusionstack.io/kusion/pkg/server/manager/variableset"
16+
"kusionstack.io/kusion/pkg/server/middleware"
1617
logutil "kusionstack.io/kusion/pkg/server/util/logging"
1718
)
1819

@@ -100,7 +101,7 @@ func (h *Handler) DeleteVariable() http.HandlerFunc {
100101
// @Failure 429 {object} error "Too Many Requests"
101102
// @Failure 404 {object} error "Not Found"
102103
// @Failure 500 {object} error "Internal Server Error"
103-
// @Router /api/v1/variables/{variableSetName}/{variableName} [put]
104+
// @Router /api/v1/variables/{variableSetName}/{variableName} [put]
104105
func (h *Handler) UpdateVariable() http.HandlerFunc {
105106
return func(w http.ResponseWriter, r *http.Request) {
106107
// Getting stuff from context.
@@ -154,7 +155,7 @@ func (h *Handler) UpdateVariable() http.HandlerFunc {
154155
// @Failure 429 {object} error "Too Many Requests"
155156
// @Failure 404 {object} error "Not Found"
156157
// @Failure 500 {object} error "Internal Server Error"
157-
// @Router /api/v1/variables/{variableSetName}/{variableName} [get]
158+
// @Router /api/v1/variables/{variableSetName}/{variableName} [get]
158159
func (h *Handler) GetVariable() http.HandlerFunc {
159160
return func(w http.ResponseWriter, r *http.Request) {
160161
// Getting stuff from context.
@@ -181,7 +182,7 @@ func (h *Handler) GetVariable() http.HandlerFunc {
181182
// @Param page query uint false "The current page to fetch. Default to 1"
182183
// @Param pageSize query uint false "The size of the page. Default to 10"
183184
// @Param sortBy query string false "Which field to sort the list by. Default to id"
184-
// @Param ascending query bool false "Whether to sort the list in ascending order. Default to false"
185+
// @Param descending query bool false "Whether to sort the list in descending order. Default to false"
185186
// @Param fetchAll query bool false "Whether to list all the variables"
186187
// @Success 200 {object} handler.Response{data=response.PaginatedVariableResponse} "Success"
187188
// @Failure 400 {object} error "Bad Request"
@@ -213,6 +214,12 @@ func (h *Handler) ListVariables() http.HandlerFunc {
213214
return
214215
}
215216

217+
// If the amount of variable sets exceeds the maximum result limit,
218+
// then indicate in the response message.
219+
if len(variableEntities.Variables) < variableEntities.Total {
220+
ctx = context.WithValue(ctx, middleware.ResponseMessageKey, "the result exceeds the maximum amount limit")
221+
}
222+
216223
paginatedResponse := response.PaginatedVariableResponse{
217224
Variables: variableEntities.Variables,
218225
Total: variableEntities.Total,

pkg/server/handler/variableset/handler.go

+16-21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"kusionstack.io/kusion/pkg/domain/response"
1616
"kusionstack.io/kusion/pkg/server/handler"
1717
"kusionstack.io/kusion/pkg/server/manager/variableset"
18+
"kusionstack.io/kusion/pkg/server/middleware"
1819
logutil "kusionstack.io/kusion/pkg/server/util/logging"
1920
)
2021

@@ -100,7 +101,7 @@ func (h *Handler) DeleteVariableSet() http.HandlerFunc {
100101
// @Failure 429 {object} error "Too Many Requests"
101102
// @Failure 404 {object} error "Not Found"
102103
// @Failure 500 {object} error "Internal Server Error"
103-
// @Router /api/v1/variablesets/{variableSetName} [put]
104+
// @Router /api/v1/variablesets/{variableSetName} [put]
104105
func (h *Handler) UpdateVariableSet() http.HandlerFunc {
105106
return func(w http.ResponseWriter, r *http.Request) {
106107
// Getting stuff from context.
@@ -147,7 +148,7 @@ func (h *Handler) UpdateVariableSet() http.HandlerFunc {
147148
// @Failure 429 {object} error "Too Many Requests"
148149
// @Failure 404 {object} error "Not Found"
149150
// @Failure 500 {object} error "Internal Server Error"
150-
// @Router /api/v1/variablesets/{variableSetName} [get]
151+
// @Router /api/v1/variablesets/{variableSetName} [get]
151152
func (h *Handler) GetVariableSet() http.HandlerFunc {
152153
return func(w http.ResponseWriter, r *http.Request) {
153154
// Getting stuff from context.
@@ -172,7 +173,7 @@ func (h *Handler) GetVariableSet() http.HandlerFunc {
172173
// @Param page query uint false "The current page to fetch. Default to 1"
173174
// @Param pageSize query uint false "The size of the page. Default to 10"
174175
// @Param sortBy query string false "Which field to sort the list by. Default to id"
175-
// @Param ascending query bool false "Whether to sort the list in ascending order. Default to false"
176+
// @Param descending query bool false "Whether to sort the list in descending order. Default to false"
176177
// @Param fetchAll query bool false "Whether to list all the variable sets"
177178
// @Success 200 {object} handler.Response{data=response.PaginatedVariableSetResponse} "Success"
178179
// @Failure 400 {object} error "Bad Request"
@@ -204,6 +205,12 @@ func (h *Handler) ListVariableSets() http.HandlerFunc {
204205
return
205206
}
206207

208+
// If the amount of variable sets exceeds the maximum result limit,
209+
// then indicate in the response message.
210+
if len(variableSetEntities.VariableSets) < variableSetEntities.Total {
211+
ctx = context.WithValue(ctx, middleware.ResponseMessageKey, "the result exceeds the maximum amount limit")
212+
}
213+
207214
paginatedResponse := response.PaginatedVariableSetResponse{
208215
VariableSets: variableSetEntities.VariableSets,
209216
Total: variableSetEntities.Total,
@@ -247,13 +254,13 @@ func (h *Handler) ListVariableSetsByLabels() http.HandlerFunc {
247254
filter := &entity.VariableSetFilter{
248255
Pagination: &entity.Pagination{
249256
Page: constant.CommonPageDefault,
250-
PageSize: constant.CommonPageSizeDefault,
257+
PageSize: constant.CommonMaxResultLimit,
251258
},
252259
FetchAll: true,
253260
}
254261
sortOptions := &entity.SortOptions{
255-
Field: constant.SortByID,
256-
Ascending: false,
262+
Field: constant.SortByID,
263+
Descending: false,
257264
}
258265

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

265272
// If the amount of variable sets exceeds the maximum result limit,
266-
// then retrieve the complete result through a looped query.
267-
for len(variableSetEntities.VariableSets) < variableSetEntities.Total {
268-
filter = &entity.VariableSetFilter{
269-
Pagination: &entity.Pagination{
270-
Page: 2,
271-
PageSize: len(variableSetEntities.VariableSets),
272-
},
273-
FetchAll: true,
274-
}
275-
tmpVariableSetEntities, err := h.variableSetManager.ListVariableSets(ctx, filter, sortOptions)
276-
if err != nil {
277-
render.Render(w, r, handler.FailureResponse(ctx, err))
278-
return
279-
}
280-
variableSetEntities.VariableSets = append(variableSetEntities.VariableSets, tmpVariableSetEntities.VariableSets...)
273+
// then indicate in the response message.
274+
if len(variableSetEntities.VariableSets) < variableSetEntities.Total {
275+
ctx = context.WithValue(ctx, middleware.ResponseMessageKey, "the result exceeds the maximum amount limit")
281276
}
282277

283278
var matchedVariableSets []*entity.VariableSet

pkg/server/manager/variable/variable_manager.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ func (v *VariableManager) BuildVariableFilterAndSortOptions(ctx context.Context,
143143
if err != nil {
144144
return nil, nil, err
145145
}
146-
SortOrderAscending, _ := strconv.ParseBool(query.Get("ascending"))
147-
variableSetSortOptions := &entity.SortOptions{
148-
Field: sortBy,
149-
Ascending: SortOrderAscending,
146+
sortOrderDescending, _ := strconv.ParseBool(query.Get("descending"))
147+
variableSortOptions := &entity.SortOptions{
148+
Field: sortBy,
149+
Descending: sortOrderDescending,
150150
}
151151

152-
return &filter, variableSetSortOptions, nil
152+
return &filter, variableSortOptions, nil
153153
}

pkg/server/manager/variableset/variable_set_manager.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ func (vs *VariableSetManager) BuildVariableSetFilterAndSortOptions(ctx context.C
135135
if err != nil {
136136
return nil, nil, err
137137
}
138-
SortOrderAscending, _ := strconv.ParseBool(query.Get("ascending"))
138+
sortOrderDescending, _ := strconv.ParseBool(query.Get("descending"))
139139
variableSetSortOptions := &entity.SortOptions{
140-
Field: sortBy,
141-
Ascending: SortOrderAscending,
140+
Field: sortBy,
141+
Descending: sortOrderDescending,
142142
}
143143

144144
return &filter, variableSetSortOptions, nil

pkg/server/middleware/message.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package middleware
2+
3+
import (
4+
"context"
5+
)
6+
7+
// Key to use when setting the response message.
8+
type (
9+
ctxKeyResponseMessage string
10+
)
11+
12+
// ResponseMessageKey is a context key used for associating a message with a response.
13+
const ResponseMessageKey ctxKeyResponseMessage = "response_message"
14+
15+
// GetResponseMessage returns a response message from the given context.
16+
func GetResponseMessage(ctx context.Context) string {
17+
if ctx == nil {
18+
return ""
19+
}
20+
if responseMessage, ok := ctx.Value(ResponseMessageKey).(string); ok {
21+
return responseMessage
22+
}
23+
24+
return ""
25+
}

0 commit comments

Comments
 (0)