From d0d995da09a93194fdd50fde85ef12249a15b0d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 28 Feb 2015 17:27:30 +0100 Subject: [PATCH] Inital work on SQL metric/annotation data source, #1542 --- pkg/api/api.go | 2 +- pkg/api/datasources.go | 10 +--- pkg/api/dtos/models.go | 23 ++++---- pkg/models/datasource.go | 23 ++++---- pkg/services/sqlstore/datasource.go | 3 +- src/app/plugins/datasource/sql/datasource.js | 20 +++++++ .../datasource/sql/partials/config.html | 53 +++++++++++++++++++ .../datasource/sql/partials/query.editor.html | 17 ++++++ src/app/plugins/datasource/sql/plugin.json | 16 ++++++ 9 files changed, 136 insertions(+), 31 deletions(-) create mode 100644 src/app/plugins/datasource/sql/datasource.js create mode 100644 src/app/plugins/datasource/sql/partials/config.html create mode 100644 src/app/plugins/datasource/sql/partials/query.editor.html create mode 100644 src/app/plugins/datasource/sql/plugin.json diff --git a/pkg/api/api.go b/pkg/api/api.go index f5b7b69bea3..e068cf044ba 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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) diff --git a/pkg/api/datasources.go b/pkg/api/datasources.go index 9dfa698769c..e405ffea3e2 100644 --- a/pkg/api/datasources.go +++ b/pkg/api/datasources.go @@ -61,6 +61,7 @@ func GetDataSourceById(c *middleware.Context) { User: ds.User, BasicAuth: ds.BasicAuth, IsDefault: ds.IsDefault, + JsonData: ds.JsonData, }) } @@ -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) diff --git a/pkg/api/dtos/models.go b/pkg/api/dtos/models.go index d7c6c955882..e5540160e1f 100644 --- a/pkg/api/dtos/models.go +++ b/pkg/api/dtos/models.go @@ -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 { diff --git a/pkg/models/datasource.go b/pkg/models/datasource.go index 0b1f0786081..5bb6539b3de 100644 --- a/pkg/models/datasource.go +++ b/pkg/models/datasource.go @@ -38,6 +38,7 @@ type DataSource struct { BasicAuthUser string BasicAuthPassword string IsDefault bool + JsonData map[string]interface{} Created time.Time Updated time.Time @@ -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 { diff --git a/pkg/services/sqlstore/datasource.go b/pkg/services/sqlstore/datasource.go index 7da46ae72da..41751fcafeb 100644 --- a/pkg/services/sqlstore/datasource.go +++ b/pkg/services/sqlstore/datasource.go @@ -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") diff --git a/src/app/plugins/datasource/sql/datasource.js b/src/app/plugins/datasource/sql/datasource.js new file mode 100644 index 00000000000..591dc5a5bb4 --- /dev/null +++ b/src/app/plugins/datasource/sql/datasource.js @@ -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; + + }); + +}); diff --git a/src/app/plugins/datasource/sql/partials/config.html b/src/app/plugins/datasource/sql/partials/config.html new file mode 100644 index 00000000000..e6b7749a2f7 --- /dev/null +++ b/src/app/plugins/datasource/sql/partials/config.html @@ -0,0 +1,53 @@ +

SQL Options

+ +
+ +
+
+
+ +
+
+
+ +
+
+ diff --git a/src/app/plugins/datasource/sql/partials/query.editor.html b/src/app/plugins/datasource/sql/partials/query.editor.html new file mode 100644 index 00000000000..0d6d21d0ad2 --- /dev/null +++ b/src/app/plugins/datasource/sql/partials/query.editor.html @@ -0,0 +1,17 @@ + +
+
+
+
Test graph
+ +

+ 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 Add query button. +

+
+
+ +
+
+ diff --git a/src/app/plugins/datasource/sql/plugin.json b/src/app/plugins/datasource/sql/plugin.json new file mode 100644 index 00000000000..b74471e20d8 --- /dev/null +++ b/src/app/plugins/datasource/sql/plugin.json @@ -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 +}