mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 18:34:52 -06:00
dashboards as cfg: use gocache for caching
This commit is contained in:
parent
f5eac2e91d
commit
288cc35529
@ -26,7 +26,7 @@ type SaveDashboardItem struct {
|
||||
TitleLower string
|
||||
OrgId int64
|
||||
Folder string
|
||||
ModTime time.Time
|
||||
UpdatedAt time.Time
|
||||
UserId int64
|
||||
Message string
|
||||
Overwrite bool
|
||||
@ -56,10 +56,11 @@ func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.D
|
||||
Message: json.Message,
|
||||
OrgId: json.OrgId,
|
||||
Overwrite: json.Overwrite,
|
||||
UserId: json.UserId,
|
||||
}
|
||||
|
||||
if !json.ModTime.IsZero() {
|
||||
cmd.UpdatedAt = json.ModTime
|
||||
if !json.UpdatedAt.IsZero() {
|
||||
cmd.UpdatedAt = json.UpdatedAt
|
||||
}
|
||||
|
||||
err := bus.Dispatch(&cmd)
|
||||
@ -69,6 +70,7 @@ func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.D
|
||||
|
||||
alertCmd := alerting.UpdateDashboardAlertsCommand{
|
||||
OrgId: json.OrgId,
|
||||
UserId: json.UserId,
|
||||
Dashboard: cmd.Result,
|
||||
}
|
||||
|
||||
|
@ -15,14 +15,15 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
gocache "github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
type fileReader struct {
|
||||
Cfg *DashboardsAsConfig
|
||||
Path string
|
||||
log log.Logger
|
||||
dashboardCache *dashboardCache
|
||||
dashboardRepo dashboards.Repository
|
||||
Cfg *DashboardsAsConfig
|
||||
Path string
|
||||
log log.Logger
|
||||
dashboardRepo dashboards.Repository
|
||||
cache *gocache.Cache
|
||||
}
|
||||
|
||||
func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
|
||||
@ -36,14 +37,32 @@ func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
|
||||
}
|
||||
|
||||
return &fileReader{
|
||||
Cfg: cfg,
|
||||
Path: path,
|
||||
log: log,
|
||||
dashboardRepo: dashboards.GetRepository(),
|
||||
dashboardCache: newDashboardCache(),
|
||||
Cfg: cfg,
|
||||
Path: path,
|
||||
log: log,
|
||||
dashboardRepo: dashboards.GetRepository(),
|
||||
cache: gocache.New(5*time.Minute, 10*time.Minute),
|
||||
}, 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 {
|
||||
ticker := time.NewTicker(time.Second * 5)
|
||||
|
||||
@ -83,8 +102,8 @@ func (fr *fileReader) walkFolder() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
cachedDashboard, exist := fr.dashboardCache.getCache(path)
|
||||
if exist && cachedDashboard.ModTime == f.ModTime() {
|
||||
cachedDashboard, exist := fr.getCache(path)
|
||||
if exist && cachedDashboard.UpdatedAt == f.ModTime() {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -140,7 +159,7 @@ func (fr *fileReader) readDashboardFromFile(path string) (*dashboards.SaveDashbo
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fr.dashboardCache.addCache(path, dash)
|
||||
fr.addCache(path, dash)
|
||||
|
||||
return dash, nil
|
||||
}
|
||||
|
@ -1,13 +1,14 @@
|
||||
package dashboards
|
||||
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/alerting"
|
||||
"os"
|
||||
"testing"
|
||||
"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/smartystreets/goconvey/convey"
|
||||
)
|
||||
@ -26,9 +27,7 @@ func TestDashboardFileReader(t *testing.T) {
|
||||
fakeRepo = &fakeDashboardRepo{}
|
||||
|
||||
bus.AddHandler("test", mockGetDashboardQuery)
|
||||
bus.AddHandler("test", mockValidateDashboardAlertsCommand)
|
||||
bus.AddHandler("test", mockSaveDashboardCommand)
|
||||
bus.AddHandler("test", mockUpdateDashboardAlertsCommand)
|
||||
dashboards.SetRepository(fakeRepo)
|
||||
logger := log.New("test.logger")
|
||||
|
||||
cfg := &DashboardsAsConfig{
|
||||
@ -117,10 +116,15 @@ func TestDashboardFileReader(t *testing.T) {
|
||||
}
|
||||
|
||||
type fakeDashboardRepo struct {
|
||||
inserted []*models.SaveDashboardCommand
|
||||
inserted []*dashboards.SaveDashboardItem
|
||||
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 {
|
||||
for _, d := range fakeRepo.getDashboard {
|
||||
if d.Slug == cmd.Slug {
|
||||
@ -131,16 +135,3 @@ func mockGetDashboardQuery(cmd *models.GetDashboardQuery) error {
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
package dashboards
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
@ -19,37 +19,12 @@ type DashboardsAsConfig struct {
|
||||
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) {
|
||||
|
||||
dash := &dashboards.SaveDashboardItem{}
|
||||
dash.Dashboard = models.NewDashboardFromJson(data)
|
||||
dash.TitleLower = strings.ToLower(dash.Dashboard.Title)
|
||||
dash.ModTime = lastModified
|
||||
dash.UpdatedAt = lastModified
|
||||
dash.OrgId = cfg.OrgId
|
||||
dash.Folder = cfg.Folder
|
||||
dash.Dashboard.Data.Set("editable", cfg.Editable)
|
||||
|
Loading…
Reference in New Issue
Block a user