mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 21:52:43 +08:00
Chore: Avoid aliasing importing models in api package (#22492)
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/alerting"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
|
||||
@ -16,7 +17,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/services/guardian"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
@ -27,12 +27,12 @@ const (
|
||||
anonString = "Anonymous"
|
||||
)
|
||||
|
||||
func isDashboardStarredByUser(c *m.ReqContext, dashID int64) (bool, error) {
|
||||
func isDashboardStarredByUser(c *models.ReqContext, dashID int64) (bool, error) {
|
||||
if !c.IsSignedIn {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
query := m.IsStarredByUserQuery{UserId: c.UserId, DashboardId: dashID}
|
||||
query := models.IsStarredByUserQuery{UserId: c.UserId, DashboardId: dashID}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return false, err
|
||||
}
|
||||
@ -48,7 +48,7 @@ func dashboardGuardianResponse(err error) Response {
|
||||
return Error(403, "Access denied to this dashboard", nil)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) GetDashboard(c *m.ReqContext) Response {
|
||||
func (hs *HTTPServer) GetDashboard(c *models.ReqContext) Response {
|
||||
dash, rsp := getDashboardHelper(c.OrgId, c.Params(":slug"), 0, c.Params(":uid"))
|
||||
if rsp != nil {
|
||||
return rsp
|
||||
@ -80,7 +80,7 @@ func (hs *HTTPServer) GetDashboard(c *m.ReqContext) Response {
|
||||
meta := dtos.DashboardMeta{
|
||||
IsStarred: isStarred,
|
||||
Slug: dash.Slug,
|
||||
Type: m.DashTypeDB,
|
||||
Type: models.DashTypeDB,
|
||||
CanStar: c.IsSignedIn,
|
||||
CanSave: canSave,
|
||||
CanEdit: canEdit,
|
||||
@ -99,7 +99,7 @@ func (hs *HTTPServer) GetDashboard(c *m.ReqContext) Response {
|
||||
|
||||
// lookup folder title
|
||||
if dash.FolderId > 0 {
|
||||
query := m.GetDashboardQuery{Id: dash.FolderId, OrgId: c.OrgId}
|
||||
query := models.GetDashboardQuery{Id: dash.FolderId, OrgId: c.OrgId}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return Error(500, "Dashboard folder could not be read", err)
|
||||
}
|
||||
@ -142,7 +142,7 @@ func (hs *HTTPServer) GetDashboard(c *m.ReqContext) Response {
|
||||
}
|
||||
|
||||
func getUserLogin(userID int64) string {
|
||||
query := m.GetUserByIdQuery{Id: userID}
|
||||
query := models.GetUserByIdQuery{Id: userID}
|
||||
err := bus.Dispatch(&query)
|
||||
if err != nil {
|
||||
return anonString
|
||||
@ -150,13 +150,13 @@ func getUserLogin(userID int64) string {
|
||||
return query.Result.Login
|
||||
}
|
||||
|
||||
func getDashboardHelper(orgID int64, slug string, id int64, uid string) (*m.Dashboard, Response) {
|
||||
var query m.GetDashboardQuery
|
||||
func getDashboardHelper(orgID int64, slug string, id int64, uid string) (*models.Dashboard, Response) {
|
||||
var query models.GetDashboardQuery
|
||||
|
||||
if len(uid) > 0 {
|
||||
query = m.GetDashboardQuery{Uid: uid, Id: id, OrgId: orgID}
|
||||
query = models.GetDashboardQuery{Uid: uid, Id: id, OrgId: orgID}
|
||||
} else {
|
||||
query = m.GetDashboardQuery{Slug: slug, Id: id, OrgId: orgID}
|
||||
query = models.GetDashboardQuery{Slug: slug, Id: id, OrgId: orgID}
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
@ -166,25 +166,25 @@ func getDashboardHelper(orgID int64, slug string, id int64, uid string) (*m.Dash
|
||||
return query.Result, nil
|
||||
}
|
||||
|
||||
func DeleteDashboardBySlug(c *m.ReqContext) Response {
|
||||
query := m.GetDashboardsBySlugQuery{OrgId: c.OrgId, Slug: c.Params(":slug")}
|
||||
func DeleteDashboardBySlug(c *models.ReqContext) Response {
|
||||
query := models.GetDashboardsBySlugQuery{OrgId: c.OrgId, Slug: c.Params(":slug")}
|
||||
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return Error(500, "Failed to retrieve dashboards by slug", err)
|
||||
}
|
||||
|
||||
if len(query.Result) > 1 {
|
||||
return JSON(412, util.DynMap{"status": "multiple-slugs-exists", "message": m.ErrDashboardsWithSameSlugExists.Error()})
|
||||
return JSON(412, util.DynMap{"status": "multiple-slugs-exists", "message": models.ErrDashboardsWithSameSlugExists.Error()})
|
||||
}
|
||||
|
||||
return deleteDashboard(c)
|
||||
}
|
||||
|
||||
func DeleteDashboardByUID(c *m.ReqContext) Response {
|
||||
func DeleteDashboardByUID(c *models.ReqContext) Response {
|
||||
return deleteDashboard(c)
|
||||
}
|
||||
|
||||
func deleteDashboard(c *m.ReqContext) Response {
|
||||
func deleteDashboard(c *models.ReqContext) Response {
|
||||
dash, rsp := getDashboardHelper(c.OrgId, c.Params(":slug"), 0, c.Params(":uid"))
|
||||
if rsp != nil {
|
||||
return rsp
|
||||
@ -196,7 +196,7 @@ func deleteDashboard(c *m.ReqContext) Response {
|
||||
}
|
||||
|
||||
err := dashboards.NewService().DeleteDashboard(dash.Id, c.OrgId)
|
||||
if err == m.ErrDashboardCannotDeleteProvisionedDashboard {
|
||||
if err == models.ErrDashboardCannotDeleteProvisionedDashboard {
|
||||
return Error(400, "Dashboard cannot be deleted because it was provisioned", err)
|
||||
} else if err != nil {
|
||||
return Error(500, "Failed to delete dashboard", err)
|
||||
@ -208,7 +208,7 @@ func deleteDashboard(c *m.ReqContext) Response {
|
||||
})
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand) Response {
|
||||
func (hs *HTTPServer) PostDashboard(c *models.ReqContext, cmd models.SaveDashboardCommand) Response {
|
||||
cmd.OrgId = c.OrgId
|
||||
cmd.UserId = c.UserId
|
||||
|
||||
@ -268,22 +268,22 @@ func (hs *HTTPServer) PostDashboard(c *m.ReqContext, cmd m.SaveDashboardCommand)
|
||||
}
|
||||
|
||||
func dashboardSaveErrorToApiResponse(err error) Response {
|
||||
if err == m.ErrDashboardTitleEmpty ||
|
||||
err == m.ErrDashboardWithSameNameAsFolder ||
|
||||
err == m.ErrDashboardFolderWithSameNameAsDashboard ||
|
||||
err == m.ErrDashboardTypeMismatch ||
|
||||
err == m.ErrDashboardInvalidUid ||
|
||||
err == m.ErrDashboardUidToLong ||
|
||||
err == m.ErrDashboardWithSameUIDExists ||
|
||||
err == m.ErrFolderNotFound ||
|
||||
err == m.ErrDashboardFolderCannotHaveParent ||
|
||||
err == m.ErrDashboardFolderNameExists ||
|
||||
err == m.ErrDashboardRefreshIntervalTooShort ||
|
||||
err == m.ErrDashboardCannotSaveProvisionedDashboard {
|
||||
if err == models.ErrDashboardTitleEmpty ||
|
||||
err == models.ErrDashboardWithSameNameAsFolder ||
|
||||
err == models.ErrDashboardFolderWithSameNameAsDashboard ||
|
||||
err == models.ErrDashboardTypeMismatch ||
|
||||
err == models.ErrDashboardInvalidUid ||
|
||||
err == models.ErrDashboardUidToLong ||
|
||||
err == models.ErrDashboardWithSameUIDExists ||
|
||||
err == models.ErrFolderNotFound ||
|
||||
err == models.ErrDashboardFolderCannotHaveParent ||
|
||||
err == models.ErrDashboardFolderNameExists ||
|
||||
err == models.ErrDashboardRefreshIntervalTooShort ||
|
||||
err == models.ErrDashboardCannotSaveProvisionedDashboard {
|
||||
return Error(400, err.Error(), nil)
|
||||
}
|
||||
|
||||
if err == m.ErrDashboardUpdateAccessDenied {
|
||||
if err == models.ErrDashboardUpdateAccessDenied {
|
||||
return Error(403, err.Error(), err)
|
||||
}
|
||||
|
||||
@ -291,15 +291,15 @@ func dashboardSaveErrorToApiResponse(err error) Response {
|
||||
return Error(422, validationErr.Error(), nil)
|
||||
}
|
||||
|
||||
if err == m.ErrDashboardWithSameNameInFolderExists {
|
||||
if err == models.ErrDashboardWithSameNameInFolderExists {
|
||||
return JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
|
||||
}
|
||||
|
||||
if err == m.ErrDashboardVersionMismatch {
|
||||
if err == models.ErrDashboardVersionMismatch {
|
||||
return JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
|
||||
}
|
||||
|
||||
if pluginErr, ok := err.(m.UpdatePluginDashboardError); ok {
|
||||
if pluginErr, ok := err.(models.UpdatePluginDashboardError); ok {
|
||||
message := "The dashboard belongs to plugin " + pluginErr.PluginId + "."
|
||||
// look up plugin name
|
||||
if pluginDef, exist := plugins.Plugins[pluginErr.PluginId]; exist {
|
||||
@ -308,24 +308,24 @@ func dashboardSaveErrorToApiResponse(err error) Response {
|
||||
return JSON(412, util.DynMap{"status": "plugin-dashboard", "message": message})
|
||||
}
|
||||
|
||||
if err == m.ErrDashboardNotFound {
|
||||
if err == models.ErrDashboardNotFound {
|
||||
return JSON(404, util.DynMap{"status": "not-found", "message": err.Error()})
|
||||
}
|
||||
|
||||
return Error(500, "Failed to save dashboard", err)
|
||||
}
|
||||
|
||||
func GetHomeDashboard(c *m.ReqContext) Response {
|
||||
prefsQuery := m.GetPreferencesWithDefaultsQuery{User: c.SignedInUser}
|
||||
func GetHomeDashboard(c *models.ReqContext) Response {
|
||||
prefsQuery := models.GetPreferencesWithDefaultsQuery{User: c.SignedInUser}
|
||||
if err := bus.Dispatch(&prefsQuery); err != nil {
|
||||
return Error(500, "Failed to get preferences", err)
|
||||
}
|
||||
|
||||
if prefsQuery.Result.HomeDashboardId != 0 {
|
||||
slugQuery := m.GetDashboardRefByIdQuery{Id: prefsQuery.Result.HomeDashboardId}
|
||||
slugQuery := models.GetDashboardRefByIdQuery{Id: prefsQuery.Result.HomeDashboardId}
|
||||
err := bus.Dispatch(&slugQuery)
|
||||
if err == nil {
|
||||
url := m.GetDashboardUrl(slugQuery.Result.Uid, slugQuery.Result.Slug)
|
||||
url := models.GetDashboardUrl(slugQuery.Result.Uid, slugQuery.Result.Slug)
|
||||
dashRedirect := dtos.DashboardRedirect{RedirectUri: url}
|
||||
return JSON(200, &dashRedirect)
|
||||
}
|
||||
@ -341,7 +341,7 @@ func GetHomeDashboard(c *m.ReqContext) Response {
|
||||
|
||||
dash := dtos.DashboardFullWithMeta{}
|
||||
dash.Meta.IsHome = true
|
||||
dash.Meta.CanEdit = c.SignedInUser.HasRole(m.ROLE_EDITOR)
|
||||
dash.Meta.CanEdit = c.SignedInUser.HasRole(models.ROLE_EDITOR)
|
||||
dash.Meta.FolderTitle = "General"
|
||||
|
||||
jsonParser := json.NewDecoder(file)
|
||||
@ -349,7 +349,7 @@ func GetHomeDashboard(c *m.ReqContext) Response {
|
||||
return Error(500, "Failed to load home dashboard", err)
|
||||
}
|
||||
|
||||
if c.HasUserRole(m.ROLE_ADMIN) && !c.HasHelpFlag(m.HelpFlagGettingStartedPanelDismissed) {
|
||||
if c.HasUserRole(models.ROLE_ADMIN) && !c.HasHelpFlag(models.HelpFlagGettingStartedPanelDismissed) {
|
||||
addGettingStartedPanelToHomeDashboard(dash.Dashboard)
|
||||
}
|
||||
|
||||
@ -375,7 +375,7 @@ func addGettingStartedPanelToHomeDashboard(dash *simplejson.Json) {
|
||||
}
|
||||
|
||||
// GetDashboardVersions returns all dashboard versions as JSON
|
||||
func GetDashboardVersions(c *m.ReqContext) Response {
|
||||
func GetDashboardVersions(c *models.ReqContext) Response {
|
||||
dashID := c.ParamsInt64(":dashboardId")
|
||||
|
||||
guardian := guardian.New(dashID, c.OrgId, c.SignedInUser)
|
||||
@ -383,7 +383,7 @@ func GetDashboardVersions(c *m.ReqContext) Response {
|
||||
return dashboardGuardianResponse(err)
|
||||
}
|
||||
|
||||
query := m.GetDashboardVersionsQuery{
|
||||
query := models.GetDashboardVersionsQuery{
|
||||
OrgId: c.OrgId,
|
||||
DashboardId: dashID,
|
||||
Limit: c.QueryInt("limit"),
|
||||
@ -414,7 +414,7 @@ func GetDashboardVersions(c *m.ReqContext) Response {
|
||||
}
|
||||
|
||||
// GetDashboardVersion returns the dashboard version with the given ID.
|
||||
func GetDashboardVersion(c *m.ReqContext) Response {
|
||||
func GetDashboardVersion(c *models.ReqContext) Response {
|
||||
dashID := c.ParamsInt64(":dashboardId")
|
||||
|
||||
guardian := guardian.New(dashID, c.OrgId, c.SignedInUser)
|
||||
@ -422,7 +422,7 @@ func GetDashboardVersion(c *m.ReqContext) Response {
|
||||
return dashboardGuardianResponse(err)
|
||||
}
|
||||
|
||||
query := m.GetDashboardVersionQuery{
|
||||
query := models.GetDashboardVersionQuery{
|
||||
OrgId: c.OrgId,
|
||||
DashboardId: dashID,
|
||||
Version: c.ParamsInt(":id"),
|
||||
@ -437,7 +437,7 @@ func GetDashboardVersion(c *m.ReqContext) Response {
|
||||
creator = getUserLogin(query.Result.CreatedBy)
|
||||
}
|
||||
|
||||
dashVersionMeta := &m.DashboardVersionMeta{
|
||||
dashVersionMeta := &models.DashboardVersionMeta{
|
||||
Id: query.Result.Id,
|
||||
DashboardId: query.Result.DashboardId,
|
||||
Data: query.Result.Data,
|
||||
@ -453,7 +453,7 @@ func GetDashboardVersion(c *m.ReqContext) Response {
|
||||
}
|
||||
|
||||
// POST /api/dashboards/calculate-diff performs diffs on two dashboards
|
||||
func CalculateDashboardDiff(c *m.ReqContext, apiOptions dtos.CalculateDiffOptions) Response {
|
||||
func CalculateDashboardDiff(c *models.ReqContext, apiOptions dtos.CalculateDiffOptions) Response {
|
||||
|
||||
guardianBase := guardian.New(apiOptions.Base.DashboardId, c.OrgId, c.SignedInUser)
|
||||
if canSave, err := guardianBase.CanSave(); err != nil || !canSave {
|
||||
@ -484,7 +484,7 @@ func CalculateDashboardDiff(c *m.ReqContext, apiOptions dtos.CalculateDiffOption
|
||||
|
||||
result, err := dashdiffs.CalculateDiff(&options)
|
||||
if err != nil {
|
||||
if err == m.ErrDashboardVersionNotFound {
|
||||
if err == models.ErrDashboardVersionNotFound {
|
||||
return Error(404, "Dashboard version not found", err)
|
||||
}
|
||||
return Error(500, "Unable to compute diff", err)
|
||||
@ -498,7 +498,7 @@ func CalculateDashboardDiff(c *m.ReqContext, apiOptions dtos.CalculateDiffOption
|
||||
}
|
||||
|
||||
// RestoreDashboardVersion restores a dashboard to the given version.
|
||||
func (hs *HTTPServer) RestoreDashboardVersion(c *m.ReqContext, apiCmd dtos.RestoreDashboardVersionCommand) Response {
|
||||
func (hs *HTTPServer) RestoreDashboardVersion(c *models.ReqContext, apiCmd dtos.RestoreDashboardVersionCommand) Response {
|
||||
dash, rsp := getDashboardHelper(c.OrgId, "", c.ParamsInt64(":dashboardId"), "")
|
||||
if rsp != nil {
|
||||
return rsp
|
||||
@ -509,14 +509,14 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *m.ReqContext, apiCmd dtos.Resto
|
||||
return dashboardGuardianResponse(err)
|
||||
}
|
||||
|
||||
versionQuery := m.GetDashboardVersionQuery{DashboardId: dash.Id, Version: apiCmd.Version, OrgId: c.OrgId}
|
||||
versionQuery := models.GetDashboardVersionQuery{DashboardId: dash.Id, Version: apiCmd.Version, OrgId: c.OrgId}
|
||||
if err := bus.Dispatch(&versionQuery); err != nil {
|
||||
return Error(404, "Dashboard version not found", nil)
|
||||
}
|
||||
|
||||
version := versionQuery.Result
|
||||
|
||||
saveCmd := m.SaveDashboardCommand{}
|
||||
saveCmd := models.SaveDashboardCommand{}
|
||||
saveCmd.RestoredFrom = version.Version
|
||||
saveCmd.OrgId = c.OrgId
|
||||
saveCmd.UserId = c.UserId
|
||||
@ -529,8 +529,8 @@ func (hs *HTTPServer) RestoreDashboardVersion(c *m.ReqContext, apiCmd dtos.Resto
|
||||
return hs.PostDashboard(c, saveCmd)
|
||||
}
|
||||
|
||||
func GetDashboardTags(c *m.ReqContext) {
|
||||
query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
|
||||
func GetDashboardTags(c *models.ReqContext) {
|
||||
query := models.GetDashboardTagsQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&query)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Failed to get tags from database", err)
|
||||
|
Reference in New Issue
Block a user