mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
This commit fixes the following golint warnings: pkg/bus/bus.go:64:9: if block ends with a return statement, so drop this else and outdent its block pkg/bus/bus.go:84:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:137:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:177:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:183:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:199:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:208:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/components/dynmap/dynmap.go:236:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:242:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:257:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:263:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:278:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:284:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:299:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:331:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:350:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:356:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:366:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:390:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:396:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:405:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:427:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:433:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:442:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:459:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:465:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:474:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:491:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:497:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:506:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:523:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:529:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:538:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:555:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:561:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:570:12: if block ends with a return statement, so drop this else and outdent its block pkg/login/ldap.go:55:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/login/ldap_test.go:372:10: if block ends with a return statement, so drop this else and outdent its block pkg/middleware/middleware_test.go:213:12: if block ends with a return statement, so drop this else and outdent its block pkg/plugins/dashboard_importer.go:153:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/dashboards_updater.go:39:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/dashboards_updater.go:121:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/plugins.go:210:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/plugins.go:235:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/eval_context.go:111:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifier.go:92:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifier.go:98:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifier.go:122:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/rule.go:108:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/rule.go:118:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/rule.go:121:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifiers/telegram.go:94:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/annotation.go:34:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/sqlstore/annotation.go:99:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/sqlstore/dashboard_test.go:107:13: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/plugin_setting.go:78:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/preferences.go:91:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/user.go:50:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/migrator/migrator.go:106:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/sqlstore/migrator/postgres_dialect.go:48:10: if block ends with a return statement, so drop this else and outdent its block pkg/tsdb/time_range.go:59:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/tsdb/time_range.go:67:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/tsdb/cloudwatch/metric_find_query.go:225:9: if block ends with a return statement, so drop this else and outdent its block pkg/util/filepath.go:68:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
266 lines
6.9 KiB
Go
266 lines
6.9 KiB
Go
package sqlstore
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/annotations"
|
|
)
|
|
|
|
type SqlAnnotationRepo struct {
|
|
}
|
|
|
|
func (r *SqlAnnotationRepo) Save(item *annotations.Item) error {
|
|
return inTransaction(func(sess *DBSession) error {
|
|
tags := models.ParseTagPairs(item.Tags)
|
|
item.Tags = models.JoinTagPairs(tags)
|
|
item.Created = time.Now().UnixNano() / int64(time.Millisecond)
|
|
item.Updated = item.Created
|
|
if item.Epoch == 0 {
|
|
item.Epoch = item.Created
|
|
}
|
|
|
|
if _, err := sess.Table("annotation").Insert(item); err != nil {
|
|
return err
|
|
}
|
|
|
|
if item.Tags != nil {
|
|
tags, err := r.ensureTagsExist(sess, tags)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, tag := range tags {
|
|
if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", item.Id, tag.Id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Will insert if needed any new key/value pars and return ids
|
|
func (r *SqlAnnotationRepo) ensureTagsExist(sess *DBSession, tags []*models.Tag) ([]*models.Tag, error) {
|
|
for _, tag := range tags {
|
|
var existingTag models.Tag
|
|
|
|
// check if it exists
|
|
if exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tag.Key, tag.Value).Get(&existingTag); err != nil {
|
|
return nil, err
|
|
} else if exists {
|
|
tag.Id = existingTag.Id
|
|
} else {
|
|
if _, err := sess.Table("tag").Insert(tag); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
return tags, nil
|
|
}
|
|
|
|
func (r *SqlAnnotationRepo) Update(item *annotations.Item) error {
|
|
return inTransaction(func(sess *DBSession) error {
|
|
var (
|
|
isExist bool
|
|
err error
|
|
)
|
|
existing := new(annotations.Item)
|
|
|
|
if item.Id == 0 && item.RegionId != 0 {
|
|
// Update region end time
|
|
isExist, err = sess.Table("annotation").Where("region_id=? AND id!=? AND org_id=?", item.RegionId, item.RegionId, item.OrgId).Get(existing)
|
|
} else {
|
|
isExist, err = sess.Table("annotation").Where("id=? AND org_id=?", item.Id, item.OrgId).Get(existing)
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !isExist {
|
|
return errors.New("Annotation not found")
|
|
}
|
|
|
|
existing.Updated = time.Now().UnixNano() / int64(time.Millisecond)
|
|
existing.Epoch = item.Epoch
|
|
existing.Text = item.Text
|
|
if item.RegionId != 0 {
|
|
existing.RegionId = item.RegionId
|
|
}
|
|
|
|
if item.Tags != nil {
|
|
tags, err := r.ensureTagsExist(sess, models.ParseTagPairs(item.Tags))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := sess.Exec("DELETE FROM annotation_tag WHERE annotation_id = ?", existing.Id); err != nil {
|
|
return err
|
|
}
|
|
for _, tag := range tags {
|
|
if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", existing.Id, tag.Id); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
existing.Tags = item.Tags
|
|
|
|
_, err = sess.Table("annotation").Id(existing.Id).Cols("epoch", "text", "region_id", "updated", "tags").Update(existing)
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) {
|
|
var sql bytes.Buffer
|
|
params := make([]interface{}, 0)
|
|
|
|
sql.WriteString(`
|
|
SELECT
|
|
annotation.id,
|
|
annotation.epoch as time,
|
|
annotation.dashboard_id,
|
|
annotation.panel_id,
|
|
annotation.new_state,
|
|
annotation.prev_state,
|
|
annotation.alert_id,
|
|
annotation.region_id,
|
|
annotation.text,
|
|
annotation.tags,
|
|
annotation.data,
|
|
annotation.created,
|
|
annotation.updated,
|
|
usr.email,
|
|
usr.login,
|
|
alert.name as alert_name
|
|
FROM annotation
|
|
LEFT OUTER JOIN ` + dialect.Quote("user") + ` as usr on usr.id = annotation.user_id
|
|
LEFT OUTER JOIN alert on alert.id = annotation.alert_id
|
|
`)
|
|
|
|
sql.WriteString(`WHERE annotation.org_id = ?`)
|
|
params = append(params, query.OrgId)
|
|
|
|
if query.AnnotationId != 0 {
|
|
fmt.Print("annotation query")
|
|
sql.WriteString(` AND annotation.id = ?`)
|
|
params = append(params, query.AnnotationId)
|
|
}
|
|
|
|
if query.RegionId != 0 {
|
|
sql.WriteString(` AND annotation.region_id = ?`)
|
|
params = append(params, query.RegionId)
|
|
}
|
|
|
|
if query.AlertId != 0 {
|
|
sql.WriteString(` AND annotation.alert_id = ?`)
|
|
params = append(params, query.AlertId)
|
|
}
|
|
|
|
if query.DashboardId != 0 {
|
|
sql.WriteString(` AND annotation.dashboard_id = ?`)
|
|
params = append(params, query.DashboardId)
|
|
}
|
|
|
|
if query.PanelId != 0 {
|
|
sql.WriteString(` AND annotation.panel_id = ?`)
|
|
params = append(params, query.PanelId)
|
|
}
|
|
|
|
if query.UserId != 0 {
|
|
sql.WriteString(` AND annotation.user_id = ?`)
|
|
params = append(params, query.UserId)
|
|
}
|
|
|
|
if query.From > 0 && query.To > 0 {
|
|
sql.WriteString(` AND annotation.epoch BETWEEN ? AND ?`)
|
|
params = append(params, query.From, query.To)
|
|
}
|
|
|
|
if query.Type == "alert" {
|
|
sql.WriteString(` AND annotation.alert_id > 0`)
|
|
} else if query.Type == "annotation" {
|
|
sql.WriteString(` AND annotation.alert_id = 0`)
|
|
}
|
|
|
|
if len(query.Tags) > 0 {
|
|
keyValueFilters := []string{}
|
|
|
|
tags := models.ParseTagPairs(query.Tags)
|
|
for _, tag := range tags {
|
|
if tag.Value == "" {
|
|
keyValueFilters = append(keyValueFilters, "(tag.key = ?)")
|
|
params = append(params, tag.Key)
|
|
} else {
|
|
keyValueFilters = append(keyValueFilters, "(tag.key = ? AND tag.value = ?)")
|
|
params = append(params, tag.Key, tag.Value)
|
|
}
|
|
}
|
|
|
|
if len(tags) > 0 {
|
|
tagsSubQuery := fmt.Sprintf(`
|
|
SELECT SUM(1) FROM annotation_tag at
|
|
INNER JOIN tag on tag.id = at.tag_id
|
|
WHERE at.annotation_id = annotation.id
|
|
AND (
|
|
%s
|
|
)
|
|
`, strings.Join(keyValueFilters, " OR "))
|
|
|
|
sql.WriteString(fmt.Sprintf(" AND (%s) = %d ", tagsSubQuery, len(tags)))
|
|
}
|
|
}
|
|
|
|
if query.Limit == 0 {
|
|
query.Limit = 100
|
|
}
|
|
|
|
sql.WriteString(fmt.Sprintf(" ORDER BY epoch DESC LIMIT %v", query.Limit))
|
|
|
|
items := make([]*annotations.ItemDTO, 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 *DBSession) error {
|
|
var (
|
|
sql string
|
|
annoTagSql string
|
|
queryParams []interface{}
|
|
)
|
|
|
|
if params.RegionId != 0 {
|
|
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE region_id = ?)"
|
|
sql = "DELETE FROM annotation WHERE region_id = ?"
|
|
queryParams = []interface{}{params.RegionId}
|
|
} else if params.Id != 0 {
|
|
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE id = ?)"
|
|
sql = "DELETE FROM annotation WHERE id = ?"
|
|
queryParams = []interface{}{params.Id}
|
|
} else {
|
|
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE dashboard_id = ? AND panel_id = ?)"
|
|
sql = "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?"
|
|
queryParams = []interface{}{params.DashboardId, params.PanelId}
|
|
}
|
|
|
|
if _, err := sess.Exec(annoTagSql, queryParams...); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := sess.Exec(sql, queryParams...); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|