Commit 16f072b3 by bergquist

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

parent 9cebb23e
......@@ -3,11 +3,12 @@ package api
import (
"encoding/json"
"fmt"
//"github.com/grafana/grafana/pkg/services/dashboards"
"os"
"path"
"strings"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/dashdiffs"
......@@ -17,7 +18,6 @@ import (
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
"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/setting"
"github.com/grafana/grafana/pkg/util"
......@@ -125,17 +125,6 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
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 {
limitReached, err := middleware.QuotaReached(c, "dashboard")
if err != nil {
......@@ -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,
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)
}
err := bus.Dispatch(&cmd)
if err != nil {
if err == m.ErrDashboardWithSameNameExists {
return Json(412, util.DynMap{"status": "name-exists", "message": err.Error()})
......@@ -178,18 +173,12 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
return ApiError(500, "Failed to save dashboard", err)
}
alertCmd := alerting.UpdateDashboardAlertsCommand{
OrgId: c.OrgId,
UserId: c.UserId,
Dashboard: cmd.Result,
}
if err := bus.Dispatch(&alertCmd); err != nil {
return ApiError(500, "Failed to save alerts", err)
if err == m.ErrDashboardFailedToUpdateAlertData {
return ApiError(500, "Invalid alert data. Cannot save dashboard", err)
}
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 {
......
......@@ -17,6 +17,7 @@ var (
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
ErrDashboardTitleEmpty = errors.New("Dashboard title cannot be empty")
ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard")
ErrDashboardFailedToUpdateAlertData = errors.New("Failed to save alert data")
)
type UpdatePluginDashboardError struct {
......
......@@ -22,11 +22,11 @@ type SaveDashboardItem struct {
Dashboard *models.Dashboard
}
func SaveDashboard(json *SaveDashboardItem) error {
func SaveDashboard(json *SaveDashboardItem) (*models.Dashboard, error) {
dashboard := json.Dashboard
if dashboard.Title == "" {
return models.ErrDashboardTitleEmpty
return nil, models.ErrDashboardTitleEmpty
}
validateAlertsCmd := alerting.ValidateDashboardAlertsCommand{
......@@ -35,7 +35,7 @@ func SaveDashboard(json *SaveDashboardItem) error {
}
if err := bus.Dispatch(&validateAlertsCmd); err != nil {
return models.ErrDashboardContainsInvalidAlertData
return nil, models.ErrDashboardContainsInvalidAlertData
}
cmd := models.SaveDashboardCommand{
......@@ -51,7 +51,7 @@ func SaveDashboard(json *SaveDashboardItem) error {
err := bus.Dispatch(&cmd)
if err != nil {
return err
return nil, err
}
alertCmd := alerting.UpdateDashboardAlertsCommand{
......@@ -60,8 +60,8 @@ func SaveDashboard(json *SaveDashboardItem) error {
}
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 {
if err == models.ErrDashboardNotFound {
fr.log.Debug("saving new dashboard", "file", path)
return dashboards.SaveDashboard(dash)
_, err = dashboards.SaveDashboard(dash)
return err
}
if err != nil {
......@@ -109,7 +110,8 @@ func (fr *fileReader) walkFolder() error {
}
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