forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.go
162 lines (130 loc) · 3.55 KB
/
dashboard.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
package models
import (
"strings"
"time"
"github.com/pkg/errors"
"github.com/toolkits/pkg/str"
"gorm.io/gorm"
)
type Dashboard struct {
Id int64 `json:"id" gorm:"primaryKey"`
GroupId int64 `json:"group_id"`
Name string `json:"name"`
Tags string `json:"-"`
TagsLst []string `json:"tags" gorm:"-"`
Configs string `json:"configs"`
CreateAt int64 `json:"create_at"`
CreateBy string `json:"create_by"`
UpdateAt int64 `json:"update_at"`
UpdateBy string `json:"update_by"`
}
func (d *Dashboard) TableName() string {
return "dashboard"
}
func (d *Dashboard) Verify() error {
if d.Name == "" {
return errors.New("Name is blank")
}
if str.Dangerous(d.Name) {
return errors.New("Name has invalid characters")
}
return nil
}
func (d *Dashboard) Add() error {
if err := d.Verify(); err != nil {
return err
}
exists, err := DashboardExists("group_id=? and name=?", d.GroupId, d.Name)
if err != nil {
return errors.WithMessage(err, "failed to count dashboard")
}
if exists {
return errors.New("Dashboard already exists")
}
now := time.Now().Unix()
d.CreateAt = now
d.UpdateAt = now
return Insert(d)
}
func (d *Dashboard) Update(selectField interface{}, selectFields ...interface{}) error {
if err := d.Verify(); err != nil {
return err
}
return DB().Model(d).Select(selectField, selectFields...).Updates(d).Error
}
func (d *Dashboard) Del() error {
cgids, err := ChartGroupIdsOf(d.Id)
if err != nil {
return err
}
if len(cgids) == 0 {
return DB().Transaction(func(tx *gorm.DB) error {
if err := tx.Where("id=?", d.Id).Delete(&Dashboard{}).Error; err != nil {
return err
}
return nil
})
}
return DB().Transaction(func(tx *gorm.DB) error {
if err := tx.Where("group_id in ?", cgids).Delete(&Chart{}).Error; err != nil {
return err
}
if err := tx.Where("dashboard_id=?", d.Id).Delete(&ChartGroup{}).Error; err != nil {
return err
}
if err := tx.Where("id=?", d.Id).Delete(&Dashboard{}).Error; err != nil {
return err
}
return nil
})
}
func DashboardGet(where string, args ...interface{}) (*Dashboard, error) {
var lst []*Dashboard
err := DB().Where(where, args...).Find(&lst).Error
if err != nil {
return nil, err
}
if len(lst) == 0 {
return nil, nil
}
lst[0].TagsLst = strings.Fields(lst[0].Tags)
return lst[0], nil
}
func DashboardCount(where string, args ...interface{}) (num int64, err error) {
return Count(DB().Model(&Dashboard{}).Where(where, args...))
}
func DashboardExists(where string, args ...interface{}) (bool, error) {
num, err := DashboardCount(where, args...)
return num > 0, err
}
func DashboardGets(groupId int64, query string) ([]Dashboard, error) {
session := DB().Where("group_id=?", groupId).Order("name")
arr := strings.Fields(query)
if len(arr) > 0 {
for i := 0; i < len(arr); i++ {
if strings.HasPrefix(arr[i], "-") {
q := "%" + arr[i][1:] + "%"
session = session.Where("name not like ? and tags not like ?", q, q)
} else {
q := "%" + arr[i] + "%"
session = session.Where("(name like ? or tags like ?)", q, q)
}
}
}
var objs []Dashboard
err := session.Select("id", "group_id", "name", "tags", "create_at", "create_by", "update_at", "update_by").Find(&objs).Error
if err == nil {
for i := 0; i < len(objs); i++ {
objs[i].TagsLst = strings.Fields(objs[i].Tags)
}
}
return objs, err
}
func DashboardGetsByIds(ids []int64) ([]Dashboard, error) {
if len(ids) == 0 {
return []Dashboard{}, nil
}
var lst []Dashboard
err := DB().Where("id in ?", ids).Order("name").Find(&lst).Error
return lst, err
}