Commit 3b4e4876 by Carl Bergquist Committed by GitHub

Merge pull request #10715 from grafana/7883_alerting

Implement new url format in alerting
parents d8d82c17 90207bcb
...@@ -105,7 +105,8 @@ func transformToDTOs(alerts []*models.Alert, c *middleware.Context) ([]*dtos.Ale ...@@ -105,7 +105,8 @@ func transformToDTOs(alerts []*models.Alert, c *middleware.Context) ([]*dtos.Ale
for _, alert := range alertDTOs { for _, alert := range alertDTOs {
for _, dash := range dashboardsQuery.Result { for _, dash := range dashboardsQuery.Result {
if alert.DashboardId == dash.Id { if alert.DashboardId == dash.Id {
alert.DashbboardUri = "db/" + dash.Slug alert.DashbboardUri = dash.GenerateUrl()
break
} }
} }
} }
......
...@@ -162,6 +162,11 @@ func (dash *Dashboard) GetUrl() string { ...@@ -162,6 +162,11 @@ func (dash *Dashboard) GetUrl() string {
return GetDashboardFolderUrl(dash.IsFolder, dash.Uid, dash.Slug) return GetDashboardFolderUrl(dash.IsFolder, dash.Uid, dash.Slug)
} }
// Return the html url for a dashboard
func (dash *Dashboard) GenerateUrl() string {
return GetDashboardUrl(dash.Uid, dash.Slug)
}
// GetDashboardFolderUrl return the html url for a folder if it's folder, otherwise for a dashboard // 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 { func GetDashboardFolderUrl(isFolder bool, uid string, slug string) string {
if isFolder { if isFolder {
...@@ -171,11 +176,16 @@ func GetDashboardFolderUrl(isFolder bool, uid string, slug string) string { ...@@ -171,11 +176,16 @@ func GetDashboardFolderUrl(isFolder bool, uid string, slug string) string {
return GetDashboardUrl(uid, slug) return GetDashboardUrl(uid, slug)
} }
// GetDashboardUrl return the html url for a dashboard // Return the html url for a dashboard
func GetDashboardUrl(uid string, slug string) string { func GetDashboardUrl(uid string, slug string) string {
return fmt.Sprintf("%s/d/%s/%s", setting.AppSubUrl, uid, slug) return fmt.Sprintf("%s/d/%s/%s", setting.AppSubUrl, uid, slug)
} }
// Return the full url for a dashboard
func GetFullDashboardUrl(uid string, slug string) string {
return fmt.Sprintf("%s%s", setting.AppUrl, GetDashboardUrl(uid, slug))
}
// GetFolderUrl return the html url for a folder // GetFolderUrl return the html url for a folder
func GetFolderUrl(folderUid string, slug string) string { func GetFolderUrl(folderUid string, slug string) string {
return fmt.Sprintf("%s/dashboards/f/%s/%s", setting.AppSubUrl, folderUid, slug) return fmt.Sprintf("%s/dashboards/f/%s/%s", setting.AppSubUrl, folderUid, slug)
...@@ -277,3 +287,13 @@ type DashboardPermissionForUser struct { ...@@ -277,3 +287,13 @@ type DashboardPermissionForUser struct {
Permission PermissionType `json:"permission"` Permission PermissionType `json:"permission"`
PermissionName string `json:"permissionName"` PermissionName string `json:"permissionName"`
} }
type DashboardRef struct {
Uid string
Slug string
}
type GetDashboardUIDByIdQuery struct {
Id int64
Result *DashboardRef
}
...@@ -22,7 +22,9 @@ type EvalContext struct { ...@@ -22,7 +22,9 @@ type EvalContext struct {
EndTime time.Time EndTime time.Time
Rule *Rule Rule *Rule
log log.Logger log log.Logger
dashboardSlug string
dashboardRef *m.DashboardRef
ImagePublicUrl string ImagePublicUrl string
ImageOnDiskPath string ImageOnDiskPath string
NoDataFound bool NoDataFound bool
...@@ -83,29 +85,30 @@ func (c *EvalContext) GetNotificationTitle() string { ...@@ -83,29 +85,30 @@ func (c *EvalContext) GetNotificationTitle() string {
return "[" + c.GetStateModel().Text + "] " + c.Rule.Name return "[" + c.GetStateModel().Text + "] " + c.Rule.Name
} }
func (c *EvalContext) GetDashboardSlug() (string, error) { func (c *EvalContext) GetDashboardUID() (*m.DashboardRef, error) {
if c.dashboardSlug != "" { if c.dashboardRef != nil {
return c.dashboardSlug, nil return c.dashboardRef, nil
} }
slugQuery := &m.GetDashboardSlugByIdQuery{Id: c.Rule.DashboardId} uidQuery := &m.GetDashboardUIDByIdQuery{Id: c.Rule.DashboardId}
if err := bus.Dispatch(slugQuery); err != nil { if err := bus.Dispatch(uidQuery); err != nil {
return "", err return nil, err
} }
c.dashboardSlug = slugQuery.Result c.dashboardRef = uidQuery.Result
return c.dashboardSlug, nil return c.dashboardRef, nil
} }
const urlFormat = "%s?fullscreen=true&edit=true&tab=alert&panelId=%d&orgId=%d"
func (c *EvalContext) GetRuleUrl() (string, error) { func (c *EvalContext) GetRuleUrl() (string, error) {
if c.IsTestRun { if c.IsTestRun {
return setting.AppUrl, nil return setting.AppUrl, nil
} }
if slug, err := c.GetDashboardSlug(); err != nil { if ref, err := c.GetDashboardUID(); err != nil {
return "", err return "", err
} else { } else {
ruleUrl := fmt.Sprintf("%sdashboard/db/%s?fullscreen&edit&tab=alert&panelId=%d&orgId=%d", setting.AppUrl, slug, c.Rule.PanelId, c.Rule.OrgId) return fmt.Sprintf(urlFormat, m.GetFullDashboardUrl(ref.Uid, ref.Slug), c.Rule.PanelId, c.Rule.OrgId), nil
return ruleUrl, nil
} }
} }
...@@ -87,10 +87,10 @@ func (n *notificationService) uploadImage(context *EvalContext) (err error) { ...@@ -87,10 +87,10 @@ func (n *notificationService) uploadImage(context *EvalContext) (err error) {
IsAlertContext: true, IsAlertContext: true,
} }
if slug, err := context.GetDashboardSlug(); err != nil { if ref, err := context.GetDashboardUID(); err != nil {
return err return err
} else { } else {
renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId) renderOpts.Path = fmt.Sprintf("d-solo/%s/%s?panelId=%d", ref.Uid, ref.Slug, context.Rule.PanelId)
} }
if imagePath, err := renderer.RenderToPng(renderOpts); err != nil { if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
......
...@@ -19,9 +19,11 @@ func init() { ...@@ -19,9 +19,11 @@ func init() {
bus.AddHandler("sql", SearchDashboards) bus.AddHandler("sql", SearchDashboards)
bus.AddHandler("sql", GetDashboardTags) bus.AddHandler("sql", GetDashboardTags)
bus.AddHandler("sql", GetDashboardSlugById) bus.AddHandler("sql", GetDashboardSlugById)
bus.AddHandler("sql", GetDashboardUIDById)
bus.AddHandler("sql", GetDashboardsByPluginId) bus.AddHandler("sql", GetDashboardsByPluginId)
bus.AddHandler("sql", GetFoldersForSignedInUser) bus.AddHandler("sql", GetFoldersForSignedInUser)
bus.AddHandler("sql", GetDashboardPermissionsForUser) bus.AddHandler("sql", GetDashboardPermissionsForUser)
bus.AddHandler("sql", GetDashboardsBySlug)
} }
var generateNewUid func() string = util.GenerateShortUid var generateNewUid func() string = util.GenerateShortUid
...@@ -159,6 +161,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { ...@@ -159,6 +161,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
return err return err
}) })
} }
func generateNewDashboardUid(sess *DBSession, orgId int64) (string, error) { func generateNewDashboardUid(sess *DBSession, orgId int64) (string, error) {
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
uid := generateNewUid() uid := generateNewUid()
...@@ -539,7 +542,7 @@ func GetDashboardSlugById(query *m.GetDashboardSlugByIdQuery) error { ...@@ -539,7 +542,7 @@ func GetDashboardSlugById(query *m.GetDashboardSlugByIdQuery) error {
var rawSql = `SELECT slug from dashboard WHERE Id=?` var rawSql = `SELECT slug from dashboard WHERE Id=?`
var slug = DashboardSlugDTO{} var slug = DashboardSlugDTO{}
exists, err := x.Sql(rawSql, query.Id).Get(&slug) exists, err := x.SQL(rawSql, query.Id).Get(&slug)
if err != nil { if err != nil {
return err return err
...@@ -552,7 +555,7 @@ func GetDashboardSlugById(query *m.GetDashboardSlugByIdQuery) error { ...@@ -552,7 +555,7 @@ func GetDashboardSlugById(query *m.GetDashboardSlugByIdQuery) error {
} }
func GetDashboardsBySlug(query *m.GetDashboardsBySlugQuery) error { func GetDashboardsBySlug(query *m.GetDashboardsBySlugQuery) error {
var dashboards = make([]*m.Dashboard, 0) var dashboards []*m.Dashboard
if err := x.Where("org_id=? AND slug=?", query.OrgId, query.Slug).Find(&dashboards); err != nil { if err := x.Where("org_id=? AND slug=?", query.OrgId, query.Slug).Find(&dashboards); err != nil {
return err return err
...@@ -561,3 +564,20 @@ func GetDashboardsBySlug(query *m.GetDashboardsBySlugQuery) error { ...@@ -561,3 +564,20 @@ func GetDashboardsBySlug(query *m.GetDashboardsBySlugQuery) error {
query.Result = dashboards query.Result = dashboards
return nil return nil
} }
func GetDashboardUIDById(query *m.GetDashboardUIDByIdQuery) error {
var rawSql = `SELECT uid, slug from dashboard WHERE Id=?`
us := &m.DashboardRef{}
exists, err := x.SQL(rawSql, query.Id).Get(us)
if err != nil {
return err
} else if exists == false {
return m.ErrDashboardNotFound
}
query.Result = us
return nil
}
...@@ -23,7 +23,7 @@ describe('AlertRuleList', () => { ...@@ -23,7 +23,7 @@ describe('AlertRuleList', () => {
.format(), .format(),
evalData: {}, evalData: {},
executionError: '', executionError: '',
dashboardUri: 'db/mygool', dashboardUri: 'd/ufkcofof/my-goal',
canEdit: true, canEdit: true,
}, },
]) ])
......
...@@ -137,7 +137,7 @@ export class AlertRuleItem extends React.Component<AlertRuleItemProps, any> { ...@@ -137,7 +137,7 @@ export class AlertRuleItem extends React.Component<AlertRuleItemProps, any> {
'fa-pause': !rule.isPaused, 'fa-pause': !rule.isPaused,
}); });
let ruleUrl = `dashboard/${rule.dashboardUri}?panelId=${rule.panelId}&fullscreen&edit&tab=alert`; let ruleUrl = `${rule.dashboardUri}?panelId=${rule.panelId}&fullscreen=true&edit=true&tab=alert`;
return ( return (
<li className="alert-rule-item"> <li className="alert-rule-item">
......
...@@ -21,7 +21,7 @@ exports[`AlertRuleList should render 1 rule 1`] = ` ...@@ -21,7 +21,7 @@ exports[`AlertRuleList should render 1 rule 1`] = `
className="alert-rule-item__name" className="alert-rule-item__name"
> >
<a <a
href="dashboard/db/mygool?panelId=3&fullscreen&edit&tab=alert" href="d/ufkcofof/my-goal?panelId=3&fullscreen=true&edit=true&tab=alert"
> >
<Highlighter <Highlighter
highlightClassName="highlight-search-match" highlightClassName="highlight-search-match"
...@@ -92,7 +92,7 @@ exports[`AlertRuleList should render 1 rule 1`] = ` ...@@ -92,7 +92,7 @@ exports[`AlertRuleList should render 1 rule 1`] = `
</button> </button>
<a <a
className="btn btn-small btn-inverse alert-list__btn width-2" className="btn btn-small btn-inverse alert-list__btn width-2"
href="dashboard/db/mygool?panelId=3&fullscreen&edit&tab=alert" href="d/ufkcofof/my-goal?panelId=3&fullscreen=true&edit=true&tab=alert"
title="Edit alert rule" title="Edit alert rule"
> >
<i <i
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment