Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContactGroup.group_type can no longer be 'U' #610

Merged
merged 2 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/models/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down
2 changes: 1 addition & 1 deletion core/models/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;`

Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
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