@@ -85,6 +85,11 @@ func getPageNumberAndSize(req *http.Request) (int, int) {
85
85
return int (pageInt ), int (sizeInt )
86
86
}
87
87
88
+ // showDuplicates returns if a request wants to retrieve charts. Default false
89
+ func showDuplicates (req * http.Request ) bool {
90
+ return len (req .FormValue ("showDuplicates" )) > 0
91
+ }
92
+
88
93
// min returns the minimum of two integers.
89
94
// We are not using math.Min since that compares float64
90
95
// and it's unnecessarily complex.
@@ -110,7 +115,7 @@ func uniqChartList(charts []*models.Chart) []*models.Chart {
110
115
return res
111
116
}
112
117
113
- func getPaginatedChartList (repo string , pageNumber , pageSize int ) (apiListResponse , interface {}, error ) {
118
+ func getPaginatedChartList (repo string , pageNumber , pageSize int , showDuplicates bool ) (apiListResponse , interface {}, error ) {
114
119
db , closer := dbSession .DB ()
115
120
defer closer ()
116
121
var charts []* models.Chart
@@ -121,17 +126,20 @@ func getPaginatedChartList(repo string, pageNumber, pageSize int) (apiListRespon
121
126
pipeline = append (pipeline , bson.M {"$match" : bson.M {"repo.name" : repo }})
122
127
}
123
128
124
- // We should query unique charts
125
- pipeline = append (pipeline ,
126
- // Add a new field to store the latest version
127
- bson.M {"$addFields" : bson.M {"firstChartVersion" : bson.M {"$arrayElemAt" : []interface {}{"$chartversions" , 0 }}}},
128
- // Group by unique digest for the latest version (remove duplicates)
129
- bson.M {"$group" : bson.M {"_id" : "$firstChartVersion.digest" , "chart" : bson.M {"$first" : "$$ROOT" }}},
130
- // Restore original object struct
131
- bson.M {"$replaceRoot" : bson.M {"newRoot" : "$chart" }},
132
- // Order by name
133
- bson.M {"$sort" : bson.M {"name" : 1 }},
134
- )
129
+ if ! showDuplicates {
130
+ // We should query unique charts
131
+ pipeline = append (pipeline ,
132
+ // Add a new field to store the latest version
133
+ bson.M {"$addFields" : bson.M {"firstChartVersion" : bson.M {"$arrayElemAt" : []interface {}{"$chartversions" , 0 }}}},
134
+ // Group by unique digest for the latest version (remove duplicates)
135
+ bson.M {"$group" : bson.M {"_id" : "$firstChartVersion.digest" , "chart" : bson.M {"$first" : "$$ROOT" }}},
136
+ // Restore original object struct
137
+ bson.M {"$replaceRoot" : bson.M {"newRoot" : "$chart" }},
138
+ )
139
+ }
140
+
141
+ // Order by name
142
+ pipeline = append (pipeline , bson.M {"$sort" : bson.M {"name" : 1 }})
135
143
136
144
totalPages := 1
137
145
if pageSize != 0 {
@@ -166,7 +174,7 @@ func getPaginatedChartList(repo string, pageNumber, pageSize int) (apiListRespon
166
174
// listCharts returns a list of charts
167
175
func listCharts (w http.ResponseWriter , req * http.Request ) {
168
176
pageNumber , pageSize := getPageNumberAndSize (req )
169
- cl , meta , err := getPaginatedChartList ("" , pageNumber , pageSize )
177
+ cl , meta , err := getPaginatedChartList ("" , pageNumber , pageSize , showDuplicates ( req ) )
170
178
if err != nil {
171
179
log .WithError (err ).Error ("could not fetch charts" )
172
180
response .NewErrorResponse (http .StatusInternalServerError , "could not fetch all charts" ).Write (w )
@@ -178,7 +186,7 @@ func listCharts(w http.ResponseWriter, req *http.Request) {
178
186
// listRepoCharts returns a list of charts in the given repo
179
187
func listRepoCharts (w http.ResponseWriter , req * http.Request , params Params ) {
180
188
pageNumber , pageSize := getPageNumberAndSize (req )
181
- cl , meta , err := getPaginatedChartList (params ["repo" ], pageNumber , pageSize )
189
+ cl , meta , err := getPaginatedChartList (params ["repo" ], pageNumber , pageSize , showDuplicates ( req ) )
182
190
if err != nil {
183
191
log .WithError (err ).Error ("could not fetch charts" )
184
192
response .NewErrorResponse (http .StatusInternalServerError , "could not fetch all charts" ).Write (w )
@@ -317,7 +325,11 @@ func listChartsWithFilters(w http.ResponseWriter, req *http.Request, params Para
317
325
// continue to return empty list
318
326
}
319
327
320
- cl := newChartListResponse (uniqChartList (charts ))
328
+ chartResponse := charts
329
+ if ! showDuplicates (req ) {
330
+ chartResponse = uniqChartList (charts )
331
+ }
332
+ cl := newChartListResponse (chartResponse )
321
333
response .NewDataResponse (cl ).Write (w )
322
334
}
323
335
@@ -355,7 +367,11 @@ func searchCharts(w http.ResponseWriter, req *http.Request, params Params) {
355
367
// continue to return empty list
356
368
}
357
369
358
- cl := newChartListResponse (uniqChartList (charts ))
370
+ chartResponse := charts
371
+ if ! showDuplicates (req ) {
372
+ chartResponse = uniqChartList (charts )
373
+ }
374
+ cl := newChartListResponse (chartResponse )
359
375
response .NewDataResponse (cl ).Write (w )
360
376
}
361
377
0 commit comments