@@ -10,6 +10,7 @@ import (
10
10
"github.com/nyaruka/mailroom/core/models"
11
11
"github.com/nyaruka/mailroom/runtime"
12
12
"github.com/nyaruka/mailroom/web"
13
+ "github.com/nyaruka/null/v3"
13
14
dto "github.com/prometheus/client_model/go"
14
15
"github.com/prometheus/common/expfmt"
15
16
"google.golang.org/protobuf/proto"
@@ -91,19 +92,19 @@ func calculateGroupCounts(ctx context.Context, rt *runtime.Runtime, org *models.
91
92
}
92
93
93
94
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
95
96
FROM channels_channel ch
96
97
LEFT OUTER JOIN channels_channelcount c ON c.channel_id = ch.id
97
98
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 ;`
99
100
100
101
type channelCountRow struct {
101
102
ID models.ChannelID `db:"id"`
102
103
UUID assets.ChannelUUID `db:"uuid"`
103
104
Name string `db:"name"`
104
105
Role string `db:"role"`
105
106
ChannelType string `db:"channel_type"`
106
- CountType * string `db:"count_type "`
107
+ Scope null. String `db:"scope "`
107
108
Count int64 `db:"count"`
108
109
}
109
110
@@ -116,6 +117,13 @@ type channelStats struct {
116
117
Counts map [string ]int64
117
118
}
118
119
120
+ var channelCountScopeToType = map [string ]string {
121
+ "text:in" : "IM" ,
122
+ "text:out" : "OM" ,
123
+ "voice:in" : "IV" ,
124
+ "voice:out" : "OV" ,
125
+ }
126
+
119
127
func calculateChannelCounts (ctx context.Context , rt * runtime.Runtime , org * models.Org ) (* dto.MetricFamily , error ) {
120
128
rows , err := rt .DB .QueryxContext (ctx , channelCountsSQL , org .ID ())
121
129
if err != nil {
@@ -162,9 +170,8 @@ func calculateChannelCounts(ctx context.Context, rt *runtime.Runtime, org *model
162
170
}
163
171
}
164
172
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
168
175
}
169
176
}
170
177
0 commit comments