Skip to content

Commit 809a3d6

Browse files
committed
Fix prometheus endpoint to match channel count changes
1 parent 456c412 commit 809a3d6

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

testsuite/testdata/postgres.dump

-2.33 KB
Binary file not shown.

web/org/metrics.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/nyaruka/mailroom/core/models"
1111
"github.com/nyaruka/mailroom/runtime"
1212
"github.com/nyaruka/mailroom/web"
13+
"github.com/nyaruka/null/v3"
1314
dto "github.com/prometheus/client_model/go"
1415
"github.com/prometheus/common/expfmt"
1516
"google.golang.org/protobuf/proto"
@@ -91,19 +92,19 @@ func calculateGroupCounts(ctx context.Context, rt *runtime.Runtime, org *models.
9192
}
9293

9394
const channelCountsSQL = `
94-
SELECT ch.id, ch.uuid, ch.name, ch.role, ch.channel_type, c.count_type, COALESCE(SUM(c.count), 0) as count
95+
SELECT ch.id, ch.uuid, ch.name, ch.role, ch.channel_type, c.scope, COALESCE(SUM(c.count), 0) as count
9596
FROM channels_channel ch
9697
LEFT OUTER JOIN channels_channelcount c ON c.channel_id = ch.id
9798
WHERE ch.org_id = $1 AND ch.is_active = TRUE
98-
GROUP BY ch.id, c.count_type;`
99+
GROUP BY ch.id, c.scope;`
99100

100101
type channelCountRow struct {
101102
ID models.ChannelID `db:"id"`
102103
UUID assets.ChannelUUID `db:"uuid"`
103104
Name string `db:"name"`
104105
Role string `db:"role"`
105106
ChannelType string `db:"channel_type"`
106-
CountType *string `db:"count_type"`
107+
Scope null.String `db:"scope"`
107108
Count int64 `db:"count"`
108109
}
109110

@@ -116,6 +117,13 @@ type channelStats struct {
116117
Counts map[string]int64
117118
}
118119

120+
var channelCountScopeToType = map[string]string{
121+
"text:in": "IM",
122+
"text:out": "OM",
123+
"voice:in": "IV",
124+
"voice:out": "OV",
125+
}
126+
119127
func calculateChannelCounts(ctx context.Context, rt *runtime.Runtime, org *models.Org) (*dto.MetricFamily, error) {
120128
rows, err := rt.DB.QueryxContext(ctx, channelCountsSQL, org.ID())
121129
if err != nil {
@@ -162,9 +170,8 @@ func calculateChannelCounts(ctx context.Context, rt *runtime.Runtime, org *model
162170
}
163171
}
164172

165-
// set our count if we have one and it isn't a channel log count
166-
if row.CountType != nil {
167-
channel.Counts[*row.CountType] = row.Count
173+
if row.Scope != "" {
174+
channel.Counts[channelCountScopeToType[string(row.Scope)]] = row.Count
168175
}
169176
}
170177

0 commit comments

Comments
 (0)