Add alerts, currency conversion and notifications.
Items fire when a page is available from an accepted seller at or below max_price, converted with the ECB's daily rates when the page uses another currency. A per-item cooldown, kept in the state file, holds back repeat alerts unless the price drops. Alerts go to Discord, ntfy and JSON webhooks with retries, and to the desktop; 'dibs notify test' sends a sample.
This commit is contained in:
1 parent
2175c0e5aa
commit
8f0eda5d4d
16 files changed
+1409
-15
No files matched your search
+46
-9
@@ -12,20 +12,27 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.security-command.org/Overlord/dibs/internal/alert"
|
||||
"git.security-command.org/Overlord/dibs/internal/config"
|
||||
"git.security-command.org/Overlord/dibs/internal/fsutil"
|
||||
"git.security-command.org/Overlord/dibs/internal/fx"
|
||||
"git.security-command.org/Overlord/dibs/internal/ratelimit"
|
||||
)
|
||||
|
||||
// Hosts and URLs untouched for forgetAfter are dropped, such as URLs removed
|
||||
// from the config.
|
||||
const forgetAfter = 7 * 24 * time.Hour
|
||||
// Hosts, URLs and alerts untouched for forgetAfter are dropped, such as URLs
|
||||
// removed from the config. An alert is kept for as long as the longest
|
||||
// cooldown lasts.
|
||||
const forgetAfter = config.MaxCooldown
|
||||
|
||||
type file struct {
|
||||
Hosts map[string]ratelimit.HostState `json:"hosts"`
|
||||
URLs map[string]time.Time `json:"urls"`
|
||||
Hosts map[string]ratelimit.HostState `json:"hosts"`
|
||||
URLs map[string]time.Time `json:"urls"`
|
||||
Alerts map[string]alert.Sent `json:"alerts,omitempty"`
|
||||
FX *fx.Rates `json:"fx,omitempty"`
|
||||
}
|
||||
|
||||
// Store is the state file. It implements ratelimit.Persister.
|
||||
// Store is the state file. It implements ratelimit.Persister, alert.Store
|
||||
// and fx.Store.
|
||||
type Store struct {
|
||||
path string
|
||||
lock string
|
||||
@@ -100,10 +107,36 @@ func (s *Store) StoreURL(url string, last time.Time) {
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) LoadAlert(item string) (alert.Sent, bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
a, ok := s.read().Alerts[item]
|
||||
return a, ok
|
||||
}
|
||||
|
||||
func (s *Store) StoreAlert(item string, a alert.Sent) {
|
||||
s.update(func(f *file) { f.Alerts[item] = a })
|
||||
}
|
||||
|
||||
func (s *Store) LoadRates() (*fx.Rates, bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
r := s.read().FX
|
||||
return r, r != nil
|
||||
}
|
||||
|
||||
func (s *Store) StoreRates(r *fx.Rates) {
|
||||
s.update(func(f *file) {
|
||||
if f.FX == nil || r.Fetched.After(f.FX.Fetched) { f.FX = r }
|
||||
})
|
||||
}
|
||||
|
||||
// read returns the file's contents, or empty state if there is none yet.
|
||||
// Callers hold mu.
|
||||
func (s *Store) read() *file {
|
||||
f := &file{Hosts: map[string]ratelimit.HostState{}, URLs: map[string]time.Time{}}
|
||||
f := &file{Hosts: map[string]ratelimit.HostState{}, URLs: map[string]time.Time{}, Alerts: map[string]alert.Sent{}}
|
||||
|
||||
data, err := os.ReadFile(s.path)
|
||||
switch {
|
||||
@@ -119,8 +152,9 @@ func (s *Store) read() *file {
|
||||
return f
|
||||
}
|
||||
|
||||
if f.Hosts == nil { f.Hosts = map[string]ratelimit.HostState{} }
|
||||
if f.URLs == nil { f.URLs = map[string]time.Time{} }
|
||||
if f.Hosts == nil { f.Hosts = map[string]ratelimit.HostState{} }
|
||||
if f.URLs == nil { f.URLs = map[string]time.Time{} }
|
||||
if f.Alerts == nil { f.Alerts = map[string]alert.Sent{} }
|
||||
|
||||
return f
|
||||
}
|
||||
@@ -139,6 +173,9 @@ func (s *Store) update(change func(f *file)) {
|
||||
for url, t := range f.URLs {
|
||||
if t.Before(cutoff) { delete(f.URLs, url) }
|
||||
}
|
||||
for item, a := range f.Alerts {
|
||||
if a.Time.Before(cutoff) { delete(f.Alerts, item) }
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(f, "", " ")
|
||||
if err == nil { err = fsutil.WriteFileAtomic(s.path, data, 0o600) }
|
||||
|
||||
Reference in new issue
Block a user