-
Notifications
You must be signed in to change notification settings - Fork 508
/
blueprint_helper.go
309 lines (287 loc) · 9.44 KB
/
blueprint_helper.go
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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 services
import (
"fmt"
"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models"
"github.com/apache/incubator-devlake/core/models/common"
"github.com/apache/incubator-devlake/impls/logruslog"
)
type BlueprintManager struct {
db dal.Dal
}
type GetBlueprintQuery struct {
Enable *bool
IsManual *bool
Label string
SkipRecords int
PageSize int
Mode string
Type string
}
type BlueprintProjectPairs struct {
Projects []string `json:"projects"`
Blueprints []string `json:"blueprints"`
}
func NewBlueprintManager(db dal.Dal) *BlueprintManager {
return &BlueprintManager{
db: db,
}
}
func NewBlueprintProjectPairs(bps []*models.Blueprint) *BlueprintProjectPairs {
pairs := &BlueprintProjectPairs{}
for _, bp := range bps {
pairs.Blueprints = append(pairs.Blueprints, bp.Name)
pairs.Projects = append(pairs.Projects, bp.ProjectName)
}
return pairs
}
// SaveDbBlueprint accepts a Blueprint instance and upsert it to database
func (b *BlueprintManager) SaveDbBlueprint(blueprint *models.Blueprint) errors.Error {
var err error
if blueprint.ID != 0 {
err = b.db.Update(&blueprint)
} else {
err = b.db.Create(&blueprint)
}
if err != nil {
return errors.Default.Wrap(err, "error creating DB blueprint")
}
err = b.db.Delete(&models.BlueprintLabel{}, dal.Where(`blueprint_id = ?`, blueprint.ID))
if err != nil {
return errors.Default.Wrap(err, "error delete DB blueprint's old labelModels")
}
if len(blueprint.Labels) > 0 {
blueprintLabels := make([]*models.BlueprintLabel, 0)
for i := range blueprint.Labels {
blueprintLabels = append(blueprintLabels, &models.BlueprintLabel{
BlueprintId: blueprint.ID,
Name: blueprint.Labels[i],
})
}
err = b.db.Create(&blueprintLabels)
if err != nil {
return errors.Default.Wrap(err, "error creating DB blueprint's labelModels")
}
}
errors.Must(b.db.Delete(&models.BlueprintConnection{}, dal.Where("blueprint_id = ?", blueprint.ID)))
errors.Must(b.db.Delete(&models.BlueprintScope{}, dal.Where("blueprint_id = ?", blueprint.ID)))
for _, conn := range blueprint.Connections {
conn.BlueprintId = blueprint.ID
// validate
errors.Must(b.db.Create(conn))
for _, scope := range conn.Scopes {
scope.BlueprintId = blueprint.ID
scope.PluginName = conn.PluginName
scope.ConnectionId = conn.ConnectionId
// validate
errors.Must(b.db.Create(scope))
}
}
return nil
}
const (
BP_TYPE_ALL = "ALL"
BP_TYPE_MANUAL = "MANUAL"
BP_TYPE_DAILY = "DAILY"
BP_TYPE_WEEKLY = "WEEKLY"
BP_TYPE_MONTHLY = "MONTHLY"
BP_TYPE_CUSTOM = "CUSTOM"
BP_CRON_DAILY = "0 0 * * *"
BP_CRON_WEEKLY = "0 0 * * 1"
BP_CRON_MONTHLY = "0 0 1 * *"
)
// GetDbBlueprints returns a paginated list of Blueprints based on `query`
func (b *BlueprintManager) GetDbBlueprints(query *GetBlueprintQuery) ([]*models.Blueprint, int64, errors.Error) {
// process query parameters
clauses := []dal.Clause{dal.From(&models.Blueprint{})}
// clauses := []dal.Clause{dal.From("_devlake_blueprints")}
if query.Enable != nil {
clauses = append(clauses, dal.Where("enable = ?", *query.Enable))
}
if query.IsManual != nil {
clauses = append(clauses, dal.Where("is_manual = ?", *query.IsManual))
} else {
switch query.Type {
case "", BP_TYPE_ALL:
// do nothing
case BP_TYPE_MANUAL:
clauses = append(clauses, dal.Where("is_manual = ?", true))
case BP_TYPE_DAILY:
clauses = append(clauses, dal.Where("is_manual = ? AND cron_config = ?", false, BP_CRON_DAILY))
case BP_TYPE_WEEKLY:
clauses = append(clauses, dal.Where("is_manual = ? AND cron_config = ?", false, BP_CRON_WEEKLY))
case BP_TYPE_MONTHLY:
clauses = append(clauses, dal.Where("is_manual = ? AND cron_config = ?", false, BP_CRON_MONTHLY))
case BP_TYPE_CUSTOM:
clauses = append(clauses, dal.Where("is_manual = ? AND cron_config NOT IN ?", false, []string{BP_CRON_DAILY, BP_CRON_WEEKLY, BP_CRON_MONTHLY}))
default:
return nil, 0, errors.BadInput.New(fmt.Sprintf("invalid type %s", query.Type))
}
}
if query.Label != "" {
clauses = append(clauses,
dal.Join("left join _devlake_blueprint_labels bl ON bl.blueprint_id = _devlake_blueprints.id"),
dal.Where("bl.name = ?", query.Label),
)
}
if query.Mode != "" {
clauses = append(clauses, dal.Where("mode = ?", query.Mode))
}
// count total records
// var count int64
count := errors.Must1(b.db.Count(clauses...))
clauses = append(clauses, dal.Orderby("id DESC"))
// load paginated blueprints from database
if query.SkipRecords != 0 {
clauses = append(clauses, dal.Offset(query.SkipRecords))
}
if query.PageSize != 0 {
clauses = append(clauses, dal.Limit(query.PageSize))
}
dbBlueprints := make([]*models.Blueprint, 0)
errors.Must(b.db.All(&dbBlueprints, clauses...))
// load labels for blueprints
for _, dbBlueprint := range dbBlueprints {
err := b.fillBlueprintDetail(dbBlueprint)
if err != nil {
return nil, 0, err
}
}
return dbBlueprints, count, nil
}
// GetDbBlueprint returns the detail of a given Blueprint ID
func (b *BlueprintManager) GetDbBlueprint(blueprintId uint64) (*models.Blueprint, errors.Error) {
blueprint := &models.Blueprint{}
err := b.db.First(blueprint, dal.Where("id = ?", blueprintId))
if err != nil {
if b.db.IsErrorNotFound(err) {
return nil, errors.NotFound.Wrap(err, "could not find blueprint in DB")
}
return nil, errors.Default.Wrap(err, "error getting blueprint from DB")
}
err = b.fillBlueprintDetail(blueprint)
if err != nil {
return nil, err
}
return blueprint, nil
}
// GetBlueprintsByScopes returns all blueprints that have these scopeIds and this connection Id
func (b *BlueprintManager) GetBlueprintsByScopeId(connectionId uint64, pluginName string, scopeId string) []*models.Blueprint {
blueprints := make([]*models.Blueprint, 0)
errors.Must(b.db.All(
&blueprints,
dal.From("_devlake_blueprints bp"),
dal.Join("JOIN _devlake_blueprint_scopes sc ON sc.blueprint_id = bp.id"),
dal.Where(
"mode = ? AND sc.connection_id = ? AND sc.plugin_name = ? AND sc.scope_id = ?",
"NORMAL",
connectionId,
pluginName,
scopeId,
),
))
return blueprints
}
// GetBlueprintsByScopes returns all blueprints that have these scopeIds and this connection Id
func (b *BlueprintManager) GetBlueprintsByConnection(plugin string, connectionId uint64) []*models.Blueprint {
blueprints := make([]*models.Blueprint, 0)
errors.Must(b.db.All(
&blueprints,
dal.From("_devlake_blueprints bp"),
dal.Join("JOIN _devlake_blueprint_connections conn ON conn.blueprint_id = bp.id"),
dal.Where(
"mode = ? AND conn.connection_id = ? AND conn.plugin_name = ?",
"NORMAL",
connectionId,
plugin,
),
))
return blueprints
}
// GetDbBlueprintByProjectName returns the detail of a given projectName
func (b *BlueprintManager) GetDbBlueprintByProjectName(projectName string) (*models.Blueprint, errors.Error) {
dbBlueprint := &models.Blueprint{}
err := b.db.First(dbBlueprint, dal.Where("project_name = ?", projectName))
if err != nil {
if b.db.IsErrorNotFound(err) {
return nil, errors.NotFound.Wrap(err, fmt.Sprintf("could not find blueprint in DB by projectName %s", projectName))
}
return nil, errors.Default.Wrap(err, fmt.Sprintf("error getting blueprint from DB by projectName %s", projectName))
}
err = b.fillBlueprintDetail(dbBlueprint)
if err != nil {
return nil, err
}
return dbBlueprint, nil
}
// DeleteBlueprint deletes a blueprint by its id
func (b *BlueprintManager) DeleteBlueprint(id uint64) errors.Error {
var err errors.Error
tx := b.db.Begin()
defer func() {
if r := recover(); r != nil || err != nil {
err = tx.Rollback()
if err != nil {
logruslog.Global.Error(err, "DeleteBlueprint: failed to rollback")
}
}
}()
err = tx.Delete(&models.BlueprintLabel{}, dal.Where("blueprint_id = ?", id))
if err != nil {
return err
}
err = tx.Delete(&models.Blueprint{
Model: common.Model{
ID: id,
},
})
if err != nil {
return err
}
errors.Must(tx.Delete(&models.BlueprintConnection{}, dal.Where("blueprint_id = ?", id)))
errors.Must(tx.Delete(&models.BlueprintScope{}, dal.Where("blueprint_id = ?", id)))
return tx.Commit()
}
func (b *BlueprintManager) fillBlueprintDetail(blueprint *models.Blueprint) errors.Error {
errors.Must(
b.db.Pluck(
"name",
&blueprint.Labels, dal.From(&models.BlueprintLabel{}), dal.Where("blueprint_id = ?", blueprint.ID),
),
)
errors.Must(
b.db.All(
&blueprint.Connections,
dal.Where("blueprint_id = ?", blueprint.ID),
),
)
for _, conn := range blueprint.Connections {
errors.Must(
b.db.All(
&conn.Scopes,
dal.Where(
"blueprint_id = ? AND plugin_name = ? AND connection_id = ?",
blueprint.ID,
conn.PluginName,
conn.ConnectionId,
),
),
)
}
return nil
}