Skip to content

Commit

Permalink
Fix broken sorting lists by subscriber_count. Closes #2151.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Dec 4, 2024
1 parent 7bfbd6a commit 5c0de6e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
5 changes: 0 additions & 5 deletions internal/core/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ func (c *Core) QueryLists(searchStr, typ, optin string, tags []string, orderBy,
if l.Tags == nil {
out[i].Tags = []string{}
}

// Total counts.
for _, c := range l.SubscriberCounts {
out[i].SubscriberCount += c
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ type List struct {
Optin string `db:"optin" json:"optin"`
Tags pq.StringArray `db:"tags" json:"tags"`
Description string `db:"description" json:"description"`
SubscriberCount int `db:"-" json:"subscriber_count"`
SubscriberCount int `db:"subscriber_count" json:"subscriber_count"`
SubscriberCounts StringIntMap `db:"subscriber_statuses" json:"subscriber_statuses"`
SubscriberID int `db:"subscriber_id" json:"-"`

Expand Down
7 changes: 4 additions & 3 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ SELECT * FROM lists WHERE (CASE WHEN $1 = '' THEN 1=1 ELSE type=$1::list_type EN

-- name: query-lists
WITH ls AS (
SELECT COUNT(*) OVER () AS total, lists.* FROM lists WHERE
SELECT COUNT(*) OVER () AS total, lists.* FROM lists WHERE
CASE
WHEN $1 > 0 THEN id = $1
WHEN $2 != '' THEN uuid = $2::UUID
Expand All @@ -451,11 +451,12 @@ WITH ls AS (
statuses AS (
SELECT
list_id,
COALESCE(JSONB_OBJECT_AGG(status, subscriber_count) FILTER (WHERE status IS NOT NULL), '{}') AS subscriber_statuses
COALESCE(JSONB_OBJECT_AGG(status, subscriber_count) FILTER (WHERE status IS NOT NULL), '{}') AS subscriber_statuses,
SUM(subscriber_count) AS subscriber_count
FROM mat_list_subscriber_stats
GROUP BY list_id
)
SELECT ls.*, COALESCE(ss.subscriber_statuses, '{}') AS subscriber_statuses
SELECT ls.*, COALESCE(ss.subscriber_statuses, '{}') AS subscriber_statuses, COALESCE(ss.subscriber_count, 0) AS subscriber_count
FROM ls LEFT JOIN statuses ss ON (ls.id = ss.list_id) ORDER BY %order%;

-- name: get-lists-by-optin
Expand Down

0 comments on commit 5c0de6e

Please sign in to comment.