/
sonar_metric_rules.go
146 lines (121 loc) · 4.01 KB
/
sonar_metric_rules.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
// Copyright (c) 2021 Terminus, 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 dao
import (
"time"
"github.com/jinzhu/gorm"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/dop/dbclient"
)
type QASonarMetricRules struct {
ID int64 `gorm:"primary_key" json:"id"`
CreatedAt time.Time `gorm:"created_at" json:"createdAt"`
UpdatedAt time.Time `gorm:"updated_at" json:"updatedAt"`
Description string `gorm:"description" json:"description"`
ScopeType string `gorm:"scope_type" json:"scopeType"`
ScopeID string `gorm:"scope_id" json:"scopeId"`
MetricKeyID int64 `gorm:"metric_key_id" json:"metricKeyId"`
MetricValue string `gorm:"metric_value" json:"metricValue"`
}
func (rule *QASonarMetricRules) ToApi() *apistructs.SonarMetricRuleDto {
dto := &apistructs.SonarMetricRuleDto{
ID: rule.ID,
CreatedAt: rule.CreatedAt,
UpdatedAt: rule.UpdatedAt,
Description: rule.Description,
ScopeType: rule.ScopeType,
ScopeID: rule.ScopeID,
MetricValue: rule.MetricValue,
MetricKeyID: rule.MetricKeyID,
}
keys := apistructs.SonarMetricKeys[dto.MetricKeyID]
if keys == nil {
return dto
}
dto.MetricKey = keys.MetricKey
dto.Operational = apistructs.GetOperationalValue(keys.Operational)
dto.MetricKeyDesc = keys.MetricKeyDesc
return dto
}
// TableName QASonar对应的数据库表qa_sonar
func (QASonarMetricRules) TableName() string {
return "qa_sonar_metric_rules"
}
// PagingTestPlan List test plan
func (client *DBClient) PagingSonarMetricRules(req apistructs.SonarMetricRulesPagingRequest) (*dbclient.Paging, error) {
var (
total int64
sonarMetricRules []QASonarMetricRules
)
sql := client.Where("scope_type = ? and scope_id = ?", req.ScopeType, req.ScopeID)
if err := sql.Offset((req.PageNo - 1) * req.PageSize).Limit(req.PageSize).
Order("updated_at desc").Find(&sonarMetricRules).
Offset(0).Limit(-1).Count(&total).Error; err != nil {
return nil, err
}
return &dbclient.Paging{
Total: total,
List: sonarMetricRules,
}, nil
}
func (client *DBClient) UpdateSonarMetricRules(updateObj *QASonarMetricRules) (err error) {
if err = client.Save(updateObj).Error; err != nil {
return err
}
return nil
}
func (client *DBClient) GetSonarMetricRules(ID int64) (*QASonarMetricRules, error) {
var result QASonarMetricRules
if err := client.First(&result, "id = ?", ID).Error; err != nil {
return nil, err
}
return &result, nil
}
func (client *DBClient) BatchInsertSonarMetricRules(rules []*QASonarMetricRules) (err error) {
sql := client.DB
for _, v := range rules {
sql = sql.Save(v)
}
if sql.Error != nil {
return err
}
return nil
}
func (client *DBClient) BatchDeleteSonarMetricRules(rules []QASonarMetricRules) (err error) {
if rules == nil || len(rules) <= 0 {
return nil
}
var IDs []int64
for _, v := range rules {
IDs = append(IDs, v.ID)
}
if err := client.Delete(&QASonarMetricRules{}, "scope_type = ? and scope_id = ? and id in (?) ", rules[0].ScopeType, rules[0].ScopeID, IDs).Error; err != nil {
return err
}
return nil
}
func (client *DBClient) ListSonarMetricRules(query *QASonarMetricRules, otherQueryFuncList ...func(sql *gorm.DB) *gorm.DB) (dbRules []QASonarMetricRules, err error) {
sql := client
var where = sql.Where("1 = ?", 1)
if otherQueryFuncList != nil {
for _, otherQueryFunc := range otherQueryFuncList {
where = otherQueryFunc(where)
}
}
dbRules = []QASonarMetricRules{}
if err := where.Find(&dbRules, query).Error; err != nil {
return nil, err
}
return dbRules, nil
}