2021-03-08 14:19:21 -06:00
|
|
|
package store
|
2020-11-12 07:11:30 -06:00
|
|
|
|
|
|
|
import (
|
2021-09-21 10:01:23 -05:00
|
|
|
"context"
|
2021-03-03 09:52:19 -06:00
|
|
|
"time"
|
2020-11-12 07:11:30 -06:00
|
|
|
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2021-05-20 07:49:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-02-16 07:15:44 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2022-10-06 01:22:58 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-10-10 14:47:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2021-03-08 14:19:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2022-07-15 13:13:30 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-11-12 07:11:30 -06:00
|
|
|
)
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// TimeNow makes it possible to test usage of time
|
|
|
|
var TimeNow = time.Now
|
|
|
|
|
2021-04-01 03:11:45 -05:00
|
|
|
// AlertDefinitionMaxTitleLength is the maximum length of the alert definition title
|
2021-03-08 14:19:21 -06:00
|
|
|
const AlertDefinitionMaxTitleLength = 190
|
|
|
|
|
2021-03-24 09:20:44 -05:00
|
|
|
// AlertingStore is the database interface used by the Alertmanager service.
|
|
|
|
type AlertingStore interface {
|
2023-03-28 03:34:35 -05:00
|
|
|
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
|
2021-09-21 10:01:23 -05:00
|
|
|
GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error)
|
2022-02-09 03:22:09 -06:00
|
|
|
SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
|
|
|
|
SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error
|
2022-04-05 16:48:51 -05:00
|
|
|
UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
|
2023-02-02 11:45:17 -06:00
|
|
|
MarkConfigurationAsApplied(ctx context.Context, cmd *models.MarkConfigurationAsAppliedCmd) error
|
2023-03-31 15:43:04 -05:00
|
|
|
GetAppliedConfigurations(ctx context.Context, orgID int64, limit int) ([]*models.HistoricAlertConfiguration, error)
|
2023-04-05 13:10:03 -05:00
|
|
|
GetHistoricalConfiguration(ctx context.Context, orgID int64, id int64) (*models.HistoricAlertConfiguration, error)
|
2021-03-03 09:52:19 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// DBstore stores the alert definitions and instances in the database.
|
|
|
|
type DBstore struct {
|
2022-07-15 13:13:30 -05:00
|
|
|
Cfg setting.UnifiedAlertingSettings
|
2022-10-06 01:22:58 -05:00
|
|
|
FeatureToggles featuremgmt.FeatureToggles
|
2022-10-14 14:33:06 -05:00
|
|
|
SQLStore db.DB
|
2022-05-24 08:24:55 -05:00
|
|
|
Logger log.Logger
|
2022-10-10 14:47:53 -05:00
|
|
|
FolderService folder.Service
|
2022-05-24 08:24:55 -05:00
|
|
|
DashboardService dashboards.DashboardService
|
2021-03-03 09:52:19 -06:00
|
|
|
}
|
2022-08-09 09:28:36 -05:00
|
|
|
|
|
|
|
func ProvideDBStore(
|
2023-11-02 14:54:30 -05:00
|
|
|
cfg *setting.Cfg, featureToggles featuremgmt.FeatureToggles, sqlstore db.DB, folderService folder.Service, dashboards dashboards.DashboardService) (*DBstore, error) {
|
2023-06-02 09:38:02 -05:00
|
|
|
store := DBstore{
|
2022-08-09 09:28:36 -05:00
|
|
|
Cfg: cfg.UnifiedAlerting,
|
2022-10-06 01:22:58 -05:00
|
|
|
FeatureToggles: featureToggles,
|
2022-08-09 09:28:36 -05:00
|
|
|
SQLStore: sqlstore,
|
2023-06-02 09:38:02 -05:00
|
|
|
Logger: log.New("ngalert.dbstore"),
|
2022-08-09 09:28:36 -05:00
|
|
|
FolderService: folderService,
|
|
|
|
DashboardService: dashboards,
|
|
|
|
}
|
2023-06-02 09:38:02 -05:00
|
|
|
if err := folderService.RegisterService(store); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &store, nil
|
2022-08-09 09:28:36 -05:00
|
|
|
}
|