mirror of https://github.com/grafana/grafana
feat(annotations): working on alert annotations, #5694
parent
c5e90b1801
commit
357358898d
@ -1,24 +0,0 @@ |
|||||||
package models |
|
||||||
|
|
||||||
import ( |
|
||||||
"time" |
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson" |
|
||||||
) |
|
||||||
|
|
||||||
type AnnotationType string |
|
||||||
|
|
||||||
type Annotation struct { |
|
||||||
Id int64 |
|
||||||
OrgId int64 |
|
||||||
Type AnnotationType |
|
||||||
Title string |
|
||||||
Text string |
|
||||||
AlertId int64 |
|
||||||
UserId int64 |
|
||||||
PreviousState string |
|
||||||
NewState string |
|
||||||
Timestamp time.Time |
|
||||||
|
|
||||||
Data *simplejson.Json |
|
||||||
} |
|
||||||
@ -0,0 +1,44 @@ |
|||||||
|
package annotations |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/components/simplejson" |
||||||
|
) |
||||||
|
|
||||||
|
type Repository interface { |
||||||
|
Save(item *Item) error |
||||||
|
} |
||||||
|
|
||||||
|
var repositoryInstance Repository |
||||||
|
|
||||||
|
func GetRepository() Repository { |
||||||
|
return repositoryInstance |
||||||
|
} |
||||||
|
|
||||||
|
func SetRepository(rep Repository) { |
||||||
|
repositoryInstance = rep |
||||||
|
} |
||||||
|
|
||||||
|
type ItemType string |
||||||
|
|
||||||
|
const ( |
||||||
|
AlertType ItemType = "alert" |
||||||
|
) |
||||||
|
|
||||||
|
type Item struct { |
||||||
|
Id int64 `json:"id"` |
||||||
|
OrgId int64 `json:"orgId"` |
||||||
|
PanelLinkId string `json:"panelLinkId"` |
||||||
|
Type ItemType `json:"type"` |
||||||
|
Title string `json:"title"` |
||||||
|
Text string `json:"text"` |
||||||
|
Metric string `json:"metric"` |
||||||
|
AlertId int64 `json:"alertId"` |
||||||
|
UserId int64 `json:"userId"` |
||||||
|
PrevState string `json:"prevState"` |
||||||
|
NewState string `json:"newState"` |
||||||
|
Timestamp time.Time `json:"timestamp"` |
||||||
|
|
||||||
|
Data *simplejson.Json `json:"data"` |
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
package sqlstore |
||||||
|
|
||||||
|
import ( |
||||||
|
"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 |
||||||
|
}) |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,40 @@ |
|||||||
|
package migrations |
||||||
|
|
||||||
|
import ( |
||||||
|
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator" |
||||||
|
) |
||||||
|
|
||||||
|
func addAnnotationMig(mg *Migrator) { |
||||||
|
table := Table{ |
||||||
|
Name: "annotation", |
||||||
|
Columns: []*Column{ |
||||||
|
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
||||||
|
{Name: "org_id", Type: DB_BigInt, Nullable: false}, |
||||||
|
{Name: "alert_id", Type: DB_BigInt, Nullable: true}, |
||||||
|
{Name: "user_id", Type: DB_BigInt, Nullable: true}, |
||||||
|
{Name: "panel_link_id", Type: DB_NVarchar, Length: 32, Nullable: false}, |
||||||
|
{Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false}, |
||||||
|
{Name: "title", Type: DB_Text, Nullable: false}, |
||||||
|
{Name: "text", Type: DB_Text, Nullable: false}, |
||||||
|
{Name: "metric", Type: DB_NVarchar, Length: 255, Nullable: true}, |
||||||
|
{Name: "prev_state", Type: DB_NVarchar, Length: 25, Nullable: false}, |
||||||
|
{Name: "new_state", Type: DB_NVarchar, Length: 25, Nullable: false}, |
||||||
|
{Name: "data", Type: DB_Text, Nullable: false}, |
||||||
|
{Name: "timestamp", Type: DB_DateTime, Nullable: false}, |
||||||
|
}, |
||||||
|
Indices: []*Index{ |
||||||
|
{Cols: []string{"org_id", "alert_id"}, Type: IndexType}, |
||||||
|
{Cols: []string{"org_id", "type"}, Type: IndexType}, |
||||||
|
{Cols: []string{"org_id", "panel_link_id"}, Type: IndexType}, |
||||||
|
{Cols: []string{"timestamp"}, Type: IndexType}, |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
mg.AddMigration("create annotation table v1", NewAddTableMigration(table)) |
||||||
|
|
||||||
|
// create indices
|
||||||
|
mg.AddMigration("add index annotation org_id & alert_id ", NewAddIndexMigration(table, table.Indices[0])) |
||||||
|
mg.AddMigration("add index annotation org_id & type", NewAddIndexMigration(table, table.Indices[1])) |
||||||
|
mg.AddMigration("add index annotation org_id & panel_link_id ", NewAddIndexMigration(table, table.Indices[2])) |
||||||
|
mg.AddMigration("add index annotation timestamp", NewAddIndexMigration(table, table.Indices[3])) |
||||||
|
} |
||||||
Loading…
Reference in new issue