mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Some refactoring that will simplify next changes for dry-run PRs. This should be no-op as far as the created ngalert resources and database state, though it does change some logs. The key change here is to modify migrateOrg to return pairs of legacy struct + ngalert struct instead of actually persisting the alerts and alertmanager config. This will allow us to capture error information during dry-run migration. It also moves most persistence-related operations such as title deduplication and folder creation to the right before we persist. This will simplify eventual partial migrations (individual alerts, dashboards, channels, ...). Additionally it changes channel code to deal with PostableGrafanaReceiver instead of PostableApiReceiver (integration instead of contact point).
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
pb "github.com/prometheus/alertmanager/silence/silencepb"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
legacymodels "github.com/grafana/grafana/pkg/services/alerting/models"
|
|
migrationStore "github.com/grafana/grafana/pkg/services/ngalert/migration/store"
|
|
"github.com/grafana/grafana/pkg/services/secrets"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
// OrgMigration is a helper struct for migrating alerts for a single org. It contains state, services, and caches.
|
|
type OrgMigration struct {
|
|
cfg *setting.Cfg
|
|
log log.Logger
|
|
|
|
migrationStore migrationStore.Store
|
|
encryptionService secrets.Service
|
|
|
|
orgID int64
|
|
silences []*pb.MeshSilence
|
|
}
|
|
|
|
// newOrgMigration creates a new OrgMigration for the given orgID.
|
|
func (ms *migrationService) newOrgMigration(orgID int64) *OrgMigration {
|
|
return &OrgMigration{
|
|
cfg: ms.cfg,
|
|
log: ms.log.New("orgID", orgID),
|
|
|
|
migrationStore: ms.migrationStore,
|
|
encryptionService: ms.encryptionService,
|
|
|
|
orgID: orgID,
|
|
silences: make([]*pb.MeshSilence, 0),
|
|
}
|
|
}
|
|
|
|
// ChannelCache caches channels by ID and UID.
|
|
type ChannelCache struct {
|
|
channels []*legacymodels.AlertNotification
|
|
cache map[any]*legacymodels.AlertNotification
|
|
fetch func(ctx context.Context, key notificationKey) (*legacymodels.AlertNotification, error)
|
|
}
|
|
|
|
func (c *ChannelCache) Get(ctx context.Context, key notificationKey) (*legacymodels.AlertNotification, error) {
|
|
if key.ID > 0 {
|
|
if channel, ok := c.cache[key.ID]; ok {
|
|
return channel, nil
|
|
}
|
|
}
|
|
if key.UID != "" {
|
|
if channel, ok := c.cache[key.UID]; ok {
|
|
return channel, nil
|
|
}
|
|
}
|
|
|
|
channel, err := c.fetch(ctx, key)
|
|
if err != nil {
|
|
if errors.Is(err, migrationStore.ErrNotFound) {
|
|
if key.ID > 0 {
|
|
c.cache[key.ID] = nil
|
|
}
|
|
if key.UID != "" {
|
|
c.cache[key.UID] = nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
c.cache[channel.ID] = channel
|
|
c.cache[channel.UID] = channel
|
|
c.channels = append(c.channels, channel)
|
|
|
|
return channel, nil
|
|
}
|
|
|
|
func (ms *migrationService) newChannelCache(orgID int64) *ChannelCache {
|
|
return &ChannelCache{
|
|
cache: make(map[any]*legacymodels.AlertNotification),
|
|
fetch: func(ctx context.Context, key notificationKey) (*legacymodels.AlertNotification, error) {
|
|
c, err := ms.migrationStore.GetNotificationChannel(ctx, migrationStore.GetNotificationChannelQuery{OrgID: orgID, ID: key.ID, UID: key.UID})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
},
|
|
}
|
|
}
|