mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
plugins: added state property to plugins
This commit is contained in:
parent
26804d630f
commit
1cd632cc43
@ -10,7 +10,6 @@ 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/log"
|
||||
"github.com/grafana/grafana/pkg/metrics"
|
||||
@ -99,16 +98,6 @@ func ProxyDataSourceRequest(c *middleware.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if ds.Type == m.DS_SQLDB {
|
||||
targetUrl, _ := url.Parse(ds.Url)
|
||||
if !checkWhiteList(c, targetUrl.Host) {
|
||||
return
|
||||
}
|
||||
|
||||
sqldb.HandleRequest(c, ds)
|
||||
return
|
||||
}
|
||||
|
||||
targetUrl, _ := url.Parse(ds.Url)
|
||||
if !checkWhiteList(c, targetUrl.Host) {
|
||||
return
|
||||
|
@ -21,6 +21,7 @@ type PluginSetting struct {
|
||||
|
||||
LatestVersion string `json:"latestVersion"`
|
||||
HasUpdate bool `json:"hasUpdate"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type PluginListItem struct {
|
||||
@ -33,6 +34,7 @@ type PluginListItem struct {
|
||||
LatestVersion string `json:"latestVersion"`
|
||||
HasUpdate bool `json:"hasUpdate"`
|
||||
DefaultNavUrl string `json:"defaultNavUrl"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type PluginList []PluginListItem
|
||||
|
@ -48,6 +48,7 @@ func GetPluginList(c *middleware.Context) Response {
|
||||
LatestVersion: pluginDef.GrafanaNetVersion,
|
||||
HasUpdate: pluginDef.GrafanaNetHasUpdate,
|
||||
DefaultNavUrl: pluginDef.DefaultNavUrl,
|
||||
State: pluginDef.State,
|
||||
}
|
||||
|
||||
if pluginSetting, exists := pluginSettingsMap[pluginDef.Id]; exists {
|
||||
@ -97,6 +98,7 @@ func GetPluginSettingById(c *middleware.Context) Response {
|
||||
DefaultNavUrl: def.DefaultNavUrl,
|
||||
LatestVersion: def.GrafanaNetVersion,
|
||||
HasUpdate: def.GrafanaNetHasUpdate,
|
||||
State: def.State,
|
||||
}
|
||||
|
||||
query := m.GetPluginSettingByIdQuery{PluginId: pluginId, OrgId: c.OrgId}
|
||||
|
@ -1,164 +0,0 @@
|
||||
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)
|
||||
}
|
@ -17,7 +17,6 @@ const (
|
||||
DS_CLOUDWATCH = "cloudwatch"
|
||||
DS_KAIROSDB = "kairosdb"
|
||||
DS_PROMETHEUS = "prometheus"
|
||||
DS_SQLDB = "sqldb"
|
||||
DS_ACCESS_DIRECT = "direct"
|
||||
DS_ACCESS_PROXY = "proxy"
|
||||
)
|
||||
@ -63,7 +62,6 @@ 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,
|
||||
|
@ -39,6 +39,7 @@ type PluginBase struct {
|
||||
Module string `json:"module"`
|
||||
BaseUrl string `json:"baseUrl"`
|
||||
HideFromList bool `json:"hideFromList"`
|
||||
State string `json:"state"`
|
||||
|
||||
IncludedInAppId string `json:"-"`
|
||||
PluginDir string `json:"-"`
|
||||
|
@ -93,6 +93,7 @@ export class DataSourceEditCtrl {
|
||||
this.hasDashboards = false;
|
||||
return this.backendSrv.get('/api/plugins/' + this.current.type + '/settings').then(pluginInfo => {
|
||||
this.datasourceMeta = pluginInfo;
|
||||
console.log(this.datasourceMeta) ;
|
||||
this.hasDashboards = _.find(pluginInfo.includes, {type: 'dashboard'});
|
||||
});
|
||||
}
|
||||
|
@ -48,6 +48,11 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info gf-form-group" ng-if="ctrl.datasourceMeta.state === 'alpha'">
|
||||
This plugin is marked as being in alpha state, which means it is in early development phase and
|
||||
updates will include breaking changes.
|
||||
</div>
|
||||
|
||||
<rebuild-on-change property="ctrl.datasourceMeta.id">
|
||||
<plugin-component type="datasource-config-ctrl">
|
||||
</plugin-component>
|
||||
|
@ -14,6 +14,7 @@
|
||||
}
|
||||
},
|
||||
|
||||
"state": "alpha",
|
||||
"alerting": true,
|
||||
"annotations": true,
|
||||
"metrics": true
|
||||
|
Loading…
Reference in New Issue
Block a user