mirror of
https://github.com/grafana/grafana.git
synced 2026-07-30 08:18:10 -05:00
Chore: Remove dashboards from models pkg (#61578)
* Copy dashboard models to dashboard pkg * Use some models from current pkg instead of models * Adjust api pkg * Adjust pkg services * Fix lint * Chore: Remove dashboards models * Remove dashboards from models pkg * Fix lint in tests * Fix lint in tests 2 * Fix for import in auth * Remove newline * Revert unused fix
This commit is contained in:
@@ -1,268 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/slugify"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
const RootFolderName = "General"
|
||||
|
||||
const (
|
||||
DashTypeDB = "db"
|
||||
DashTypeSnapshot = "snapshot"
|
||||
)
|
||||
|
||||
// Dashboard model
|
||||
type Dashboard struct {
|
||||
Id int64
|
||||
Uid string
|
||||
Slug string
|
||||
OrgId int64
|
||||
GnetId int64
|
||||
Version int
|
||||
PluginId string
|
||||
|
||||
Created time.Time
|
||||
Updated time.Time
|
||||
|
||||
UpdatedBy int64
|
||||
CreatedBy int64
|
||||
FolderId int64
|
||||
IsFolder bool
|
||||
HasACL bool `xorm:"has_acl"`
|
||||
|
||||
Title string
|
||||
Data *simplejson.Json
|
||||
}
|
||||
|
||||
func (d *Dashboard) SetId(id int64) {
|
||||
d.Id = id
|
||||
d.Data.Set("id", id)
|
||||
}
|
||||
|
||||
func (d *Dashboard) SetUid(uid string) {
|
||||
d.Uid = uid
|
||||
d.Data.Set("uid", uid)
|
||||
}
|
||||
|
||||
func (d *Dashboard) SetVersion(version int) {
|
||||
d.Version = version
|
||||
d.Data.Set("version", version)
|
||||
}
|
||||
|
||||
// NewDashboard creates a new dashboard
|
||||
func NewDashboard(title string) *Dashboard {
|
||||
dash := &Dashboard{}
|
||||
dash.Data = simplejson.New()
|
||||
dash.Data.Set("title", title)
|
||||
dash.Title = title
|
||||
dash.Created = time.Now()
|
||||
dash.Updated = time.Now()
|
||||
dash.UpdateSlug()
|
||||
return dash
|
||||
}
|
||||
|
||||
// NewDashboardFolder creates a new dashboard folder
|
||||
func NewDashboardFolder(title string) *Dashboard {
|
||||
folder := NewDashboard(title)
|
||||
folder.IsFolder = true
|
||||
folder.Data.Set("schemaVersion", 17)
|
||||
folder.Data.Set("version", 0)
|
||||
folder.IsFolder = true
|
||||
return folder
|
||||
}
|
||||
|
||||
// GetTags turns the tags in data json into go string array
|
||||
func (d *Dashboard) GetTags() []string {
|
||||
return d.Data.Get("tags").MustStringArray()
|
||||
}
|
||||
|
||||
func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
|
||||
dash := &Dashboard{}
|
||||
dash.Data = data
|
||||
dash.Title = dash.Data.Get("title").MustString()
|
||||
dash.UpdateSlug()
|
||||
update := false
|
||||
|
||||
if id, err := dash.Data.Get("id").Float64(); err == nil {
|
||||
dash.Id = int64(id)
|
||||
update = true
|
||||
}
|
||||
|
||||
if uid, err := dash.Data.Get("uid").String(); err == nil {
|
||||
dash.Uid = uid
|
||||
update = true
|
||||
}
|
||||
|
||||
if version, err := dash.Data.Get("version").Float64(); err == nil && update {
|
||||
dash.Version = int(version)
|
||||
dash.Updated = time.Now()
|
||||
} else {
|
||||
dash.Data.Set("version", 0)
|
||||
dash.Created = time.Now()
|
||||
dash.Updated = time.Now()
|
||||
}
|
||||
|
||||
if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
|
||||
dash.GnetId = int64(gnetId)
|
||||
}
|
||||
|
||||
return dash
|
||||
}
|
||||
|
||||
// GetDashboardModel turns the command into the saveable model
|
||||
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
||||
dash := NewDashboardFromJson(cmd.Dashboard)
|
||||
userId := cmd.UserId
|
||||
|
||||
if userId == 0 {
|
||||
userId = -1
|
||||
}
|
||||
|
||||
dash.UpdatedBy = userId
|
||||
dash.OrgId = cmd.OrgId
|
||||
dash.PluginId = cmd.PluginId
|
||||
dash.IsFolder = cmd.IsFolder
|
||||
dash.FolderId = cmd.FolderId
|
||||
dash.UpdateSlug()
|
||||
return dash
|
||||
}
|
||||
|
||||
// UpdateSlug updates the slug
|
||||
func (d *Dashboard) UpdateSlug() {
|
||||
title := d.Data.Get("title").MustString()
|
||||
d.Slug = slugify.Slugify(title)
|
||||
}
|
||||
|
||||
// GetUrl return the html url for a folder if it's folder, otherwise for a dashboard
|
||||
func (d *Dashboard) GetUrl() string {
|
||||
return GetDashboardFolderUrl(d.IsFolder, d.Uid, d.Slug)
|
||||
}
|
||||
|
||||
// GetDashboardFolderUrl return the html url for a folder if it's folder, otherwise for a dashboard
|
||||
func GetDashboardFolderUrl(isFolder bool, uid string, slug string) string {
|
||||
if isFolder {
|
||||
return GetFolderUrl(uid, slug)
|
||||
}
|
||||
|
||||
return GetDashboardUrl(uid, slug)
|
||||
}
|
||||
|
||||
// GetDashboardUrl returns the HTML url for a dashboard.
|
||||
func GetDashboardUrl(uid string, slug string) string {
|
||||
return fmt.Sprintf("%s/d/%s/%s", setting.AppSubUrl, uid, slug)
|
||||
}
|
||||
|
||||
// GetKioskModeDashboardUrl returns the HTML url for a dashboard in kiosk mode.
|
||||
func GetKioskModeDashboardUrl(uid string, slug string, theme Theme) string {
|
||||
return fmt.Sprintf("%s?kiosk&theme=%s", GetDashboardUrl(uid, slug), string(theme))
|
||||
}
|
||||
|
||||
// GetFullDashboardUrl returns the full URL for a dashboard.
|
||||
func GetFullDashboardUrl(uid string, slug string) string {
|
||||
return fmt.Sprintf("%sd/%s/%s", setting.AppUrl, uid, slug)
|
||||
}
|
||||
|
||||
// GetFolderUrl returns the HTML url for a folder.
|
||||
func GetFolderUrl(folderUid string, slug string) string {
|
||||
return fmt.Sprintf("%s/dashboards/f/%s/%s", setting.AppSubUrl, folderUid, slug)
|
||||
}
|
||||
|
||||
//
|
||||
// COMMANDS
|
||||
//
|
||||
|
||||
type SaveDashboardCommand struct {
|
||||
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
|
||||
UserId int64 `json:"userId"`
|
||||
Overwrite bool `json:"overwrite"`
|
||||
Message string `json:"message"`
|
||||
OrgId int64 `json:"-"`
|
||||
RestoredFrom int `json:"-"`
|
||||
PluginId string `json:"-"`
|
||||
FolderId int64 `json:"folderId"`
|
||||
FolderUid string `json:"folderUid"`
|
||||
IsFolder bool `json:"isFolder"`
|
||||
|
||||
UpdatedAt time.Time
|
||||
|
||||
Result *Dashboard `json:"-"`
|
||||
}
|
||||
|
||||
type ValidateDashboardCommand struct {
|
||||
Dashboard string `json:"dashboard" binding:"Required"`
|
||||
}
|
||||
|
||||
type TrimDashboardCommand struct {
|
||||
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
|
||||
Meta *simplejson.Json `json:"meta"`
|
||||
Result *Dashboard `json:"-"`
|
||||
}
|
||||
|
||||
type DashboardProvisioning struct {
|
||||
Id int64
|
||||
DashboardId int64
|
||||
Name string
|
||||
ExternalId string
|
||||
CheckSum string
|
||||
Updated int64
|
||||
}
|
||||
|
||||
type DeleteDashboardCommand struct {
|
||||
Id int64
|
||||
OrgId int64
|
||||
ForceDeleteFolderRules bool
|
||||
}
|
||||
|
||||
type DeleteOrphanedProvisionedDashboardsCommand struct {
|
||||
ReaderNames []string
|
||||
}
|
||||
|
||||
//
|
||||
// QUERIES
|
||||
//
|
||||
|
||||
type GetDashboardQuery struct {
|
||||
Slug string // required if no Id or Uid is specified
|
||||
Id int64 // optional if slug is set
|
||||
Uid string // optional if slug is set
|
||||
OrgId int64
|
||||
|
||||
Result *Dashboard
|
||||
}
|
||||
|
||||
type DashboardTagCloudItem struct {
|
||||
Term string `json:"term"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type GetDashboardTagsQuery struct {
|
||||
OrgId int64
|
||||
Result []*DashboardTagCloudItem
|
||||
}
|
||||
|
||||
type GetDashboardsQuery struct {
|
||||
DashboardIds []int64
|
||||
DashboardUIds []string
|
||||
Result []*Dashboard
|
||||
}
|
||||
|
||||
type GetDashboardsByPluginIdQuery struct {
|
||||
OrgId int64
|
||||
PluginId string
|
||||
Result []*Dashboard
|
||||
}
|
||||
|
||||
type DashboardRef struct {
|
||||
Uid string
|
||||
Slug string
|
||||
}
|
||||
|
||||
type GetDashboardRefByIdQuery struct {
|
||||
Id int64
|
||||
Result *DashboardRef
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/slugify"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetDashboardUrl(t *testing.T) {
|
||||
origAppURL := setting.AppUrl
|
||||
t.Cleanup(func() { setting.AppUrl = origAppURL })
|
||||
|
||||
setting.AppUrl = ""
|
||||
url := GetDashboardUrl("uid", "my-dashboard")
|
||||
assert.Equal(t, "/d/uid/my-dashboard", url)
|
||||
}
|
||||
|
||||
func TestGetFullDashboardUrl(t *testing.T) {
|
||||
origAppURL := setting.AppUrl
|
||||
t.Cleanup(func() { setting.AppUrl = origAppURL })
|
||||
|
||||
setting.AppUrl = "http://grafana.local/"
|
||||
url := GetFullDashboardUrl("uid", "my-dashboard")
|
||||
assert.Equal(t, "http://grafana.local/d/uid/my-dashboard", url)
|
||||
}
|
||||
|
||||
func TestDashboard_UpdateSlug(t *testing.T) {
|
||||
dashboard := NewDashboard("Grafana Play Home")
|
||||
assert.Equal(t, "grafana-play-home", dashboard.Slug)
|
||||
|
||||
dashboard.UpdateSlug()
|
||||
assert.Equal(t, "grafana-play-home", dashboard.Slug)
|
||||
}
|
||||
|
||||
func TestNewDashboardFromJson(t *testing.T) {
|
||||
json := simplejson.New()
|
||||
json.Set("title", "test dash")
|
||||
json.Set("tags", "")
|
||||
|
||||
dash := NewDashboardFromJson(json)
|
||||
assert.Equal(t, "test dash", dash.Title)
|
||||
require.Empty(t, dash.GetTags())
|
||||
}
|
||||
|
||||
func TestSaveDashboardCommand_GetDashboardModel(t *testing.T) {
|
||||
t.Run("should set IsFolder", func(t *testing.T) {
|
||||
json := simplejson.New()
|
||||
json.Set("title", "test dash")
|
||||
|
||||
cmd := &SaveDashboardCommand{Dashboard: json, IsFolder: true}
|
||||
dash := cmd.GetDashboardModel()
|
||||
|
||||
assert.Equal(t, "test dash", dash.Title)
|
||||
assert.True(t, dash.IsFolder)
|
||||
})
|
||||
|
||||
t.Run("should set FolderId", func(t *testing.T) {
|
||||
json := simplejson.New()
|
||||
json.Set("title", "test dash")
|
||||
|
||||
cmd := &SaveDashboardCommand{Dashboard: json, FolderId: 1}
|
||||
dash := cmd.GetDashboardModel()
|
||||
|
||||
assert.Equal(t, int64(1), dash.FolderId)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSlugifyTitle(t *testing.T) {
|
||||
testCases := map[string]string{
|
||||
"Grafana Play Home": "grafana-play-home",
|
||||
"snöräv-över-ån": "snorav-over-an",
|
||||
"漢字": "5ryi5a2X", // "han-zi", // Hanzi for hanzi
|
||||
"🇦🇶": "8J-HpvCfh7Y", // flag of Antarctica-emoji, using fallback
|
||||
"𒆠": "8JKGoA", // cuneiform Ki, using fallback
|
||||
}
|
||||
|
||||
for input, expected := range testCases {
|
||||
t.Run(input, func(t *testing.T) {
|
||||
slug := slugify.Slugify(input)
|
||||
assert.Equal(t, expected, slug)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -30,22 +30,6 @@ func NewFolder(title string) *Folder {
|
||||
return folder
|
||||
}
|
||||
|
||||
// DashboardToFolder converts Dashboard to Folder
|
||||
func DashboardToFolder(dash *Dashboard) *Folder {
|
||||
return &Folder{
|
||||
Id: dash.Id,
|
||||
Uid: dash.Uid,
|
||||
Title: dash.Title,
|
||||
HasACL: dash.HasACL,
|
||||
Url: dash.GetUrl(),
|
||||
Version: dash.Version,
|
||||
Created: dash.Created,
|
||||
CreatedBy: dash.CreatedBy,
|
||||
Updated: dash.Updated,
|
||||
UpdatedBy: dash.UpdatedBy,
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// COMMANDS
|
||||
//
|
||||
|
||||
@@ -67,22 +67,6 @@ type ChannelHandlerFactory interface {
|
||||
GetHandlerForPath(path string) (ChannelHandler, error)
|
||||
}
|
||||
|
||||
// DashboardActivityChannel is a service to advertise dashboard activity
|
||||
type DashboardActivityChannel interface {
|
||||
// Called when a dashboard is saved -- this includes the error so we can support a
|
||||
// gitops workflow that knows if the value was saved to the local database or not
|
||||
// in many cases all direct save requests will fail, but the request should be forwarded
|
||||
// to any gitops observers
|
||||
DashboardSaved(orgID int64, user *user.UserDisplayDTO, message string, dashboard *Dashboard, err error) error
|
||||
|
||||
// Called when a dashboard is deleted
|
||||
DashboardDeleted(orgID int64, user *user.UserDisplayDTO, uid string) error
|
||||
|
||||
// Experimental! Indicate is GitOps is active. This really means
|
||||
// someone is subscribed to the `grafana/dashboards/gitops` channel
|
||||
HasGitOpsObserver(orgID int64) bool
|
||||
}
|
||||
|
||||
type LiveMessage struct {
|
||||
Id int64
|
||||
OrgId int64
|
||||
|
||||
Reference in New Issue
Block a user