annotation.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package sqlstore
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "github.com/go-xorm/xorm"
  7. "github.com/grafana/grafana/pkg/services/annotations"
  8. )
  9. type SqlAnnotationRepo struct {
  10. }
  11. func (r *SqlAnnotationRepo) Save(item *annotations.Item) error {
  12. return inTransaction(func(sess *xorm.Session) error {
  13. if _, err := sess.Table("annotation").Insert(item); err != nil {
  14. return err
  15. }
  16. return nil
  17. })
  18. }
  19. func (r *SqlAnnotationRepo) Update(item *annotations.Item) error {
  20. return inTransaction(func(sess *xorm.Session) error {
  21. if _, err := sess.Table("annotation").Id(item.Id).Update(item); err != nil {
  22. return err
  23. }
  24. return nil
  25. })
  26. }
  27. func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.Item, error) {
  28. var sql bytes.Buffer
  29. params := make([]interface{}, 0)
  30. sql.WriteString(`SELECT *
  31. from annotation
  32. `)
  33. sql.WriteString(`WHERE org_id = ?`)
  34. params = append(params, query.OrgId)
  35. if query.AlertId != 0 {
  36. sql.WriteString(` AND alert_id = ?`)
  37. params = append(params, query.AlertId)
  38. }
  39. if query.AlertId != 0 {
  40. sql.WriteString(` AND alert_id = ?`)
  41. params = append(params, query.AlertId)
  42. }
  43. if query.DashboardId != 0 {
  44. sql.WriteString(` AND dashboard_id = ?`)
  45. params = append(params, query.DashboardId)
  46. }
  47. if query.PanelId != 0 {
  48. sql.WriteString(` AND panel_id = ?`)
  49. params = append(params, query.PanelId)
  50. }
  51. if query.From > 0 && query.To > 0 {
  52. sql.WriteString(` AND epoch BETWEEN ? AND ?`)
  53. params = append(params, query.From, query.To)
  54. }
  55. if query.Type != "" {
  56. sql.WriteString(` AND type = ?`)
  57. params = append(params, string(query.Type))
  58. }
  59. if len(query.NewState) > 0 {
  60. sql.WriteString(` AND new_state IN (?` + strings.Repeat(",?", len(query.NewState)-1) + ")")
  61. for _, v := range query.NewState {
  62. params = append(params, v)
  63. }
  64. }
  65. if query.Limit == 0 {
  66. query.Limit = 10
  67. }
  68. sql.WriteString(fmt.Sprintf(" ORDER BY epoch DESC LIMIT %v", query.Limit))
  69. items := make([]*annotations.Item, 0)
  70. if err := x.Sql(sql.String(), params...).Find(&items); err != nil {
  71. return nil, err
  72. }
  73. return items, nil
  74. }
  75. func (r *SqlAnnotationRepo) Delete(params *annotations.DeleteParams) error {
  76. return inTransaction(func(sess *xorm.Session) error {
  77. sql := "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?"
  78. _, err := sess.Exec(sql, params.DashboardId, params.PanelId)
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. })
  84. }