Skip to content
19 changes: 9 additions & 10 deletions acceleration/internal/store/balance_notices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package store

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -49,10 +48,6 @@ func (s *Store) NoteBalance(ctx context.Context, notice BalanceNotice, delivery
if notice.Kind != NoticeBalanceLow && notice.Kind != NoticeBalanceExhausted {
return false, 0, stack.Wrap(fmt.Errorf("store: a balance notice is %s or %s, not %q", NoticeBalanceLow, NoticeBalanceExhausted, notice.Kind))
}
headers, err := json.Marshal(delivery.Headers)
if err != nil {
return false, 0, stack.Wrap(err)
}
err = s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
written, err := tx.NewInsert().Model(&notice).On("CONFLICT DO NOTHING").Exec(ctx)
if err != nil {
Expand Down Expand Up @@ -80,11 +75,15 @@ func (s *Store) NoteBalance(ctx context.Context, notice BalanceNotice, delivery
if err != nil {
return err
}
if _, err := tx.NewRaw(`
INSERT INTO connector_event_deliveries (destination_id, id, headers, body, attempts, next_attempt_at)
VALUES (?, ?, ?::jsonb, ?, 0, ?)
ON CONFLICT (destination_id, id) DO NOTHING`,
destination.ID, delivery.ID, string(headers), payload, delivery.NextAttemptAt.UTC()).Exec(ctx); err != nil {
row := EventDelivery{
DestinationID: destination.ID, ID: delivery.ID, Headers: delivery.Headers, Body: payload,
NextAttemptAt: delivery.NextAttemptAt.UTC(),
}
_, err = tx.NewInsert().Model(&row).
Column("destination_id", "id", "headers", "body", "attempts", "next_attempt_at").
On("CONFLICT (destination_id, id) DO NOTHING").
Exec(ctx)
if err != nil {
return err
}
queued++
Expand Down
26 changes: 19 additions & 7 deletions acceleration/internal/store/campaign_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ WHERE cc.campaign_id = ? AND EXISTS (SELECT 1 FROM campaign_attempts ca
Outcome string
Count int
}
err = s.db.NewRaw(`SELECT outcome, count(*) AS count FROM campaign_contacts
WHERE campaign_id = ? AND stage = ? GROUP BY outcome`, campaignID, ContactCompleted).Scan(ctx, &outcomes)
err = s.db.NewSelect().Model((*Contact)(nil)).
Column("cc.outcome").
ColumnExpr("count(*) AS count").
Where("cc.campaign_id = ? AND cc.stage = ?", campaignID, ContactCompleted).
Group("cc.outcome").
Scan(ctx, &outcomes)
if err != nil {
return CampaignAnalytics{}, stack.Wrap(fmt.Errorf("store: count outcomes: %w", err))
}
Expand All @@ -85,8 +89,12 @@ WHERE campaign_id = ? AND stage = ? GROUP BY outcome`, campaignID, ContactComple
CallOutcome string
Count int
}
err = s.db.NewRaw(`SELECT call_outcome, count(*) AS count FROM campaign_attempts
WHERE campaign_id = ? AND call_outcome IS NOT NULL GROUP BY call_outcome`, campaignID).Scan(ctx, &calls)
err = s.db.NewSelect().Model((*CampaignAttempt)(nil)).
Column("ca.call_outcome").
ColumnExpr("count(*) AS count").
Where("ca.campaign_id = ? AND ca.call_outcome IS NOT NULL", campaignID).
Group("ca.call_outcome").
Scan(ctx, &calls)
if err != nil {
return CampaignAnalytics{}, stack.Wrap(fmt.Errorf("store: count call outcomes: %w", err))
}
Expand Down Expand Up @@ -119,9 +127,13 @@ func (s *Store) ContactStages(ctx context.Context, campaignIDs []string) (map[st
Stage string
Count int
}
err := s.db.NewRaw(`SELECT campaign_id, CASE WHEN stage = ? THEN ? ELSE stage END AS stage, count(*) AS count
FROM campaign_contacts WHERE campaign_id IN (?) GROUP BY 1, 2`,
AwaitingOutcome, InProgress, bun.In(campaignIDs)).Scan(ctx, &rows)
err := s.db.NewSelect().Model((*Contact)(nil)).
Column("cc.campaign_id").
ColumnExpr("CASE WHEN cc.stage = ? THEN ? ELSE cc.stage END AS stage", AwaitingOutcome, InProgress).
ColumnExpr("count(*) AS count").
Where("cc.campaign_id IN (?)", bun.In(campaignIDs)).
GroupExpr("1, 2").
Scan(ctx, &rows)
if err != nil {
return nil, stack.Wrap(fmt.Errorf("store: count contact stages: %w", err))
}
Expand Down
36 changes: 21 additions & 15 deletions acceleration/internal/store/campaign_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,21 @@ func (s *Store) TakeOverExpired(ctx context.Context, owner string, now time.Time
return nil, nil
}

expired := s.db.NewSelect().Model((*Contact)(nil)).
Column("cc.id").
Where("cc.stage = ? AND cc.leased_until < ?", InProgress, now).
Where("NOT EXISTS (SELECT 1 FROM campaigns cp WHERE cp.id = cc.campaign_id AND cp.move_paused)").
Order("cc.leased_until").
Limit(limit).
For("UPDATE SKIP LOCKED")
var contacts []Contact
err := s.db.NewRaw(`WITH expired AS (
SELECT id FROM campaign_contacts cc
WHERE stage = ? AND leased_until < ?
AND NOT EXISTS (SELECT 1 FROM campaigns cp WHERE cp.id = cc.campaign_id AND cp.move_paused)
ORDER BY leased_until
LIMIT ?
FOR UPDATE SKIP LOCKED
)
UPDATE campaign_contacts cc SET leased_by = ?, leased_until = ?
FROM expired WHERE cc.id = expired.id
RETURNING cc.*`, InProgress, now, limit, owner, now.Add(lease)).Scan(ctx, &contacts)
err := s.db.NewUpdate().With("expired", expired).Model((*Contact)(nil)).
TableExpr("expired").
Set("leased_by = ?", owner).
Set("leased_until = ?", now.Add(lease)).
Where("cc.id = expired.id").
Returning("cc.*").
Scan(ctx, &contacts)
if err != nil {
return nil, stack.Wrap(fmt.Errorf("store: take over expired leases: %w", err))
}
Expand Down Expand Up @@ -457,10 +460,13 @@ func (s *Store) PutOffClassification(ctx context.Context, owner, attemptID strin
}
var released int64
err := s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
result, err := tx.ExecContext(ctx, `UPDATE campaign_contacts cc SET leased_by = '', leased_until = NULL
FROM campaign_attempts ca
WHERE ca.id = ? AND ca.classify_state = ? AND cc.id = ca.contact_id AND cc.stage = ? AND cc.leased_by = ?`,
attemptID, classifyPending, AwaitingOutcome, owner)
result, err := tx.NewUpdate().Model((*Contact)(nil)).
TableExpr("campaign_attempts AS ca").
Set("leased_by = ''").
Set("leased_until = NULL").
Where("ca.id = ? AND ca.classify_state = ?", attemptID, classifyPending).
Where("cc.id = ca.contact_id AND cc.stage = ? AND cc.leased_by = ?", AwaitingOutcome, owner).
Exec(ctx)
if err != nil {
return fmt.Errorf("store: release a contact to classify later: %w", err)
}
Expand Down
46 changes: 26 additions & 20 deletions acceleration/internal/store/channel_inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ func queueChannelInbound(ctx context.Context, tx bun.Tx, message ChannelInbound)
if _, err := tx.NewRaw("SELECT 1 FROM channel_threads WHERE channel_id = ? FOR NO KEY UPDATE", message.ChannelID).Exec(ctx); err != nil {
return false, stack.Wrap(fmt.Errorf("store: queue channel inbound: %w", err))
}
// Not the insert builder: bun writes a JSON \u0000 as the text \\u0000, and a delivery jsonb
// cannot hold must fail here, so it stays unclaimed and the provider's retry takes it again.
media, err := json.Marshal(message.Media)
if err != nil {
return false, stack.Wrap(err)
Expand Down Expand Up @@ -309,13 +311,13 @@ func (s *Store) claimChannelInbound(ctx context.Context, query string, now time.
// found is false when no message is queued at all.
func (s *Store) NextChannelInboundAt(ctx context.Context) (next time.Time, found bool, err error) {
var at sql.NullTime
if err := s.db.NewRaw(`
SELECT min(head.next_attempt_at) FROM (
SELECT DISTINCT ON (channel_id) next_attempt_at
FROM channel_thread_inbound
WHERE state = 'queued'
ORDER BY channel_id, seq
) AS head`).Scan(ctx, &at); err != nil {
head := s.db.NewSelect().Model((*ChannelInbound)(nil)).
DistinctOn("cti.channel_id").
Column("cti.next_attempt_at").
Where("cti.state = ?", ChannelInboundQueued).
Order("cti.channel_id", "cti.seq")
err = s.db.NewSelect().TableExpr("(?) AS head", head).ColumnExpr("min(head.next_attempt_at)").Scan(ctx, &at)
if err != nil {
return time.Time{}, false, stack.Wrap(fmt.Errorf("store: next channel inbound: %w", err))
}
return at.Time, at.Valid, nil
Expand Down Expand Up @@ -357,8 +359,11 @@ func (s *Store) FailChannelInbound(ctx context.Context, claimed ChannelInbound,
if forward(ctx) != nil {
return nil
}
if _, err := tx.NewRaw("UPDATE channel_thread_inbound SET forward = jsonb_set(forward, '{forwarded}', 'true') WHERE seq = ?",
claimed.Seq).Exec(ctx); err != nil {
_, err = tx.NewUpdate().Model((*ChannelInbound)(nil)).
Set("forward = jsonb_set(forward, '{forwarded}', 'true')").
Where("seq = ?", claimed.Seq).
Exec(ctx)
if err != nil {
return fmt.Errorf("store: fail channel inbound: %w", err)
}
return nil
Expand All @@ -383,13 +388,14 @@ func lockChannelInboundDelivery(ctx context.Context, tx bun.Tx, claimed ChannelI
// it: one with the same provider id for the delivery, or with none and the same body, as the
// event destinations key a forward (eventforward.deliveryID).
func channelInboundDeliveryForwarded(ctx context.Context, tx bun.Tx, claimed ChannelInbound) (bool, error) {
var forwarded bool
if err := tx.NewRaw(`
SELECT EXISTS (
SELECT 1 FROM channel_thread_inbound
WHERE state = 'failed' AND forward->>'forwarded' = 'true' AND customer_id = ? AND connector_id = ?
AND forward->>'id' = ? AND (? <> '' OR raw = ?)
)`, claimed.CustomerID, claimed.ConnectorID, claimed.Forward.ID, claimed.Forward.ID, claimed.Raw).Scan(ctx, &forwarded); err != nil {
forwarded, err := tx.NewSelect().Model((*ChannelInbound)(nil)).
Where("state = ?", ChannelInboundFailed).
Where("forward->>'forwarded' = 'true'").
Where("customer_id = ? AND connector_id = ?", claimed.CustomerID, claimed.ConnectorID).
Where("forward->>'id' = ?", claimed.Forward.ID).
Where("(? <> '' OR raw = ?)", claimed.Forward.ID, claimed.Raw).
Exists(ctx)
if err != nil {
return false, fmt.Errorf("store: fail channel inbound: %w", err)
}
return forwarded, nil
Expand Down Expand Up @@ -452,10 +458,10 @@ func updateClaimedChannelInbound(ctx context.Context, claimed ChannelInbound, up
// PruneChannelInbound deletes the rows of written messages finished a day before now, and of
// given-up ones 30 days before, and returns how many it deleted.
func (s *Store) PruneChannelInbound(ctx context.Context, now time.Time) (int, error) {
result, err := s.db.NewRaw(`
DELETE FROM channel_thread_inbound
WHERE (state = 'done' AND finished_at < ?) OR (state = 'failed' AND finished_at < ?)`,
now.Add(-channelInboundDoneKeep).UTC(), now.Add(-channelInboundFailedKeep).UTC()).Exec(ctx)
result, err := s.db.NewDelete().Model((*ChannelInbound)(nil)).
Where("(state = ? AND finished_at < ?) OR (state = ? AND finished_at < ?)",
ChannelInboundDone, now.Add(-channelInboundDoneKeep).UTC(), ChannelInboundFailed, now.Add(-channelInboundFailedKeep).UTC()).
Exec(ctx)
if err != nil {
return 0, stack.Wrap(fmt.Errorf("store: prune channel inbound: %w", err))
}
Expand Down
58 changes: 34 additions & 24 deletions acceleration/internal/store/chat_inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ func queueChatInbound(ctx context.Context, db bun.IDB, delivery ChatInbound) (bo
if delivery.StreamApp == 0 {
return false, stack.Wrap(errors.New("store: a Stream app is required: a message queued without one would be queued again once the app is known"))
}
result, err := db.NewRaw(`
INSERT INTO chat_inbound (stream_app, message_id, customer_id, deployment, api_key, body, next_attempt_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (stream_app, message_id) DO NOTHING`,
delivery.StreamApp, delivery.MessageID, delivery.CustomerID, delivery.Deployment, delivery.APIKey, delivery.Body,
delivery.NextAttemptAt.UTC()).Exec(ctx)
delivery.NextAttemptAt = delivery.NextAttemptAt.UTC()
result, err := db.NewInsert().Model(&delivery).
Column("stream_app", "message_id", "customer_id", "deployment", "api_key", "body", "next_attempt_at").
On("CONFLICT (stream_app, message_id) DO NOTHING").
Exec(ctx)
if err != nil {
return false, stack.Wrap(fmt.Errorf("store: queue chat inbound: %w", err))
}
Expand Down Expand Up @@ -164,7 +163,7 @@ func (s *Store) claimChatInbound(ctx context.Context, query string, now time.Tim
// ChatInboundBySeq returns the message queued as seq, in whatever state it is.
func (s *Store) ChatInboundBySeq(ctx context.Context, seq int64) (ChatInbound, error) {
var delivery ChatInbound
err := s.db.NewRaw("SELECT "+chatInboundColumns+" FROM chat_inbound AS cd WHERE cd.seq = ?", seq).Scan(ctx, &delivery)
err := s.db.NewSelect().Model(&delivery).Where("cd.seq = ?", seq).Scan(ctx)
if errors.Is(err, sql.ErrNoRows) {
return ChatInbound{}, stack.Wrap(fmt.Errorf("%w: %d", ErrNoChatInbound, seq))
}
Expand All @@ -178,7 +177,11 @@ func (s *Store) ChatInboundBySeq(ctx context.Context, seq int64) (ChatInbound, e
// their lease. found is false when no message is queued at all.
func (s *Store) NextChatInboundAt(ctx context.Context) (next time.Time, found bool, err error) {
var at sql.NullTime
if err := s.db.NewRaw("SELECT min(next_attempt_at) FROM chat_inbound WHERE state = 'queued'").Scan(ctx, &at); err != nil {
err = s.db.NewSelect().Model((*ChatInbound)(nil)).
ColumnExpr("min(next_attempt_at)").
Where("state = ?", ChatInboundQueued).
Scan(ctx, &at)
if err != nil {
return time.Time{}, false, stack.Wrap(fmt.Errorf("store: next chat inbound: %w", err))
}
return at.Time, at.Valid, nil
Expand Down Expand Up @@ -239,10 +242,12 @@ func updateClaimedChatInbound(ctx context.Context, claimed ChatInbound, states [
// nothing is queued under streamApp and messageID: there is nothing to answer then. The lease
// is left as it is, for the worker holding it to finish the row (FinishChatInbound).
func (s *Store) StartChatInboundAnswer(ctx context.Context, streamApp int64, messageID string, at time.Time) (started bool, err error) {
result, err := s.db.NewRaw(`
UPDATE chat_inbound SET state = 'done', finished_at = ?
WHERE stream_app = ? AND message_id = ? AND state = 'queued'`,
at.UTC(), streamApp, messageID).Exec(ctx)
result, err := s.db.NewUpdate().Model((*ChatInbound)(nil)).
Set("state = ?", ChatInboundDone).
Set("finished_at = ?", at.UTC()).
Where("stream_app = ? AND message_id = ?", streamApp, messageID).
Where("state = ?", ChatInboundQueued).
Exec(ctx)
if err != nil {
return false, stack.Wrap(fmt.Errorf("store: start chat inbound answer: %w", err))
}
Expand All @@ -257,10 +262,12 @@ WHERE stream_app = ? AND message_id = ? AND state = 'queued'`,
// messageID said, so a later try of it hands that on rather than hearing it again. What was
// kept first stays.
func (s *Store) KeepChatInboundHeard(ctx context.Context, streamApp int64, messageID, heard string) error {
if _, err := s.db.NewRaw(`
UPDATE chat_inbound SET heard = ?
WHERE stream_app = ? AND message_id = ? AND heard IS NULL`,
heard, streamApp, messageID).Exec(ctx); err != nil {
_, err := s.db.NewUpdate().Model((*ChatInbound)(nil)).
Set("heard = ?", heard).
Where("stream_app = ? AND message_id = ?", streamApp, messageID).
Where("heard IS NULL").
Exec(ctx)
if err != nil {
return stack.Wrap(fmt.Errorf("store: keep chat inbound heard: %w", err))
}
return nil
Expand All @@ -270,10 +277,13 @@ WHERE stream_app = ? AND message_id = ? AND heard IS NULL`,
// and that nothing then took, such as a dispatch worker that was not connected, so a later try
// answers it. The lease is left as it is, for the worker holding it to retry the row.
func (s *Store) ReleaseChatInboundAnswer(ctx context.Context, streamApp int64, messageID string) error {
if _, err := s.db.NewRaw(`
UPDATE chat_inbound SET state = 'queued', finished_at = NULL
WHERE stream_app = ? AND message_id = ? AND state = 'done'`,
streamApp, messageID).Exec(ctx); err != nil {
_, err := s.db.NewUpdate().Model((*ChatInbound)(nil)).
Set("state = ?", ChatInboundQueued).
Set("finished_at = NULL").
Where("stream_app = ? AND message_id = ?", streamApp, messageID).
Where("state = ?", ChatInboundDone).
Exec(ctx)
if err != nil {
return stack.Wrap(fmt.Errorf("store: release chat inbound answer: %w", err))
}
return nil
Expand All @@ -282,10 +292,10 @@ WHERE stream_app = ? AND message_id = ? AND state = 'done'`,
// PruneChatInbound deletes the rows of answered messages finished a day before now, and of
// given-up ones 30 days before, and returns how many it deleted.
func (s *Store) PruneChatInbound(ctx context.Context, now time.Time) (int, error) {
result, err := s.db.NewRaw(`
DELETE FROM chat_inbound
WHERE (state = 'done' AND finished_at < ?) OR (state = 'failed' AND finished_at < ?)`,
now.Add(-chatInboundDoneKeep).UTC(), now.Add(-chatInboundFailedKeep).UTC()).Exec(ctx)
result, err := s.db.NewDelete().Model((*ChatInbound)(nil)).
Where("(state = ? AND finished_at < ?) OR (state = ? AND finished_at < ?)",
ChatInboundDone, now.Add(-chatInboundDoneKeep).UTC(), ChatInboundFailed, now.Add(-chatInboundFailedKeep).UTC()).
Exec(ctx)
if err != nil {
return 0, stack.Wrap(fmt.Errorf("store: prune chat inbound: %w", err))
}
Expand Down
Loading
Loading