Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
288cc355
Commit
288cc355
authored
Dec 01, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dashboards as cfg: use gocache for caching
parent
f5eac2e9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
65 deletions
+52
-65
pkg/services/dashboards/dashboards.go
+5
-3
pkg/services/provisioning/dashboards/file_reader.go
+32
-13
pkg/services/provisioning/dashboards/file_reader_test.go
+11
-20
pkg/services/provisioning/dashboards/types.go
+4
-29
No files found.
pkg/services/dashboards/dashboards.go
View file @
288cc355
...
...
@@ -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
,
}
...
...
pkg/services/provisioning/dashboards/file_reader.go
View file @
288cc355
...
...
@@ -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
dashboard
Cache
*
dashboardCache
dashboardRepo
dashboards
.
Repository
Cfg
*
DashboardsAsConfig
Path
string
log
log
.
Logger
dashboard
Repo
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
}
pkg/services/provisioning/dashboards/file_reader_test.go
View file @
288cc355
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
}
pkg/services/provisioning/dashboards/types.go
View file @
288cc355
package
dashboards
import
(
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboards"
"strings"
"sync"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboards"
"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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment