Compare commits
16 Commits
main
...
fix/delete
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a7b18e4d4 | ||
|
|
a3af7bf2c6 | ||
|
|
66c7dc9bdc | ||
|
|
e0a654182e | ||
|
|
7df6881a52 | ||
|
|
55d4ba3ab7 | ||
|
|
6aa9601fe4 | ||
|
|
3f11ba9409 | ||
|
|
88ff32d0bf | ||
|
|
0634a88d80 | ||
|
|
cebc4df68c | ||
|
|
1a680579a6 | ||
|
|
485f032155 | ||
|
|
150efdecf1 | ||
|
|
f73929ee00 | ||
|
|
13884cc753 |
@@ -816,6 +816,10 @@ func (aH *APIHandler) createDowntimeSchedule(w http.ResponseWriter, r *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
if len(schedule.RuleIDs) == 0 {
|
||||
schedule.SilenceAll = true
|
||||
}
|
||||
|
||||
_, err = aH.ruleManager.MaintenanceStore().CreatePlannedMaintenance(r.Context(), schedule)
|
||||
if err != nil {
|
||||
render.Error(w, err)
|
||||
@@ -843,6 +847,10 @@ func (aH *APIHandler) editDowntimeSchedule(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
if len(schedule.RuleIDs) == 0 {
|
||||
schedule.SilenceAll = true
|
||||
}
|
||||
|
||||
err = aH.ruleManager.MaintenanceStore().EditPlannedMaintenance(r.Context(), schedule, id)
|
||||
if err != nil {
|
||||
render.Error(w, err)
|
||||
|
||||
@@ -79,6 +79,10 @@ func (m *MockSQLRuleStore) ExpectEditRule(rule *ruletypes.Rule) {
|
||||
|
||||
// ExpectDeleteRule sets up SQL expectations for DeleteRule operation
|
||||
func (m *MockSQLRuleStore) ExpectDeleteRule(ruleID valuer.UUID) {
|
||||
plannedMaintenancePattern := `DELETE FROM "planned_maintenance_rule".+WHERE \(rule_id = '` + ruleID.StringValue() + `'\)`
|
||||
m.mock.ExpectExec(plannedMaintenancePattern).
|
||||
WillReturnResult(sqlmock.NewResult(0, 1))
|
||||
|
||||
expectedPattern := `DELETE FROM "rule".+WHERE \(id = '` + ruleID.StringValue() + `'\)`
|
||||
m.mock.ExpectExec(expectedPattern).
|
||||
WillReturnResult(sqlmock.NewResult(1, 1))
|
||||
|
||||
@@ -78,6 +78,7 @@ func (r *maintenance) CreatePlannedMaintenance(ctx context.Context, maintenance
|
||||
Description: maintenance.Description,
|
||||
Schedule: maintenance.Schedule,
|
||||
OrgID: claims.OrgID,
|
||||
SilenceAll: maintenance.SilenceAll,
|
||||
}
|
||||
|
||||
maintenanceRules := make([]*ruletypes.StorablePlannedMaintenanceRule, 0)
|
||||
@@ -163,6 +164,7 @@ func (r *maintenance) EditPlannedMaintenance(ctx context.Context, maintenance ru
|
||||
Description: maintenance.Description,
|
||||
Schedule: maintenance.Schedule,
|
||||
OrgID: claims.OrgID,
|
||||
SilenceAll: maintenance.SilenceAll,
|
||||
}
|
||||
|
||||
storablePlannedMaintenanceRules := make([]*ruletypes.StorablePlannedMaintenanceRule, 0)
|
||||
|
||||
@@ -44,6 +44,7 @@ func (r *rule) EditRule(ctx context.Context, storedRule *ruletypes.Rule, cb func
|
||||
NewUpdate().
|
||||
Model(storedRule).
|
||||
Where("id = ?", storedRule.ID.StringValue()).
|
||||
Where("deleted = ?", false).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -58,8 +59,20 @@ func (r *rule) DeleteRule(ctx context.Context, id valuer.UUID, cb func(context.C
|
||||
_, err := r.sqlstore.
|
||||
BunDBCtx(ctx).
|
||||
NewDelete().
|
||||
Model(new(ruletypes.Rule)).
|
||||
Model(new(ruletypes.StorablePlannedMaintenanceRule)).
|
||||
Where("rule_id = ?", id.StringValue()).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = r.sqlstore.
|
||||
BunDBCtx(ctx).
|
||||
NewUpdate().
|
||||
Model((*ruletypes.Rule)(nil)).
|
||||
Set("deleted = ?", true).
|
||||
Where("id = ?", id.StringValue()).
|
||||
Where("deleted = ?", false).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -79,7 +92,7 @@ func (r *rule) GetStoredRules(ctx context.Context, orgID string) ([]*ruletypes.R
|
||||
BunDB().
|
||||
NewSelect().
|
||||
Model(&rules).
|
||||
Where("org_id = ?", orgID).
|
||||
Where("org_id = ?", orgID).Where("deleted = ?", false).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return rules, err
|
||||
@@ -94,7 +107,7 @@ func (r *rule) GetStoredRule(ctx context.Context, id valuer.UUID) (*ruletypes.Ru
|
||||
BunDB().
|
||||
NewSelect().
|
||||
Model(rule).
|
||||
Where("id = ?", id.StringValue()).
|
||||
Where("id = ?", id.StringValue()).Where("deleted = ?", false).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -138,6 +138,7 @@ func NewSQLMigrationProviderFactories(
|
||||
sqlmigration.NewUpdateTTLSettingForCustomRetentionFactory(sqlstore, sqlschema),
|
||||
sqlmigration.NewAddRoutePolicyFactory(sqlstore, sqlschema),
|
||||
sqlmigration.NewAddAuthTokenFactory(sqlstore, sqlschema),
|
||||
sqlmigration.NewAddSilenceAllColumnFactory(sqlstore, sqlschema),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
155
pkg/sqlmigration/051_add_silence_all_column.go
Normal file
155
pkg/sqlmigration/051_add_silence_all_column.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package sqlmigration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/SigNoz/signoz/pkg/errors"
|
||||
"github.com/SigNoz/signoz/pkg/factory"
|
||||
"github.com/SigNoz/signoz/pkg/sqlschema"
|
||||
"github.com/SigNoz/signoz/pkg/sqlstore"
|
||||
"github.com/SigNoz/signoz/pkg/types"
|
||||
"github.com/SigNoz/signoz/pkg/valuer"
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/migrate"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type addSilenceAllColumn struct {
|
||||
sqlstore sqlstore.SQLStore
|
||||
sqlschema sqlschema.SQLSchema
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type plannedMaintenance struct {
|
||||
bun.BaseModel `bun:"table:planned_maintenance"`
|
||||
types.Identifiable
|
||||
SilenceAll bool `bun:"silence_all,type:boolean"`
|
||||
}
|
||||
|
||||
type plannedMaintenanceRule struct {
|
||||
bun.BaseModel `bun:"table:planned_maintenance_rule"`
|
||||
types.Identifiable
|
||||
PlannedMaintenanceID valuer.UUID `bun:"planned_maintenance_id,type:text"`
|
||||
RuleID valuer.UUID `bun:"rule_id,type:text"`
|
||||
}
|
||||
|
||||
func NewAddSilenceAllColumnFactory(sqlstore sqlstore.SQLStore, sqlschema sqlschema.SQLSchema) factory.ProviderFactory[SQLMigration, Config] {
|
||||
return factory.NewProviderFactory(factory.MustNewName("add_silence_all_column"), func(ctx context.Context, providerSettings factory.ProviderSettings, config Config) (SQLMigration, error) {
|
||||
return newAddSilenceAllColumn(ctx, providerSettings, config, sqlstore, sqlschema)
|
||||
})
|
||||
}
|
||||
|
||||
func newAddSilenceAllColumn(_ context.Context, settings factory.ProviderSettings, _ Config, sqlstore sqlstore.SQLStore, sqlschema sqlschema.SQLSchema) (SQLMigration, error) {
|
||||
return &addSilenceAllColumn{
|
||||
sqlstore: sqlstore,
|
||||
sqlschema: sqlschema,
|
||||
logger: settings.Logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (migration *addSilenceAllColumn) Register(migrations *migrate.Migrations) error {
|
||||
if err := migrations.Register(migration.Up, migration.Down); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (migration *addSilenceAllColumn) Up(ctx context.Context, db *bun.DB) error {
|
||||
table, _, err := migration.sqlschema.GetTable(ctx, sqlschema.TableName("planned_maintenance"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, column := range table.Columns {
|
||||
if column.Name == "silence_all" {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var joinTableBackup []plannedMaintenanceRule
|
||||
err = db.NewSelect().
|
||||
Model(&joinTableBackup).
|
||||
Scan(ctx)
|
||||
if err != nil {
|
||||
return errors.NewInternalf(errors.CodeInternal, "failed to backup planned_maintenance_rule data: %v", err)
|
||||
}
|
||||
|
||||
maintenanceIDsMap := make(map[string]bool)
|
||||
for _, record := range joinTableBackup {
|
||||
maintenanceIDsMap[record.PlannedMaintenanceID.StringValue()] = true
|
||||
}
|
||||
var maintenanceIDsWithRules []string
|
||||
for id := range maintenanceIDsMap {
|
||||
maintenanceIDsWithRules = append(maintenanceIDsWithRules, id)
|
||||
}
|
||||
|
||||
tx, err := db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
|
||||
sqls := [][]byte{}
|
||||
|
||||
column := &sqlschema.Column{
|
||||
Name: "silence_all",
|
||||
DataType: sqlschema.DataTypeBoolean,
|
||||
Nullable: false,
|
||||
Default: "false",
|
||||
}
|
||||
|
||||
columnSQLs := migration.sqlschema.Operator().AddColumn(table, nil, column, nil)
|
||||
sqls = append(sqls, columnSQLs...)
|
||||
|
||||
for _, sqlStmt := range sqls {
|
||||
if _, err := tx.ExecContext(ctx, string(sqlStmt)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(joinTableBackup) > 0 {
|
||||
_, err = tx.NewInsert().
|
||||
Model(&joinTableBackup).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return errors.NewInternalf(errors.CodeInternal, "failed to restore planned_maintenance_rule data: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = migration.backfillSilenceAll(ctx, tx, maintenanceIDsWithRules)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (migration *addSilenceAllColumn) backfillSilenceAll(ctx context.Context, tx bun.Tx, maintenanceIDsWithRules []string) error {
|
||||
if len(maintenanceIDsWithRules) == 0 {
|
||||
_, err := tx.NewUpdate().
|
||||
Model((*plannedMaintenance)(nil)).
|
||||
Set("silence_all = ?", true).
|
||||
Where("1 = 1").
|
||||
Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := tx.NewUpdate().
|
||||
Model((*plannedMaintenance)(nil)).
|
||||
Set("silence_all = ?", true).
|
||||
Where("id NOT IN (?)", bun.In(maintenanceIDsWithRules)).
|
||||
Exec(ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (migration *addSilenceAllColumn) Down(ctx context.Context, db *bun.DB) error {
|
||||
return nil
|
||||
}
|
||||
@@ -24,6 +24,7 @@ type StorablePlannedMaintenance struct {
|
||||
Description string `bun:"description,type:text"`
|
||||
Schedule *Schedule `bun:"schedule,type:text,notnull"`
|
||||
OrgID string `bun:"org_id,type:text"`
|
||||
SilenceAll bool `bun:"silence_all,type:boolean"`
|
||||
}
|
||||
|
||||
type GettablePlannedMaintenance struct {
|
||||
@@ -38,6 +39,7 @@ type GettablePlannedMaintenance struct {
|
||||
UpdatedBy string `json:"updatedBy"`
|
||||
Status string `json:"status"`
|
||||
Kind string `json:"kind"`
|
||||
SilenceAll bool `json:"silenceAll"`
|
||||
}
|
||||
|
||||
type StorablePlannedMaintenanceRule struct {
|
||||
@@ -64,7 +66,7 @@ func (m *GettablePlannedMaintenance) ShouldSkip(ruleID string, now time.Time) bo
|
||||
}
|
||||
}
|
||||
// If no alert ids, then skip all alerts
|
||||
if len(m.RuleIDs) == 0 {
|
||||
if m.SilenceAll {
|
||||
found = true
|
||||
}
|
||||
|
||||
@@ -295,6 +297,7 @@ func (m GettablePlannedMaintenance) MarshalJSON() ([]byte, error) {
|
||||
UpdatedBy string `json:"updatedBy" db:"updated_by"`
|
||||
Status string `json:"status"`
|
||||
Kind string `json:"kind"`
|
||||
SilenceAll bool `json:"silenceAll" db:"silence_all"`
|
||||
}{
|
||||
Id: m.Id,
|
||||
Name: m.Name,
|
||||
@@ -307,6 +310,7 @@ func (m GettablePlannedMaintenance) MarshalJSON() ([]byte, error) {
|
||||
UpdatedBy: m.UpdatedBy,
|
||||
Status: status,
|
||||
Kind: kind,
|
||||
SilenceAll: m.SilenceAll,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -328,6 +332,7 @@ func (m *GettablePlannedMaintenanceRule) ConvertGettableMaintenanceRuleToGettabl
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
CreatedBy: m.CreatedBy,
|
||||
UpdatedBy: m.UpdatedBy,
|
||||
SilenceAll: m.SilenceAll,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "only-on-saturday",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "Europe/London",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -38,6 +39,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "weekly-across-midnight-previous-day",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -55,6 +57,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "weekly-across-midnight-previous-day",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -72,6 +75,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "weekly-across-midnight-previous-day",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -89,6 +93,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "weekly-across-midnight-previous-day-not-in-repeaton",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -106,6 +111,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "daily-maintenance-across-midnight",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -122,6 +128,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "at-start-time-boundary",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -138,6 +145,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "at-end-time-boundary",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -154,6 +162,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-multi-day-duration",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -170,6 +179,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "weekly-multi-day-duration",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -187,6 +197,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-crosses-to-next-month",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -203,6 +214,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "timezone-offset-test",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "America/New_York", // UTC-5 or UTC-4 depending on DST
|
||||
Recurrence: &Recurrence{
|
||||
@@ -219,6 +231,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "daily-maintenance-time-outside-window",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -235,6 +248,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring-maintenance-with-past-end-date",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -252,6 +266,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-maintenance-spans-month-end",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -268,6 +283,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "weekly-empty-repeaton",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -285,6 +301,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-maintenance-february-fewer-days",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -300,6 +317,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "daily-maintenance-crosses-midnight",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -315,6 +333,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-maintenance-crosses-month-end",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -330,6 +349,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-maintenance-crosses-month-end-and-duration-is-2-days",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -345,6 +365,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "weekly-maintenance-crosses-midnight",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -361,6 +382,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-maintenance-crosses-month-end-and-duration-is-2-days",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -376,6 +398,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "daily-maintenance-crosses-midnight",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -391,6 +414,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "monthly-maintenance-crosses-month-end-and-duration-is-2-hours",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -406,6 +430,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "fixed planned maintenance start <= ts <= end",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
StartTime: time.Now().UTC().Add(-time.Hour),
|
||||
@@ -418,6 +443,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "fixed planned maintenance start >= ts",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
StartTime: time.Now().UTC().Add(time.Hour),
|
||||
@@ -430,6 +456,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "fixed planned maintenance ts < start",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
StartTime: time.Now().UTC().Add(time.Hour),
|
||||
@@ -442,6 +469,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat sunday, saturday, weekly for 24 hours, in Us/Eastern timezone",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "US/Eastern",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -458,6 +486,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat daily from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -473,6 +502,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat daily from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -488,6 +518,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat daily from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -503,6 +534,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat weekly on monday from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -519,6 +551,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat weekly on monday from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -535,6 +568,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat weekly on monday from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -551,6 +585,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat weekly on monday from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -567,6 +602,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat weekly on monday from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -583,6 +619,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat monthly on 4th from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -598,6 +635,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat monthly on 4th from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -613,6 +651,7 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
{
|
||||
name: "recurring maintenance, repeat monthly on 4th from 12:00 to 14:00",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
@@ -634,3 +673,144 @@ func TestShouldSkipMaintenance(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSilenceAllFunctionality(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
maintenance *GettablePlannedMaintenance
|
||||
ruleID string
|
||||
ts time.Time
|
||||
skip bool
|
||||
}{
|
||||
{
|
||||
name: "SilenceAll=true with specific ruleIDs - should silence rule in list during maintenance window",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
RuleIDs: []string{"rule-1", "rule-2"},
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
StartTime: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
Duration: Duration(time.Hour * 2),
|
||||
RepeatType: RepeatTypeDaily,
|
||||
},
|
||||
},
|
||||
},
|
||||
ruleID: "rule-1",
|
||||
ts: time.Date(2024, 1, 1, 12, 30, 0, 0, time.UTC),
|
||||
skip: true,
|
||||
},
|
||||
{
|
||||
name: "SilenceAll=true with specific ruleIDs - should silence rule NOT in list during maintenance window",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
RuleIDs: []string{"rule-1", "rule-2"},
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
StartTime: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
Duration: Duration(time.Hour * 2),
|
||||
RepeatType: RepeatTypeDaily,
|
||||
},
|
||||
},
|
||||
},
|
||||
ruleID: "rule-3",
|
||||
ts: time.Date(2024, 1, 1, 12, 30, 0, 0, time.UTC),
|
||||
skip: true,
|
||||
},
|
||||
{
|
||||
name: "SilenceAll=false with specific ruleIDs - should silence rule in list during maintenance window",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
RuleIDs: []string{"rule-1", "rule-2"},
|
||||
SilenceAll: false,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
StartTime: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
Duration: Duration(time.Hour * 2),
|
||||
RepeatType: RepeatTypeDaily,
|
||||
},
|
||||
},
|
||||
},
|
||||
ruleID: "rule-1",
|
||||
ts: time.Date(2024, 1, 1, 12, 30, 0, 0, time.UTC),
|
||||
skip: true,
|
||||
},
|
||||
{
|
||||
name: "SilenceAll=true with empty ruleIDs - should silence all rules during maintenance window",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
RuleIDs: []string{},
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
StartTime: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
Duration: Duration(time.Hour * 2),
|
||||
RepeatType: RepeatTypeDaily,
|
||||
},
|
||||
},
|
||||
},
|
||||
ruleID: "any-rule",
|
||||
ts: time.Date(2024, 1, 1, 12, 30, 0, 0, time.UTC),
|
||||
skip: true,
|
||||
},
|
||||
{
|
||||
name: "SilenceAll=false with empty ruleIDs - should not silence any rules",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
RuleIDs: []string{},
|
||||
SilenceAll: false,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
StartTime: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
Duration: Duration(time.Hour * 2),
|
||||
RepeatType: RepeatTypeDaily,
|
||||
},
|
||||
},
|
||||
},
|
||||
ruleID: "any-rule",
|
||||
ts: time.Date(2024, 1, 1, 12, 30, 0, 0, time.UTC),
|
||||
skip: false,
|
||||
},
|
||||
{
|
||||
name: "SilenceAll=true with fixed maintenance window - should not skip outside window",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
RuleIDs: []string{},
|
||||
SilenceAll: true,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
StartTime: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
EndTime: time.Date(2024, 1, 1, 14, 0, 0, 0, time.UTC),
|
||||
},
|
||||
},
|
||||
ruleID: "any-rule",
|
||||
ts: time.Date(2024, 1, 1, 15, 0, 0, 0, time.UTC),
|
||||
skip: false,
|
||||
},
|
||||
{
|
||||
name: "SilenceAll=false with nil ruleIDs - should not silence any rules",
|
||||
maintenance: &GettablePlannedMaintenance{
|
||||
RuleIDs: nil,
|
||||
SilenceAll: false,
|
||||
Schedule: &Schedule{
|
||||
Timezone: "UTC",
|
||||
Recurrence: &Recurrence{
|
||||
StartTime: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
|
||||
Duration: Duration(time.Hour * 2),
|
||||
RepeatType: RepeatTypeDaily,
|
||||
},
|
||||
},
|
||||
},
|
||||
ruleID: "any-rule",
|
||||
ts: time.Date(2024, 1, 1, 12, 30, 0, 0, time.UTC),
|
||||
skip: false,
|
||||
},
|
||||
}
|
||||
|
||||
for idx, c := range cases {
|
||||
result := c.maintenance.ShouldSkip(c.ruleID, c.ts)
|
||||
if result != c.skip {
|
||||
t.Errorf("skip %v, got %v, case:%d - %s", c.skip, result, idx, c.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user