Skip to content

Commit

Permalink
Merge pull request #2895 from target/status-update-refactor-pt3
Browse files Browse the repository at this point in the history
engine/statusmgr: cleanup old queries
  • Loading branch information
m17ch authored Mar 27, 2023
2 parents 22a605c + 0785a54 commit 0b5433b
Showing 1 changed file with 1 addition and 46 deletions.
47 changes: 1 addition & 46 deletions engine/statusmgr/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ import (
"database/sql"

"github.com/target/goalert/engine/processinglock"
"github.com/target/goalert/util"
)

// DB manages outgoing status updates.
type DB struct {
lock *processinglock.Lock

latestLogEntry *sql.Stmt
needsUpdate *sql.Stmt
insertMessage *sql.Stmt
updateStatus *sql.Stmt
deleteSub *sql.Stmt
cmWantsUpdates *sql.Stmt
}

// Name returns the name of the module.
Expand All @@ -32,45 +24,8 @@ func NewDB(ctx context.Context, db *sql.DB) (*DB, error) {
if err != nil {
return nil, err
}
p := &util.Prepare{DB: db, Ctx: ctx}

return &DB{
lock: lock,

cmWantsUpdates: p.P(`
select user_id, type
from user_contact_methods
where id = $1 and not disabled and enable_status_updates
`),

needsUpdate: p.P(`
select sub.id, channel_id, contact_method_id, alert_id, (select status from alerts a where a.id = sub.alert_id)
from alert_status_subscriptions sub
where sub.last_alert_status != (select status from alerts a where a.id = sub.alert_id)
limit 1
for update skip locked
`),

insertMessage: p.P(`
insert into outgoing_messages(
id,
message_type,
channel_id,
contact_method_id,
user_id,
alert_id,
alert_log_id
) values ($1, 'alert_status_update', $2, $3, $4, $5, $6)
`),

latestLogEntry: p.P(`
select id, sub_user_id from alert_logs
where alert_id = $1 and event = $2 and timestamp > now() - '1 hour'::interval
order by id desc
limit 1
`),

updateStatus: p.P(`update alert_status_subscriptions set last_alert_status = $2 where id = $1`),
deleteSub: p.P(`delete from alert_status_subscriptions where id = $1`),
}, p.Err
}, nil
}

0 comments on commit 0b5433b

Please sign in to comment.