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
6586cc40
Commit
6586cc40
authored
Jun 13, 2016
by
Nozomi Anzai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the datasource of RDBMS (PostgreSQL and MySQL)
parent
22407fc8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
172 additions
and
0 deletions
+172
-0
pkg/api/dataproxy.go
+6
-0
pkg/api/sqldb/sqldb.go
+164
-0
pkg/models/datasource.go
+2
-0
No files found.
pkg/api/dataproxy.go
View file @
6586cc40
...
...
@@ -9,6 +9,7 @@ import (
"time"
"github.com/grafana/grafana/pkg/api/cloudwatch"
"github.com/grafana/grafana/pkg/api/sqldb"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
...
...
@@ -43,6 +44,7 @@ func NewReverseProxy(ds *m.DataSource, proxyPath string, targetUrl *url.URL) *ht
}
else
if
ds
.
Type
==
m
.
DS_INFLUXDB
{
req
.
URL
.
Path
=
util
.
JoinUrlFragments
(
targetUrl
.
Path
,
proxyPath
)
req
.
URL
.
RawQuery
=
reqQueryVals
.
Encode
()
reqQueryVals
.
Add
(
"db"
,
ds
.
Database
)
if
!
ds
.
BasicAuth
{
req
.
Header
.
Del
(
"Authorization"
)
req
.
Header
.
Add
(
"Authorization"
,
util
.
GetBasicAuthHeader
(
ds
.
User
,
ds
.
Password
))
...
...
@@ -100,6 +102,10 @@ func ProxyDataSourceRequest(c *middleware.Context) {
if
ds
.
Type
==
m
.
DS_CLOUDWATCH
{
cloudwatch
.
HandleRequest
(
c
,
ds
)
}
else
if
ds
.
Type
==
m
.
DS_SQLDB
{
sqldb
.
HandleRequest
(
c
,
ds
)
}
else
{
proxyPath
:=
c
.
Params
(
"*"
)
proxy
:=
NewReverseProxy
(
ds
,
proxyPath
,
targetUrl
)
...
...
pkg/api/sqldb/sqldb.go
0 → 100644
View file @
6586cc40
package
sqldb
import
(
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strings"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
_
"github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_
"github.com/lib/pq"
_
"github.com/mattn/go-sqlite3"
)
type
sqlDataRequest
struct
{
Query
string
`json:"query"`
Body
[]
byte
`json:"-"`
}
type
seriesStruct
struct
{
Columns
[]
string
`json:"columns"`
Name
string
`json:"name"`
Values
[][]
interface
{}
`json:"values"`
}
type
resultsStruct
struct
{
Series
[]
seriesStruct
`json:"series"`
}
type
dataStruct
struct
{
Results
[]
resultsStruct
`json:"results"`
}
func
getEngine
(
ds
*
m
.
DataSource
)
(
*
xorm
.
Engine
,
error
)
{
dbms
,
err
:=
ds
.
JsonData
.
Get
(
"dbms"
)
.
String
()
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Invalid DBMS"
)
}
host
,
err
:=
ds
.
JsonData
.
Get
(
"host"
)
.
String
()
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Invalid host"
)
}
port
,
err
:=
ds
.
JsonData
.
Get
(
"port"
)
.
String
()
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"Invalid port"
)
}
constr
:=
""
switch
dbms
{
case
"mysql"
:
constr
=
fmt
.
Sprintf
(
"%s:%s@tcp(%s:%s)/%s?charset=utf8"
,
ds
.
User
,
ds
.
Password
,
host
,
port
,
ds
.
Database
)
case
"postgres"
:
sslEnabled
,
_
:=
ds
.
JsonData
.
Get
(
"ssl"
)
.
Bool
()
sslMode
:=
"disable"
if
sslEnabled
{
sslMode
=
"require"
}
constr
=
fmt
.
Sprintf
(
"user=%s password=%s host=%s port=%s dbname=%s sslmode=%s"
,
ds
.
User
,
ds
.
Password
,
host
,
port
,
ds
.
Database
,
sslMode
)
default
:
return
nil
,
fmt
.
Errorf
(
"Unknown DBMS: %s"
,
dbms
)
}
return
xorm
.
NewEngine
(
dbms
,
constr
)
}
func
getData
(
db
*
core
.
DB
,
req
*
sqlDataRequest
)
(
interface
{},
error
)
{
queries
:=
strings
.
Split
(
req
.
Query
,
";"
)
data
:=
dataStruct
{}
data
.
Results
=
make
([]
resultsStruct
,
1
)
data
.
Results
[
0
]
.
Series
=
make
([]
seriesStruct
,
0
)
for
i
:=
range
queries
{
if
queries
[
i
]
==
""
{
continue
}
rows
,
err
:=
db
.
Query
(
queries
[
i
])
if
err
!=
nil
{
return
nil
,
err
}
defer
rows
.
Close
()
name
:=
fmt
.
Sprintf
(
"table_%d"
,
i
+
1
)
series
,
err
:=
arrangeResult
(
rows
,
name
)
if
err
!=
nil
{
return
nil
,
err
}
data
.
Results
[
0
]
.
Series
=
append
(
data
.
Results
[
0
]
.
Series
,
series
.
(
seriesStruct
))
}
return
data
,
nil
}
func
arrangeResult
(
rows
*
core
.
Rows
,
name
string
)
(
interface
{},
error
)
{
columnNames
,
err
:=
rows
.
Columns
()
series
:=
seriesStruct
{}
series
.
Columns
=
columnNames
series
.
Name
=
name
for
rows
.
Next
()
{
columnValues
:=
make
([]
interface
{},
len
(
columnNames
))
err
=
rows
.
ScanSlice
(
&
columnValues
)
if
err
!=
nil
{
return
nil
,
err
}
// bytes -> string
for
i
:=
range
columnValues
{
switch
columnValues
[
i
]
.
(
type
)
{
case
[]
byte
:
columnValues
[
i
]
=
fmt
.
Sprintf
(
"%s"
,
columnValues
[
i
])
}
}
series
.
Values
=
append
(
series
.
Values
,
columnValues
)
}
return
series
,
err
}
func
HandleRequest
(
c
*
middleware
.
Context
,
ds
*
m
.
DataSource
)
{
var
req
sqlDataRequest
req
.
Body
,
_
=
ioutil
.
ReadAll
(
c
.
Req
.
Request
.
Body
)
json
.
Unmarshal
(
req
.
Body
,
&
req
)
log
.
Debug
(
"SQL request: query='%v'"
,
req
.
Query
)
engine
,
err
:=
getEngine
(
ds
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
"Unable to open SQL connection"
,
err
)
return
}
defer
engine
.
Close
()
session
:=
engine
.
NewSession
()
defer
session
.
Close
()
db
:=
session
.
DB
()
result
,
err
:=
getData
(
db
,
&
req
)
if
err
!=
nil
{
c
.
JsonApiErr
(
500
,
fmt
.
Sprintf
(
"Data error: %v, Query: %s"
,
err
.
Error
(),
req
.
Query
),
err
)
return
}
c
.
JSON
(
200
,
result
)
}
pkg/models/datasource.go
View file @
6586cc40
...
...
@@ -16,6 +16,7 @@ const (
DS_CLOUDWATCH
=
"cloudwatch"
DS_KAIROSDB
=
"kairosdb"
DS_PROMETHEUS
=
"prometheus"
DS_SQLDB
=
"sqldb"
DS_ACCESS_DIRECT
=
"direct"
DS_ACCESS_PROXY
=
"proxy"
)
...
...
@@ -59,6 +60,7 @@ var knownDatasourcePlugins map[string]bool = map[string]bool{
DS_CLOUDWATCH
:
true
,
DS_PROMETHEUS
:
true
,
DS_OPENTSDB
:
true
,
DS_SQLDB
:
true
,
"opennms"
:
true
,
"druid"
:
true
,
"dalmatinerdb"
:
true
,
...
...
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