Skip to content

Commit

Permalink
Inital work on SQL metric/annotation data source, #1542
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelo committed Feb 28, 2015
1 parent a3fe1ef commit d0d995d
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Register(r *macaron.Macaron) {

// Data sources
r.Group("/datasources", func() {
r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(UpdateDataSource)
r.Combo("/").Get(GetDataSources).Put(AddDataSource).Post(bind(m.UpdateDataSourceCommand{}), UpdateDataSource)
r.Delete("/:id", DeleteDataSource)
r.Get("/:id", GetDataSourceById)
r.Get("/plugins", GetDataSourcePlugins)
Expand Down
10 changes: 2 additions & 8 deletions pkg/api/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func GetDataSourceById(c *middleware.Context) {
User: ds.User,
BasicAuth: ds.BasicAuth,
IsDefault: ds.IsDefault,
JsonData: ds.JsonData,
})
}

Expand Down Expand Up @@ -101,14 +102,7 @@ func AddDataSource(c *middleware.Context) {
c.JsonOK("Datasource added")
}

func UpdateDataSource(c *middleware.Context) {
cmd := m.UpdateDataSourceCommand{}

if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation failed", nil)
return
}

func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) {
cmd.OrgId = c.OrgId

err := bus.Dispatch(&cmd)
Expand Down
23 changes: 12 additions & 11 deletions pkg/api/dtos/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ type Dashboard struct {
}

type DataSource struct {
Id int64 `json:"id"`
OrgId int64 `json:"orgId"`
Name string `json:"name"`
Type string `json:"type"`
Access m.DsAccess `json:"access"`
Url string `json:"url"`
Password string `json:"password"`
User string `json:"user"`
Database string `json:"database"`
BasicAuth bool `json:"basicAuth"`
IsDefault bool `json:"isDefault"`
Id int64 `json:"id"`
OrgId int64 `json:"orgId"`
Name string `json:"name"`
Type string `json:"type"`
Access m.DsAccess `json:"access"`
Url string `json:"url"`
Password string `json:"password"`
User string `json:"user"`
Database string `json:"database"`
BasicAuth bool `json:"basicAuth"`
IsDefault bool `json:"isDefault"`
JsonData map[string]interface{} `json:"jsonData"`
}

type MetricQueryResultDto struct {
Expand Down
23 changes: 13 additions & 10 deletions pkg/models/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type DataSource struct {
BasicAuthUser string
BasicAuthPassword string
IsDefault bool
JsonData map[string]interface{}

Created time.Time
Updated time.Time
Expand All @@ -63,16 +64,18 @@ type AddDataSourceCommand struct {

// Also acts as api DTO
type UpdateDataSourceCommand struct {
Id int64
OrgId int64
Name string
Type string
Access DsAccess
Url string
Password string
User string
Database string
IsDefault bool
Id int64 `json:"id" binding:"Required"`
Name string `json:"name" binding:"Required"`
Type string `json:"type" binding:"Required"`
Access DsAccess `json:"access" binding:"Required"`
Url string `json:"url"`
Password string `json:"password"`
User string `json:"user"`
Database string `json:"database"`
IsDefault bool `json:"isDefault"`
JsonData map[string]interface{} `json:"jsonData"`

OrgId int64 `json:"-"`
}

type DeleteDataSourceCommand struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/services/sqlstore/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error {
User: cmd.User,
Password: cmd.Password,
Database: cmd.Database,
Updated: time.Now(),
IsDefault: cmd.IsDefault,
JsonData: cmd.JsonData,
Updated: time.Now(),
}

sess.UseBool("is_default")
Expand Down
20 changes: 20 additions & 0 deletions src/app/plugins/datasource/sql/datasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
define([
'angular',
'lodash',
'kbn',
],
function (angular) {
'use strict';

var module = angular.module('grafana.services');

module.factory('SqlDatasource', function() {

function SqlDatasource() {
}

return SqlDatasource;

});

});
53 changes: 53 additions & 0 deletions src/app/plugins/datasource/sql/partials/config.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<h2>SQL Options</h2>

<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
DB Type
</li>
<li>
<select class="input-medium tight-form-input" ng-model="current.jsonData.dbType" ng-options="f for f in ['sqlite3','mysql','postgres']"></select>
</li>
<li class="tight-form-item" style="width: 80px">
Host
</li>
<li>
<input type="text" class="tight-form-input input-medium" ng-model='current.jsonData.host' placeholder="localhost:3306">
</li>
<li class="tight-form-item" ng-if="current.jsonData.dbType === 'postgres'">
SSL&nbsp;
<input class="cr1" id="jsonData.ssl" type="checkbox" ng-model="current.jsonData.ssl" ng-checked="current.jsonData.ssl">
<label for="jsonData.ssl" class="cr1"></label>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
Database
</li>
<li>
<input type="text" class="tight-form-input input-medium" ng-model='current.database' placeholder="">
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="tight-form">
<ul class="tight-form-list">
<li class="tight-form-item" style="width: 80px">
User
</li>
<li>
<input type="text" class="tight-form-input input-medium" ng-model='current.user' placeholder="">
</li>
<li class="tight-form-item" style="width: 80px">
Password
</li>
<li>
<input type="password" class="tight-form-input input-medium" ng-model='current.password' placeholder="">
</li>
</ul>
<div class="clearfix"></div>
</div>

17 changes: 17 additions & 0 deletions src/app/plugins/datasource/sql/partials/query.editor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

<div class="fluid-row" style="margin-top: 20px">
<div class="span2"></div>
<div class="grafana-info-box span8">
<h5>Test graph</h5>

<p>
This is just a test data source that generates random walk series. If this is your only data source
open the left side menu and navigate to the data sources admin screen and add your data sources. You can change
data source using the button to the left of the <strong>Add query</strong> button.
</p>
</div>
<div class="span2"></div>

<div class="clearfix"></div>
</div>

16 changes: 16 additions & 0 deletions src/app/plugins/datasource/sql/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"pluginType": "datasource",
"name": "Generic SQL (prototype)",

"type": "generic_sql",
"serviceName": "SqlDatasource",

"module": "plugins/datasource/sql/datasource",

"partials": {
"config": "app/plugins/datasource/sql/partials/config.html",
"query": "app/plugins/datasource/sql/partials/query.editor.html"
},

"metrics": true
}

0 comments on commit d0d995d

Please sign in to comment.