-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathAppListingRepositoryQueryBuilder.go
More file actions
303 lines (274 loc) · 12.2 KB
/
AppListingRepositoryQueryBuilder.go
File metadata and controls
303 lines (274 loc) · 12.2 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
/*
* 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"
)
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"`
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
const (
Asc SortOrder = "ASC"
Desc SortOrder = "DESC"
)
const (
AppNameSortBy SortBy = "appNameSort"
LastDeployedSortBy = "lastDeployedSort"
)
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))
}
if len(appListingFilter.AppIds) > 0 {
whereCondition += " and a.id IN (?) "
queryParams = append(queryParams, pg.In(appListingFilter.AppIds))
}
return whereCondition, queryParams
}
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
}