Skip to content

Commit

Permalink
Fix metrics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Mar 29, 2022
1 parent 799dea8 commit b517840
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 55 deletions.
7 changes: 6 additions & 1 deletion testsuite/testdata/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ func InsertContact(db *sqlx.DB, org *Org, uuid flows.ContactUUID, name string, l

// InsertContactGroup inserts a contact group
func InsertContactGroup(db *sqlx.DB, org *Org, uuid assets.GroupUUID, name, query string) *Group {
groupType := "M"
if query != "" {
groupType = "Q"
}

var id models.GroupID
must(db.Get(&id,
`INSERT INTO contacts_contactgroup(uuid, org_id, group_type, name, query, status, is_system, is_active, created_by_id, created_on, modified_by_id, modified_on)
VALUES($1, $2, 'U', $3, $4, 'R', FALSE, TRUE, 1, NOW(), 1, NOW()) RETURNING id`, uuid, org.ID, name, null.String(query),
VALUES($1, $2, $3, $4, $5, 'R', FALSE, TRUE, 1, NOW(), 1, NOW()) RETURNING id`, uuid, org.ID, groupType, name, null.String(query),
))
return &Group{id, uuid}
}
Expand Down
80 changes: 26 additions & 54 deletions web/org/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,18 @@ func init() {
}

const groupCountsSQL = `
SELECT
g.id AS id,
g.name AS name,
g.uuid AS uuid,
g.group_type AS group_type,
COALESCE(SUM(c.count), 0) AS count
FROM
contacts_contactgroup g
LEFT OUTER JOIN
contacts_contactgroupcount c
ON
c.group_id = g.id
WHERE
g.org_id = $1 AND
g.is_active = TRUE
GROUP BY
g.id;
`
SELECT g.id, g.name, g.uuid, g.is_system, COALESCE(SUM(c.count), 0) AS count
FROM contacts_contactgroup g
LEFT OUTER JOIN contacts_contactgroupcount c ON c.group_id = g.id
WHERE g.org_id = $1 AND g.is_active = TRUE
GROUP BY g.id;`

type groupCountRow struct {
ID models.GroupID `db:"id"`
Name string `db:"name"`
UUID assets.GroupUUID `db:"uuid"`
Type string `db:"group_type"`
Count int64 `db:"count"`
ID models.GroupID `db:"id"`
Name string `db:"name"`
UUID assets.GroupUUID `db:"uuid"`
IsSystem bool `db:"is_system"`
Count int64 `db:"count"`
}

func calculateGroupCounts(ctx context.Context, rt *runtime.Runtime, org *models.OrgReference) (*dto.MetricFamily, error) {
Expand All @@ -70,26 +57,26 @@ func calculateGroupCounts(ctx context.Context, rt *runtime.Runtime, org *models.
}

groupType := "user"
if row.Type != "U" {
if row.IsSystem {
groupType = "system"
}

family.Metric = append(family.Metric,
&dto.Metric{
Label: []*dto.LabelPair{
&dto.LabelPair{
{
Name: proto.String("group_name"),
Value: proto.String(row.Name),
},
&dto.LabelPair{
{
Name: proto.String("group_uuid"),
Value: proto.String(string(row.UUID)),
},
&dto.LabelPair{
{
Name: proto.String("group_type"),
Value: proto.String(groupType),
},
&dto.LabelPair{
{
Name: proto.String("org"),
Value: proto.String(org.Name),
},
Expand All @@ -105,26 +92,11 @@ func calculateGroupCounts(ctx context.Context, rt *runtime.Runtime, org *models.
}

const channelCountsSQL = `
SELECT
ch.id AS id,
ch.uuid AS uuid,
ch.name AS name,
ch.role AS role,
ch.channel_type AS channel_type,
c.count_type AS count_type,
COALESCE(SUM(c.count), 0) as count
FROM
channels_channel ch
LEFT OUTER JOIN
channels_channelcount c
ON
c.channel_id = ch.id
WHERE
ch.org_id = $1 AND
ch.is_active = TRUE
GROUP BY
(ch.id, c.count_type);
`
SELECT ch.id, ch.uuid, ch.name, ch.role, ch.channel_type, c.count_type, COALESCE(SUM(c.count), 0) as count
FROM channels_channel ch
LEFT OUTER JOIN channels_channelcount c ON c.channel_id = ch.id
WHERE ch.org_id = $1 AND ch.is_active = TRUE
GROUP BY ch.id, c.count_type;`

type channelCountRow struct {
ID models.ChannelID `db:"id"`
Expand Down Expand Up @@ -225,27 +197,27 @@ func calculateChannelCounts(ctx context.Context, rt *runtime.Runtime, org *model
family.Metric = append(family.Metric,
&dto.Metric{
Label: []*dto.LabelPair{
&dto.LabelPair{
{
Name: proto.String("channel_name"),
Value: proto.String(channel.Name),
},
&dto.LabelPair{
{
Name: proto.String("channel_uuid"),
Value: proto.String(string(channel.UUID)),
},
&dto.LabelPair{
{
Name: proto.String("channel_type"),
Value: proto.String(channel.ChannelType),
},
&dto.LabelPair{
{
Name: proto.String("msg_direction"),
Value: proto.String(direction),
},
&dto.LabelPair{
{
Name: proto.String("msg_type"),
Value: proto.String(countType),
},
&dto.LabelPair{
{
Name: proto.String("org"),
Value: proto.String(org.Name),
},
Expand Down

0 comments on commit b517840

Please sign in to comment.