grafana/pkg/models/dashboards.go

75 lines
1.5 KiB
Go
Raw Normal View History

2014-08-08 05:35:15 -05:00
package models
import (
"encoding/json"
2014-11-20 05:11:07 -06:00
"errors"
2014-08-08 05:35:15 -05:00
"io"
"regexp"
"strings"
2014-08-21 15:09:48 -05:00
"time"
2014-08-08 05:35:15 -05:00
)
2014-10-06 14:31:54 -05:00
var (
2014-11-20 05:11:07 -06:00
GetDashboard func(slug string, accountId int64) (*Dashboard, error)
2014-10-06 14:31:54 -05:00
SaveDashboard func(dash *Dashboard) error
2014-11-20 05:11:07 -06:00
DeleteDashboard func(slug string, accountId int64) error
SearchQuery func(query string, acccountId int64) ([]*SearchResult, error)
)
// Typed errors
var (
ErrDashboardNotFound = errors.New("Account not found")
2014-10-06 14:31:54 -05:00
)
2014-08-08 05:35:15 -05:00
type Dashboard struct {
2014-11-20 05:11:07 -06:00
Id int64
Slug string `xorm:"index(IX_AccountIdSlug)"`
AccountId int64 `xorm:"index(IX_AccountIdSlug)"`
Created time.Time `xorm:"CREATED"`
Updated time.Time `xorm:"UPDATED"`
2014-08-21 15:09:48 -05:00
Title string
Tags []string
Data map[string]interface{}
}
2014-08-08 05:35:15 -05:00
type SearchResult struct {
Id string `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
2014-08-08 05:35:15 -05:00
}
func NewDashboard(title string) *Dashboard {
dash := &Dashboard{}
2014-11-20 05:11:07 -06:00
dash.Id = 0
2014-08-08 05:35:15 -05:00
dash.Data = make(map[string]interface{})
dash.Data["title"] = title
dash.Title = title
dash.UpdateSlug()
2014-08-08 05:35:15 -05: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-21 15:09:48 -05:00
return dash, nil
2014-08-08 05:35:15 -05: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, ""), "-")
}