The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/pkg/models/dashboards.go

92 lines
2.0 KiB

package models
import (
"errors"
"regexp"
"strings"
"time"
)
var (
GetDashboard func(slug string, accountId int64) (*Dashboard, error)
DeleteDashboard func(slug string, accountId int64) error
SearchQuery func(query string, acccountId int64) ([]*SearchResult, error)
)
// Typed errors
var (
ErrDashboardNotFound = errors.New("Account not found")
)
type Dashboard struct {
Id int64
Slug string `xorm:"index(IX_AccountIdSlug)"`
AccountId int64 `xorm:"index(IX_AccountIdSlug)"`
Created time.Time `xorm:"CREATED"`
Updated time.Time `xorm:"UPDATED"`
Title string
Tags []string
Data map[string]interface{}
}
type SearchResult struct {
Id string `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
}
type SaveDashboardCommand struct {
Id string `json:"id"`
Title string `json:"title"`
Dashboard map[string]interface{} `json:"dashboard"`
AccountId int64 `json:"-"`
Result *Dashboard
}
func convertToStringArray(arr []interface{}) []string {
b := make([]string, len(arr))
for i := range arr {
b[i] = arr[i].(string)
}
return b
}
func NewDashboard(title string) *Dashboard {
dash := &Dashboard{}
dash.Data = make(map[string]interface{})
dash.Data["title"] = title
dash.Title = title
dash.UpdateSlug()
return dash
}
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
dash := &Dashboard{}
dash.Data = cmd.Dashboard
dash.Title = dash.Data["title"].(string)
dash.AccountId = cmd.AccountId
dash.Tags = convertToStringArray(dash.Data["tags"].([]interface{}))
dash.UpdateSlug()
if dash.Data["id"] != nil {
dash.Id = int64(dash.Data["id"].(float64))
}
return dash
}
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, ""), "-")
}