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
f4078e19
Commit
f4078e19
authored
Dec 27, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: for skipping with hidden folders
parent
11081010
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
23 deletions
+92
-23
pkg/services/provisioning/dashboards/file_reader.go
+33
-22
pkg/services/provisioning/dashboards/file_reader_test.go
+59
-1
No files found.
pkg/services/provisioning/dashboards/file_reader.go
View file @
f4078e19
...
...
@@ -18,12 +18,18 @@ import (
gocache
"github.com/patrickmn/go-cache"
)
var
(
checkDiskForChangesInterval
time
.
Duration
=
time
.
Second
*
3
)
type
fileReader
struct
{
Cfg
*
DashboardsAsConfig
Path
string
FolderId
int64
log
log
.
Logger
dashboardRepo
dashboards
.
Repository
cache
*
gocache
.
Cache
createWalkFunc
func
(
fr
*
fileReader
)
filepath
.
WalkFunc
}
func
NewDashboardFileReader
(
cfg
*
DashboardsAsConfig
,
log
log
.
Logger
)
(
*
fileReader
,
error
)
{
...
...
@@ -42,29 +48,12 @@ func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
log
:
log
,
dashboardRepo
:
dashboards
.
GetRepository
(),
cache
:
gocache
.
New
(
5
*
time
.
Minute
,
30
*
time
.
Minute
),
createWalkFunc
:
createWalkFn
,
},
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
*
3
)
ticker
:=
time
.
NewTicker
(
checkDiskForChangesInterval
)
if
err
:=
fr
.
walkFolder
();
err
!=
nil
{
fr
.
log
.
Error
(
"failed to search for dashboards"
,
"error"
,
err
)
...
...
@@ -95,7 +84,11 @@ func (fr *fileReader) walkFolder() error {
}
}
return
filepath
.
Walk
(
fr
.
Path
,
func
(
path
string
,
fileInfo
os
.
FileInfo
,
err
error
)
error
{
return
filepath
.
Walk
(
fr
.
Path
,
fr
.
createWalkFunc
(
fr
))
//omg this is so ugly :(
}
func
createWalkFn
(
fr
*
fileReader
)
filepath
.
WalkFunc
{
return
func
(
path
string
,
fileInfo
os
.
FileInfo
,
err
error
)
error
{
if
err
!=
nil
{
return
err
}
...
...
@@ -147,7 +140,7 @@ func (fr *fileReader) walkFolder() error {
fr
.
log
.
Debug
(
"loading dashboard from disk into database."
,
"file"
,
path
)
_
,
err
=
fr
.
dashboardRepo
.
SaveDashboard
(
dash
)
return
err
}
)
}
}
func
(
fr
*
fileReader
)
readDashboardFromFile
(
path
string
)
(
*
dashboards
.
SaveDashboardItem
,
error
)
{
...
...
@@ -172,7 +165,25 @@ func (fr *fileReader) readDashboardFromFile(path string) (*dashboards.SaveDashbo
return
nil
,
err
}
fr
.
addCache
(
path
,
dash
)
fr
.
add
Dashboard
Cache
(
path
,
dash
)
return
dash
,
nil
}
func
(
fr
*
fileReader
)
addDashboardCache
(
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
}
pkg/services/provisioning/dashboards/file_reader_test.go
View file @
f4078e19
...
...
@@ -2,6 +2,7 @@ package dashboards
import
(
"os"
"path/filepath"
"testing"
"time"
...
...
@@ -22,7 +23,9 @@ var (
)
func
TestDashboardFileReader
(
t
*
testing
.
T
)
{
Convey
(
"Reading dashboards from disk"
,
t
,
func
()
{
Convey
(
"Dashboard file reader"
,
t
,
func
()
{
Convey
(
"Reading dashboards from disk"
,
func
()
{
bus
.
ClearBusHandlers
()
fakeRepo
=
&
fakeDashboardRepo
{}
...
...
@@ -113,6 +116,61 @@ func TestDashboardFileReader(t *testing.T) {
So
(
err
,
ShouldBeNil
)
})
})
Convey
(
"Walking"
,
func
()
{
cfg
:=
&
DashboardsAsConfig
{
Name
:
"Default"
,
Type
:
"file"
,
OrgId
:
1
,
Folder
:
""
,
Options
:
map
[
string
]
interface
{}{
"folder"
:
defaultDashboards
,
},
}
reader
,
err
:=
NewDashboardFileReader
(
cfg
,
log
.
New
(
"test-logger"
))
So
(
err
,
ShouldBeNil
)
Convey
(
"should skip dirs that starts with ."
,
func
()
{
shouldSkip
:=
reader
.
createWalkFunc
(
reader
)(
"path"
,
&
FakeFileInfo
{
isDirectory
:
true
,
name
:
".folder"
},
nil
)
So
(
shouldSkip
,
ShouldEqual
,
filepath
.
SkipDir
)
})
Convey
(
"should keep walking if file is not .json"
,
func
()
{
shouldSkip
:=
reader
.
createWalkFunc
(
reader
)(
"path"
,
&
FakeFileInfo
{
isDirectory
:
true
,
name
:
"folder"
},
nil
)
So
(
shouldSkip
,
ShouldBeNil
)
})
})
})
}
type
FakeFileInfo
struct
{
isDirectory
bool
name
string
}
func
(
ffi
*
FakeFileInfo
)
IsDir
()
bool
{
return
ffi
.
isDirectory
}
func
(
ffi
FakeFileInfo
)
Size
()
int64
{
return
1
}
func
(
ffi
FakeFileInfo
)
Mode
()
os
.
FileMode
{
return
0777
}
func
(
ffi
FakeFileInfo
)
Name
()
string
{
return
ffi
.
name
}
func
(
ffi
FakeFileInfo
)
ModTime
()
time
.
Time
{
return
time
.
Time
{}
}
func
(
ffi
FakeFileInfo
)
Sys
()
interface
{}
{
return
nil
}
type
fakeDashboardRepo
struct
{
...
...
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