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
16f072b3
Commit
16f072b3
authored
Dec 01, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dashboards as cfg: move saving logic for dashboards into its own service
parent
9cebb23e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
34 deletions
+26
-34
pkg/api/dashboard.go
+15
-26
pkg/models/dashboards.go
+1
-0
pkg/services/dashboards/dashboards.go
+6
-6
pkg/services/provisioning/dashboards/file_reader.go
+4
-2
No files found.
pkg/api/dashboard.go
View file @
16f072b3
...
...
@@ -3,11 +3,12 @@ package api
import
(
"encoding/json"
"fmt"
//"github.com/grafana/grafana/pkg/services/dashboards"
"os"
"path"
"strings"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/dashdiffs"
...
...
@@ -17,7 +18,6 @@ import (
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
...
...
@@ -125,17 +125,6 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
dash
:=
cmd
.
GetDashboardModel
()
// dashItem := &dashboards.SaveDashboardItem{
// Dashboard: dash,
// Message: cmd.Message,
// }
// err := dashboards.SaveDashboard(dashItem)
// Check if Title is empty
if
dash
.
Title
==
""
{
return
ApiError
(
400
,
m
.
ErrDashboardTitleEmpty
.
Error
(),
nil
)
}
if
dash
.
Id
==
0
{
limitReached
,
err
:=
middleware
.
QuotaReached
(
c
,
"dashboard"
)
if
err
!=
nil
{
...
...
@@ -146,17 +135,23 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
}
}
validateAlertsCmd
:=
alerting
.
ValidateDashboardAlertsCommand
{
dashItem
:=
&
dashboards
.
SaveDashboardItem
{
Dashboard
:
dash
,
Message
:
cmd
.
Message
,
OrgId
:
c
.
OrgId
,
UserId
:
c
.
UserId
,
Dashboard
:
dash
,
}
if
err
:=
bus
.
Dispatch
(
&
validateAlertsCmd
);
err
!=
nil
{
dashboard
,
err
:=
dashboards
.
SaveDashboard
(
dashItem
)
if
err
==
m
.
ErrDashboardTitleEmpty
{
return
ApiError
(
400
,
m
.
ErrDashboardTitleEmpty
.
Error
(),
nil
)
}
if
err
==
m
.
ErrDashboardContainsInvalidAlertData
{
return
ApiError
(
500
,
"Invalid alert data. Cannot save dashboard"
,
err
)
}
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
if
err
==
m
.
ErrDashboardWithSameNameExists
{
return
Json
(
412
,
util
.
DynMap
{
"status"
:
"name-exists"
,
"message"
:
err
.
Error
()})
...
...
@@ -178,18 +173,12 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
return
ApiError
(
500
,
"Failed to save dashboard"
,
err
)
}
alertCmd
:=
alerting
.
UpdateDashboardAlertsCommand
{
OrgId
:
c
.
OrgId
,
UserId
:
c
.
UserId
,
Dashboard
:
cmd
.
Result
,
}
if
err
:=
bus
.
Dispatch
(
&
alertCmd
);
err
!=
nil
{
return
ApiError
(
500
,
"Failed to save alerts"
,
err
)
if
err
==
m
.
ErrDashboardFailedToUpdateAlertData
{
return
ApiError
(
500
,
"Invalid alert data. Cannot save dashboard"
,
err
)
}
c
.
TimeRequest
(
metrics
.
M_Api_Dashboard_Save
)
return
Json
(
200
,
util
.
DynMap
{
"status"
:
"success"
,
"slug"
:
cmd
.
Result
.
Slug
,
"version"
:
cmd
.
Result
.
Version
})
return
Json
(
200
,
util
.
DynMap
{
"status"
:
"success"
,
"slug"
:
dashboard
.
Slug
,
"version"
:
dashboard
.
Version
})
}
func
canEditDashboard
(
role
m
.
RoleType
)
bool
{
...
...
pkg/models/dashboards.go
View file @
16f072b3
...
...
@@ -17,6 +17,7 @@ var (
ErrDashboardVersionMismatch
=
errors
.
New
(
"The dashboard has been changed by someone else"
)
ErrDashboardTitleEmpty
=
errors
.
New
(
"Dashboard title cannot be empty"
)
ErrDashboardContainsInvalidAlertData
=
errors
.
New
(
"Invalid alert data. Cannot save dashboard"
)
ErrDashboardFailedToUpdateAlertData
=
errors
.
New
(
"Failed to save alert data"
)
)
type
UpdatePluginDashboardError
struct
{
...
...
pkg/services/dashboards/dashboards.go
View file @
16f072b3
...
...
@@ -22,11 +22,11 @@ type SaveDashboardItem struct {
Dashboard
*
models
.
Dashboard
}
func
SaveDashboard
(
json
*
SaveDashboardItem
)
error
{
func
SaveDashboard
(
json
*
SaveDashboardItem
)
(
*
models
.
Dashboard
,
error
)
{
dashboard
:=
json
.
Dashboard
if
dashboard
.
Title
==
""
{
return
models
.
ErrDashboardTitleEmpty
return
nil
,
models
.
ErrDashboardTitleEmpty
}
validateAlertsCmd
:=
alerting
.
ValidateDashboardAlertsCommand
{
...
...
@@ -35,7 +35,7 @@ func SaveDashboard(json *SaveDashboardItem) error {
}
if
err
:=
bus
.
Dispatch
(
&
validateAlertsCmd
);
err
!=
nil
{
return
models
.
ErrDashboardContainsInvalidAlertData
return
nil
,
models
.
ErrDashboardContainsInvalidAlertData
}
cmd
:=
models
.
SaveDashboardCommand
{
...
...
@@ -51,7 +51,7 @@ func SaveDashboard(json *SaveDashboardItem) error {
err
:=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
alertCmd
:=
alerting
.
UpdateDashboardAlertsCommand
{
...
...
@@ -60,8 +60,8 @@ func SaveDashboard(json *SaveDashboardItem) error {
}
if
err
:=
bus
.
Dispatch
(
&
alertCmd
);
err
!=
nil
{
return
err
return
nil
,
models
.
ErrDashboardFailedToUpdateAlertData
}
return
nil
return
cmd
.
Result
,
nil
}
pkg/services/provisioning/dashboards/file_reader.go
View file @
16f072b3
...
...
@@ -96,7 +96,8 @@ func (fr *fileReader) walkFolder() error {
if
err
==
models
.
ErrDashboardNotFound
{
fr
.
log
.
Debug
(
"saving new dashboard"
,
"file"
,
path
)
return
dashboards
.
SaveDashboard
(
dash
)
_
,
err
=
dashboards
.
SaveDashboard
(
dash
)
return
err
}
if
err
!=
nil
{
...
...
@@ -109,7 +110,8 @@ func (fr *fileReader) walkFolder() error {
}
fr
.
log
.
Debug
(
"no dashboard in cache. loading dashboard from disk into database."
,
"file"
,
path
)
return
dashboards
.
SaveDashboard
(
dash
)
_
,
err
=
dashboards
.
SaveDashboard
(
dash
)
return
err
})
}
...
...
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