1
1
package memory
2
2
3
3
import (
4
+ "context"
4
5
"database/sql"
5
6
"fmt"
6
7
"time"
@@ -23,7 +24,7 @@ func New(_, _ string) (*DB, error) {
23
24
}
24
25
25
26
// GetConfig gets the user's configuration.
26
- func (d * DB ) GetConfig (userID string ) (configs.View , error ) {
27
+ func (d * DB ) GetConfig (ctx context. Context , userID string ) (configs.View , error ) {
27
28
c , ok := d .cfgs [userID ]
28
29
if ! ok {
29
30
return configs.View {}, sql .ErrNoRows
@@ -32,7 +33,7 @@ func (d *DB) GetConfig(userID string) (configs.View, error) {
32
33
}
33
34
34
35
// SetConfig sets configuration for a user.
35
- func (d * DB ) SetConfig (userID string , cfg configs.Config ) error {
36
+ func (d * DB ) SetConfig (ctx context. Context , userID string , cfg configs.Config ) error {
36
37
if ! cfg .RulesConfig .FormatVersion .IsValid () {
37
38
return fmt .Errorf ("invalid rule format version %v" , cfg .RulesConfig .FormatVersion )
38
39
}
@@ -42,12 +43,12 @@ func (d *DB) SetConfig(userID string, cfg configs.Config) error {
42
43
}
43
44
44
45
// GetAllConfigs gets all of the configs.
45
- func (d * DB ) GetAllConfigs () (map [string ]configs.View , error ) {
46
+ func (d * DB ) GetAllConfigs (ctx context. Context ) (map [string ]configs.View , error ) {
46
47
return d .cfgs , nil
47
48
}
48
49
49
50
// GetConfigs gets all of the configs that have changed recently.
50
- func (d * DB ) GetConfigs (since configs.ID ) (map [string ]configs.View , error ) {
51
+ func (d * DB ) GetConfigs (ctx context. Context , since configs.ID ) (map [string ]configs.View , error ) {
51
52
cfgs := map [string ]configs.View {}
52
53
for user , c := range d .cfgs {
53
54
if c .ID > since {
@@ -60,8 +61,8 @@ func (d *DB) GetConfigs(since configs.ID) (map[string]configs.View, error) {
60
61
// SetDeletedAtConfig sets a deletedAt for configuration
61
62
// by adding a single new row with deleted_at set
62
63
// the same as SetConfig is actually insert
63
- func (d * DB ) SetDeletedAtConfig (userID string , deletedAt time.Time ) error {
64
- cv , err := d .GetConfig (userID )
64
+ func (d * DB ) SetDeletedAtConfig (ctx context. Context , userID string , deletedAt time.Time ) error {
65
+ cv , err := d .GetConfig (ctx , userID )
65
66
if err != nil {
66
67
return err
67
68
}
@@ -73,13 +74,13 @@ func (d *DB) SetDeletedAtConfig(userID string, deletedAt time.Time) error {
73
74
}
74
75
75
76
// DeactivateConfig deactivates configuration for a user by creating new configuration with DeletedAt set to now
76
- func (d * DB ) DeactivateConfig (userID string ) error {
77
- return d .SetDeletedAtConfig (userID , time .Now ())
77
+ func (d * DB ) DeactivateConfig (ctx context. Context , userID string ) error {
78
+ return d .SetDeletedAtConfig (ctx , userID , time .Now ())
78
79
}
79
80
80
81
// RestoreConfig restores deactivated configuration for a user by creating new configuration with empty DeletedAt
81
- func (d * DB ) RestoreConfig (userID string ) error {
82
- return d .SetDeletedAtConfig (userID , time.Time {})
82
+ func (d * DB ) RestoreConfig (ctx context. Context , userID string ) error {
83
+ return d .SetDeletedAtConfig (ctx , userID , time.Time {})
83
84
}
84
85
85
86
// Close finishes using the db. Noop.
@@ -88,7 +89,7 @@ func (d *DB) Close() error {
88
89
}
89
90
90
91
// GetRulesConfig gets the rules config for a user.
91
- func (d * DB ) GetRulesConfig (userID string ) (configs.VersionedRulesConfig , error ) {
92
+ func (d * DB ) GetRulesConfig (ctx context. Context , userID string ) (configs.VersionedRulesConfig , error ) {
92
93
c , ok := d .cfgs [userID ]
93
94
if ! ok {
94
95
return configs.VersionedRulesConfig {}, sql .ErrNoRows
@@ -101,22 +102,22 @@ func (d *DB) GetRulesConfig(userID string) (configs.VersionedRulesConfig, error)
101
102
}
102
103
103
104
// SetRulesConfig sets the rules config for a user.
104
- func (d * DB ) SetRulesConfig (userID string , oldConfig , newConfig configs.RulesConfig ) (bool , error ) {
105
+ func (d * DB ) SetRulesConfig (ctx context. Context , userID string , oldConfig , newConfig configs.RulesConfig ) (bool , error ) {
105
106
c , ok := d .cfgs [userID ]
106
107
if ! ok {
107
- return true , d .SetConfig (userID , configs.Config {RulesConfig : newConfig })
108
+ return true , d .SetConfig (ctx , userID , configs.Config {RulesConfig : newConfig })
108
109
}
109
110
if ! oldConfig .Equal (c .Config .RulesConfig ) {
110
111
return false , nil
111
112
}
112
- return true , d .SetConfig (userID , configs.Config {
113
+ return true , d .SetConfig (ctx , userID , configs.Config {
113
114
AlertmanagerConfig : c .Config .AlertmanagerConfig ,
114
115
RulesConfig : newConfig ,
115
116
})
116
117
}
117
118
118
119
// GetAllRulesConfigs gets the rules configs for all users that have them.
119
- func (d * DB ) GetAllRulesConfigs () (map [string ]configs.VersionedRulesConfig , error ) {
120
+ func (d * DB ) GetAllRulesConfigs (ctx context. Context ) (map [string ]configs.VersionedRulesConfig , error ) {
120
121
cfgs := map [string ]configs.VersionedRulesConfig {}
121
122
for user , c := range d .cfgs {
122
123
cfg := c .GetVersionedRulesConfig ()
@@ -129,7 +130,7 @@ func (d *DB) GetAllRulesConfigs() (map[string]configs.VersionedRulesConfig, erro
129
130
130
131
// GetRulesConfigs gets the rules configs that have changed
131
132
// since the given config version.
132
- func (d * DB ) GetRulesConfigs (since configs.ID ) (map [string ]configs.VersionedRulesConfig , error ) {
133
+ func (d * DB ) GetRulesConfigs (ctx context. Context , since configs.ID ) (map [string ]configs.VersionedRulesConfig , error ) {
133
134
cfgs := map [string ]configs.VersionedRulesConfig {}
134
135
for user , c := range d .cfgs {
135
136
if c .ID <= since {
0 commit comments