From 052f7373fca1ff6eb3c7af528500e94e4dde880f Mon Sep 17 00:00:00 2001 From: "J. David Lee" Date: Tue, 11 Jun 2019 09:44:35 +0200 Subject: [PATCH] Code cleanup --- actions.go | 29 +++++++++++++---------------- background.go | 2 +- db.go | 25 +++++++++++-------------- migration.go | 4 +--- report-handler.go | 4 ++-- templates/log.html | 2 +- templates/sources.html | 22 ---------------------- tmpl_gen.go | 24 +----------------------- types.go | 3 +-- user-handlers.go | 4 ---- 10 files changed, 31 insertions(+), 88 deletions(-) diff --git a/actions.go b/actions.go index f5bd9d1..568d525 100644 --- a/actions.go +++ b/actions.go @@ -15,30 +15,27 @@ func run(cmd, name, text string) { } } -func runLogAction(action, sourceName, text string) { +func runLogAction(sourceName, text string) { log.Printf("[%s] %s", sourceName, text) - if action == "" { - dir, err := os.Getwd() - if err != nil { - log.Printf("Failed to get working directory: %v", err) - return - } - action = filepath.Join(dir, "log-action") + dir, err := os.Getwd() + if err != nil { + log.Printf("Failed to get working directory: %v", err) + return } + action := filepath.Join(dir, "log-action") run(action, sourceName, text) } -func runAlertAction(action, sourceName, text string) { +func runAlertAction(sourceName, text string) { log.Printf("[%s] ALERT %s", sourceName, text) - if action == "" { - dir, err := os.Getwd() - if err != nil { - log.Printf("Failed to get working directory: %v", err) - return - } - action = filepath.Join(dir, "alert-action") + + dir, err := os.Getwd() + if err != nil { + log.Printf("Failed to get working directory: %v", err) + return } + action := filepath.Join(dir, "alert-action") run(action, sourceName, text) } diff --git a/background.go b/background.go index 738e4e2..07b437c 100644 --- a/background.go +++ b/background.go @@ -20,7 +20,7 @@ func processTimeouts() { } _ = db.LogInsert(e) - runAlertAction(src.AlertAction, src.Name, e.Text) + runAlertAction(src.Name, e.Text) _ = db.SourceUpdateAlertedAt(src.SourceID) } } diff --git a/db.go b/db.go index 2188a5d..99e7ef4 100644 --- a/db.go +++ b/db.go @@ -177,12 +177,10 @@ func (db *dbal) SourceInsert(s *Source) error { _, err := db.Exec(`INSERT INTO sources(`+ ` SourceID,Name,APIKey,Description,`+ - ` LastSeenAt,AlertTimeout,AlertedAt,`+ - ` LogAction,AlertAction`+ - `)VALUES(?,?,?,?,?,?,?,?,?)`, + ` LastSeenAt,AlertTimeout,AlertedAt`+ + `)VALUES(?,?,?,?,?,?,?)`, s.SourceID, s.Name, s.APIKey, s.Description, - s.LastSeenAt, s.AlertTimeout, s.AlertedAt, - s.LogAction, s.AlertAction) + s.LastSeenAt, s.AlertTimeout, s.AlertedAt) if err != nil { db.logf("Failed to insert source: %v", err) } @@ -190,16 +188,14 @@ func (db *dbal) SourceInsert(s *Source) error { } const sourceCols = `SourceID,Name,APIKey,Description,` + - `LastSeenAt,AlertTimeout,AlertedAt,` + - `LogAction,AlertAction` + `LastSeenAt,AlertTimeout,AlertedAt` func (db *dbal) scanSource( row interface{ Scan(...interface{}) error }, ) (s Source, err error) { err = row.Scan( &s.SourceID, &s.Name, &s.APIKey, &s.Description, - &s.LastSeenAt, &s.AlertTimeout, &s.AlertedAt, - &s.LogAction, &s.AlertAction) + &s.LastSeenAt, &s.AlertTimeout, &s.AlertedAt) if err != nil { db.logf("Failed to scan source: %v", err) } @@ -234,12 +230,12 @@ func (db *dbal) SourceList() (l []Source, err error) { return l, nil } -// Updates Description, AlertTimeout, LogAction, AlertAction. +// Updates Description and AlertTimeout. func (db *dbal) SourceUpdate(s Source) error { _, err := db.Exec(`UPDATE sources `+ - `SET Description=?,AlertTimeout=?,LogAction=?,AlertAction=? `+ + `SET Description=?,AlertTimeout=? `+ `WHERE SourceID=?`, - s.Description, s.AlertTimeout, s.LogAction, s.AlertAction, s.SourceID) + s.Description, s.AlertTimeout, s.SourceID) if err != nil { db.logf("Failed to update source %s: %v", s.Name, err) } @@ -313,7 +309,7 @@ func (db *dbal) LogList(args LogListArgs) (l []EntryListRow, err error) { qArgs := []interface{}{} query := `SELECT ` + - `l.LogID,s.Name,l.TS,l.Alert,l.Text ` + + `l.LogID,s.SourceID,s.Name,l.TS,l.Alert,l.Text ` + `FROM log l JOIN sources s ON l.SourceID=s.SourceID WHERE 1` if args.BeforeID != 0 { query += " AND LogID{{.TS.Format "2006-01-02 15:04"}} {{if .Alert}}!{{end}} - {{.SourceName}} + {{.SourceName}} {{.Text}} {{end}} diff --git a/templates/sources.html b/templates/sources.html index bb51650..a458125 100644 --- a/templates/sources.html +++ b/templates/sources.html @@ -27,14 +27,6 @@ -
  • - - -
  • -
  • - - -
  • @@ -104,12 +96,6 @@
    Alerted At
    {{.AlertedAt.Format "2006-01-02 15:04"}}
    - -
    Log Action
    -
    {{.LogAction}}
    - -
    Alert Action
    -
    {{.AlertAction}}
    @@ -145,14 +131,6 @@ -
  • - - -
  • -
  • - - -
  • diff --git a/tmpl_gen.go b/tmpl_gen.go index d13c4c1..c87b3ea 100644 --- a/tmpl_gen.go +++ b/tmpl_gen.go @@ -81,7 +81,7 @@ var tmpls = ` {{.TS.Format "2006-01-02 15:04"}} {{if .Alert}}!{{end}} - {{.SourceName}} + {{.SourceName}} {{.Text}} {{end}} @@ -123,14 +123,6 @@ var tmpls = ` -
  • - - -
  • -
  • - - -
  • @@ -200,12 +192,6 @@ var tmpls = `
    Alerted At
    {{.AlertedAt.Format "2006-01-02 15:04"}}
    - -
    Log Action
    -
    {{.LogAction}}
    - -
    Alert Action
    -
    {{.AlertAction}}
    @@ -241,14 +227,6 @@ var tmpls = ` -
  • - - -
  • -
  • - - -
  • diff --git a/types.go b/types.go index beaaccc..d9f6db5 100644 --- a/types.go +++ b/types.go @@ -15,8 +15,6 @@ type Source struct { LastSeenAt time.Time AlertTimeout int64 // In seconds. AlertedAt time.Time // Timeout alert time. - LogAction string // Override log action. - AlertAction string // Override alert action. } func (s Source) TimedOut() bool { @@ -37,6 +35,7 @@ type Entry struct { type EntryListRow struct { LogID int64 + SourceID string SourceName string TS time.Time Alert bool diff --git a/user-handlers.go b/user-handlers.go index 03444cb..192d8e6 100644 --- a/user-handlers.go +++ b/user-handlers.go @@ -223,8 +223,6 @@ func handleSourceInsert(w http.ResponseWriter, r *http.Request) { Name: r.Form.Get("Name"), Description: r.Form.Get("Description"), AlertTimeout: formGetInt(r, "AlertTimeout"), - LogAction: r.Form.Get("LogAction"), - AlertAction: r.Form.Get("AlertAction"), } if err := db.SourceInsert(&s); err != nil { @@ -271,8 +269,6 @@ func handleSourceUpdate(w http.ResponseWriter, r *http.Request) { s.Description = r.Form.Get("Description") s.AlertTimeout = formGetInt(r, "AlertTimeout") - s.LogAction = r.Form.Get("LogAction") - s.AlertAction = r.Form.Get("AlertAction") if err := db.SourceUpdate(s); err != nil { execTmpl(w, "Error", err)