/
githubmirror
/
grafana
Обзор
Документация
Войти
/
githubmirror
/
grafana
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
pkg/services/annotations/annotationsimpl/cleanup.go
59 строк
2 KB
Craig O'Donnell
fix(annotations): avoid mutating annotation item query within repository (#127499)
30 июн 2026, 18:58
Не верифицирован
30 июн 2026, 18:58
f255fe3
Код
Авторство
О чём код?
package annotationsimpl import ( "context" "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/services/annotations" "github.com/grafana/grafana/pkg/setting" ) // CleanupServiceImpl is responsible for cleaning old annotations. type CleanupServiceImpl struct { store Store } func ProvideCleanupService(db db.DB, cfg *setting.Cfg) *CleanupServiceImpl { return &CleanupServiceImpl{ store: NewXormStore(cfg, log.New("annotations"), db, nil, nil), } } const ( alertAnnotationType = "alert_id <> 0" dashboardAnnotationType = "dashboard_id <> 0 AND alert_id = 0" apiAnnotationType = "alert_id = 0 AND dashboard_id = 0" ) // Run deletes old annotations created by alert rules, API // requests and human made in the UI. It subsequently deletes orphaned rows // from the annotation_tag table. Cleanup actions are performed in batches // so that no query takes too long to complete. // // Returns the number of annotation and annotation_tag rows deleted. If an // error occurs, it returns the number of rows affected so far. func (cs *CleanupServiceImpl) Run(ctx context.Context, settings annotations.CleanupSettings) (int64, int64, error) { var totalCleanedAnnotations int64 affected, err := cs.store.CleanAnnotations(ctx, settings.Alerting, alertAnnotationType) totalCleanedAnnotations += affected if err != nil { return totalCleanedAnnotations, 0, err } affected, err = cs.store.CleanAnnotations(ctx, settings.API, apiAnnotationType) totalCleanedAnnotations += affected if err != nil { return totalCleanedAnnotations, 0, err } affected, err = cs.store.CleanAnnotations(ctx, settings.Dashboard, dashboardAnnotationType) totalCleanedAnnotations += affected if err != nil { return totalCleanedAnnotations, 0, err } if totalCleanedAnnotations > 0 { affected, err = cs.store.CleanOrphanedAnnotationTags(ctx) } return totalCleanedAnnotations, affected, err }