/
deploy.go
433 lines (377 loc) · 11.8 KB
/
deploy.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Copyright 2022 The KodeRover Authors.
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 mongodb
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/koderover/zadig/v2/pkg/microservice/aslan/config"
models "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/stat/repository/models"
mongotool "github.com/koderover/zadig/v2/pkg/tool/mongo"
)
type DeployTotalPipeResp struct {
ID DeployTotalItem `bson:"_id" json:"_id"`
TotalSuccess int `bson:"total_deploy_success" json:"total_deploy_success"`
TotalFailure int `bson:"total_deploy_failure" json:"total_deploy_failure"`
}
type DeployStat struct {
TotalSuccess int `bson:"total_deploy_success" json:"total_deploy_success"`
TotalFailure int `bson:"total_deploy_failure" json:"total_deploy_failure"`
}
type DeployTotalItem struct {
ProductName string `bson:"product_name" json:"product_name"`
TotalSuccess int `bson:"total_deploy_success" json:"total_deploy_success"`
TotalFailure int `bson:"total_deploy_failure" json:"total_deploy_failure"`
}
type DeployDailyPipeResp struct {
ID DeployDailyItem `bson:"_id" json:"_id"`
TotalSuccess int `bson:"total_deploy_success" json:"total_deploy_success"`
TotalFailure int `bson:"total_deploy_failure" json:"total_deploy_failure"`
}
type DeployDailyItem struct {
Date string `bson:"date" json:"date"`
TotalSuccess int `bson:"total_deploy_success" json:"total_deploy_success"`
TotalFailure int `bson:"total_deploy_failure" json:"total_deploy_failure"`
}
type DeployPipeResp struct {
ID DeployPipeInfo `bson:"_id" json:"_id"`
MaxDeployServiceNum int `bson:"max_deploy_service_num" json:"maxDeployServiceNum"`
}
type DeployPipeInfo struct {
MaxDeployServiceName string `bson:"max_deploy_service_name" json:"maxDeployServiceName"`
}
type DeployFailurePipeResp struct {
ID DeployFailurePipeInfo `bson:"_id" json:"_id"`
MaxDeployFailureServiceNum int `bson:"max_deploy_failure_service_num" json:"maxDeployFailureServiceNum"`
}
type DeployFailurePipeInfo struct {
ProductName string `bson:"product_name" json:"productName"`
MaxDeployFailureServiceName string `bson:"max_deploy_failure_service_name" json:"maxDeployFailureServiceName"`
}
type DeployStatColl struct {
*mongo.Collection
coll string
}
func NewDeployStatColl() *DeployStatColl {
name := models.DeployStat{}.TableName()
return &DeployStatColl{Collection: mongotool.Database(config.MongoDatabase()).Collection(name), coll: name}
}
func (c *DeployStatColl) GetCollectionName() string {
return c.coll
}
func (c *DeployStatColl) EnsureIndex(ctx context.Context) error {
mod := mongo.IndexModel{
Keys: bson.D{
bson.E{Key: "product_name", Value: 1},
bson.E{Key: "date", Value: 1},
},
Options: options.Index().SetUnique(true),
}
_, err := c.Indexes().CreateOne(ctx, mod)
return err
}
func (c *DeployStatColl) Create(args *models.DeployStat) error {
if args == nil {
return errors.New("nil deployStat args")
}
_, err := c.InsertOne(context.TODO(), args)
return err
}
type DeployStatGetOption struct {
MaxDeployServiceNum int
ServiceName string
}
func (c *DeployStatColl) Get(args *DeployStatGetOption) (*models.DeployStat, error) {
ret := new(models.DeployStat)
query := bson.M{"max_deploy_service_num": args.MaxDeployServiceNum, "max_deploy_service_name": args.ServiceName}
err := c.FindOne(context.TODO(), query).Decode(ret)
if err != nil {
return nil, err
}
return ret, nil
}
func (c *DeployStatColl) Update(args *models.DeployStat) error {
if args == nil {
return errors.New("nil deployStat args")
}
query := bson.M{
"date": args.Date,
"product_name": args.ProductName,
}
update := bson.M{"$set": args}
_, err := c.UpdateOne(context.TODO(), query, update)
return err
}
func (c *DeployStatColl) FindCount() (int, error) {
count, err := c.CountDocuments(context.TODO(), bson.M{})
return int(count), err
}
func (c *DeployStatColl) GetDeployTotalAndSuccess() ([]*DeployTotalItem, error) {
var result []*DeployTotalPipeResp
var resp []*DeployTotalItem
pipeline := []bson.M{{
"$group": bson.M{
"_id": bson.M{
"product_name": "$product_name",
},
"total_deploy_success": bson.M{
"$sum": "$total_deploy_success",
},
"total_deploy_failure": bson.M{
"$sum": "$total_deploy_failure",
},
},
}}
cursor, err := c.Aggregate(context.TODO(), pipeline)
if err != nil {
return nil, err
}
if err := cursor.All(context.TODO(), &result); err != nil {
return nil, err
}
for _, res := range result {
deployItem := &DeployTotalItem{
ProductName: res.ID.ProductName,
TotalSuccess: res.TotalSuccess,
TotalFailure: res.TotalFailure,
}
resp = append(resp, deployItem)
}
return resp, nil
}
func (c *DeployStatColl) GetDeployTotalAndSuccessByTime(startTime, endTime int64) (int64, int64, error) {
var result []*DeployStat
pipeline := []bson.M{
{
"$match": bson.M{
"create_time": bson.M{
"$gte": startTime,
"$lte": endTime,
},
},
},
{
"$group": bson.M{
"_id": "null",
"total_deploy_success": bson.M{
"$sum": "$total_deploy_success",
},
"total_deploy_failure": bson.M{
"$sum": "$total_deploy_failure",
},
},
},
}
cursor, err := c.Aggregate(context.TODO(), pipeline)
if err != nil {
return 0, 0, err
}
if err := cursor.All(context.TODO(), &result); err != nil {
return 0, 0, err
}
var totalSuccess, totalFailure int64
for _, res := range result {
totalSuccess += int64(res.TotalSuccess)
totalFailure += int64(res.TotalFailure)
}
return totalSuccess, totalFailure, nil
}
func (c *DeployStatColl) GetDeployStats(args *models.DeployStatOption) ([]*DeployTotalItem, error) {
var result []*DeployTotalPipeResp
var resp []*DeployTotalItem
filter := bson.M{}
if args.StartDate > 0 {
filter["create_time"] = bson.M{"$gte": args.StartDate, "$lte": args.EndDate}
}
if len(args.ProductNames) > 0 {
filter["product_name"] = bson.M{"$in": args.ProductNames}
}
pipeline := []bson.M{
{
"$match": filter,
},
{
"$group": bson.M{
"_id": bson.M{
"product_name": "$product_name",
},
"total_deploy_success": bson.M{
"$sum": "$total_deploy_success",
},
"total_deploy_failure": bson.M{
"$sum": "$total_deploy_failure",
},
},
},
}
cursor, err := c.Aggregate(context.TODO(), pipeline)
if err != nil {
return nil, err
}
if err := cursor.All(context.TODO(), &result); err != nil {
return nil, err
}
for _, res := range result {
deployItem := &DeployTotalItem{
ProductName: res.ID.ProductName,
TotalSuccess: res.TotalSuccess,
TotalFailure: res.TotalFailure,
}
resp = append(resp, deployItem)
}
return resp, nil
}
func (c *DeployStatColl) ListDeployStat(option *models.DeployStatOption) ([]*models.DeployStat, error) {
ret := make([]*models.DeployStat, 0)
query := bson.M{}
if option.Limit > 0 {
var deployStatQuery []bson.M
if len(option.ProductNames) > 0 {
query["product_name"] = bson.M{"$in": option.ProductNames}
}
if option.StartDate > 0 {
query["create_time"] = bson.M{
"$gte": option.StartDate,
"$lte": option.EndDate,
}
}
deployStatQuery = append(deployStatQuery, bson.M{"$match": query})
if option.IsMaxDeploy {
var results []DeployPipeResp
deployStatQuery = append(deployStatQuery,
bson.M{"$group": bson.M{
"_id": bson.M{"max_deploy_service_name": "$max_deploy_service_name"},
"max_deploy_service_num": bson.M{"$max": "$max_deploy_service_num"}},
})
deployStatQuery = append(deployStatQuery, bson.M{"$sort": bson.M{"max_deploy_service_num": -1}})
deployStatQuery = append(deployStatQuery, bson.M{"$limit": 5})
deployStatQuery = append(deployStatQuery, bson.M{"$skip": 0})
cursor, err := c.Aggregate(context.TODO(), deployStatQuery)
if err != nil {
return nil, err
}
if err := cursor.All(context.TODO(), &results); err != nil {
return nil, err
}
for _, result := range results {
deployItem := &models.DeployStat{
MaxDeployServiceName: result.ID.MaxDeployServiceName,
MaxDeployServiceNum: result.MaxDeployServiceNum,
}
ret = append(ret, deployItem)
}
} else {
var results []DeployFailurePipeResp
deployStatQuery = append(deployStatQuery,
bson.M{"$group": bson.M{
"_id": bson.M{
"max_deploy_failure_service_name": "$max_deploy_failure_service_name",
"product_name": "$product_name",
},
"max_deploy_failure_service_num": bson.M{
"$max": "$max_deploy_failure_service_num",
},
}})
deployStatQuery = append(deployStatQuery, bson.M{"$sort": bson.M{"max_deploy_failure_service_num": -1}})
deployStatQuery = append(deployStatQuery, bson.M{"$limit": 5})
deployStatQuery = append(deployStatQuery, bson.M{"$skip": 0})
cursor, err := c.Aggregate(context.TODO(), deployStatQuery)
if err != nil {
return nil, err
}
if err := cursor.All(context.TODO(), &results); err != nil {
return nil, err
}
for _, result := range results {
deployItem := &models.DeployStat{
ProductName: result.ID.ProductName,
MaxDeployFailureServiceName: result.ID.MaxDeployFailureServiceName,
MaxDeployFailureServiceNum: result.MaxDeployFailureServiceNum,
}
ret = append(ret, deployItem)
}
}
return ret, nil
}
if len(option.ProductNames) > 0 {
query["product_name"] = bson.M{"$in": option.ProductNames}
}
if option.StartDate > 0 {
query["create_time"] = bson.M{"$gte": option.StartDate, "$lte": option.EndDate}
}
opt := &options.FindOptions{}
if option.IsAsc {
opt.SetSort(bson.D{{"create_time", 1}})
} else {
opt.SetSort(bson.D{{"create_time", -1}})
}
cursor, err := c.Collection.Find(context.TODO(), query, opt)
if err != nil {
return nil, err
}
err = cursor.All(context.TODO(), &ret)
if err != nil {
return nil, err
}
return ret, nil
}
func (c *DeployStatColl) GetDeployDailyTotal(args *models.DeployStatOption) ([]*DeployDailyItem, error) {
var result []*DeployDailyPipeResp
var resp []*DeployDailyItem
pipeline := []bson.M{{
"$sort": bson.M{
"create_time": 1,
},
}}
if args.StartDate > 0 || args.EndDate > 0 {
timeRange := bson.M{}
if args.StartDate > 0 {
timeRange["$gte"] = args.StartDate
}
if args.EndDate > 0 {
timeRange["$lte"] = args.EndDate
}
pipeline = append(pipeline, bson.M{"$match": bson.M{"create_time": timeRange}})
}
pipeline = append(pipeline, bson.M{
"$group": bson.M{
"_id": bson.M{
"date": "$date",
},
"total_deploy_success": bson.M{
"$sum": "$total_deploy_success",
},
"total_deploy_failure": bson.M{
"$sum": "$total_deploy_failure",
},
},
})
cursor, err := c.Aggregate(context.TODO(), pipeline)
if err != nil {
return nil, err
}
if err := cursor.All(context.TODO(), &result); err != nil {
return nil, err
}
for _, res := range result {
deployDailyItem := &DeployDailyItem{
Date: res.ID.Date,
TotalSuccess: res.TotalSuccess,
TotalFailure: res.TotalFailure,
}
resp = append(resp, deployDailyItem)
}
return resp, nil
}