mirror of https://github.com/grafana/grafana
parent
d7cd2b970e
commit
f5b1b192a0
@ -0,0 +1,60 @@ |
||||
# Description: |
||||
# A way to interact with the Google Images API. |
||||
# |
||||
# Commands: |
||||
# hubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result. |
||||
# hubot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead. |
||||
# hubot mustache me <url> - Adds a mustache to the specified URL. |
||||
# hubot mustache me <query> - Searches Google Images for the specified query and mustaches it. |
||||
|
||||
module.exports = (robot) -> |
||||
robot.hear /grafana (.*)/i, (msg) -> |
||||
sendUrl msg.match[1] |
||||
|
||||
robot.router.get '/hubot/test', (req, res) -> |
||||
sendUrl() |
||||
res.send 'OK ' |
||||
|
||||
imageMe = (msg, cb) -> |
||||
cb 'http://localhost:3000/render/dashboard/solo/grafana-play-home?from=now-1h&to=now&panelId=4&fullscreen' |
||||
|
||||
sendUrl = (params) -> |
||||
https = require 'https' |
||||
querystring = require 'querystring' |
||||
opts = params.split(' ') |
||||
dashboard = opts[0] |
||||
panelId = opts[1] |
||||
from = opts[2] |
||||
|
||||
imageUrl = "http://localhost:3000/render/dashboard/solo/#{dashboard}/?panelId=#{panelId}" |
||||
link = "http://localhost:3000/dashboard/db/#{dashboard}/?panelId=#{panelId}&fullscreen" |
||||
if from |
||||
imageUrl += "&from=#{from}" |
||||
link += "&from=#{from}" |
||||
|
||||
console.log 'imageUrl: ' + imageUrl |
||||
|
||||
hipchat = {} |
||||
hipchat.format = 'json' |
||||
hipchat.auth_token = process.env.HUBOT_HIPCHAT_TOKEN |
||||
console.log 'token: ' + hipchat.auth_token |
||||
|
||||
hipchat.room_id = '877465' |
||||
hipchat.message = "<a href='#{link}'><img src='#{imageUrl}'></img></a>" |
||||
hipchat.from = "hubot" |
||||
hipchat.message_format = "html" |
||||
|
||||
params = querystring.stringify(hipchat) |
||||
|
||||
path = "/v1/rooms/message/?#{params}" |
||||
|
||||
data = '' |
||||
|
||||
https.get {host: 'api.hipchat.com', path: path}, (res) -> |
||||
res.on 'data', (chunk) -> |
||||
data += chunk.toString() |
||||
res.on 'end', () -> |
||||
json = JSON.parse(data) |
||||
console.log "Hipchat response ", data |
||||
|
||||
|
||||
@ -0,0 +1,109 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"path" |
||||
"strings" |
||||
|
||||
"github.com/go-xorm/xorm" |
||||
_ "github.com/mattn/go-sqlite3" |
||||
"github.com/torkelo/grafana-pro/pkg/setting" |
||||
) |
||||
|
||||
var ( |
||||
x *xorm.Engine |
||||
tables []interface{} |
||||
|
||||
HasEngine bool |
||||
|
||||
DbCfg struct { |
||||
Type, Host, Name, User, Pwd, Path, SslMode string |
||||
} |
||||
|
||||
UseSQLite3 bool |
||||
) |
||||
|
||||
type AccountDto struct { |
||||
Id int64 |
||||
Email string `xorm:"UNIQUE NOT NULL"` |
||||
Passwd string `xorm:"NOT NULL"` |
||||
} |
||||
|
||||
func init() { |
||||
tables = append(tables, new(AccountDto)) |
||||
} |
||||
|
||||
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", "passwd") |
||||
} |
||||
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 |
||||
} |
||||
if err = x.Sync2(tables...); err != nil { |
||||
return fmt.Errorf("sync database struct error: %v\n", 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) |
||||
} |
||||
|
||||
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) |
||||
} |
||||
x.Logger = xorm.NewSimpleLogger(f) |
||||
|
||||
x.ShowSQL = true |
||||
x.ShowInfo = true |
||||
x.ShowDebug = true |
||||
x.ShowErr = true |
||||
x.ShowWarn = true |
||||
return nil |
||||
} |
||||
|
||||
func getEngine() (*xorm.Engine, error) { |
||||
cnnstr := "" |
||||
switch DbCfg.Type { |
||||
case "mysql": |
||||
cnnstr = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", |
||||
DbCfg.User, DbCfg.Pwd, DbCfg.Host, DbCfg.Name) |
||||
case "postgres": |
||||
var host, port = "127.0.0.1", "5432" |
||||
fields := strings.Split(DbCfg.Host, ":") |
||||
if len(fields) > 0 && len(strings.TrimSpace(fields[0])) > 0 { |
||||
host = fields[0] |
||||
} |
||||
if len(fields) > 1 && len(strings.TrimSpace(fields[1])) > 0 { |
||||
port = fields[1] |
||||
} |
||||
cnnstr = fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s", |
||||
DbCfg.User, DbCfg.Pwd, host, port, DbCfg.Name, DbCfg.SslMode) |
||||
case "sqlite3": |
||||
os.MkdirAll(path.Dir(DbCfg.Path), os.ModePerm) |
||||
cnnstr = "file:" + DbCfg.Path + "?cache=shared&mode=rwc" |
||||
default: |
||||
return nil, fmt.Errorf("Unknown database type: %s", DbCfg.Type) |
||||
} |
||||
return xorm.NewEngine(DbCfg.Type, cnnstr) |
||||
} |
||||
@ -0,0 +1,40 @@ |
||||
package sqlstore |
||||
|
||||
import "github.com/torkelo/grafana-pro/pkg/log" |
||||
|
||||
func SaveAccount() error { |
||||
var err error |
||||
|
||||
sess := x.NewSession() |
||||
defer sess.Close() |
||||
|
||||
if err = sess.Begin(); err != nil { |
||||
return err |
||||
} |
||||
|
||||
u := &AccountDto{ |
||||
Email: "asdasdas", |
||||
Passwd: "MyPassWd", |
||||
} |
||||
|
||||
if _, err = sess.Insert(u); err != nil { |
||||
sess.Rollback() |
||||
return err |
||||
} else if err = sess.Commit(); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func GetAccounts() { |
||||
var resp = make([]*AccountDto, 1) |
||||
err := x.Find(&resp) |
||||
if err != nil { |
||||
log.Error(4, "Error", err) |
||||
} |
||||
|
||||
for _, i := range resp { |
||||
log.Info("Item %v", i) |
||||
} |
||||
} |
||||
Loading…
Reference in new issue