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
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/dashboards"
"os"
"path/filepath"
"strings"
......@@ -96,7 +96,7 @@ func (fr *fileReader) walkFolder() error {
if err == models.ErrDashboardNotFound {
fr.log.Debug("saving new dashboard", "file", path)
return fr.saveDashboard(dash)
return dashboards.SaveDashboard(dash)
}
if err != nil {
......@@ -109,11 +109,11 @@ func (fr *fileReader) walkFolder() error {
}
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)
if err != nil {
return nil, err
......@@ -139,44 +139,3 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error)
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
import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboards"
"strings"
"sync"
"time"
......@@ -18,42 +19,34 @@ type DashboardsAsConfig struct {
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 {
mutex *sync.Mutex
dashboards map[string]*DashboardJson
dashboards map[string]*dashboards.SaveDashboardItem
}
func newDashboardCache() *dashboardCache {
return &dashboardCache{
dashboards: map[string]*DashboardJson{},
dashboards: map[string]*dashboards.SaveDashboardItem{},
mutex: &sync.Mutex{},
}
}
func (dc *dashboardCache) addCache(key string, json *DashboardJson) {
func (dc *dashboardCache) addCache(key string, json *dashboards.SaveDashboardItem) {
dc.mutex.Lock()
defer dc.mutex.Unlock()
dc.dashboards[key] = json
}
func (dc *dashboardCache) getCache(key string) (*DashboardJson, bool) {
func (dc *dashboardCache) getCache(key string) (*dashboards.SaveDashboardItem, bool) {
dc.mutex.Lock()
defer dc.mutex.Unlock()
v, exist := dc.dashboards[key]
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.TitleLower = strings.ToLower(dash.Dashboard.Title)
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