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
c7ed348e
Commit
c7ed348e
authored
Dec 16, 2014
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Lots of progress on command/query bus concept, in memory sql testing, datasource admin
parent
27f07e9d
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
160 additions
and
42 deletions
+160
-42
pkg/api/api_datasources.go
+1
-0
pkg/bus/bus.go
+0
-2
pkg/cmd/web.go
+1
-6
pkg/models/account.go
+3
-2
pkg/models/datasource.go
+2
-2
pkg/stores/sqlstore/sqlstore.go
+67
-25
pkg/stores/sqlstore/sqlstore_datasource.go
+32
-5
pkg/stores/sqlstore/sqlstore_test.go
+54
-0
No files found.
pkg/api/api_datasources.go
View file @
c7ed348e
...
...
@@ -30,4 +30,5 @@ func AddDataSource(c *middleware.Context) {
return
}
c
.
Status
(
204
)
}
pkg/bus/bus.go
View file @
c7ed348e
...
...
@@ -2,7 +2,6 @@ package bus
import
(
"errors"
"fmt"
"reflect"
)
...
...
@@ -50,7 +49,6 @@ func (b *InProcBus) Dispatch(msg Msg) error {
func
(
b
*
InProcBus
)
AddHandler
(
handler
HandlerFunc
)
{
handlerType
:=
reflect
.
TypeOf
(
handler
)
queryTypeName
:=
handlerType
.
In
(
0
)
.
Elem
()
.
Name
()
fmt
.
Printf
(
"QueryType %v
\n
"
,
queryTypeName
)
b
.
handlers
[
queryTypeName
]
=
handler
}
...
...
pkg/cmd/web.go
View file @
c7ed348e
...
...
@@ -70,12 +70,7 @@ func runWeb(*cli.Context) {
social
.
NewOAuthService
()
sqlstore
.
Init
()
// init database
sqlstore
.
LoadModelsConfig
()
if
err
:=
sqlstore
.
NewEngine
();
err
!=
nil
{
log
.
Fatal
(
4
,
"fail to initialize orm engine: %v"
,
err
)
}
sqlstore
.
NewEngine
()
m
:=
newMacaron
()
api
.
Register
(
m
)
...
...
pkg/models/account.go
View file @
c7ed348e
...
...
@@ -37,6 +37,7 @@ type Account struct {
Company
string
NextDashboardId
int
UsingAccountId
int64
Created
time
.
Time
`xorm:"CREATED"`
Updated
time
.
Time
`xorm:"UPDATED"`
Created
time
.
Time
Updated
time
.
Time
}
pkg/models/datasource.go
View file @
c7ed348e
...
...
@@ -6,8 +6,8 @@ const (
DS_GRAPHITE
=
"GRAPHITE"
DS_INFLUXDB
=
"INFLUXDB"
DS_ES
=
"ES"
DS_AC
ES
SS_DIRECT
=
"DIRECT"
DS_AC
ES
SS_PROXY
=
"PROXY"
DS_AC
CE
SS_DIRECT
=
"DIRECT"
DS_AC
CE
SS_PROXY
=
"PROXY"
)
type
DsType
string
...
...
pkg/stores/sqlstore/sqlstore.go
View file @
c7ed348e
...
...
@@ -6,6 +6,7 @@ import (
"path"
"strings"
"github.com/torkelo/grafana-pro/pkg/log"
m
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/torkelo/grafana-pro/pkg/setting"
...
...
@@ -26,10 +27,14 @@ var (
UseSQLite3
bool
)
func
Init
()
{
func
init
()
{
tables
=
make
([]
interface
{},
0
)
tables
=
append
(
tables
,
new
(
m
.
Account
),
new
(
m
.
Dashboard
),
new
(
m
.
Collaborator
),
new
(
m
.
DataSource
))
}
func
Init
()
{
m
.
SaveAccount
=
SaveAccount
m
.
GetAccount
=
GetAccount
m
.
GetAccountByLogin
=
GetAccountByLogin
...
...
@@ -42,43 +47,36 @@ func Init() {
m
.
AddCollaborator
=
AddCollaborator
}
func
LoadModelsConfig
()
{
DbCfg
.
Type
=
setting
.
Cfg
.
MustValue
(
"database"
,
"type"
)
if
DbCfg
.
Type
==
"sqlite3"
{
UseSQLite3
=
true
}
DbCfg
.
Host
=
setting
.
Cfg
.
MustValue
(
"database"
,
"host"
)
DbCfg
.
Name
=
setting
.
Cfg
.
MustValue
(
"database"
,
"name"
)
DbCfg
.
User
=
setting
.
Cfg
.
MustValue
(
"database"
,
"user"
)
if
len
(
DbCfg
.
Pwd
)
==
0
{
DbCfg
.
Pwd
=
setting
.
Cfg
.
MustValue
(
"database"
,
"password"
)
}
DbCfg
.
SslMode
=
setting
.
Cfg
.
MustValue
(
"database"
,
"ssl_mode"
)
DbCfg
.
Path
=
setting
.
Cfg
.
MustValue
(
"database"
,
"path"
,
"data/grafana.db"
)
}
func
NewEngine
()
(
err
error
)
{
if
err
=
SetEngine
();
err
!=
nil
{
return
err
x
,
err
=
getEngine
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"sqlstore.init(fail to connect to database): %v"
,
err
)
}
if
err
=
x
.
Sync2
(
tables
...
);
err
!=
nil
{
return
fmt
.
Errorf
(
"sync database struct error: %v
\n
"
,
err
)
err
=
SetEngine
(
x
,
true
)
if
err
!=
nil
{
log
.
Fatal
(
4
,
"fail to initialize orm engine: %v"
,
err
)
}
return
nil
}
func
SetEngine
()
(
err
error
)
{
x
,
err
=
getEngine
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"models.init(fail to connect to database): %v"
,
err
)
func
SetEngine
(
engine
*
xorm
.
Engine
,
enableLog
bool
)
(
err
error
)
{
x
=
engine
if
err
:=
x
.
Sync2
(
tables
...
);
err
!=
nil
{
return
fmt
.
Errorf
(
"sync database struct error: %v
\n
"
,
err
)
}
if
enableLog
{
logPath
:=
path
.
Join
(
setting
.
LogRootPath
,
"xorm.log"
)
os
.
MkdirAll
(
path
.
Dir
(
logPath
),
os
.
ModePerm
)
f
,
err
:=
os
.
Create
(
logPath
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"models
.init(fail to create xorm.log): %v"
,
err
)
return
fmt
.
Errorf
(
"sqlstore
.init(fail to create xorm.log): %v"
,
err
)
}
x
.
Logger
=
xorm
.
NewSimpleLogger
(
f
)
...
...
@@ -87,10 +85,14 @@ func SetEngine() (err error) {
x
.
ShowDebug
=
true
x
.
ShowErr
=
true
x
.
ShowWarn
=
true
}
return
nil
}
func
getEngine
()
(
*
xorm
.
Engine
,
error
)
{
LoadConfig
()
cnnstr
:=
""
switch
DbCfg
.
Type
{
case
"mysql"
:
...
...
@@ -113,5 +115,45 @@ func getEngine() (*xorm.Engine, error) {
default
:
return
nil
,
fmt
.
Errorf
(
"Unknown database type: %s"
,
DbCfg
.
Type
)
}
return
xorm
.
NewEngine
(
DbCfg
.
Type
,
cnnstr
)
}
func
LoadConfig
()
{
DbCfg
.
Type
=
setting
.
Cfg
.
MustValue
(
"database"
,
"type"
)
if
DbCfg
.
Type
==
"sqlite3"
{
UseSQLite3
=
true
}
DbCfg
.
Host
=
setting
.
Cfg
.
MustValue
(
"database"
,
"host"
)
DbCfg
.
Name
=
setting
.
Cfg
.
MustValue
(
"database"
,
"name"
)
DbCfg
.
User
=
setting
.
Cfg
.
MustValue
(
"database"
,
"user"
)
if
len
(
DbCfg
.
Pwd
)
==
0
{
DbCfg
.
Pwd
=
setting
.
Cfg
.
MustValue
(
"database"
,
"password"
)
}
DbCfg
.
SslMode
=
setting
.
Cfg
.
MustValue
(
"database"
,
"ssl_mode"
)
DbCfg
.
Path
=
setting
.
Cfg
.
MustValue
(
"database"
,
"path"
,
"data/grafana.db"
)
}
type
dbTransactionFunc
func
(
sess
*
xorm
.
Session
)
error
func
inTransaction
(
callback
dbTransactionFunc
)
error
{
var
err
error
sess
:=
x
.
NewSession
()
defer
sess
.
Close
()
if
err
=
sess
.
Begin
();
err
!=
nil
{
return
err
}
err
=
callback
(
sess
)
if
err
!=
nil
{
sess
.
Rollback
()
return
err
}
else
if
err
=
sess
.
Commit
();
err
!=
nil
{
return
err
}
return
nil
}
pkg/stores/sqlstore/sqlstore_datasource.go
View file @
c7ed348e
package
sqlstore
import
(
"errors"
"time"
"github.com/torkelo/grafana-pro/pkg/bus"
m
"github.com/torkelo/grafana-pro/pkg/models"
"github.com/go-xorm/xorm"
)
func
init
()
{
bus
.
AddHandler
(
"sql"
,
GetDataSources
Query
)
bus
.
AddHandler
(
"sql"
,
GetDataSources
)
bus
.
AddHandler
(
"sql"
,
AddDataSource
)
}
func
GetDataSourcesQuery
(
query
*
m
.
GetDataSourcesQuery
)
error
{
return
errors
.
New
(
"Hello from query handler"
)
func
GetDataSources
(
query
*
m
.
GetDataSourcesQuery
)
error
{
sess
:=
x
.
Limit
(
100
,
0
)
.
Where
(
"account_id=?"
,
query
.
AccountId
)
query
.
Resp
=
make
([]
*
m
.
DataSource
,
0
)
return
sess
.
Find
(
&
query
.
Resp
)
}
func
AddDataSource
(
cmd
*
m
.
AddDataSourceCommand
)
error
{
return
errors
.
New
(
"Hello from command handler"
)
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
var
err
error
ds
:=
m
.
DataSource
{
AccountId
:
cmd
.
AccountId
,
Name
:
cmd
.
Name
,
Type
:
cmd
.
Type
,
Access
:
cmd
.
Access
,
Url
:
cmd
.
Url
,
Created
:
time
.
Now
(),
Updated
:
time
.
Now
(),
}
if
ds
.
Id
==
0
{
_
,
err
=
sess
.
Insert
(
ds
)
}
else
{
_
,
err
=
sess
.
Id
(
ds
.
Id
)
.
Update
(
ds
)
}
return
err
})
}
pkg/stores/sqlstore/sqlstore_test.go
0 → 100644
View file @
c7ed348e
package
sqlstore
import
(
"testing"
"github.com/go-xorm/xorm"
.
"github.com/smartystreets/goconvey/convey"
m
"github.com/torkelo/grafana-pro/pkg/models"
)
func
InitTestDB
(
t
*
testing
.
T
)
{
x
,
err
:=
xorm
.
NewEngine
(
"sqlite3"
,
":memory:"
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to init in memory sqllite3 db %v"
,
err
)
}
SetEngine
(
x
,
false
)
}
type
Test
struct
{
Id
int64
Name
string
}
func
TestDataAccess
(
t
*
testing
.
T
)
{
Convey
(
"Testing DB"
,
t
,
func
()
{
InitTestDB
(
t
)
Convey
(
"Can add datasource"
,
func
()
{
err
:=
AddDataSource
(
&
m
.
AddDataSourceCommand
{
AccountId
:
10
,
Type
:
m
.
DS_GRAPHITE
,
Access
:
m
.
DS_ACCESS_DIRECT
,
Url
:
"http://test"
,
})
So
(
err
,
ShouldBeNil
)
query
:=
m
.
GetDataSourcesQuery
{
AccountId
:
10
}
err
=
GetDataSources
(
&
query
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
query
.
Resp
),
ShouldEqual
,
1
)
})
})
}
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