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
68cc3f86
Commit
68cc3f86
authored
Jan 09, 2015
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle default datasource management
parent
d562dcd9
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
106 additions
and
29 deletions
+106
-29
grafana
+1
-1
pkg/api/datasources.go
+1
-2
pkg/api/dtos/models.go
+1
-0
pkg/api/frontendsettings.go
+1
-0
pkg/bus/bus.go
+42
-0
pkg/bus/bus_test.go
+23
-0
pkg/models/account.go
+0
-6
pkg/models/datasource.go
+6
-1
pkg/services/account/handlers.go
+0
-9
pkg/stores/sqlstore/datasource.go
+31
-10
pkg/stores/sqlstore/datasource_test.go
+0
-0
No files found.
grafana
@
1d769fe4
Subproject commit 1
b9b8ba2bff651a38592805c9210bd1864dc1e79
Subproject commit 1
d769fe41cf348d88636938fcb06afcf3425d45b
pkg/api/datasources.go
View file @
68cc3f86
...
@@ -29,6 +29,7 @@ func GetDataSources(c *middleware.Context) {
...
@@ -29,6 +29,7 @@ func GetDataSources(c *middleware.Context) {
Database
:
ds
.
Database
,
Database
:
ds
.
Database
,
User
:
ds
.
User
,
User
:
ds
.
User
,
BasicAuth
:
ds
.
BasicAuth
,
BasicAuth
:
ds
.
BasicAuth
,
IsDefault
:
ds
.
IsDefault
,
}
}
}
}
...
@@ -69,8 +70,6 @@ func AddDataSource(c *middleware.Context) {
...
@@ -69,8 +70,6 @@ func AddDataSource(c *middleware.Context) {
return
return
}
}
//bus.Publish(&m.DataSourceCreatedEvent{Account: c.GetAccountId(), })
c
.
JsonOK
(
"Datasource added"
)
c
.
JsonOK
(
"Datasource added"
)
}
}
...
...
pkg/api/dtos/models.go
View file @
68cc3f86
...
@@ -30,6 +30,7 @@ type DataSource struct {
...
@@ -30,6 +30,7 @@ type DataSource struct {
User
string
`json:"user"`
User
string
`json:"user"`
Database
string
`json:"database"`
Database
string
`json:"database"`
BasicAuth
bool
`json:"basicAuth"`
BasicAuth
bool
`json:"basicAuth"`
IsDefault
bool
`json:"isDefault"`
}
}
type
MetricQueryResultDto
struct
{
type
MetricQueryResultDto
struct
{
...
...
pkg/api/frontendsettings.go
View file @
68cc3f86
...
@@ -35,6 +35,7 @@ func getFrontendSettings(c *middleware.Context) (map[string]interface{}, error)
...
@@ -35,6 +35,7 @@ func getFrontendSettings(c *middleware.Context) (map[string]interface{}, error)
var
dsMap
=
map
[
string
]
interface
{}{
var
dsMap
=
map
[
string
]
interface
{}{
"type"
:
ds
.
Type
,
"type"
:
ds
.
Type
,
"url"
:
url
,
"url"
:
url
,
"default"
:
ds
.
IsDefault
,
}
}
if
ds
.
Type
==
m
.
DS_INFLUXDB
{
if
ds
.
Type
==
m
.
DS_INFLUXDB
{
...
...
pkg/bus/bus.go
View file @
68cc3f86
...
@@ -10,11 +10,14 @@ type Msg interface{}
...
@@ -10,11 +10,14 @@ type Msg interface{}
type
Bus
interface
{
type
Bus
interface
{
Dispatch
(
msg
Msg
)
error
Dispatch
(
msg
Msg
)
error
Publish
(
msg
Msg
)
error
AddHandler
(
handler
HandlerFunc
)
AddHandler
(
handler
HandlerFunc
)
AddEventListener
(
handler
HandlerFunc
)
}
}
type
InProcBus
struct
{
type
InProcBus
struct
{
handlers
map
[
string
]
HandlerFunc
handlers
map
[
string
]
HandlerFunc
listeners
map
[
string
][]
HandlerFunc
}
}
// temp stuff, not sure how to handle bus instance, and init yet
// temp stuff, not sure how to handle bus instance, and init yet
...
@@ -23,6 +26,7 @@ var globalBus = New()
...
@@ -23,6 +26,7 @@ var globalBus = New()
func
New
()
Bus
{
func
New
()
Bus
{
bus
:=
&
InProcBus
{}
bus
:=
&
InProcBus
{}
bus
.
handlers
=
make
(
map
[
string
]
HandlerFunc
)
bus
.
handlers
=
make
(
map
[
string
]
HandlerFunc
)
bus
.
listeners
=
make
(
map
[
string
][]
HandlerFunc
)
return
bus
return
bus
}
}
...
@@ -46,17 +50,55 @@ func (b *InProcBus) Dispatch(msg Msg) error {
...
@@ -46,17 +50,55 @@ func (b *InProcBus) Dispatch(msg Msg) error {
}
}
}
}
func
(
b
*
InProcBus
)
Publish
(
msg
Msg
)
error
{
var
msgName
=
reflect
.
TypeOf
(
msg
)
.
Elem
()
.
Name
()
var
listeners
=
b
.
listeners
[
msgName
]
if
len
(
listeners
)
==
0
{
return
nil
}
var
params
=
make
([]
reflect
.
Value
,
1
)
params
[
0
]
=
reflect
.
ValueOf
(
msg
)
for
listenerHandler
:=
range
listeners
{
ret
:=
reflect
.
ValueOf
(
listenerHandler
)
.
Call
(
params
)
err
:=
ret
[
0
]
.
Interface
()
if
err
!=
nil
{
return
err
.
(
error
)
}
}
return
nil
}
func
(
b
*
InProcBus
)
AddHandler
(
handler
HandlerFunc
)
{
func
(
b
*
InProcBus
)
AddHandler
(
handler
HandlerFunc
)
{
handlerType
:=
reflect
.
TypeOf
(
handler
)
handlerType
:=
reflect
.
TypeOf
(
handler
)
queryTypeName
:=
handlerType
.
In
(
0
)
.
Elem
()
.
Name
()
queryTypeName
:=
handlerType
.
In
(
0
)
.
Elem
()
.
Name
()
b
.
handlers
[
queryTypeName
]
=
handler
b
.
handlers
[
queryTypeName
]
=
handler
}
}
func
(
b
*
InProcBus
)
AddEventListener
(
handler
HandlerFunc
)
{
handlerType
:=
reflect
.
TypeOf
(
handler
)
eventName
:=
handlerType
.
In
(
0
)
.
Elem
()
.
Name
()
list
,
exists
:=
b
.
listeners
[
eventName
]
if
!
exists
{
list
=
make
([]
HandlerFunc
,
0
)
b
.
listeners
[
eventName
]
=
list
}
list
=
append
(
list
,
handler
)
}
// Package level functions
// Package level functions
func
AddHandler
(
implName
string
,
handler
HandlerFunc
)
{
func
AddHandler
(
implName
string
,
handler
HandlerFunc
)
{
globalBus
.
AddHandler
(
handler
)
globalBus
.
AddHandler
(
handler
)
}
}
// Package level functions
func
AddEventListener
(
handler
HandlerFunc
)
{
globalBus
.
AddEventListener
(
handler
)
}
func
Dispatch
(
msg
Msg
)
error
{
func
Dispatch
(
msg
Msg
)
error
{
return
globalBus
.
Dispatch
(
msg
)
return
globalBus
.
Dispatch
(
msg
)
}
}
pkg/bus/bus_test.go
View file @
68cc3f86
...
@@ -43,3 +43,26 @@ func TestQueryHandlerReturn(t *testing.T) {
...
@@ -43,3 +43,26 @@ func TestQueryHandlerReturn(t *testing.T) {
t
.
Fatal
(
"Failed to get response from handler"
)
t
.
Fatal
(
"Failed to get response from handler"
)
}
}
}
}
func
TestEventListeners
(
t
*
testing
.
T
)
{
bus
:=
New
()
count
:=
0
bus
.
AddEventListener
(
func
(
query
*
TestQuery
)
error
{
count
+=
1
return
nil
})
bus
.
AddEventListener
(
func
(
query
*
TestQuery
)
error
{
count
+=
10
return
nil
})
err
:=
bus
.
Publish
(
&
TestQuery
{})
if
err
!=
nil
{
t
.
Fatal
(
"Publish event failed "
+
err
.
Error
())
}
else
if
count
!=
0
{
t
.
Fatal
(
"Publish event failed, listeners called: %v, expected: %v"
,
count
,
11
)
}
}
pkg/models/account.go
View file @
68cc3f86
...
@@ -23,7 +23,6 @@ type Account struct {
...
@@ -23,7 +23,6 @@ type Account struct {
Company
string
Company
string
NextDashboardId
int
NextDashboardId
int
UsingAccountId
int64
UsingAccountId
int64
DefaultDataSourceId
int64
Created
time
.
Time
Created
time
.
Time
Updated
time
.
Time
Updated
time
.
Time
...
@@ -48,11 +47,6 @@ type SetUsingAccountCommand struct {
...
@@ -48,11 +47,6 @@ type SetUsingAccountCommand struct {
UsingAccountId
int64
UsingAccountId
int64
}
}
type
SetDefaultDataSourceCommand
struct
{
AccountId
int64
DataSourceId
int64
}
// ----------------------
// ----------------------
// QUERIES
// QUERIES
...
...
pkg/models/datasource.go
View file @
68cc3f86
...
@@ -33,6 +33,7 @@ type DataSource struct {
...
@@ -33,6 +33,7 @@ type DataSource struct {
User
string
User
string
Database
string
Database
string
BasicAuth
bool
BasicAuth
bool
IsDefault
bool
Created
time
.
Time
Created
time
.
Time
Updated
time
.
Time
Updated
time
.
Time
...
@@ -41,8 +42,9 @@ type DataSource struct {
...
@@ -41,8 +42,9 @@ type DataSource struct {
// ----------------------
// ----------------------
// COMMANDS
// COMMANDS
// Also acts as api DTO
type
AddDataSourceCommand
struct
{
type
AddDataSourceCommand
struct
{
AccountId
int64
AccountId
int64
`json:"-"`
Name
string
Name
string
Type
DsType
Type
DsType
Access
DsAccess
Access
DsAccess
...
@@ -50,10 +52,12 @@ type AddDataSourceCommand struct {
...
@@ -50,10 +52,12 @@ type AddDataSourceCommand struct {
Password
string
Password
string
Database
string
Database
string
User
string
User
string
IsDefault
bool
Result
*
DataSource
Result
*
DataSource
}
}
// Also acts as api DTO
type
UpdateDataSourceCommand
struct
{
type
UpdateDataSourceCommand
struct
{
Id
int64
Id
int64
AccountId
int64
AccountId
int64
...
@@ -64,6 +68,7 @@ type UpdateDataSourceCommand struct {
...
@@ -64,6 +68,7 @@ type UpdateDataSourceCommand struct {
Password
string
Password
string
User
string
User
string
Database
string
Database
string
IsDefault
bool
}
}
type
DeleteDataSourceCommand
struct
{
type
DeleteDataSourceCommand
struct
{
...
...
pkg/services/account/handlers.go
deleted
100644 → 0
View file @
d562dcd9
package
account
import
(
"github.com/torkelo/grafana-pro/pkg/bus"
)
func
InitAccountService
()
{
bus
.
ListenTo
()
}
pkg/stores/sqlstore/datasource.go
View file @
68cc3f86
...
@@ -45,9 +45,7 @@ func DeleteDataSource(cmd *m.DeleteDataSourceCommand) error {
...
@@ -45,9 +45,7 @@ func DeleteDataSource(cmd *m.DeleteDataSourceCommand) error {
func
AddDataSource
(
cmd
*
m
.
AddDataSourceCommand
)
error
{
func
AddDataSource
(
cmd
*
m
.
AddDataSourceCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
err
error
ds
:=
&
m
.
DataSource
{
ds
:=
m
.
DataSource
{
AccountId
:
cmd
.
AccountId
,
AccountId
:
cmd
.
AccountId
,
Name
:
cmd
.
Name
,
Name
:
cmd
.
Name
,
Type
:
cmd
.
Type
,
Type
:
cmd
.
Type
,
...
@@ -56,23 +54,38 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error {
...
@@ -56,23 +54,38 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error {
User
:
cmd
.
User
,
User
:
cmd
.
User
,
Password
:
cmd
.
Password
,
Password
:
cmd
.
Password
,
Database
:
cmd
.
Database
,
Database
:
cmd
.
Database
,
IsDefault
:
cmd
.
IsDefault
,
Created
:
time
.
Now
(),
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
}
_
,
err
=
sess
.
Insert
(
&
ds
)
if
_
,
err
:=
sess
.
Insert
(
ds
);
err
!=
nil
{
cmd
.
Result
=
&
ds
return
err
}
if
err
:=
updateIsDefaultFlag
(
ds
,
sess
);
err
!=
nil
{
return
err
return
err
}
cmd
.
Result
=
ds
return
nil
})
})
}
}
func
updateIsDefaultFlag
(
ds
*
m
.
DataSource
,
sess
*
xorm
.
Session
)
error
{
// Handle is default flag
if
ds
.
IsDefault
{
rawSql
:=
"UPDATE data_source SET is_default = 0 WHERE account_id=? AND id <> ?"
if
_
,
err
:=
sess
.
Exec
(
rawSql
,
ds
.
AccountId
,
ds
.
Id
);
err
!=
nil
{
return
err
}
}
return
nil
}
func
UpdateDataSource
(
cmd
*
m
.
UpdateDataSourceCommand
)
error
{
func
UpdateDataSource
(
cmd
*
m
.
UpdateDataSourceCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
err
error
ds
:=
&
m
.
DataSource
{
ds
:=
m
.
DataSource
{
Id
:
cmd
.
Id
,
Id
:
cmd
.
Id
,
AccountId
:
cmd
.
AccountId
,
AccountId
:
cmd
.
AccountId
,
Name
:
cmd
.
Name
,
Name
:
cmd
.
Name
,
...
@@ -83,9 +96,17 @@ func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
...
@@ -83,9 +96,17 @@ func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
Password
:
cmd
.
Password
,
Password
:
cmd
.
Password
,
Database
:
cmd
.
Database
,
Database
:
cmd
.
Database
,
Updated
:
time
.
Now
(),
Updated
:
time
.
Now
(),
IsDefault
:
cmd
.
IsDefault
,
}
sess
.
UseBool
(
"is_default"
)
_
,
err
:=
sess
.
Where
(
"id=? and account_id=?"
,
ds
.
Id
,
ds
.
AccountId
)
.
Update
(
ds
)
if
err
!=
nil
{
return
err
}
}
_
,
err
=
sess
.
Where
(
"id=? and account_id=?"
,
ds
.
Id
,
ds
.
AccountId
)
.
Update
(
&
d
s
)
err
=
updateIsDefaultFlag
(
ds
,
ses
s
)
return
err
return
err
})
})
}
}
pkg/stores/sqlstore/
sqlstor
e_test.go
→
pkg/stores/sqlstore/
datasourc
e_test.go
View file @
68cc3f86
File moved
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