Commit f5eac2e9 by bergquist

dashboards as cfg: expose dashboard service as interface

parent 16f072b3
...@@ -142,7 +142,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response { ...@@ -142,7 +142,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
UserId: c.UserId, UserId: c.UserId,
} }
dashboard, err := dashboards.SaveDashboard(dashItem) dashboard, err := dashboards.GetRepository().SaveDashboard(dashItem)
if err == m.ErrDashboardTitleEmpty { if err == m.ErrDashboardTitleEmpty {
return ApiError(400, m.ErrDashboardTitleEmpty.Error(), nil) return ApiError(400, m.ErrDashboardTitleEmpty.Error(), nil)
......
package dashboards package dashboards
import ( import (
"time"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting" "github.com/grafana/grafana/pkg/services/alerting"
"time"
) )
type Repository interface { type Repository interface {
SaveDashboard(*SaveDashboardItem) error SaveDashboard(*SaveDashboardItem) (*models.Dashboard, error)
}
var repositoryInstance Repository
func GetRepository() Repository {
return repositoryInstance
}
func SetRepository(rep Repository) {
repositoryInstance = rep
} }
type SaveDashboardItem struct { type SaveDashboardItem struct {
...@@ -22,7 +33,9 @@ type SaveDashboardItem struct { ...@@ -22,7 +33,9 @@ type SaveDashboardItem struct {
Dashboard *models.Dashboard Dashboard *models.Dashboard
} }
func SaveDashboard(json *SaveDashboardItem) (*models.Dashboard, error) { type dashboardRepository struct{}
func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.Dashboard, error) {
dashboard := json.Dashboard dashboard := json.Dashboard
if dashboard.Title == "" { if dashboard.Title == "" {
......
...@@ -3,12 +3,13 @@ package dashboards ...@@ -3,12 +3,13 @@ package dashboards
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/grafana/grafana/pkg/services/dashboards"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
...@@ -21,6 +22,7 @@ type fileReader struct { ...@@ -21,6 +22,7 @@ type fileReader struct {
Path string Path string
log log.Logger log log.Logger
dashboardCache *dashboardCache dashboardCache *dashboardCache
dashboardRepo dashboards.Repository
} }
func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) { func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
...@@ -37,6 +39,7 @@ func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade ...@@ -37,6 +39,7 @@ func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
Cfg: cfg, Cfg: cfg,
Path: path, Path: path,
log: log, log: log,
dashboardRepo: dashboards.GetRepository(),
dashboardCache: newDashboardCache(), dashboardCache: newDashboardCache(),
}, nil }, nil
} }
...@@ -96,7 +99,7 @@ func (fr *fileReader) walkFolder() error { ...@@ -96,7 +99,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)
_, err = dashboards.SaveDashboard(dash) _, err = fr.dashboardRepo.SaveDashboard(dash)
return err return err
} }
...@@ -110,7 +113,7 @@ func (fr *fileReader) walkFolder() error { ...@@ -110,7 +113,7 @@ 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)
_, err = dashboards.SaveDashboard(dash) _, err = fr.dashboardRepo.SaveDashboard(dash)
return err 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