Commit 74e12c26 by bergquist

dashboards as cfg: move dashboard saving into its own service

parent 09cb0f37
package dashboards
import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"time"
)
type Repository interface {
SaveDashboard(*SaveDashboardItem) error
}
type SaveDashboardItem struct {
TitleLower string
OrgId int64
Folder string
ModTime time.Time
UserId int64
Message string
Overwrite bool
Dashboard *models.Dashboard
}
func SaveDashboard(json *SaveDashboardItem) error {
dashboard := json.Dashboard
if dashboard.Title == "" {
return models.ErrDashboardTitleEmpty
}
validateAlertsCmd := alerting.ValidateDashboardAlertsCommand{
OrgId: json.OrgId,
Dashboard: dashboard,
}
if err := bus.Dispatch(&validateAlertsCmd); err != nil {
return models.ErrDashboardContainsInvalidAlertData
}
cmd := models.SaveDashboardCommand{
Dashboard: dashboard.Data,
Message: json.Message,
OrgId: json.OrgId,
Overwrite: json.Overwrite,
}
if !json.ModTime.IsZero() {
cmd.UpdatedAt = json.ModTime
}
err := bus.Dispatch(&cmd)
if err != nil {
return err
}
alertCmd := alerting.UpdateDashboardAlertsCommand{
OrgId: json.OrgId,
Dashboard: cmd.Result,
}
if err := bus.Dispatch(&alertCmd); err != nil {
return err
}
return nil
}
...@@ -3,7 +3,7 @@ package dashboard ...@@ -3,7 +3,7 @@ package dashboard
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/grafana/grafana/pkg/services/alerting" "github.com/grafana/grafana/pkg/services/dashboards"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
...@@ -96,7 +96,7 @@ func (fr *fileReader) walkFolder() error { ...@@ -96,7 +96,7 @@ 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 fr.saveDashboard(dash) return dashboards.SaveDashboard(dash)
} }
if err != nil { if err != nil {
...@@ -109,11 +109,11 @@ func (fr *fileReader) walkFolder() error { ...@@ -109,11 +109,11 @@ 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 fr.saveDashboard(dash) return dashboards.SaveDashboard(dash)
}) })
} }
func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error) { func (fr *fileReader) readDashboardFromFile(path string) (*dashboards.SaveDashboardItem, error) {
reader, err := os.Open(path) reader, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -139,44 +139,3 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error) ...@@ -139,44 +139,3 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error)
return dash, nil return dash, nil
} }
func (fr *fileReader) saveDashboard(json *DashboardJson) error {
dashboard := json.Dashboard
if dashboard.Title == "" {
return models.ErrDashboardTitleEmpty
}
validateAlertsCmd := alerting.ValidateDashboardAlertsCommand{
OrgId: json.OrgId,
Dashboard: dashboard,
}
if err := bus.Dispatch(&validateAlertsCmd); err != nil {
return models.ErrDashboardContainsInvalidAlertData
}
cmd := models.SaveDashboardCommand{
Dashboard: dashboard.Data,
Message: "Dashboard created from file.",
OrgId: json.OrgId,
Overwrite: true,
UpdatedAt: json.ModTime,
}
err := bus.Dispatch(&cmd)
if err != nil {
return err
}
alertCmd := alerting.UpdateDashboardAlertsCommand{
OrgId: json.OrgId,
Dashboard: cmd.Result,
}
if err := bus.Dispatch(&alertCmd); err != nil {
return err
}
return nil
}
...@@ -2,6 +2,7 @@ package dashboard ...@@ -2,6 +2,7 @@ package dashboard
import ( import (
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboards"
"strings" "strings"
"sync" "sync"
"time" "time"
...@@ -18,42 +19,34 @@ type DashboardsAsConfig struct { ...@@ -18,42 +19,34 @@ type DashboardsAsConfig struct {
Options map[string]interface{} `json:"options" yaml:"options"` Options map[string]interface{} `json:"options" yaml:"options"`
} }
type DashboardJson struct {
TitleLower string
OrgId int64
Folder string
ModTime time.Time
Dashboard *models.Dashboard
}
type dashboardCache struct { type dashboardCache struct {
mutex *sync.Mutex mutex *sync.Mutex
dashboards map[string]*DashboardJson dashboards map[string]*dashboards.SaveDashboardItem
} }
func newDashboardCache() *dashboardCache { func newDashboardCache() *dashboardCache {
return &dashboardCache{ return &dashboardCache{
dashboards: map[string]*DashboardJson{}, dashboards: map[string]*dashboards.SaveDashboardItem{},
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
} }
} }
func (dc *dashboardCache) addCache(key string, json *DashboardJson) { func (dc *dashboardCache) addCache(key string, json *dashboards.SaveDashboardItem) {
dc.mutex.Lock() dc.mutex.Lock()
defer dc.mutex.Unlock() defer dc.mutex.Unlock()
dc.dashboards[key] = json dc.dashboards[key] = json
} }
func (dc *dashboardCache) getCache(key string) (*DashboardJson, bool) { func (dc *dashboardCache) getCache(key string) (*dashboards.SaveDashboardItem, bool) {
dc.mutex.Lock() dc.mutex.Lock()
defer dc.mutex.Unlock() defer dc.mutex.Unlock()
v, exist := dc.dashboards[key] v, exist := dc.dashboards[key]
return v, exist return v, exist
} }
func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *DashboardsAsConfig) (*DashboardJson, error) { func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *DashboardsAsConfig) (*dashboards.SaveDashboardItem, error) {
dash := &DashboardJson{} dash := &dashboards.SaveDashboardItem{}
dash.Dashboard = models.NewDashboardFromJson(data) dash.Dashboard = models.NewDashboardFromJson(data)
dash.TitleLower = strings.ToLower(dash.Dashboard.Title) dash.TitleLower = strings.ToLower(dash.Dashboard.Title)
dash.ModTime = lastModified dash.ModTime = lastModified
......
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