forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotation.go
100 lines (76 loc) · 2.13 KB
/
annotation.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
package sqlstore
import (
"bytes"
"fmt"
"strings"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/services/annotations"
)
type SqlAnnotationRepo struct {
}
func (r *SqlAnnotationRepo) Save(item *annotations.Item) error {
return inTransaction(func(sess *xorm.Session) error {
if _, err := sess.Table("annotation").Insert(item); err != nil {
return err
}
return nil
})
}
func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.Item, error) {
var sql bytes.Buffer
params := make([]interface{}, 0)
sql.WriteString(`SELECT *
from annotation
`)
sql.WriteString(`WHERE org_id = ?`)
params = append(params, query.OrgId)
if query.AlertId != 0 {
sql.WriteString(` AND alert_id = ?`)
params = append(params, query.AlertId)
}
if query.AlertId != 0 {
sql.WriteString(` AND alert_id = ?`)
params = append(params, query.AlertId)
}
if query.DashboardId != 0 {
sql.WriteString(` AND dashboard_id = ?`)
params = append(params, query.DashboardId)
}
if query.PanelId != 0 {
sql.WriteString(` AND panel_id = ?`)
params = append(params, query.PanelId)
}
if query.From > 0 && query.To > 0 {
sql.WriteString(` AND epoch BETWEEN ? AND ?`)
params = append(params, query.From, query.To)
}
if query.Type != "" {
sql.WriteString(` AND type = ?`)
params = append(params, string(query.Type))
}
if len(query.NewState) > 0 {
sql.WriteString(` AND new_state IN (?` + strings.Repeat(",?", len(query.NewState)-1) + ")")
for _, v := range query.NewState {
params = append(params, v)
}
}
if query.Limit == 0 {
query.Limit = 10
}
sql.WriteString(fmt.Sprintf(" ORDER BY epoch DESC LIMIT %v", query.Limit))
items := make([]*annotations.Item, 0)
if err := x.Sql(sql.String(), params...).Find(&items); err != nil {
return nil, err
}
return items, nil
}
func (r *SqlAnnotationRepo) Delete(params *annotations.DeleteParams) error {
return inTransaction(func(sess *xorm.Session) error {
sql := "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?"
_, err := sess.Exec(sql, params.DashboardId, params.PanelId)
if err != nil {
return err
}
return nil
})
}