Skip to content

Commit 1ef7e24

Browse files
committed
feat(config): add state change detection for dynamic updates
- Introduced `ConfigState` to track timestamp updates for assets, rules, filters, and patterns. - Implemented `hasChanges` to detect modifications in database configurations and trigger updates accordingly.
1 parent 2eb5741 commit 1ef7e24

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

plugins/config/main.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ type ExpressionBackend struct {
7373
Value interface{} `yaml:"value"`
7474
}
7575

76+
type ConfigState struct {
77+
AssetsLastUpdate time.Time
78+
RulesLastUpdate time.Time
79+
FiltersLastUpdate time.Time
80+
PatternsLastUpdate time.Time
81+
}
82+
7683
func (b *ExpressionBackend) ToExpression() Expression {
7784
return Expression{
7885
Field: b.Field,
@@ -267,6 +274,8 @@ func main() {
267274
return
268275
}
269276

277+
state := &ConfigState{}
278+
270279
for {
271280
func() {
272281
db, err := connect()
@@ -284,6 +293,17 @@ func main() {
284293
}
285294
}()
286295

296+
changed, newState, err := hasChanges(db, state)
297+
if err != nil {
298+
_ = catcher.Error("failed to check for changes", err, map[string]any{"process": "plugin_com.utmstack.config"})
299+
time.Sleep(30 * time.Second)
300+
return
301+
}
302+
303+
if !changed {
304+
return
305+
}
306+
287307
filters, err := getFilters(db)
288308
if err != nil {
289309
_ = catcher.Error("failed to get filters", err, map[string]any{"process": "plugin_com.utmstack.config"})
@@ -398,12 +418,46 @@ func main() {
398418
time.Sleep(30 * time.Second)
399419
return
400420
}
421+
422+
*state = newState
401423
}()
402424

403-
time.Sleep(5 * time.Minute)
425+
time.Sleep(30 * time.Second)
404426
}
405427
}
406428

429+
func hasChanges(db *sql.DB, state *ConfigState) (bool, ConfigState, error) {
430+
newState := ConfigState{}
431+
changed := false
432+
433+
queries := []struct {
434+
query string
435+
target *time.Time
436+
old time.Time
437+
}{
438+
{"SELECT MAX(last_update) FROM utm_tenant_config", &newState.AssetsLastUpdate, state.AssetsLastUpdate},
439+
{"SELECT MAX(rule_last_update) FROM utm_correlation_rules", &newState.RulesLastUpdate, state.RulesLastUpdate},
440+
{"SELECT MAX(updated_at) FROM utm_logstash_filter", &newState.FiltersLastUpdate, state.FiltersLastUpdate},
441+
{"SELECT MAX(last_update) FROM utm_regex_pattern", &newState.PatternsLastUpdate, state.PatternsLastUpdate},
442+
}
443+
444+
for _, q := range queries {
445+
var lastUpdate sql.NullTime
446+
err := db.QueryRow(q.query).Scan(&lastUpdate)
447+
if err != nil {
448+
return false, newState, err
449+
}
450+
if lastUpdate.Valid {
451+
*q.target = lastUpdate.Time
452+
}
453+
if (*q.target).After(q.old) {
454+
changed = true
455+
}
456+
}
457+
458+
return changed, newState, nil
459+
}
460+
407461
// connect to postgres database
408462
func connect() (*sql.DB, error) {
409463
pCfg := plugins.PluginCfg("com.utmstack")

0 commit comments

Comments
 (0)