Commit 288cc355 by bergquist

dashboards as cfg: use gocache for caching

parent f5eac2e9
...@@ -26,7 +26,7 @@ type SaveDashboardItem struct { ...@@ -26,7 +26,7 @@ type SaveDashboardItem struct {
TitleLower string TitleLower string
OrgId int64 OrgId int64
Folder string Folder string
ModTime time.Time UpdatedAt time.Time
UserId int64 UserId int64
Message string Message string
Overwrite bool Overwrite bool
...@@ -56,10 +56,11 @@ func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.D ...@@ -56,10 +56,11 @@ func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.D
Message: json.Message, Message: json.Message,
OrgId: json.OrgId, OrgId: json.OrgId,
Overwrite: json.Overwrite, Overwrite: json.Overwrite,
UserId: json.UserId,
} }
if !json.ModTime.IsZero() { if !json.UpdatedAt.IsZero() {
cmd.UpdatedAt = json.ModTime cmd.UpdatedAt = json.UpdatedAt
} }
err := bus.Dispatch(&cmd) err := bus.Dispatch(&cmd)
...@@ -69,6 +70,7 @@ func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.D ...@@ -69,6 +70,7 @@ func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.D
alertCmd := alerting.UpdateDashboardAlertsCommand{ alertCmd := alerting.UpdateDashboardAlertsCommand{
OrgId: json.OrgId, OrgId: json.OrgId,
UserId: json.UserId,
Dashboard: cmd.Result, Dashboard: cmd.Result,
} }
......
...@@ -15,14 +15,15 @@ import ( ...@@ -15,14 +15,15 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
gocache "github.com/patrickmn/go-cache"
) )
type fileReader struct { type fileReader struct {
Cfg *DashboardsAsConfig Cfg *DashboardsAsConfig
Path string Path string
log log.Logger log log.Logger
dashboardCache *dashboardCache
dashboardRepo dashboards.Repository dashboardRepo dashboards.Repository
cache *gocache.Cache
} }
func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) { func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
...@@ -40,10 +41,28 @@ func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade ...@@ -40,10 +41,28 @@ func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
Path: path, Path: path,
log: log, log: log,
dashboardRepo: dashboards.GetRepository(), dashboardRepo: dashboards.GetRepository(),
dashboardCache: newDashboardCache(), cache: gocache.New(5*time.Minute, 10*time.Minute),
}, nil }, nil
} }
func (fr *fileReader) addCache(key string, json *dashboards.SaveDashboardItem) {
fr.cache.Add(key, json, time.Minute*10)
}
func (fr *fileReader) getCache(key string) (*dashboards.SaveDashboardItem, bool) {
obj, exist := fr.cache.Get(key)
if !exist {
return nil, exist
}
dash, ok := obj.(*dashboards.SaveDashboardItem)
if !ok {
return nil, ok
}
return dash, ok
}
func (fr *fileReader) ReadAndListen(ctx context.Context) error { func (fr *fileReader) ReadAndListen(ctx context.Context) error {
ticker := time.NewTicker(time.Second * 5) ticker := time.NewTicker(time.Second * 5)
...@@ -83,8 +102,8 @@ func (fr *fileReader) walkFolder() error { ...@@ -83,8 +102,8 @@ func (fr *fileReader) walkFolder() error {
return nil return nil
} }
cachedDashboard, exist := fr.dashboardCache.getCache(path) cachedDashboard, exist := fr.getCache(path)
if exist && cachedDashboard.ModTime == f.ModTime() { if exist && cachedDashboard.UpdatedAt == f.ModTime() {
return nil return nil
} }
...@@ -140,7 +159,7 @@ func (fr *fileReader) readDashboardFromFile(path string) (*dashboards.SaveDashbo ...@@ -140,7 +159,7 @@ func (fr *fileReader) readDashboardFromFile(path string) (*dashboards.SaveDashbo
return nil, err return nil, err
} }
fr.dashboardCache.addCache(path, dash) fr.addCache(path, dash)
return dash, nil return dash, nil
} }
package dashboards package dashboards
import ( import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"os" "os"
"testing" "testing"
"time" "time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/log"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
...@@ -26,9 +27,7 @@ func TestDashboardFileReader(t *testing.T) { ...@@ -26,9 +27,7 @@ func TestDashboardFileReader(t *testing.T) {
fakeRepo = &fakeDashboardRepo{} fakeRepo = &fakeDashboardRepo{}
bus.AddHandler("test", mockGetDashboardQuery) bus.AddHandler("test", mockGetDashboardQuery)
bus.AddHandler("test", mockValidateDashboardAlertsCommand) dashboards.SetRepository(fakeRepo)
bus.AddHandler("test", mockSaveDashboardCommand)
bus.AddHandler("test", mockUpdateDashboardAlertsCommand)
logger := log.New("test.logger") logger := log.New("test.logger")
cfg := &DashboardsAsConfig{ cfg := &DashboardsAsConfig{
...@@ -117,10 +116,15 @@ func TestDashboardFileReader(t *testing.T) { ...@@ -117,10 +116,15 @@ func TestDashboardFileReader(t *testing.T) {
} }
type fakeDashboardRepo struct { type fakeDashboardRepo struct {
inserted []*models.SaveDashboardCommand inserted []*dashboards.SaveDashboardItem
getDashboard []*models.Dashboard getDashboard []*models.Dashboard
} }
func (repo *fakeDashboardRepo) SaveDashboard(json *dashboards.SaveDashboardItem) (*models.Dashboard, error) {
repo.inserted = append(repo.inserted, json)
return json.Dashboard, nil
}
func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error { func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error {
for _, d := range fakeRepo.getDashboard { for _, d := range fakeRepo.getDashboard {
if d.Slug == cmd.Slug { if d.Slug == cmd.Slug {
...@@ -131,16 +135,3 @@ func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error { ...@@ -131,16 +135,3 @@ func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error {
return models.ErrDashboardNotFound return models.ErrDashboardNotFound
} }
func mockValidateDashboardAlertsCommand(cmd *alerting.ValidateDashboardAlertsCommand) error {
return nil
}
func mockSaveDashboardCommand(cmd *models.SaveDashboardCommand) error {
fakeRepo.inserted = append(fakeRepo.inserted, cmd)
return nil
}
func mockUpdateDashboardAlertsCommand(cmd *alerting.UpdateDashboardAlertsCommand) error {
return nil
}
package dashboards package dashboards
import ( import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboards"
"strings" "strings"
"sync"
"time" "time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
) )
...@@ -19,37 +19,12 @@ type DashboardsAsConfig struct { ...@@ -19,37 +19,12 @@ type DashboardsAsConfig struct {
Options map[string]interface{} `json:"options" yaml:"options"` Options map[string]interface{} `json:"options" yaml:"options"`
} }
type dashboardCache struct {
mutex *sync.Mutex
dashboards map[string]*dashboards.SaveDashboardItem
}
func newDashboardCache() *dashboardCache {
return &dashboardCache{
dashboards: map[string]*dashboards.SaveDashboardItem{},
mutex: &sync.Mutex{},
}
}
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) (*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) (*dashboards.SaveDashboardItem, error) { func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *DashboardsAsConfig) (*dashboards.SaveDashboardItem, error) {
dash := &dashboards.SaveDashboardItem{} 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.UpdatedAt = lastModified
dash.OrgId = cfg.OrgId dash.OrgId = cfg.OrgId
dash.Folder = cfg.Folder dash.Folder = cfg.Folder
dash.Dashboard.Data.Set("editable", cfg.Editable) dash.Dashboard.Data.Set("editable", cfg.Editable)
......
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