diff --git a/core/models/contacts.go b/core/models/contacts.go index 829508aed..1df22d5fe 100644 --- a/core/models/contacts.go +++ b/core/models/contacts.go @@ -1041,7 +1041,7 @@ func StopContact(ctx context.Context, db Queryer, orgID OrgID, contactID Contact const sqlDeleteAllContactGroups = ` DELETE FROM contacts_contactgroup_contacts WHERE contact_id = $2 AND contactgroup_id = ANY( - SELECT id from contacts_contactgroup WHERE org_id = $1 and group_type IN ('M', 'Q', 'U') + SELECT id from contacts_contactgroup WHERE org_id = $1 and group_type IN ('M', 'Q') )` const sqlDeleteAllContactTriggers = ` diff --git a/core/models/groups.go b/core/models/groups.go index e83e6dbdd..0f49c3b31 100644 --- a/core/models/groups.go +++ b/core/models/groups.go @@ -79,7 +79,7 @@ const selectGroupsSQL = ` SELECT ROW_TO_JSON(r) FROM ( SELECT id, uuid, name, query FROM contacts_contactgroup - WHERE org_id = $1 AND group_type IN ('M', 'Q', 'U') AND is_active = TRUE + WHERE org_id = $1 AND group_type IN ('M', 'Q') AND is_active = TRUE ORDER BY name ASC ) r;` diff --git a/mailroom_test.dump b/mailroom_test.dump index f94196968..d0defeec0 100644 Binary files a/mailroom_test.dump and b/mailroom_test.dump differ diff --git a/testsuite/testdata/contacts.go b/testsuite/testdata/contacts.go index 8b9dcc41d..bbc742edc 100644 --- a/testsuite/testdata/contacts.go +++ b/testsuite/testdata/contacts.go @@ -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} } diff --git a/web/org/metrics.go b/web/org/metrics.go index bcfe6be68..7f6617959 100644 --- a/web/org/metrics.go +++ b/web/org/metrics.go @@ -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) { @@ -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), }, @@ -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"` @@ -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), },