Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

engine/statusmgr: cleanup old queries #2895

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}