mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Delete expired images from the database (#53236)
This commit adds a DeleteExpiredService that deletes expired images from the database. It is run in the periodic collector service.
This commit is contained in:
@@ -5,8 +5,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ErrImageNotFound is returned when the image does not exist.
|
||||
var ErrImageNotFound = errors.New("image not found")
|
||||
var (
|
||||
// ErrImageNotFound is returned when the image does not exist.
|
||||
ErrImageNotFound = errors.New("image not found")
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
ID int64 `xorm:"pk autoincr 'id'"`
|
||||
@@ -17,6 +19,27 @@ type Image struct {
|
||||
ExpiresAt time.Time `xorm:"expires_at"`
|
||||
}
|
||||
|
||||
// ExtendDuration extends the expiration time of the image. It can shorten
|
||||
// the duration of the image if d is negative.
|
||||
func (i *Image) ExtendDuration(d time.Duration) {
|
||||
i.ExpiresAt = i.ExpiresAt.Add(d)
|
||||
}
|
||||
|
||||
// HasExpired returns true if the image has expired.
|
||||
func (i *Image) HasExpired() bool {
|
||||
return time.Now().After(i.ExpiresAt)
|
||||
}
|
||||
|
||||
// HasPath returns true if the image has a path on disk.
|
||||
func (i *Image) HasPath() bool {
|
||||
return i.Path != ""
|
||||
}
|
||||
|
||||
// HasURL returns true if the image has a URL.
|
||||
func (i *Image) HasURL() bool {
|
||||
return i.URL != ""
|
||||
}
|
||||
|
||||
// A XORM interface that defines the used table for this struct.
|
||||
func (i *Image) TableName() string {
|
||||
return "alert_image"
|
||||
|
||||
48
pkg/services/ngalert/models/image_test.go
Normal file
48
pkg/services/ngalert/models/image_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestImage_ExtendDuration(t *testing.T) {
|
||||
var i Image
|
||||
d := time.Now().Add(time.Minute)
|
||||
i.ExpiresAt = d
|
||||
// extend the duration for 1 minute
|
||||
i.ExtendDuration(time.Minute)
|
||||
assert.Equal(t, d.Add(time.Minute), i.ExpiresAt)
|
||||
// can shorten the duration too
|
||||
i.ExtendDuration(-time.Minute)
|
||||
assert.Equal(t, d, i.ExpiresAt)
|
||||
}
|
||||
|
||||
func TestImage_HasExpired(t *testing.T) {
|
||||
var i Image
|
||||
i.ExpiresAt = time.Now().Add(time.Minute)
|
||||
assert.False(t, i.HasExpired())
|
||||
i.ExpiresAt = time.Now()
|
||||
assert.True(t, i.HasExpired())
|
||||
i.ExpiresAt = time.Now().Add(-time.Minute)
|
||||
assert.True(t, i.HasExpired())
|
||||
}
|
||||
|
||||
func TestImage_HasPath(t *testing.T) {
|
||||
var i Image
|
||||
assert.False(t, i.HasPath())
|
||||
i.Path = "/"
|
||||
assert.True(t, i.HasPath())
|
||||
i.Path = "/tmp/image.png"
|
||||
assert.True(t, i.HasPath())
|
||||
}
|
||||
|
||||
func TestImage_HasURL(t *testing.T) {
|
||||
var i Image
|
||||
assert.False(t, i.HasURL())
|
||||
i.URL = "/"
|
||||
assert.True(t, i.HasURL())
|
||||
i.URL = "https://example.com/image.png"
|
||||
assert.True(t, i.HasURL())
|
||||
}
|
||||
Reference in New Issue
Block a user