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
8faa806c
Commit
8faa806c
authored
Dec 29, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring dashboard delete and search
parent
164d11c8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
32 deletions
+51
-32
pkg/api/dashboard.go
+13
-9
pkg/models/dashboards.go
+19
-6
pkg/stores/sqlstore/dashboards.go
+19
-14
pkg/stores/sqlstore/sqlstore.go
+0
-3
No files found.
pkg/api/dashboard.go
View file @
8faa806c
...
...
@@ -10,47 +10,51 @@ import (
func
GetDashboard
(
c
*
middleware
.
Context
)
{
slug
:=
c
.
Params
(
":slug"
)
dash
,
err
:=
m
.
GetDashboard
(
slug
,
c
.
GetAccountId
())
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
AccountId
:
c
.
GetAccountId
()}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Dashboard not found"
,
nil
)
return
}
dash
.
Data
[
"id"
]
=
dash
.
Id
query
.
Result
.
Data
[
"id"
]
=
query
.
Result
.
Id
c
.
JSON
(
200
,
dash
.
Data
)
c
.
JSON
(
200
,
query
.
Result
.
Data
)
}
func
DeleteDashboard
(
c
*
middleware
.
Context
)
{
slug
:=
c
.
Params
(
":slug"
)
dash
,
err
:=
m
.
GetDashboard
(
slug
,
c
.
GetAccountId
())
query
:=
m
.
GetDashboardQuery
{
Slug
:
slug
,
AccountId
:
c
.
GetAccountId
()}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
c
.
JsonApiErr
(
404
,
"Dashboard not found"
,
nil
)
return
}
err
=
m
.
DeleteDashboard
(
slug
,
c
.
GetAccountId
())
cmd
:=
m
.
DeleteDashboardCommand
{
Slug
:
slug
,
AccountId
:
c
.
GetAccountId
()}
err
=
bus
.
Dispatch
(
&
cmd
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Failed to delete dashboard"
,
err
)
return
}
var
resp
=
map
[
string
]
interface
{}{
"title"
:
dash
.
Title
}
var
resp
=
map
[
string
]
interface
{}{
"title"
:
query
.
Result
.
Title
}
c
.
JSON
(
200
,
resp
)
}
func
Search
(
c
*
middleware
.
Context
)
{
query
:=
c
.
Query
(
"q"
)
query
Text
:=
c
.
Query
(
"q"
)
results
,
err
:=
m
.
SearchQuery
(
query
,
c
.
GetAccountId
())
query
:=
m
.
SearchDashboardsQuery
{
Query
:
queryText
,
AccountId
:
c
.
GetAccountId
()}
err
:=
bus
.
Dispatch
(
&
query
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Search failed"
,
err
)
return
}
c
.
JSON
(
200
,
results
)
c
.
JSON
(
200
,
query
.
Result
)
}
func
PostDashboard
(
c
*
middleware
.
Context
)
{
...
...
pkg/models/dashboards.go
View file @
8faa806c
...
...
@@ -7,12 +7,6 @@ import (
"time"
)
var
(
GetDashboard
func
(
slug
string
,
accountId
int64
)
(
*
Dashboard
,
error
)
DeleteDashboard
func
(
slug
string
,
accountId
int64
)
error
SearchQuery
func
(
query
string
,
acccountId
int64
)
([]
*
SearchResult
,
error
)
)
// Typed errors
var
(
ErrDashboardNotFound
=
errors
.
New
(
"Account not found"
)
...
...
@@ -37,6 +31,13 @@ type SearchResult struct {
Slug
string
`json:"slug"`
}
type
SearchDashboardsQuery
struct
{
Query
string
AccountId
int64
Result
[]
*
SearchResult
}
type
SaveDashboardCommand
struct
{
Id
string
`json:"id"`
Title
string
`json:"title"`
...
...
@@ -46,6 +47,18 @@ type SaveDashboardCommand struct {
Result
*
Dashboard
}
type
DeleteDashboardCommand
struct
{
Slug
string
AccountId
int64
}
type
GetDashboardQuery
struct
{
Slug
string
AccountId
int64
Result
*
Dashboard
}
func
convertToStringArray
(
arr
[]
interface
{})
[]
string
{
b
:=
make
([]
string
,
len
(
arr
))
for
i
:=
range
arr
{
...
...
pkg/stores/sqlstore/dashboards.go
View file @
8faa806c
...
...
@@ -7,10 +7,13 @@ import (
)
func
init
()
{
bus
.
AddHandler
(
"sql"
,
SaveDashboard2
)
bus
.
AddHandler
(
"sql"
,
SaveDashboard
)
bus
.
AddHandler
(
"sql"
,
GetDashboard
)
bus
.
AddHandler
(
"sql"
,
DeleteDashboard
)
bus
.
AddHandler
(
"sql"
,
SearchDashboards
)
}
func
SaveDashboard
2
(
cmd
*
m
.
SaveDashboardCommand
)
error
{
func
SaveDashboard
(
cmd
*
m
.
SaveDashboardCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
dash
:=
cmd
.
GetDashboardModel
()
...
...
@@ -27,34 +30,36 @@ func SaveDashboard2(cmd *m.SaveDashboardCommand) error {
})
}
func
GetDashboard
(
slug
string
,
accountId
int64
)
(
*
m
.
Dashboard
,
error
)
{
dashboard
:=
m
.
Dashboard
{
Slug
:
slug
,
AccountId
:
a
ccountId
}
func
GetDashboard
(
query
*
m
.
GetDashboardQuery
)
error
{
dashboard
:=
m
.
Dashboard
{
Slug
:
query
.
Slug
,
AccountId
:
query
.
A
ccountId
}
has
,
err
:=
x
.
Get
(
&
dashboard
)
if
err
!=
nil
{
return
nil
,
err
return
err
}
else
if
has
==
false
{
return
nil
,
m
.
ErrDashboardNotFound
return
m
.
ErrDashboardNotFound
}
return
&
dashboard
,
nil
query
.
Result
=
&
dashboard
return
nil
}
func
Search
Query
(
query
string
,
accountId
int64
)
([]
*
m
.
SearchResult
,
error
)
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"account_id=?"
,
a
ccountId
)
func
Search
Dashboards
(
query
*
m
.
SearchDashboardsQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"account_id=?"
,
query
.
A
ccountId
)
sess
.
Table
(
"Dashboard"
)
results
:
=
make
([]
*
m
.
SearchResult
,
0
)
err
:=
sess
.
Find
(
&
results
)
query
.
Result
=
make
([]
*
m
.
SearchResult
,
0
)
err
:=
sess
.
Find
(
&
query
.
Result
)
return
results
,
err
return
err
}
func
DeleteDashboard
(
slug
string
,
accountId
int64
)
error
{
func
DeleteDashboard
(
cmd
*
m
.
DeleteDashboardCommand
)
error
{
sess
:=
x
.
NewSession
()
defer
sess
.
Close
()
rawSql
:=
"DELETE FROM Dashboard WHERE account_id=? and slug=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
accountId
,
s
lug
)
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
AccountId
,
cmd
.
S
lug
)
return
err
}
pkg/stores/sqlstore/sqlstore.go
View file @
8faa806c
...
...
@@ -35,9 +35,6 @@ func init() {
}
func
Init
()
{
m
.
GetDashboard
=
GetDashboard
m
.
SearchQuery
=
SearchQuery
m
.
DeleteDashboard
=
DeleteDashboard
}
func
NewEngine
()
(
err
error
)
{
...
...
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