Alerting: Use URLs in image annotations (#66804)

* use tokens or urls in image annotations

* improve tests, fix some comments

* fix empty tokens

* code review changes, check for url before checking for token (support old token formats)
This commit is contained in:
Santiago
2023-04-26 13:06:18 -03:00
committed by GitHub
parent e1ab9cc9d8
commit b0881daf23
10 changed files with 146 additions and 35 deletions

View File

@@ -20,6 +20,10 @@ type ImageStore interface {
// if the image has expired or if an image with the token does not exist.
GetImage(ctx context.Context, token string) (*models.Image, error)
// GetImageByURL looks for a image by its URL. It returns ErrImageNotFound
// if the image has expired or if there is no image associated with the URL.
GetImageByURL(ctx context.Context, url string) (*models.Image, error)
// GetImages returns all images that match the tokens. If one or more images
// have expired or do not exist then it also returns the unmatched tokens
// and an ErrImageNotFound error.
@@ -54,6 +58,23 @@ func (st DBstore) GetImage(ctx context.Context, token string) (*models.Image, er
return &image, nil
}
func (st DBstore) GetImageByURL(ctx context.Context, url string) (*models.Image, error) {
var image models.Image
if err := st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
exists, err := sess.Where("url = ? AND expires_at > ?", url, TimeNow().UTC()).Limit(1).Get(&image)
if err != nil {
return fmt.Errorf("failed to get image: %w", err)
} else if !exists {
return models.ErrImageNotFound
} else {
return nil
}
}); err != nil {
return nil, err
}
return &image, nil
}
func (st DBstore) GetImages(ctx context.Context, tokens []string) ([]models.Image, []string, error) {
var images []models.Image
if err := st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {