grafana/pkg/models/dashboards.go

72 lines
1.6 KiB
Go
Raw Normal View History

2014-08-08 18:35:15 +08:00
package models
import (
"encoding/json"
"io"
"regexp"
"strings"
2014-08-22 04:09:48 +08:00
"time"
2014-08-08 18:35:15 +08:00
)
2014-10-07 03:31:54 +08:00
var (
GetDashboard func(slug string, accountId int) (*Dashboard, error)
SaveDashboard func(dash *Dashboard) error
DeleteDashboard func(slug string, accountId int) error
SearchQuery func(query string, acccountId int) ([]*SearchResult, error)
)
2014-08-08 18:35:15 +08:00
type Dashboard struct {
2014-08-22 04:09:48 +08:00
Id string `gorethink:"id,omitempty"`
Slug string
AccountId int
2014-08-22 04:09:48 +08:00
LastModifiedByUserId string
LastModifiedByDate time.Time
CreatedDate time.Time
Title string
Tags []string
Data map[string]interface{}
}
2014-08-08 18:35:15 +08:00
type SearchResult struct {
Id string `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
2014-08-08 18:35:15 +08:00
}
func NewDashboard(title string) *Dashboard {
dash := &Dashboard{}
2014-08-22 04:09:48 +08:00
dash.Id = ""
dash.LastModifiedByDate = time.Now()
dash.CreatedDate = time.Now()
dash.LastModifiedByUserId = "123"
2014-08-08 18:35:15 +08:00
dash.Data = make(map[string]interface{})
dash.Data["title"] = title
dash.Title = title
dash.UpdateSlug()
2014-08-08 18:35:15 +08:00
return dash
}
func NewFromJson(reader io.Reader) (*Dashboard, error) {
dash := NewDashboard("temp")
jsonParser := json.NewDecoder(reader)
if err := jsonParser.Decode(&dash.Data); err != nil {
return nil, err
}
2014-08-22 04:09:48 +08:00
return dash, nil
2014-08-08 18:35:15 +08:00
}
func (dash *Dashboard) GetString(prop string) string {
return dash.Data[prop].(string)
}
func (dash *Dashboard) UpdateSlug() {
title := strings.ToLower(dash.Data["title"].(string))
re := regexp.MustCompile("[^\\w ]+")
re2 := regexp.MustCompile("\\s")
dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
}