Commit 16f072b3 by bergquist

dashboards as cfg: move saving logic for dashboards into its own service

parent 9cebb23e
...@@ -3,11 +3,12 @@ package api ...@@ -3,11 +3,12 @@ package api
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
//"github.com/grafana/grafana/pkg/services/dashboards"
"os" "os"
"path" "path"
"strings" "strings"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/dashdiffs" "github.com/grafana/grafana/pkg/components/dashdiffs"
...@@ -17,7 +18,6 @@ import ( ...@@ -17,7 +18,6 @@ import (
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/search" "github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
...@@ -125,17 +125,6 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response { ...@@ -125,17 +125,6 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
dash := cmd.GetDashboardModel() dash := cmd.GetDashboardModel()
// dashItem := &dashboards.SaveDashboardItem{
// Dashboard: dash,
// Message: cmd.Message,
// }
// err := dashboards.SaveDashboard(dashItem)
// Check if Title is empty
if dash.Title == "" {
return ApiError(400, m.ErrDashboardTitleEmpty.Error(), nil)
}
if dash.Id == 0 { if dash.Id == 0 {
limitReached, err := middleware.QuotaReached(c, "dashboard") limitReached, err := middleware.QuotaReached(c, "dashboard")
if err != nil { if err != nil {
...@@ -146,17 +135,23 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response { ...@@ -146,17 +135,23 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
} }
} }
validateAlertsCmd := alerting.ValidateDashboardAlertsCommand{ dashItem := &dashboards.SaveDashboardItem{
Dashboard: dash,
Message: cmd.Message,
OrgId: c.OrgId, OrgId: c.OrgId,
UserId: c.UserId, UserId: c.UserId,
Dashboard: dash,
} }
if err := bus.Dispatch(&validateAlertsCmd); err != nil { dashboard, err := dashboards.SaveDashboard(dashItem)
if err == m.ErrDashboardTitleEmpty {
return ApiError(400, m.ErrDashboardTitleEmpty.Error(), nil)
}
if err == m.ErrDashboardContainsInvalidAlertData {
return ApiError(500, "Invalid alert data. Cannot save dashboard", err) return ApiError(500, "Invalid alert data. Cannot save dashboard", err)
} }
err := bus.Dispatch(&cmd)
if err != nil { if err != nil {
if err == m.ErrDashboardWithSameNameExists { if err == m.ErrDashboardWithSameNameExists {
return Json(412, util.DynMap{"status": "name-exists", "message": err.Error()}) return Json(412, util.DynMap{"status": "name-exists", "message": err.Error()})
...@@ -178,18 +173,12 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response { ...@@ -178,18 +173,12 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
return ApiError(500, "Failed to save dashboard", err) return ApiError(500, "Failed to save dashboard", err)
} }
alertCmd := alerting.UpdateDashboardAlertsCommand{ if err == m.ErrDashboardFailedToUpdateAlertData {
OrgId: c.OrgId, return ApiError(500, "Invalid alert data. Cannot save dashboard", err)
UserId: c.UserId,
Dashboard: cmd.Result,
}
if err := bus.Dispatch(&alertCmd); err != nil {
return ApiError(500, "Failed to save alerts", err)
} }
c.TimeRequest(metrics.M_Api_Dashboard_Save) c.TimeRequest(metrics.M_Api_Dashboard_Save)
return Json(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version}) return Json(200, util.DynMap{"status": "success", "slug": dashboard.Slug, "version": dashboard.Version})
} }
func canEditDashboard(role m.RoleType) bool { func canEditDashboard(role m.RoleType) bool {
......
...@@ -17,6 +17,7 @@ var ( ...@@ -17,6 +17,7 @@ var (
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else") ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
ErrDashboardTitleEmpty = errors.New("Dashboard title cannot be empty") ErrDashboardTitleEmpty = errors.New("Dashboard title cannot be empty")
ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard") ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard")
ErrDashboardFailedToUpdateAlertData = errors.New("Failed to save alert data")
) )
type UpdatePluginDashboardError struct { type UpdatePluginDashboardError struct {
......
...@@ -22,11 +22,11 @@ type SaveDashboardItem struct { ...@@ -22,11 +22,11 @@ type SaveDashboardItem struct {
Dashboard *models.Dashboard Dashboard *models.Dashboard
} }
func SaveDashboard(json *SaveDashboardItem) error { func SaveDashboard(json *SaveDashboardItem) (*models.Dashboard, error) {
dashboard := json.Dashboard dashboard := json.Dashboard
if dashboard.Title == "" { if dashboard.Title == "" {
return models.ErrDashboardTitleEmpty return nil, models.ErrDashboardTitleEmpty
} }
validateAlertsCmd := alerting.ValidateDashboardAlertsCommand{ validateAlertsCmd := alerting.ValidateDashboardAlertsCommand{
...@@ -35,7 +35,7 @@ func SaveDashboard(json *SaveDashboardItem) error { ...@@ -35,7 +35,7 @@ func SaveDashboard(json *SaveDashboardItem) error {
} }
if err := bus.Dispatch(&validateAlertsCmd); err != nil { if err := bus.Dispatch(&validateAlertsCmd); err != nil {
return models.ErrDashboardContainsInvalidAlertData return nil, models.ErrDashboardContainsInvalidAlertData
} }
cmd := models.SaveDashboardCommand{ cmd := models.SaveDashboardCommand{
...@@ -51,7 +51,7 @@ func SaveDashboard(json *SaveDashboardItem) error { ...@@ -51,7 +51,7 @@ func SaveDashboard(json *SaveDashboardItem) error {
err := bus.Dispatch(&cmd) err := bus.Dispatch(&cmd)
if err != nil { if err != nil {
return err return nil, err
} }
alertCmd := alerting.UpdateDashboardAlertsCommand{ alertCmd := alerting.UpdateDashboardAlertsCommand{
...@@ -60,8 +60,8 @@ func SaveDashboard(json *SaveDashboardItem) error { ...@@ -60,8 +60,8 @@ func SaveDashboard(json *SaveDashboardItem) error {
} }
if err := bus.Dispatch(&alertCmd); err != nil { if err := bus.Dispatch(&alertCmd); err != nil {
return err return nil, models.ErrDashboardFailedToUpdateAlertData
} }
return nil return cmd.Result, nil
} }
...@@ -96,7 +96,8 @@ func (fr *fileReader) walkFolder() error { ...@@ -96,7 +96,8 @@ func (fr *fileReader) walkFolder() error {
if err == models.ErrDashboardNotFound { if err == models.ErrDashboardNotFound {
fr.log.Debug("saving new dashboard", "file", path) fr.log.Debug("saving new dashboard", "file", path)
return dashboards.SaveDashboard(dash) _, err = dashboards.SaveDashboard(dash)
return err
} }
if err != nil { if err != nil {
...@@ -109,7 +110,8 @@ func (fr *fileReader) walkFolder() error { ...@@ -109,7 +110,8 @@ func (fr *fileReader) walkFolder() error {
} }
fr.log.Debug("no dashboard in cache. loading dashboard from disk into database.", "file", path) fr.log.Debug("no dashboard in cache. loading dashboard from disk into database.", "file", path)
return dashboards.SaveDashboard(dash) _, err = dashboards.SaveDashboard(dash)
return err
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment