mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Refactoring dashboard data access
This commit is contained in:
@@ -1,16 +1,16 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/torkelo/grafana-pro/pkg/api/dtos"
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||||
"github.com/torkelo/grafana-pro/pkg/middleware"
|
"github.com/torkelo/grafana-pro/pkg/middleware"
|
||||||
"github.com/torkelo/grafana-pro/pkg/models"
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||||
"github.com/torkelo/grafana-pro/pkg/utils"
|
"github.com/torkelo/grafana-pro/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetDashboard(c *middleware.Context) {
|
func GetDashboard(c *middleware.Context) {
|
||||||
slug := c.Params(":slug")
|
slug := c.Params(":slug")
|
||||||
|
|
||||||
dash, err := models.GetDashboard(slug, c.GetAccountId())
|
dash, err := m.GetDashboard(slug, c.GetAccountId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(404, "Dashboard not found", nil)
|
c.JsonApiErr(404, "Dashboard not found", nil)
|
||||||
return
|
return
|
||||||
@@ -24,13 +24,13 @@ func GetDashboard(c *middleware.Context) {
|
|||||||
func DeleteDashboard(c *middleware.Context) {
|
func DeleteDashboard(c *middleware.Context) {
|
||||||
slug := c.Params(":slug")
|
slug := c.Params(":slug")
|
||||||
|
|
||||||
dash, err := models.GetDashboard(slug, c.GetAccountId())
|
dash, err := m.GetDashboard(slug, c.GetAccountId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(404, "Dashboard not found", nil)
|
c.JsonApiErr(404, "Dashboard not found", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = models.DeleteDashboard(slug, c.GetAccountId())
|
err = m.DeleteDashboard(slug, c.GetAccountId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "Failed to delete dashboard", err)
|
c.JsonApiErr(500, "Failed to delete dashboard", err)
|
||||||
return
|
return
|
||||||
@@ -44,7 +44,7 @@ func DeleteDashboard(c *middleware.Context) {
|
|||||||
func Search(c *middleware.Context) {
|
func Search(c *middleware.Context) {
|
||||||
query := c.Query("q")
|
query := c.Query("q")
|
||||||
|
|
||||||
results, err := models.SearchQuery(query, c.GetAccountId())
|
results, err := m.SearchQuery(query, c.GetAccountId())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "Search failed", err)
|
c.JsonApiErr(500, "Search failed", err)
|
||||||
return
|
return
|
||||||
@@ -53,39 +53,21 @@ func Search(c *middleware.Context) {
|
|||||||
c.JSON(200, results)
|
c.JSON(200, results)
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertToStringArray(arr []interface{}) []string {
|
|
||||||
b := make([]string, len(arr))
|
|
||||||
for i := range arr {
|
|
||||||
b[i] = arr[i].(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func PostDashboard(c *middleware.Context) {
|
func PostDashboard(c *middleware.Context) {
|
||||||
var command dtos.SaveDashboardCommand
|
var cmd m.SaveDashboardCommand
|
||||||
|
|
||||||
if !c.JsonBody(&command) {
|
if !c.JsonBody(&cmd) {
|
||||||
c.JsonApiErr(400, "bad request", nil)
|
c.JsonApiErr(400, "bad request", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dashboard := models.NewDashboard("test")
|
cmd.AccountId = c.GetAccountId()
|
||||||
dashboard.Data = command.Dashboard
|
|
||||||
dashboard.Title = dashboard.Data["title"].(string)
|
|
||||||
dashboard.AccountId = c.GetAccountId()
|
|
||||||
dashboard.Tags = convertToStringArray(dashboard.Data["tags"].([]interface{}))
|
|
||||||
dashboard.UpdateSlug()
|
|
||||||
|
|
||||||
if dashboard.Data["id"] != nil {
|
err := bus.Dispatch(cmd)
|
||||||
dashboard.Id = int64(dashboard.Data["id"].(float64))
|
|
||||||
}
|
|
||||||
|
|
||||||
err := models.SaveDashboard(dashboard)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "Failed to save dashboard", err)
|
c.JsonApiErr(500, "Failed to save dashboard", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, utils.DynMap{"status": "success", "slug": dashboard.Slug})
|
c.JSON(200, utils.DynMap{"status": "success", "slug": cmd.Result.Slug})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
package dtos
|
|
||||||
|
|
||||||
type AddCollaboratorCommand struct {
|
|
||||||
Email string `json:"email" binding:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type SaveDashboardCommand struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
Title string `json:"title"`
|
|
||||||
Dashboard map[string]interface{} `json:"dashboard"`
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -11,7 +9,6 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
GetDashboard func(slug string, accountId int64) (*Dashboard, error)
|
GetDashboard func(slug string, accountId int64) (*Dashboard, error)
|
||||||
SaveDashboard func(dash *Dashboard) error
|
|
||||||
DeleteDashboard func(slug string, accountId int64) error
|
DeleteDashboard func(slug string, accountId int64) error
|
||||||
SearchQuery func(query string, acccountId int64) ([]*SearchResult, error)
|
SearchQuery func(query string, acccountId int64) ([]*SearchResult, error)
|
||||||
)
|
)
|
||||||
@@ -40,26 +37,46 @@ type SearchResult struct {
|
|||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SaveDashboardCommand struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Dashboard map[string]interface{} `json:"dashboard"`
|
||||||
|
AccountId int64 `json:"-"`
|
||||||
|
|
||||||
|
Result *Dashboard
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertToStringArray(arr []interface{}) []string {
|
||||||
|
b := make([]string, len(arr))
|
||||||
|
for i := range arr {
|
||||||
|
b[i] = arr[i].(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func NewDashboard(title string) *Dashboard {
|
func NewDashboard(title string) *Dashboard {
|
||||||
dash := &Dashboard{}
|
dash := &Dashboard{}
|
||||||
dash.Id = 0
|
|
||||||
dash.Data = make(map[string]interface{})
|
dash.Data = make(map[string]interface{})
|
||||||
dash.Data["title"] = title
|
dash.Data["title"] = title
|
||||||
dash.Title = title
|
dash.Title = title
|
||||||
dash.UpdateSlug()
|
dash.UpdateSlug()
|
||||||
|
|
||||||
return dash
|
return dash
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFromJson(reader io.Reader) (*Dashboard, error) {
|
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
||||||
dash := NewDashboard("temp")
|
dash := &Dashboard{}
|
||||||
jsonParser := json.NewDecoder(reader)
|
dash.Data = cmd.Dashboard
|
||||||
|
dash.Title = dash.Data["title"].(string)
|
||||||
|
dash.AccountId = cmd.AccountId
|
||||||
|
dash.Tags = convertToStringArray(dash.Data["tags"].([]interface{}))
|
||||||
|
dash.UpdateSlug()
|
||||||
|
|
||||||
if err := jsonParser.Decode(&dash.Data); err != nil {
|
if dash.Data["id"] != nil {
|
||||||
return nil, err
|
dash.Id = int64(dash.Data["id"].(float64))
|
||||||
}
|
}
|
||||||
|
|
||||||
return dash, nil
|
return dash
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dash *Dashboard) GetString(prop string) string {
|
func (dash *Dashboard) GetString(prop string) string {
|
||||||
|
|||||||
60
pkg/stores/sqlstore/dashboards.go
Normal file
60
pkg/stores/sqlstore/dashboards.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package sqlstore
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-xorm/xorm"
|
||||||
|
"github.com/torkelo/grafana-pro/pkg/bus"
|
||||||
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
bus.AddHandler("sql", SaveDashboard2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveDashboard2(cmd *m.SaveDashboardCommand) error {
|
||||||
|
return inTransaction(func(sess *xorm.Session) error {
|
||||||
|
dash := cmd.GetDashboardModel()
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if dash.Id == 0 {
|
||||||
|
_, err = sess.Insert(dash)
|
||||||
|
} else {
|
||||||
|
_, err = sess.Id(dash.Id).Update(dash)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Result = dash
|
||||||
|
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDashboard(slug string, accountId int64) (*m.Dashboard, error) {
|
||||||
|
dashboard := m.Dashboard{Slug: slug, AccountId: accountId}
|
||||||
|
has, err := x.Get(&dashboard)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if has == false {
|
||||||
|
return nil, m.ErrDashboardNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return &dashboard, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SearchQuery(query string, accountId int64) ([]*m.SearchResult, error) {
|
||||||
|
sess := x.Limit(100, 0).Where("account_id=?", accountId)
|
||||||
|
sess.Table("Dashboard")
|
||||||
|
|
||||||
|
results := make([]*m.SearchResult, 0)
|
||||||
|
err := sess.Find(&results)
|
||||||
|
|
||||||
|
return results, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteDashboard(slug string, accountId int64) error {
|
||||||
|
sess := x.NewSession()
|
||||||
|
defer sess.Close()
|
||||||
|
|
||||||
|
rawSql := "DELETE FROM Dashboard WHERE account_id=? and slug=?"
|
||||||
|
_, err := sess.Exec(rawSql, accountId, slug)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -36,7 +36,6 @@ func init() {
|
|||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
m.GetDashboard = GetDashboard
|
m.GetDashboard = GetDashboard
|
||||||
m.SaveDashboard = SaveDashboard
|
|
||||||
m.SearchQuery = SearchQuery
|
m.SearchQuery = SearchQuery
|
||||||
m.DeleteDashboard = DeleteDashboard
|
m.DeleteDashboard = DeleteDashboard
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
package sqlstore
|
|
||||||
|
|
||||||
import "github.com/torkelo/grafana-pro/pkg/models"
|
|
||||||
|
|
||||||
func SaveDashboard(dash *models.Dashboard) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
sess := x.NewSession()
|
|
||||||
defer sess.Close()
|
|
||||||
|
|
||||||
if err = sess.Begin(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if dash.Id == 0 {
|
|
||||||
_, err = sess.Insert(dash)
|
|
||||||
} else {
|
|
||||||
_, err = sess.Id(dash.Id).Update(dash)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
sess.Rollback()
|
|
||||||
return err
|
|
||||||
} else if err = sess.Commit(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDashboard(slug string, accountId int64) (*models.Dashboard, error) {
|
|
||||||
|
|
||||||
dashboard := models.Dashboard{Slug: slug, AccountId: accountId}
|
|
||||||
has, err := x.Get(&dashboard)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if has == false {
|
|
||||||
return nil, models.ErrDashboardNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return &dashboard, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func SearchQuery(query string, accountId int64) ([]*models.SearchResult, error) {
|
|
||||||
sess := x.Limit(100, 0).Where("account_id=?", accountId)
|
|
||||||
sess.Table("Dashboard")
|
|
||||||
|
|
||||||
results := make([]*models.SearchResult, 0)
|
|
||||||
err := sess.Find(&results)
|
|
||||||
|
|
||||||
return results, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func DeleteDashboard(slug string, accountId int64) error {
|
|
||||||
sess := x.NewSession()
|
|
||||||
defer sess.Close()
|
|
||||||
|
|
||||||
rawSql := "DELETE FROM Dashboard WHERE account_id=? and slug=?"
|
|
||||||
_, err := sess.Exec(rawSql, accountId, slug)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user