Commit 07d8b542 by Torkel Ödegaard

working on rethinkdb stuff

parent d8dca203
Subproject commit 34ab1e529b499af836631f8076c2c4df02be5860 Subproject commit 06de80f2a764b76df6047fc4700c74aaf5734521
go get code.google.com/p/goprotobuf/{proto,protoc-gen-go}
package api package api
import ( import (
log "github.com/alecthomas/log4go"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/torkelo/grafana-pro/pkg/models" "github.com/torkelo/grafana-pro/pkg/models"
) )
...@@ -16,7 +17,7 @@ func init() { ...@@ -16,7 +17,7 @@ func init() {
func (self *HttpServer) getDashboard(c *gin.Context) { func (self *HttpServer) getDashboard(c *gin.Context) {
id := c.Params.ByName("id") id := c.Params.ByName("id")
dash, err := self.store.GetById(id) dash, err := self.store.GetDashboardByTitle(id, "test")
if err != nil { if err != nil {
c.JSON(404, newErrorResponse("Dashboard not found")) c.JSON(404, newErrorResponse("Dashboard not found"))
return return
...@@ -30,6 +31,7 @@ func (self *HttpServer) search(c *gin.Context) { ...@@ -30,6 +31,7 @@ func (self *HttpServer) search(c *gin.Context) {
results, err := self.store.Query(query) results, err := self.store.Query(query)
if err != nil { if err != nil {
log.Error("Store query error: %v", err)
c.JSON(500, newErrorResponse("Failed")) c.JSON(500, newErrorResponse("Failed"))
return return
} }
...@@ -41,9 +43,17 @@ func (self *HttpServer) postDashboard(c *gin.Context) { ...@@ -41,9 +43,17 @@ func (self *HttpServer) postDashboard(c *gin.Context) {
var command saveDashboardCommand var command saveDashboardCommand
if c.EnsureBody(&command) { if c.EnsureBody(&command) {
err := self.store.Save(&models.Dashboard{Data: command.Dashboard}) dashboard := models.NewDashboard("test")
dashboard.Data = command.Dashboard
dashboard.Title = dashboard.Data["title"].(string)
if dashboard.Data["id"] != nil {
dashboard.Id = dashboard.Data["id"].(string)
}
err := self.store.SaveDashboard(dashboard)
if err == nil { if err == nil {
c.JSON(200, gin.H{"status": "saved"}) c.JSON(200, gin.H{"status": "success", "id": dashboard.Id})
return return
} }
} }
......
...@@ -3,20 +3,39 @@ package models ...@@ -3,20 +3,39 @@ package models
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"time"
) )
type Dashboard struct { type Dashboard struct {
Data map[string]interface{} Id string `gorethink:"id,omitempty"`
AccountId string
LastModifiedByUserId string
LastModifiedByDate time.Time
CreatedDate time.Time
Title string
Tags []string
Data map[string]interface{}
}
type UserContext struct {
UserId string
AccountId string
} }
type SearchResult struct { type SearchResult struct {
Type string `json:"title"`
Id string `json:"id"` Id string `json:"id"`
Title string `json:"title"` Title string `json:"title"`
} }
func NewDashboard(title string) *Dashboard { func NewDashboard(title string) *Dashboard {
dash := &Dashboard{} dash := &Dashboard{}
dash.Id = ""
dash.AccountId = "test"
dash.LastModifiedByDate = time.Now()
dash.CreatedDate = time.Now()
dash.LastModifiedByUserId = "123"
dash.Title = title
dash.Data = make(map[string]interface{}) dash.Data = make(map[string]interface{})
dash.Data["title"] = title dash.Data["title"] = title
...@@ -31,23 +50,11 @@ func NewFromJson(reader io.Reader) (*Dashboard, error) { ...@@ -31,23 +50,11 @@ func NewFromJson(reader io.Reader) (*Dashboard, error) {
return nil, err return nil, err
} }
return dash, nil dash.Title = dash.Data["title"].(string)
}
/*type DashboardServices struct { return dash, nil
}
type DashboardServicesFilter struct {
} }
type DashboardServicesFilterTime struct {
From string To string
}*/
func (dash *Dashboard) GetString(prop string) string { func (dash *Dashboard) GetString(prop string) string {
return dash.Data[prop].(string) return dash.Data[prop].(string)
} }
func (dash *Dashboard) Title() string {
return dash.GetString("title")
}
package stores package stores
import ( //
"encoding/json" // import (
"io" // "encoding/json"
"os" // "io"
"path/filepath" // "os"
"strings" // "path/filepath"
// "strings"
log "github.com/alecthomas/log4go" //
"github.com/torkelo/grafana-pro/pkg/models" // log "github.com/alecthomas/log4go"
) // "github.com/torkelo/grafana-pro/pkg/models"
// )
type fileStore struct { //
dataDir string // type fileStore struct {
dashDir string // dataDir string
cache map[string]*models.Dashboard // dashDir string
} // cache map[string]*models.Dashboard
// }
func NewFileStore(dataDir string) *fileStore { //
// func NewFileStore(dataDir string) *fileStore {
if dirDoesNotExist(dataDir) { //
log.Crashf("FileStore failed to initialize, dataDir does not exist %v", dataDir) // if dirDoesNotExist(dataDir) {
} // log.Crashf("FileStore failed to initialize, dataDir does not exist %v", dataDir)
// }
dashDir := filepath.Join(dataDir, "dashboards") //
// dashDir := filepath.Join(dataDir, "dashboards")
if dirDoesNotExist(dashDir) { //
log.Debug("Did not find dashboard dir, creating...") // if dirDoesNotExist(dashDir) {
err := os.Mkdir(dashDir, 0777) // log.Debug("Did not find dashboard dir, creating...")
if err != nil { // err := os.Mkdir(dashDir, 0777)
log.Crashf("FileStore failed to initialize, could not create directory %v, error: %v", dashDir, err) // if err != nil {
} // log.Crashf("FileStore failed to initialize, could not create directory %v, error: %v", dashDir, err)
} // }
// }
store := &fileStore{} //
store.dataDir = dataDir // store := &fileStore{}
store.dashDir = dashDir // store.dataDir = dataDir
store.cache = make(map[string]*models.Dashboard) // store.dashDir = dashDir
go store.scanFiles() // store.cache = make(map[string]*models.Dashboard)
// store.scanFiles()
return store //
} // return store
// }
func (store *fileStore) scanFiles() { //
visitor := func(path string, f os.FileInfo, err error) error { // func (store *fileStore) scanFiles() {
if err != nil { // visitor := func(path string, f os.FileInfo, err error) error {
return err // if err != nil {
} // return err
if f.IsDir() { // }
return nil // if f.IsDir() {
} // return nil
if strings.HasSuffix(f.Name(), ".json") { // }
err = store.loadDashboardIntoCache(path) // if strings.HasSuffix(f.Name(), ".json") {
if err != nil { // err = store.loadDashboardIntoCache(path)
return err // if err != nil {
} // return err
} // }
return nil // }
} // return nil
// }
err := filepath.Walk(store.dashDir, visitor) //
if err != nil { // err := filepath.Walk(store.dashDir, visitor)
log.Error("FileStore::updateCache failed %v", err) // if err != nil {
} // log.Error("FileStore::updateCache failed %v", err)
} // }
// }
func (store fileStore) loadDashboardIntoCache(filename string) error { //
log.Info("Loading dashboard file %v into cache", filename) // func (store fileStore) loadDashboardIntoCache(filename string) error {
dash, err := loadDashboardFromFile(filename) // log.Info("Loading dashboard file %v into cache", filename)
if err != nil { // dash, err := loadDashboardFromFile(filename)
return err // if err != nil {
} // return err
// }
store.cache[dash.Title()] = dash //
// store.cache[dash.Title] = dash
return nil //
} // return nil
// }
func (store *fileStore) Close() { //
// func (store *fileStore) Close() {
} //
// }
func (store *fileStore) GetById(id string) (*models.Dashboard, error) { //
log.Debug("FileStore::GetById id = %v", id) // func (store *fileStore) GetById(id string) (*models.Dashboard, error) {
filename := store.getFilePathForDashboard(id) // log.Debug("FileStore::GetById id = %v", id)
// filename := store.getFilePathForDashboard(id)
return loadDashboardFromFile(filename) //
} // return loadDashboardFromFile(filename)
// }
func (store *fileStore) Save(dash *models.Dashboard) error { //
filename := store.getFilePathForDashboard(dash.Title()) // func (store *fileStore) Save(dash *models.Dashboard) error {
// filename := store.getFilePathForDashboard(dash.Title)
log.Debug("Saving dashboard %v to %v", dash.Title(), filename) //
// log.Debug("Saving dashboard %v to %v", dash.Title, filename)
var err error //
var data []byte // var err error
if data, err = json.Marshal(dash.Data); err != nil { // var data []byte
return err // if data, err = json.Marshal(dash.Data); err != nil {
} // return err
// }
return writeFile(filename, data) //
} // return writeFile(filename, data)
// }
func (store *fileStore) Query(query string) ([]*models.SearchResult, error) { //
results := make([]*models.SearchResult, 0, 50) // func (store *fileStore) Query(query string) ([]*models.SearchResult, error) {
// results := make([]*models.SearchResult, 0, 50)
for _, dash := range store.cache { //
item := &models.SearchResult{ // for _, dash := range store.cache {
Id: dash.Title(), // item := &models.SearchResult{
Type: "dashboard", // Id: dash.Title,
} // Type: "dashboard",
results = append(results, item) // }
} // results = append(results, item)
// }
return results, nil //
} // return results, nil
// }
func loadDashboardFromFile(filename string) (*models.Dashboard, error) { //
log.Debug("FileStore::loading dashboard from file %v", filename) // func loadDashboardFromFile(filename string) (*models.Dashboard, error) {
// log.Debug("FileStore::loading dashboard from file %v", filename)
configFile, err := os.Open(filename) //
if err != nil { // configFile, err := os.Open(filename)
return nil, err // if err != nil {
} // return nil, err
// }
return models.NewFromJson(configFile) //
} // return models.NewFromJson(configFile)
// }
func (store *fileStore) getFilePathForDashboard(id string) string { //
id = strings.ToLower(id) // func (store *fileStore) getFilePathForDashboard(id string) string {
id = strings.Replace(id, " ", "-", -1) // id = strings.ToLower(id)
return filepath.Join(store.dashDir, id) + ".json" // id = strings.Replace(id, " ", "-", -1)
} // return filepath.Join(store.dashDir, id) + ".json"
// }
func dirDoesNotExist(dir string) bool { //
_, err := os.Stat(dir) // func dirDoesNotExist(dir string) bool {
return os.IsNotExist(err) // _, err := os.Stat(dir)
} // return os.IsNotExist(err)
// }
func writeFile(filename string, data []byte) error { //
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) // func writeFile(filename string, data []byte) error {
if err != nil { // f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
return err // if err != nil {
} // return err
n, err := f.Write(data) // }
if err == nil && n < len(data) { // n, err := f.Write(data)
err = io.ErrShortWrite // if err == nil && n < len(data) {
} // err = io.ErrShortWrite
if err1 := f.Close(); err == nil { // }
err = err1 // if err1 := f.Close(); err == nil {
} // err = err1
// }
return err //
} // return err
// }
package stores package stores
import ( //
"fmt" // import (
"io" // "fmt"
"io/ioutil" // "io"
"os" // "io/ioutil"
"path/filepath" // "os"
"testing" // "path/filepath"
// "testing"
. "github.com/smartystreets/goconvey/convey" //
"github.com/torkelo/grafana-pro/pkg/models" // . "github.com/smartystreets/goconvey/convey"
) // "github.com/torkelo/grafana-pro/pkg/models"
// )
func TestFileStore(t *testing.T) { //
// func TestFileStore(t *testing.T) {
GivenFileStore("When saving a dashboard", t, func(store *fileStore) { //
dashboard := models.NewDashboard("hello") // GivenFileStore("When saving a dashboard", t, func(store *fileStore) {
// dashboard := models.NewDashboard("hello")
err := store.Save(dashboard) //
// err := store.Save(dashboard)
Convey("should be saved to disk", func() { //
So(err, ShouldBeNil) // Convey("should be saved to disk", func() {
// So(err, ShouldBeNil)
_, err = os.Stat(store.getFilePathForDashboard("hello")) //
So(err, ShouldBeNil) // _, err = os.Stat(store.getFilePathForDashboard("hello"))
}) // So(err, ShouldBeNil)
}) // })
// })
GivenFileStore("When getting a saved dashboard", t, func(store *fileStore) { //
copyDashboardToTempData("default.json", "", store.dashDir) // GivenFileStore("When getting a saved dashboard", t, func(store *fileStore) {
dash, err := store.GetById("default") // copyDashboardToTempData("default.json", "", store.dashDir)
// dash, err := store.GetById("default")
Convey("should be read from disk", func() { //
So(err, ShouldBeNil) // Convey("should be read from disk", func() {
So(dash, ShouldNotBeNil) // So(err, ShouldBeNil)
// So(dash, ShouldNotBeNil)
So(dash.Title(), ShouldEqual, "Grafana Play Home") //
}) // So(dash.Title, ShouldEqual, "Grafana Play Home")
}) // })
// })
GivenFileStore("when getting dashboard with capital letters", t, func(store *fileStore) { //
copyDashboardToTempData("annotations.json", "", store.dashDir) // GivenFileStore("when getting dashboard with capital letters", t, func(store *fileStore) {
dash, err := store.GetById("AnnoTations") // copyDashboardToTempData("annotations.json", "", store.dashDir)
// dash, err := store.GetById("AnnoTations")
Convey("should be read from disk", func() { //
So(err, ShouldBeNil) // Convey("should be read from disk", func() {
So(dash, ShouldNotBeNil) // So(err, ShouldBeNil)
// So(dash, ShouldNotBeNil)
So(dash.Title(), ShouldEqual, "Annotations") //
}) // So(dash.Title, ShouldEqual, "Annotations")
}) // })
// })
GivenFileStore("When copying dashboards into data dir", t, func(store *fileStore) { //
copyDashboardToTempData("annotations.json", "", store.dashDir) // GivenFileStore("When copying dashboards into data dir", t, func(store *fileStore) {
copyDashboardToTempData("default.json", "", store.dashDir) // copyDashboardToTempData("annotations.json", "", store.dashDir)
copyDashboardToTempData("graph-styles.json", "", store.dashDir) // copyDashboardToTempData("default.json", "", store.dashDir)
store.scanFiles() // copyDashboardToTempData("graph-styles.json", "", store.dashDir)
// store.scanFiles()
Convey("scan should generate index of all dashboards", func() { //
// Convey("scan should generate index of all dashboards", func() {
result, err := store.Query("*") //
So(err, ShouldBeNil) // result, err := store.Query("*")
So(len(result), ShouldEqual, 3) // So(err, ShouldBeNil)
}) // So(len(result), ShouldEqual, 3)
}) // })
} // })
// }
func copyDashboardToTempData(name string, destName string, dir string) { //
if destName == "" { // func copyDashboardToTempData(name string, destName string, dir string) {
destName = name // if destName == "" {
} // destName = name
source, _ := filepath.Abs("../../data/dashboards/" + name) // }
dest := filepath.Join(dir, destName) // source, _ := filepath.Abs("../../data/dashboards/" + name)
err := copyFile(dest, source) // dest := filepath.Join(dir, destName)
if err != nil { // err := copyFile(dest, source)
panic(fmt.Sprintf("failed to copy file %v", name)) // if err != nil {
} // panic(fmt.Sprintf("failed to copy file %v", name))
} // }
// }
func GivenFileStore(desc string, t *testing.T, f func(store *fileStore)) { //
Convey(desc, t, func() { // func GivenFileStore(desc string, t *testing.T, f func(store *fileStore)) {
tempDir, _ := ioutil.TempDir("", "store") // Convey(desc, t, func() {
// tempDir, _ := ioutil.TempDir("", "store")
store := NewFileStore(tempDir) //
// store := NewFileStore(tempDir)
f(store) //
// f(store)
Reset(func() { //
os.RemoveAll(tempDir) // Reset(func() {
}) // os.RemoveAll(tempDir)
}) // })
} // })
// }
func copyFile(dst, src string) error { //
in, err := os.Open(src) // func copyFile(dst, src string) error {
if err != nil { // in, err := os.Open(src)
return err // if err != nil {
} // return err
defer in.Close() // }
out, err := os.Create(dst) // defer in.Close()
if err != nil { // out, err := os.Create(dst)
return err // if err != nil {
} // return err
defer out.Close() // }
_, err = io.Copy(out, in) // defer out.Close()
cerr := out.Close() // _, err = io.Copy(out, in)
if err != nil { // cerr := out.Close()
return err // if err != nil {
} // return err
return cerr // }
} // return cerr
// }
package stores
import (
"time"
log "github.com/alecthomas/log4go"
r "github.com/dancannon/gorethink"
"github.com/torkelo/grafana-pro/pkg/models"
)
type rethinkStore struct {
session *r.Session
}
type RethinkCfg struct {
DatabaseName string
}
func NewRethinkStore(config *RethinkCfg) *rethinkStore {
log.Info("Initializing rethink storage")
session, err := r.Connect(r.ConnectOpts{
Address: "localhost:28015",
Database: config.DatabaseName,
MaxIdle: 10,
IdleTimeout: time.Second * 10,
})
if err != nil {
log.Crash("Failed to connect to rethink database %v", err)
}
r.DbCreate(config.DatabaseName).Exec(session)
r.Db(config.DatabaseName).TableCreate("dashboards").Exec(session)
r.Db(config.DatabaseName).Table("dashboards").IndexCreateFunc("AccountIdTitle", func(row r.Term) interface{} {
return []interface{}{row.Field("AccountId"), row.Field("Title")}
}).Exec(session)
return &rethinkStore{
session: session,
}
}
func (self *rethinkStore) SaveDashboard(dash *models.Dashboard) error {
resp, err := r.Table("dashboards").Insert(dash).RunWrite(self.session)
if err != nil {
return err
}
log.Info("Inserted: %v, Errors: %v, Updated: %v", resp.Inserted, resp.Errors, resp.Updated)
log.Info("First error:", resp.FirstError)
if len(resp.GeneratedKeys) > 0 {
dash.Id = resp.GeneratedKeys[0]
}
return nil
}
func (self *rethinkStore) GetDashboardByTitle(title string, accountId string) (*models.Dashboard, error) {
resp, err := r.Table("dashboards").GetAllByIndex("AccountIdTitle", []interface{}{accountId, title}).Run(self.session)
if err != nil {
return nil, err
}
var dashboard models.Dashboard
err = resp.One(&dashboard)
if err != nil {
return nil, err
}
return &dashboard, nil
}
func (self *rethinkStore) Query(query string) ([]*models.SearchResult, error) {
docs, err := r.Table("dashboards").Filter(r.Row.Field("Title").Match(".*")).Run(self.session)
if err != nil {
return nil, err
}
results := make([]*models.SearchResult, 0, 50)
var dashboard models.Dashboard
for docs.Next(&dashboard) {
log.Info("title: ", dashboard.Title)
results = append(results, &models.SearchResult{
Title: dashboard.Title,
Id: dashboard.Id,
})
}
return results, nil
}
func (self *rethinkStore) Close() {}
package stores
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/torkelo/grafana-pro/pkg/models"
)
func TestRethinkStore(t *testing.T) {
Convey("Insert dashboard", t, func() {
store := NewRethinkStore(&RethinkCfg{DatabaseName: "tests"})
//defer r.DbDrop("tests").Exec(store.session)
dashboard := models.NewDashboard("test")
dashboard.AccountId = "123"
err := store.SaveDashboard(dashboard)
So(err, ShouldBeNil)
So(dashboard.Id, ShouldNotBeEmpty)
read, err := store.GetDashboardByTitle("test", "123")
So(err, ShouldBeNil)
So(read, ShouldNotBeNil)
})
}
...@@ -5,12 +5,12 @@ import ( ...@@ -5,12 +5,12 @@ import (
) )
type Store interface { type Store interface {
GetById(id string) (*models.Dashboard, error) GetDashboardByTitle(id string, accountId string) (*models.Dashboard, error)
Save(dash *models.Dashboard) error SaveDashboard(dash *models.Dashboard) error
Query(query string) ([]*models.SearchResult, error) Query(query string) ([]*models.SearchResult, error)
Close() Close()
} }
func New() Store { func New() Store {
return NewFileStore("data") return NewRethinkStore(&RethinkCfg{DatabaseName: "grafana"})
} }
docker kill gfdev
docker rm gfdev
docker run -d -p 8180:8080 -p 28015:28015 -p 29015:29015 \
--name rethinkdb \
-v /var/docker/grafana-pro-rethinkdb:/data \
dockerfile/rethinkdb
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment