Skip to content

Commit 4e033e3

Browse files
committed
Replace "top hosts" category with "most mouths fed"
1 parent ee28d8c commit 4e033e3

5 files changed

Lines changed: 36 additions & 30 deletions

File tree

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ FROM golang:1.22-bookworm as builder
22
WORKDIR /go/src/bolt
33
COPY . .
44

5-
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o /bolt cmd/main.go && chmod +x /bolt
5+
# The "json1" build tag enables JSON SQL functions in go-sqlite3
6+
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -tags json1 -o /bolt cmd/main.go && chmod +x /bolt
67

78
FROM gcr.io/distroless/base
89
COPY --from=builder /bolt /bolt
20.1 KB
Loading

order/order.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ type VenueOrderCount struct {
2727
LastCreatedAt string `db:"last_created_at"` // The driver returns this column as a string
2828
}
2929

30-
type HostOrderCount struct {
31-
HostId string `db:"host_id"`
32-
HostName string `db:"host"`
33-
OrderCount int `db:"order_count"`
34-
LastCreatedAt string `db:"last_created_at"` // The driver returns this column as a string
30+
type MouthsFedCount struct {
31+
HostId string `db:"host_id"`
32+
HostName string `db:"host"`
33+
MouthsFedCount int `db:"mouths_fed_count"`
34+
LastCreatedAt string `db:"last_created_at"` // The driver returns this column as a string
3535
}
3636

3737
type Order struct {
@@ -53,6 +53,6 @@ type Order struct {
5353
type Store interface {
5454
SaveOrder(ctx context.Context, order *Order) error
5555
GetVenuesWithMostOrders(startTime time.Time, limit uint64, channelId string, filteredVenueIds []string) ([]VenueOrderCount, error)
56-
GetHostsWithMostOrders(startTime time.Time, limit uint64, channelId string, filteredHostIds []string) ([]HostOrderCount, error)
56+
GetHostsWithMostMouthsFed(startTime time.Time, limit uint64, channelId string, filteredHostIds []string) ([]MouthsFedCount, error)
5757
GetActiveChannelIds(lastDateConsideredActive time.Time) ([]string, error)
5858
}

service/digest.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,24 @@ func buildTopVenuesMessageBlocks(monthlyTopVenues []order.VenueOrderCount, month
6969
return topVenuesBlocks, nil
7070
}
7171

72-
func buildTopHostsMessageBlocks(monthlyTopHosts []order.HostOrderCount, monthlyTopHostsTotalCounts []order.HostOrderCount) ([]slack.Block, error) {
73-
hostIdToTotalOrderCount := make(map[string]int)
72+
func buildTopHostsMessageBlocks(monthlyTopHosts []order.MouthsFedCount, monthlyTopHostsTotalCounts []order.MouthsFedCount) ([]slack.Block, error) {
73+
hostIdToTotalMouthsFedCount := make(map[string]int)
7474
for _, host := range monthlyTopHostsTotalCounts {
75-
hostIdToTotalOrderCount[host.HostId] = host.OrderCount
75+
hostIdToTotalMouthsFedCount[host.HostId] = host.MouthsFedCount
7676
}
7777

7878
topHostsHeader := slack.NewSectionBlock(
7979
nil,
8080
[]*slack.TextBlockObject{
81-
slack.NewTextBlockObject("mrkdwn", ":star: *Top hosts*", false, false),
81+
slack.NewTextBlockObject("mrkdwn", ":spoon: *Most mouths fed*", false, false),
8282
slack.NewTextBlockObject("mrkdwn", fmt.Sprintf("*%s (Total)*", dateOneMonthAgo.Month().String()), false, false),
8383
},
8484
nil,
8585
)
8686

8787
topHostsRows := make([]*slack.TextBlockObject, 0, len(monthlyTopHosts)*2)
8888
for i, host := range monthlyTopHosts {
89-
totalOrderCount, hostExists := hostIdToTotalOrderCount[host.HostId]
89+
totalMouthsFedCount, hostExists := hostIdToTotalMouthsFedCount[host.HostId]
9090
if !hostExists {
9191
return nil, fmt.Errorf("host %s (%s) is not in monthlyTopHostsTotalCounts", host.HostId, host.HostName)
9292
}
@@ -97,11 +97,11 @@ func buildTopHostsMessageBlocks(monthlyTopHosts []order.HostOrderCount, monthlyT
9797
}
9898

9999
leftColumnString := fmt.Sprintf("%s %s%s%s", positionEmoji, UnicodeLeftToRightMark, host.HostName, UnicodeLeftToRightMark)
100-
if host.OrderCount == totalOrderCount {
100+
if host.MouthsFedCount == totalMouthsFedCount {
101101
leftColumnString += " :new:"
102102
}
103103

104-
rightColumnString := fmt.Sprintf("%d (%d)", host.OrderCount, totalOrderCount)
104+
rightColumnString := fmt.Sprintf("%d (%d)", host.MouthsFedCount, totalMouthsFedCount)
105105

106106
topHostsRows = append(topHostsRows,
107107
slack.NewTextBlockObject("mrkdwn", leftColumnString, false, false),
@@ -125,10 +125,10 @@ func venueOrderCountsToVenueIds(venueOrderCounts []order.VenueOrderCount) []stri
125125
return venueIds
126126
}
127127

128-
func hostOrderCountsToHostIds(hostOrderCounts []order.HostOrderCount) []string {
128+
func mouthsFedCountsToHostIds(mouthsFedCounts []order.MouthsFedCount) []string {
129129
var hostIds []string
130-
for _, hostOrderCount := range hostOrderCounts {
131-
hostIds = append(hostIds, hostOrderCount.HostId)
130+
for _, mouthsFedCount := range mouthsFedCounts {
131+
hostIds = append(hostIds, mouthsFedCount.HostId)
132132
}
133133
return hostIds
134134
}
@@ -149,13 +149,13 @@ func (h *Service) getTopVenuesMessageBlocks(channelId string) ([]slack.Block, er
149149
}
150150

151151
func (h *Service) getTopHostsMessageBlocks(channelId string) ([]slack.Block, error) {
152-
monthlyTopHosts, err := h.orderStore.GetHostsWithMostOrders(dateOneMonthAgo, numberOfDigestRows, channelId, []string{})
152+
monthlyTopHosts, err := h.orderStore.GetHostsWithMostMouthsFed(dateOneMonthAgo, numberOfDigestRows, channelId, []string{})
153153
if err != nil {
154154
return nil, fmt.Errorf("error getting top hosts of the last month: %w", err)
155155
}
156156

157-
monthlyTopHostIds := hostOrderCountsToHostIds(monthlyTopHosts)
158-
monthlyTopHostsTotalCounts, err := h.orderStore.GetHostsWithMostOrders(time.Time{}, numberOfDigestRows, channelId, monthlyTopHostIds)
157+
monthlyTopHostIds := mouthsFedCountsToHostIds(monthlyTopHosts)
158+
monthlyTopHostsTotalCounts, err := h.orderStore.GetHostsWithMostMouthsFed(time.Time{}, numberOfDigestRows, channelId, monthlyTopHostIds)
159159
if err != nil {
160160
return nil, fmt.Errorf("error getting top hosts of all time: %w", err)
161161
}

storage/db/orders.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,22 @@ func (d *DBStore) GetVenuesWithMostOrders(startTime time.Time, limit uint64, cha
7272
return venueOrderCounts, nil
7373
}
7474

75-
func (d *DBStore) GetHostsWithMostOrders(startTime time.Time, limit uint64, channelId string, filteredHostIds []string) ([]order.HostOrderCount, error) {
76-
query := sq.Select("host_id", "host", "COUNT(*) as order_count", "MAX(created_at) as last_created_at").
77-
From("orders").
75+
func (d *DBStore) GetHostsWithMostMouthsFed(startTime time.Time, limit uint64, channelId string, filteredHostIds []string) ([]order.MouthsFedCount, error) {
76+
subquery := sq.Select("orders.*", "json_each.value as participant").
77+
From("orders, json_each(orders.participants)").
78+
Where(sq.Gt{"json_extract(participant, '$.amount')": 0}).
79+
Where("json_extract(participant, '$.name') != host").
7880
Where(sq.Eq{"receiver": channelId}).
7981
Where(sq.Eq{"status": order.StatusDone}).
80-
Where(sq.GtOrEq{"created_at": startTime}).
81-
GroupBy("host_id").
82-
OrderBy("order_count DESC", "last_created_at ASC")
82+
Where(sq.GtOrEq{"created_at": startTime})
8383
if len(filteredHostIds) > 0 {
84-
query = query.Where(sq.Eq{"host_id": filteredHostIds})
84+
subquery = subquery.Where(sq.Eq{"host_id": filteredHostIds})
8585
}
86+
87+
query := sq.Select("host_id", "host", "COUNT(*) as mouths_fed_count", "MAX(created_at) as last_created_at").
88+
FromSelect(subquery, "extracted_participants").
89+
GroupBy("host_id").
90+
OrderBy("mouths_fed_count DESC", "last_created_at ASC")
8691
if limit > 0 {
8792
query = query.Limit(limit)
8893
}
@@ -92,13 +97,13 @@ func (d *DBStore) GetHostsWithMostOrders(startTime time.Time, limit uint64, chan
9297
return nil, fmt.Errorf("building SELECT query: %w", err)
9398
}
9499

95-
var hostOrderCounts []order.HostOrderCount
96-
err = d.db.Select(&hostOrderCounts, sql, args...)
100+
var mouthsFedCount []order.MouthsFedCount
101+
err = d.db.Select(&mouthsFedCount, sql, args...)
97102
if err != nil {
98103
return nil, fmt.Errorf("executing SELECT query: %w", err)
99104
}
100105

101-
return hostOrderCounts, nil
106+
return mouthsFedCount, nil
102107
}
103108

104109
func (d *DBStore) GetActiveChannelIds(lastDateConsideredActive time.Time) ([]string, error) {

0 commit comments

Comments
 (0)