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
74e12c26
Commit
74e12c26
authored
Nov 30, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dashboards as cfg: move dashboard saving into its own service
parent
09cb0f37
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
59 deletions
+78
-59
pkg/services/dashboards/dashboards.go
+67
-0
pkg/services/provisioning/dashboard/file_reader.go
+4
-45
pkg/services/provisioning/dashboard/types.go
+7
-14
No files found.
pkg/services/dashboards/dashboards.go
0 → 100644
View file @
74e12c26
package
dashboards
import
(
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
"time"
)
type
Repository
interface
{
SaveDashboard
(
*
SaveDashboardItem
)
error
}
type
SaveDashboardItem
struct
{
TitleLower
string
OrgId
int64
Folder
string
ModTime
time
.
Time
UserId
int64
Message
string
Overwrite
bool
Dashboard
*
models
.
Dashboard
}
func
SaveDashboard
(
json
*
SaveDashboardItem
)
error
{
dashboard
:=
json
.
Dashboard
if
dashboard
.
Title
==
""
{
return
models
.
ErrDashboardTitleEmpty
}
validateAlertsCmd
:=
alerting
.
ValidateDashboardAlertsCommand
{
OrgId
:
json
.
OrgId
,
Dashboard
:
dashboard
,
}
if
err
:=
bus
.
Dispatch
(
&
validateAlertsCmd
);
err
!=
nil
{
return
models
.
ErrDashboardContainsInvalidAlertData
}
cmd
:=
models
.
SaveDashboardCommand
{
Dashboard
:
dashboard
.
Data
,
Message
:
json
.
Message
,
OrgId
:
json
.
OrgId
,
Overwrite
:
json
.
Overwrite
,
}
if
!
json
.
ModTime
.
IsZero
()
{
cmd
.
UpdatedAt
=
json
.
ModTime
}
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
return
err
}
alertCmd
:=
alerting
.
UpdateDashboardAlertsCommand
{
OrgId
:
json
.
OrgId
,
Dashboard
:
cmd
.
Result
,
}
if
err
:=
bus
.
Dispatch
(
&
alertCmd
);
err
!=
nil
{
return
err
}
return
nil
}
pkg/services/provisioning/dashboard/file_reader.go
View file @
74e12c26
...
@@ -3,7 +3,7 @@ package dashboard
...
@@ -3,7 +3,7 @@ package dashboard
import
(
import
(
"context"
"context"
"fmt"
"fmt"
"github.com/grafana/grafana/pkg/services/
alerting
"
"github.com/grafana/grafana/pkg/services/
dashboards
"
"os"
"os"
"path/filepath"
"path/filepath"
"strings"
"strings"
...
@@ -96,7 +96,7 @@ func (fr *fileReader) walkFolder() error {
...
@@ -96,7 +96,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
)
return
fr
.
s
aveDashboard
(
dash
)
return
dashboards
.
S
aveDashboard
(
dash
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -109,11 +109,11 @@ func (fr *fileReader) walkFolder() error {
...
@@ -109,11 +109,11 @@ 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
)
return
fr
.
s
aveDashboard
(
dash
)
return
dashboards
.
S
aveDashboard
(
dash
)
})
})
}
}
func
(
fr
*
fileReader
)
readDashboardFromFile
(
path
string
)
(
*
DashboardJson
,
error
)
{
func
(
fr
*
fileReader
)
readDashboardFromFile
(
path
string
)
(
*
dashboards
.
SaveDashboardItem
,
error
)
{
reader
,
err
:=
os
.
Open
(
path
)
reader
,
err
:=
os
.
Open
(
path
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -139,44 +139,3 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error)
...
@@ -139,44 +139,3 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error)
return
dash
,
nil
return
dash
,
nil
}
}
func
(
fr
*
fileReader
)
saveDashboard
(
json
*
DashboardJson
)
error
{
dashboard
:=
json
.
Dashboard
if
dashboard
.
Title
==
""
{
return
models
.
ErrDashboardTitleEmpty
}
validateAlertsCmd
:=
alerting
.
ValidateDashboardAlertsCommand
{
OrgId
:
json
.
OrgId
,
Dashboard
:
dashboard
,
}
if
err
:=
bus
.
Dispatch
(
&
validateAlertsCmd
);
err
!=
nil
{
return
models
.
ErrDashboardContainsInvalidAlertData
}
cmd
:=
models
.
SaveDashboardCommand
{
Dashboard
:
dashboard
.
Data
,
Message
:
"Dashboard created from file."
,
OrgId
:
json
.
OrgId
,
Overwrite
:
true
,
UpdatedAt
:
json
.
ModTime
,
}
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
return
err
}
alertCmd
:=
alerting
.
UpdateDashboardAlertsCommand
{
OrgId
:
json
.
OrgId
,
Dashboard
:
cmd
.
Result
,
}
if
err
:=
bus
.
Dispatch
(
&
alertCmd
);
err
!=
nil
{
return
err
}
return
nil
}
pkg/services/provisioning/dashboard/types.go
View file @
74e12c26
...
@@ -2,6 +2,7 @@ package dashboard
...
@@ -2,6 +2,7 @@ package dashboard
import
(
import
(
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/dashboards"
"strings"
"strings"
"sync"
"sync"
"time"
"time"
...
@@ -18,42 +19,34 @@ type DashboardsAsConfig struct {
...
@@ -18,42 +19,34 @@ type DashboardsAsConfig struct {
Options
map
[
string
]
interface
{}
`json:"options" yaml:"options"`
Options
map
[
string
]
interface
{}
`json:"options" yaml:"options"`
}
}
type
DashboardJson
struct
{
TitleLower
string
OrgId
int64
Folder
string
ModTime
time
.
Time
Dashboard
*
models
.
Dashboard
}
type
dashboardCache
struct
{
type
dashboardCache
struct
{
mutex
*
sync
.
Mutex
mutex
*
sync
.
Mutex
dashboards
map
[
string
]
*
DashboardJson
dashboards
map
[
string
]
*
dashboards
.
SaveDashboardItem
}
}
func
newDashboardCache
()
*
dashboardCache
{
func
newDashboardCache
()
*
dashboardCache
{
return
&
dashboardCache
{
return
&
dashboardCache
{
dashboards
:
map
[
string
]
*
DashboardJson
{},
dashboards
:
map
[
string
]
*
dashboards
.
SaveDashboardItem
{},
mutex
:
&
sync
.
Mutex
{},
mutex
:
&
sync
.
Mutex
{},
}
}
}
}
func
(
dc
*
dashboardCache
)
addCache
(
key
string
,
json
*
DashboardJson
)
{
func
(
dc
*
dashboardCache
)
addCache
(
key
string
,
json
*
dashboards
.
SaveDashboardItem
)
{
dc
.
mutex
.
Lock
()
dc
.
mutex
.
Lock
()
defer
dc
.
mutex
.
Unlock
()
defer
dc
.
mutex
.
Unlock
()
dc
.
dashboards
[
key
]
=
json
dc
.
dashboards
[
key
]
=
json
}
}
func
(
dc
*
dashboardCache
)
getCache
(
key
string
)
(
*
DashboardJson
,
bool
)
{
func
(
dc
*
dashboardCache
)
getCache
(
key
string
)
(
*
dashboards
.
SaveDashboardItem
,
bool
)
{
dc
.
mutex
.
Lock
()
dc
.
mutex
.
Lock
()
defer
dc
.
mutex
.
Unlock
()
defer
dc
.
mutex
.
Unlock
()
v
,
exist
:=
dc
.
dashboards
[
key
]
v
,
exist
:=
dc
.
dashboards
[
key
]
return
v
,
exist
return
v
,
exist
}
}
func
createDashboardJson
(
data
*
simplejson
.
Json
,
lastModified
time
.
Time
,
cfg
*
DashboardsAsConfig
)
(
*
DashboardJson
,
error
)
{
func
createDashboardJson
(
data
*
simplejson
.
Json
,
lastModified
time
.
Time
,
cfg
*
DashboardsAsConfig
)
(
*
dashboards
.
SaveDashboardItem
,
error
)
{
dash
:=
&
DashboardJson
{}
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
.
ModTime
=
lastModified
...
...
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