2014-12-22 05:25:08 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
2015-02-04 04:35:59 -06:00
|
|
|
"bytes"
|
2015-02-05 04:10:56 -06:00
|
|
|
"fmt"
|
2015-02-04 04:35:59 -06:00
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
"github.com/go-xorm/xorm"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-03-22 14:14:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2014-12-22 05:25:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2014-12-29 06:58:06 -06:00
|
|
|
bus.AddHandler("sql", SaveDashboard)
|
|
|
|
bus.AddHandler("sql", GetDashboard)
|
|
|
|
bus.AddHandler("sql", DeleteDashboard)
|
|
|
|
bus.AddHandler("sql", SearchDashboards)
|
2015-01-06 11:39:26 -06:00
|
|
|
bus.AddHandler("sql", GetDashboardTags)
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func SaveDashboard(cmd *m.SaveDashboardCommand) error {
|
2014-12-22 05:25:08 -06:00
|
|
|
return inTransaction(func(sess *xorm.Session) error {
|
|
|
|
dash := cmd.GetDashboardModel()
|
|
|
|
|
2015-01-05 10:04:29 -06:00
|
|
|
// try get existing dashboard
|
2015-05-04 01:19:29 -05:00
|
|
|
var existing, sameTitle m.Dashboard
|
2015-01-05 10:04:29 -06:00
|
|
|
|
2015-05-04 01:19:29 -05:00
|
|
|
if dash.Id > 0 {
|
|
|
|
dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !dashWithIdExists {
|
|
|
|
return m.ErrDashboardNotFound
|
2015-03-02 15:24:01 -06:00
|
|
|
}
|
2015-05-04 01:19:29 -05:00
|
|
|
|
2015-03-02 15:24:01 -06:00
|
|
|
// check for is someone else has written in between
|
|
|
|
if dash.Version != existing.Version {
|
|
|
|
if cmd.Overwrite {
|
|
|
|
dash.Version = existing.Version
|
|
|
|
} else {
|
|
|
|
return m.ErrDashboardVersionMismatch
|
|
|
|
}
|
|
|
|
}
|
2015-01-05 10:04:29 -06:00
|
|
|
}
|
|
|
|
|
2015-05-04 01:19:29 -05:00
|
|
|
sameTitleExists, err := sess.Where("org_id=? AND slug=?", dash.OrgId, dash.Slug).Get(&sameTitle)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sameTitleExists {
|
|
|
|
// another dashboard with same name
|
|
|
|
if dash.Id != sameTitle.Id {
|
|
|
|
if cmd.Overwrite {
|
|
|
|
dash.Id = sameTitle.Id
|
|
|
|
} else {
|
|
|
|
return m.ErrDashboardWithSameNameExists
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:46:46 -05:00
|
|
|
affectedRows := int64(0)
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
if dash.Id == 0 {
|
2015-03-22 14:14:00 -05:00
|
|
|
metrics.M_Models_Dashboard_Insert.Inc(1)
|
2015-05-04 00:46:46 -05:00
|
|
|
affectedRows, err = sess.Insert(dash)
|
2014-12-22 05:25:08 -06:00
|
|
|
} else {
|
2015-03-02 15:24:01 -06:00
|
|
|
dash.Version += 1
|
|
|
|
dash.Data["version"] = dash.Version
|
2015-05-04 00:46:46 -05:00
|
|
|
affectedRows, err = sess.Id(dash.Id).Update(dash)
|
|
|
|
}
|
|
|
|
|
|
|
|
if affectedRows == 0 {
|
|
|
|
return m.ErrDashboardNotFound
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
// delete existing tabs
|
|
|
|
_, err = sess.Exec("DELETE FROM dashboard_tag WHERE dashboard_id=?", dash.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert new tags
|
|
|
|
tags := dash.GetTags()
|
|
|
|
if len(tags) > 0 {
|
|
|
|
for _, tag := range tags {
|
2015-01-20 07:15:48 -06:00
|
|
|
if _, err := sess.Insert(&DashboardTag{DashboardId: dash.Id, Term: tag}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-07 05:37:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
cmd.Result = dash
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func GetDashboard(query *m.GetDashboardQuery) error {
|
2015-02-23 13:07:49 -06:00
|
|
|
dashboard := m.Dashboard{Slug: query.Slug, OrgId: query.OrgId}
|
2014-12-22 05:25:08 -06:00
|
|
|
has, err := x.Get(&dashboard)
|
|
|
|
if err != nil {
|
2014-12-29 06:58:06 -06:00
|
|
|
return err
|
2014-12-22 05:25:08 -06:00
|
|
|
} else if has == false {
|
2014-12-29 06:58:06 -06:00
|
|
|
return m.ErrDashboardNotFound
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2015-01-29 05:10:34 -06:00
|
|
|
dashboard.Data["id"] = dashboard.Id
|
2014-12-29 06:58:06 -06:00
|
|
|
query.Result = &dashboard
|
|
|
|
|
|
|
|
return nil
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
type DashboardSearchProjection struct {
|
|
|
|
Id int64
|
|
|
|
Title string
|
|
|
|
Slug string
|
|
|
|
Term string
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func SearchDashboards(query *m.SearchDashboardsQuery) error {
|
2015-02-04 04:35:59 -06:00
|
|
|
var sql bytes.Buffer
|
|
|
|
params := make([]interface{}, 0)
|
|
|
|
|
|
|
|
sql.WriteString(`SELECT
|
|
|
|
dashboard.id,
|
|
|
|
dashboard.title,
|
|
|
|
dashboard.slug,
|
|
|
|
dashboard_tag.term
|
|
|
|
FROM dashboard
|
|
|
|
LEFT OUTER JOIN dashboard_tag on dashboard_tag.dashboard_id = dashboard.id`)
|
|
|
|
|
|
|
|
if query.IsStarred {
|
|
|
|
sql.WriteString(" INNER JOIN star on star.dashboard_id = dashboard.id")
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
sql.WriteString(` WHERE dashboard.org_id=?`)
|
2015-02-04 04:35:59 -06:00
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
params = append(params, query.OrgId)
|
2015-01-06 10:15:52 -06:00
|
|
|
|
2015-02-04 04:35:59 -06:00
|
|
|
if query.IsStarred {
|
|
|
|
sql.WriteString(` AND star.user_id=?`)
|
|
|
|
params = append(params, query.UserId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(query.Title) > 0 {
|
|
|
|
sql.WriteString(" AND dashboard.title LIKE ?")
|
|
|
|
params = append(params, "%"+query.Title+"%")
|
|
|
|
}
|
2014-12-22 05:25:08 -06:00
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
if len(query.Tag) > 0 {
|
2015-02-04 04:35:59 -06:00
|
|
|
sql.WriteString(" AND dashboard_tag.term=?")
|
|
|
|
params = append(params, query.Tag)
|
2015-01-07 05:37:24 -06:00
|
|
|
}
|
|
|
|
|
2015-02-05 08:53:15 -06:00
|
|
|
if query.Limit == 0 || query.Limit > 10000 {
|
|
|
|
query.Limit = 300
|
|
|
|
}
|
|
|
|
|
2015-03-31 15:06:19 -05:00
|
|
|
sql.WriteString(fmt.Sprintf(" ORDER BY dashboard.title ASC LIMIT %d", query.Limit))
|
2015-02-05 04:10:56 -06:00
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
var res []DashboardSearchProjection
|
2015-02-04 04:35:59 -06:00
|
|
|
err := x.Sql(sql.String(), params...).Find(&res)
|
2015-01-07 05:37:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = make([]*m.DashboardSearchHit, 0)
|
|
|
|
hits := make(map[int64]*m.DashboardSearchHit)
|
|
|
|
|
|
|
|
for _, item := range res {
|
|
|
|
hit, exists := hits[item.Id]
|
|
|
|
if !exists {
|
|
|
|
hit = &m.DashboardSearchHit{
|
2015-02-02 10:17:57 -06:00
|
|
|
Id: item.Id,
|
2015-01-07 05:37:24 -06:00
|
|
|
Title: item.Title,
|
2015-05-12 07:11:30 -05:00
|
|
|
Uri: "db/" + item.Slug,
|
2015-05-13 03:45:53 -05:00
|
|
|
Type: m.DashTypeDB,
|
2015-01-07 05:37:24 -06:00
|
|
|
Tags: []string{},
|
|
|
|
}
|
|
|
|
query.Result = append(query.Result, hit)
|
|
|
|
hits[item.Id] = hit
|
|
|
|
}
|
|
|
|
if len(item.Term) > 0 {
|
|
|
|
hit.Tags = append(hit.Tags, item.Term)
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 05:25:08 -06:00
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
return err
|
2014-12-22 05:25:08 -06:00
|
|
|
}
|
|
|
|
|
2015-01-06 11:39:26 -06:00
|
|
|
func GetDashboardTags(query *m.GetDashboardTagsQuery) error {
|
2015-01-20 08:23:14 -06:00
|
|
|
sql := `SELECT
|
|
|
|
COUNT(*) as count,
|
|
|
|
term
|
|
|
|
FROM dashboard
|
|
|
|
INNER JOIN dashboard_tag on dashboard_tag.dashboard_id = dashboard.id
|
2015-02-23 13:07:49 -06:00
|
|
|
WHERE dashboard.org_id=?
|
2015-01-20 08:23:14 -06:00
|
|
|
GROUP BY term`
|
|
|
|
|
|
|
|
query.Result = make([]*m.DashboardTagCloudItem, 0)
|
2015-02-23 13:07:49 -06:00
|
|
|
sess := x.Sql(sql, query.OrgId)
|
2015-01-07 05:37:24 -06:00
|
|
|
err := sess.Find(&query.Result)
|
|
|
|
return err
|
2015-01-06 11:39:26 -06:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:58:06 -06:00
|
|
|
func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
|
2014-12-22 05:25:08 -06:00
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
|
2015-02-25 07:31:44 -06:00
|
|
|
rawSql := "DELETE FROM dashboard WHERE org_id=? and slug=?"
|
2015-02-23 13:07:49 -06:00
|
|
|
_, err := sess.Exec(rawSql, cmd.OrgId, cmd.Slug)
|
2014-12-22 05:25:08 -06:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|