-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathAppListingRepositoryQueryBuilder.go
More file actions
412 lines (374 loc) · 16.8 KB
/
AppListingRepositoryQueryBuilder.go
File metadata and controls
412 lines (374 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2020-2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package helper
import (
"fmt"
"github.com/devtron-labs/devtron/util"
"github.com/go-pg/pg"
"go.uber.org/zap"
"strings"
)
type AppType int
const (
CustomApp AppType = 0 // cicd app
ChartStoreApp AppType = 1 // helm app
Job AppType = 2 // jobs
// ExternalChartStoreApp app-type is not stored in db
ExternalChartStoreApp AppType = 3 // external helm app
)
type AppListingRepositoryQueryBuilder struct {
logger *zap.SugaredLogger
}
func NewAppListingRepositoryQueryBuilder(logger *zap.SugaredLogger) AppListingRepositoryQueryBuilder {
return AppListingRepositoryQueryBuilder{
logger: logger,
}
}
type AppListingFilter struct {
Environments []int `json:"environments"`
Statuses []string `json:"statutes"`
Teams []int `json:"teams"`
AppStatuses []string `json:"appStatuses"`
TagFilters []TagFilter `json:"tagFilters"`
AppNameSearch string `json:"appNameSearch"`
SortOrder SortOrder `json:"sortOrder"`
SortBy SortBy `json:"sortBy"`
Offset int `json:"offset"`
Size int `json:"size"`
DeploymentGroupId int `json:"deploymentGroupId"`
AppIds []int `json:"-"` // internal use only
}
type SortBy string
type SortOrder string
type TagFilterOperator string
// TagFilter holds one row of label filter sent by UI.
// key is always required.
// value is required for EQUALS/DOES_NOT_EQUAL/CONTAINS/DOES_NOT_CONTAIN.
// value must be absent for EXISTS/DOES_NOT_EXIST.
type TagFilter struct {
Key string `json:"key" validate:"required"`
Operator TagFilterOperator `json:"operator" validate:"required"`
Value *string `json:"value"`
}
const (
Asc SortOrder = "ASC"
Desc SortOrder = "DESC"
)
const (
TagFilterOperatorEquals TagFilterOperator = "EQUALS"
TagFilterOperatorDoesNotEqual TagFilterOperator = "DOES_NOT_EQUAL"
TagFilterOperatorContains TagFilterOperator = "CONTAINS"
TagFilterOperatorDoesNotContain TagFilterOperator = "DOES_NOT_CONTAIN"
TagFilterOperatorExists TagFilterOperator = "EXISTS"
TagFilterOperatorDoesNotExist TagFilterOperator = "DOES_NOT_EXIST"
)
const (
AppNameSortBy SortBy = "appNameSort"
LastDeployedSortBy = "lastDeployedSort"
)
var likePatternEscaper = strings.NewReplacer("\\", "\\\\", "%", "\\%", "_", "\\_")
func (operator TagFilterOperator) IsValid() bool {
switch operator {
case TagFilterOperatorEquals,
TagFilterOperatorDoesNotEqual,
TagFilterOperatorContains,
TagFilterOperatorDoesNotContain,
TagFilterOperatorExists,
TagFilterOperatorDoesNotExist:
return true
default:
return false
}
}
func (impl AppListingRepositoryQueryBuilder) BuildJobListingQuery(appIDs []int, statuses []string, environmentIds []int, sortOrder string) (string, []interface{}) {
var queryParams []interface{}
query := `select ci_pipeline.name as ci_pipeline_name,ci_pipeline.id as ci_pipeline_id,app.id as job_id,app.display_name
as job_name, app.app_name,app.description,app.created_by,app.team_id,cwr.started_on,cwr.status,cem.environment_id,cwr.environment_id as last_triggered_environment_id from app left join ci_pipeline on
app.id = ci_pipeline.app_id and ci_pipeline.active=true left join (select cw.ci_pipeline_id, cw.status, cw.started_on, cw.environment_id
from ci_workflow cw inner join (select ci_pipeline_id, MAX(started_on) max_started_on from ci_workflow group by ci_pipeline_id )
cws on cw.ci_pipeline_id = cws.ci_pipeline_id
and cw.started_on = cws.max_started_on order by cw.ci_pipeline_id) cwr on cwr.ci_pipeline_id = ci_pipeline.id
LEFT JOIN ci_env_mapping cem on cem.ci_pipeline_id = ci_pipeline.id
where app.active = true and app.app_type = 2 `
if len(appIDs) > 0 {
query += " and app.id IN (?) "
queryParams = append(queryParams, pg.In(appIDs))
}
if len(statuses) > 0 {
query += " and cwr.status IN (?) "
queryParams = append(queryParams, pg.In(statuses))
}
if len(environmentIds) > 0 {
query += " and cwr.environment_id IN (?) "
queryParams = append(queryParams, pg.In(environmentIds))
}
query += " order by app.display_name"
if sortOrder == "DESC" {
query += " DESC "
}
return query, queryParams
}
func (impl AppListingRepositoryQueryBuilder) OverviewCiPipelineQuery() string {
query := "select ci_pipeline.id as ci_pipeline_id,ci_pipeline.name " +
"as ci_pipeline_name,cwr.status,cwr.started_on,cem.environment_id,cwr.environment_id as last_triggered_environment_id from ci_pipeline" +
" left join (select cw.ci_pipeline_id,cw.status,cw.started_on,cw.environment_id from ci_workflow cw" +
" inner join (SELECT ci_pipeline_id, MAX(started_on) max_started_on FROM ci_workflow GROUP BY ci_pipeline_id)" +
" cws on cw.ci_pipeline_id = cws.ci_pipeline_id and cw.started_on = cws.max_started_on order by cw.ci_pipeline_id)" +
" cwr on cwr.ci_pipeline_id = ci_pipeline.id" +
" LEFT JOIN ci_env_mapping cem on cem.ci_pipeline_id = ci_pipeline.id " +
"where ci_pipeline.active = true and ci_pipeline.app_id = ? ;"
return query
}
// use this query with atleast 1 cipipeline id
func (impl AppListingRepositoryQueryBuilder) JobsLastSucceededOnTimeQuery(ciPipelineIDs []int) (string, []interface{}) {
// use this query with atleast 1 cipipeline id
query := "select cw.ci_pipeline_id,cw.finished_on " +
"as last_succeeded_on from ci_workflow cw inner join " +
"(SELECT ci_pipeline_id, MAX(finished_on) finished_on " +
"FROM ci_workflow WHERE ci_workflow.status = 'Succeeded'" +
"GROUP BY ci_pipeline_id) cws on cw.ci_pipeline_id = cws.ci_pipeline_id and cw.finished_on = cws.finished_on " +
"where cw.ci_pipeline_id IN (?); "
return query, []interface{}{pg.In(ciPipelineIDs)}
}
func getAppListingCommonQueryString() string {
return " FROM pipeline p" +
" INNER JOIN environment env ON env.id=p.environment_id" +
" INNER JOIN cluster cluster ON cluster.id=env.cluster_id" +
" RIGHT JOIN app a ON a.id=p.app_id and p.deleted=false" +
" RIGHT JOIN team t ON t.id=a.team_id " +
" LEFT JOIN app_status aps on aps.app_id = a.id and p.environment_id = aps.env_id "
}
func (impl AppListingRepositoryQueryBuilder) GetQueryForAppEnvContainers(appListingFilter AppListingFilter) (string, []interface{}) {
query := "SELECT p.environment_id , a.id AS app_id, a.app_name,p.id as pipeline_id, a.team_id ,aps.status as app_status "
queryTemp, queryParams := impl.TestForCommonAppFilter(appListingFilter)
query += queryTemp
return query, queryParams
}
func (impl AppListingRepositoryQueryBuilder) CommonJoinSubQuery(appListingFilter AppListingFilter) (string, []interface{}) {
var queryParams []interface{}
query := ` LEFT JOIN pipeline p ON a.id=p.app_id and p.deleted=?
LEFT JOIN deployment_config dc ON ( p.app_id=dc.app_id and p.environment_id=dc.environment_id and dc.active=? )
LEFT JOIN app_status aps on aps.app_id = a.id and p.environment_id = aps.env_id `
queryParams = append(queryParams, false, true)
if appListingFilter.DeploymentGroupId != 0 {
query = query + " INNER JOIN deployment_group_app dga ON a.id = dga.app_id "
}
whereCondition, whereConditionParams := impl.buildAppListingWhereCondition(appListingFilter)
query = query + whereCondition
queryParams = append(queryParams, whereConditionParams...)
return query, queryParams
}
func (impl AppListingRepositoryQueryBuilder) TestForCommonAppFilter(appListingFilter AppListingFilter) (string, []interface{}) {
queryTemp, queryParams := impl.CommonJoinSubQuery(appListingFilter)
query := " FROM app a " + queryTemp
return query, queryParams
}
func (impl AppListingRepositoryQueryBuilder) BuildAppListingQueryLastDeploymentTimeV2(pipelineIDs []int) (string, []interface{}) {
whereCondition := ""
queryParams := []interface{}{}
if len(pipelineIDs) > 0 {
whereCondition += " Where pco.pipeline_id IN (?) "
queryParams = append(queryParams, pg.In(pipelineIDs))
}
query := "select pco.pipeline_id , MAX(pco.created_on) as last_deployed_time" +
" from pipeline_config_override pco" + whereCondition +
" GROUP BY pco.pipeline_id;"
return query, queryParams
}
func (impl AppListingRepositoryQueryBuilder) GetAppIdsQueryWithPaginationForLastDeployedSearch(appListingFilter AppListingFilter) (string, []interface{}) {
join, queryParams := impl.CommonJoinSubQuery(appListingFilter)
countQuery := " (SELECT count(distinct(a.id)) as count FROM app a " + join + ") AS total_count "
// appending query params for count query as well
queryParams = append(queryParams, queryParams...)
query := "SELECT a.id as app_id,MAX(pco.id) as last_deployed_time, " + countQuery +
` FROM pipeline p
INNER JOIN pipeline_config_override pco ON pco.pipeline_id = p.id and p.deleted=false
RIGHT JOIN ( SELECT DISTINCT(a.id) as id FROM app a ` + join +
` ) da on p.app_id = da.id and p.deleted=false
INNER JOIN app a ON da.id = a.id `
if appListingFilter.SortOrder == Desc {
query += ` GROUP BY a.id,total_count ORDER BY last_deployed_time DESC NULLS `
} else {
query += ` GROUP BY a.id,total_count ORDER BY last_deployed_time ASC NULLS `
}
if appListingFilter.SortOrder == "DESC" {
query += " LAST "
} else {
query += " FIRST "
}
query += " LIMIT ? OFFSET ? "
queryParams = append(queryParams, appListingFilter.Size, appListingFilter.Offset)
return query, queryParams
}
func (impl AppListingRepositoryQueryBuilder) GetAppIdsQueryWithPaginationForAppNameSearch(appListingFilter AppListingFilter) (string, []interface{}) {
orderByClause := impl.buildAppListingSortBy(appListingFilter)
join, queryParams := impl.CommonJoinSubQuery(appListingFilter)
countQuery := "( SELECT count(distinct(a.id)) as count FROM app a" + join + " ) as total_count"
query := "SELECT DISTINCT(a.id) as app_id, a.app_name, " + countQuery +
" FROM app a " + join
if appListingFilter.SortBy == "appNameSort" {
query += orderByClause
}
query += " LIMIT ? OFFSET ? "
//adding queryParams two times because join query is used in countQuery and mainQuery two times
queryParams = append(queryParams, queryParams...)
queryParams = append(queryParams, appListingFilter.Size, appListingFilter.Offset)
return query, queryParams
}
func (impl AppListingRepositoryQueryBuilder) buildAppListingSortBy(appListingFilter AppListingFilter) string {
orderByCondition := " ORDER BY a.app_name "
if appListingFilter.SortOrder != "ASC" {
orderByCondition += " DESC "
}
return orderByCondition
}
func (impl AppListingRepositoryQueryBuilder) buildAppListingWhereCondition(appListingFilter AppListingFilter) (string, []interface{}) {
var queryParams []interface{}
whereCondition := " WHERE a.active = ? and a.app_type = ? "
queryParams = append(queryParams, true, CustomApp)
if len(appListingFilter.Environments) > 0 {
whereCondition += " and p.environment_id IN (?) "
queryParams = append(queryParams, pg.In(appListingFilter.Environments))
}
if len(appListingFilter.Teams) > 0 {
whereCondition += " and a.team_id IN (?) "
queryParams = append(queryParams, pg.In(appListingFilter.Teams))
}
if appListingFilter.AppNameSearch != "" {
whereCondition += " and a.app_name like ? "
queryParams = append(queryParams, util.GetLIKEClauseQueryParam(appListingFilter.AppNameSearch))
}
if appListingFilter.DeploymentGroupId > 0 {
whereCondition += " and dga.deployment_group_id = ? "
queryParams = append(queryParams, appListingFilter.DeploymentGroupId)
}
// add app-status filter here
var appStatusExcludingNotDeployed []string
var isNotDeployedFilterApplied bool
if len(appListingFilter.AppStatuses) > 0 {
for _, status := range appListingFilter.AppStatuses {
if status == "NOT DEPLOYED" {
isNotDeployedFilterApplied = true
} else {
appStatusExcludingNotDeployed = append(appStatusExcludingNotDeployed, status)
}
}
}
if isNotDeployedFilterApplied {
deploymentAppType := "manifest_download"
whereCondition += " and (p.deployment_app_created=? and (p.deployment_app_type <> ? or dc.deployment_app_type <> ? ) or a.id NOT IN (SELECT app_id from pipeline) "
queryParams = append(queryParams, false, deploymentAppType, deploymentAppType)
if len(appStatusExcludingNotDeployed) > 0 {
whereCondition += " or aps.status IN (?) "
queryParams = append(queryParams, pg.In(appStatusExcludingNotDeployed))
}
whereCondition += " ) "
} else if len(appStatusExcludingNotDeployed) > 0 {
whereCondition += " and aps.status IN (?) "
queryParams = append(queryParams, pg.In(appStatusExcludingNotDeployed))
}
// Tag filters are AND-combined for now as requested by product.
// Each row translates to a correlated EXISTS/NOT EXISTS on app_label.
tagWhereCondition, tagQueryParams := impl.buildTagFiltersWhereConditionAND(appListingFilter.TagFilters)
whereCondition += tagWhereCondition
queryParams = append(queryParams, tagQueryParams...)
if len(appListingFilter.AppIds) > 0 {
whereCondition += " and a.id IN (?) "
queryParams = append(queryParams, pg.In(appListingFilter.AppIds))
}
return whereCondition, queryParams
}
func (impl AppListingRepositoryQueryBuilder) buildTagFiltersWhereConditionAND(tagFilters []TagFilter) (string, []interface{}) {
if len(tagFilters) == 0 {
return "", nil
}
var queryBuilder strings.Builder
queryParams := make([]interface{}, 0, len(tagFilters)*2)
for _, tagFilter := range tagFilters {
predicate, predicateParams := impl.buildTagFilterPredicate(tagFilter)
queryBuilder.WriteString(" and ")
queryBuilder.WriteString(predicate)
queryParams = append(queryParams, predicateParams...)
}
return queryBuilder.String(), queryParams
}
// buildTagFilterPredicate converts one UI tag filter row into a SQL predicate.
// Operator behavior (all case-sensitive):
// - EQUALS: key exists with exact value match.
// - DOES_NOT_EQUAL: key exists with at least one value different from target.
// - CONTAINS: key exists with at least one value containing target substring.
// - DOES_NOT_CONTAIN: key exists with at least one value not containing target substring.
// - EXISTS: key exists.
// - DOES_NOT_EXIST: key does not exist.
func (impl AppListingRepositoryQueryBuilder) buildTagFilterPredicate(tagFilter TagFilter) (string, []interface{}) {
value := ""
if tagFilter.Value != nil {
value = *tagFilter.Value
}
switch tagFilter.Operator {
case TagFilterOperatorEquals:
return "EXISTS (SELECT 1 FROM app_label al WHERE al.app_id = a.id and al.key = ? and al.value = ?)",
[]interface{}{tagFilter.Key, value}
case TagFilterOperatorDoesNotEqual:
// Best-practice semantics for multi-value keys:
// include app when key exists and at least one value is different from target.
return "EXISTS (SELECT 1 FROM app_label al WHERE al.app_id = a.id and al.key = ? and al.value <> ?)",
[]interface{}{tagFilter.Key, value}
case TagFilterOperatorContains:
return "EXISTS (SELECT 1 FROM app_label al WHERE al.app_id = a.id and al.key = ? and al.value LIKE ? ESCAPE '\\')",
[]interface{}{tagFilter.Key, buildContainsPattern(value)}
case TagFilterOperatorDoesNotContain:
// Best-practice semantics for multi-value keys:
// include app when key exists and at least one value does not contain target.
return "EXISTS (SELECT 1 FROM app_label al WHERE al.app_id = a.id and al.key = ? and al.value NOT LIKE ? ESCAPE '\\')",
[]interface{}{tagFilter.Key, buildContainsPattern(value)}
case TagFilterOperatorExists:
return "EXISTS (SELECT 1 FROM app_label al WHERE al.app_id = a.id and al.key = ?)",
[]interface{}{tagFilter.Key}
case TagFilterOperatorDoesNotExist:
return "NOT EXISTS (SELECT 1 FROM app_label al WHERE al.app_id = a.id and al.key = ?)",
[]interface{}{tagFilter.Key}
default:
// Invalid operator should never reach here due request validation.
// Returning false condition keeps query safe if validation is bypassed.
return "1 = 0", nil
}
}
func buildContainsPattern(value string) string {
// Escape SQL LIKE wildcard chars so "contains" behaves like plain substring search.
escaped := likePatternEscaper.Replace(value)
return "%" + escaped + "%"
}
func GetCommaSepratedString[T int | string](request []T) string {
respString := ""
for i, item := range request {
respString += fmt.Sprintf("%v", item)
if i != len(request)-1 {
respString += ","
}
}
return respString
}
func GetCommaSepratedStringWithComma[T int | string](appIds []T) string {
appIdsString := ""
for i, appId := range appIds {
appIdsString += fmt.Sprintf("'%v'", appId)
if i != len(appIds)-1 {
appIdsString += ","
}
}
return appIdsString
}