From d73826cf240fdecf6e1fd109cf250b1047daf934 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Tue, 20 Aug 2024 16:03:22 -0300 Subject: [PATCH] treat bots as participants --- server/sqlstore/playbook_run.go | 19 ++++++++++--------- server/sqlstore/playbook_run_test.go | 2 +- server/sqlstore/stats.go | 6 ++---- server/sqlstore/stats_test.go | 14 +++++++------- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/server/sqlstore/playbook_run.go b/server/sqlstore/playbook_run.go index 82f2b6d9c1..e5aaab9ae3 100644 --- a/server/sqlstore/playbook_run.go +++ b/server/sqlstore/playbook_run.go @@ -156,13 +156,15 @@ func NewPlaybookRunStore(pluginAPI PluginAPIClient, sqlStore *SQLStore) app.Play // the user is not a member of the channel they won't have permissions to get the user list participantsCol := ` COALESCE( - (SELECT string_agg(rp.UserId, ',') + (SELECT string_agg(x.UserId, ',') FROM ( + SELECT rp.UserId FROM IR_Incident as i2 JOIN IR_Run_Participants as rp on rp.IncidentID = i2.ID + LEFT JOIN Bots b ON (b.UserId = rp.UserId) WHERE i2.Id = i.Id AND rp.IsParticipant = true - AND rp.UserId NOT IN (SELECT UserId FROM Bots) - ), '' + ORDER BY (CASE WHEN b.UserId IS NULL THEN 0 ELSE 1 END), rp.UserId + ) x), '' ) AS ConcatenatedParticipantIDs` if sqlStore.db.DriverName() == model.DatabaseDriverMysql { participantsCol = ` @@ -170,9 +172,10 @@ func NewPlaybookRunStore(pluginAPI PluginAPIClient, sqlStore *SQLStore) app.Play (SELECT group_concat(rp.UserId separator ',') FROM IR_Incident as i2 JOIN IR_Run_Participants as rp on rp.IncidentID = i2.ID + LEFT JOIN Bots b ON (b.UserId = rp.UserId) WHERE i2.Id = i.Id AND rp.IsParticipant = true - AND rp.UserId NOT IN (SELECT UserId FROM Bots) + ORDER BY (CASE WHEN b.UserId IS NULL THEN 0 ELSE 1 END), rp.UserId ), '' ) AS ConcatenatedParticipantIDs` } @@ -891,13 +894,12 @@ func (s *playbookRunStore) GetPlaybookRunIDsForChannel(channelID string) ([]stri } // GetHistoricalPlaybookRunParticipantsCount returns the count of all members of a playbook run's channel -// since the beginning of the playbook run, excluding bots. +// since the beginning of the playbook run. func (s *playbookRunStore) GetHistoricalPlaybookRunParticipantsCount(channelID string) (int64, error) { query := s.queryBuilder. Select("COUNT(DISTINCT cmh.UserId)"). From("ChannelMemberHistory AS cmh"). - Where(sq.Eq{"cmh.ChannelId": channelID}). - Where(sq.Expr("cmh.UserId NOT IN (SELECT UserId FROM Bots)")) + Where(sq.Eq{"cmh.ChannelId": channelID}) var numParticipants int64 err := s.store.getBuilder(s.store.db, &numParticipants, query) @@ -1380,8 +1382,7 @@ func (s *playbookRunStore) GetParticipantsActiveTotal() (int64, error) { From("IR_Run_Participants as rp"). Join("IR_Incident AS i ON i.ID = rp.IncidentID"). Where(sq.Eq{"i.CurrentStatus": app.StatusInProgress}). - Where(sq.Eq{"rp.IsParticipant": true}). - Where(sq.Expr("rp.UserId NOT IN (SELECT UserId FROM Bots)")) + Where(sq.Eq{"rp.IsParticipant": true}) if err := s.store.getBuilder(s.store.db, &count, query); err != nil { return 0, errors.Wrap(err, "failed to count active participants") diff --git a/server/sqlstore/playbook_run_test.go b/server/sqlstore/playbook_run_test.go index 428224a3cd..845c5bc18c 100644 --- a/server/sqlstore/playbook_run_test.go +++ b/server/sqlstore/playbook_run_test.go @@ -1166,7 +1166,7 @@ func TestGetParticipantsActiveTotal(t *testing.T) { createRuns(store, playbookRunStore, playbook1ID, []userInfo{alice, bob, bot1}, team1ID, 3, app.StatusInProgress) createRuns(store, playbookRunStore, playbook2ID, []userInfo{tom, bob}, team2ID, 5, app.StatusInProgress) - expected := 2*3 + 2*5 // ignore bots + expected := 3*3 + 2*5 actual, err := playbookRunStore.GetParticipantsActiveTotal() require.NoError(t, err) require.Equal(t, int64(expected), actual) diff --git a/server/sqlstore/stats.go b/server/sqlstore/stats.go index 42e93deab4..b152bc7710 100644 --- a/server/sqlstore/stats.go +++ b/server/sqlstore/stats.go @@ -96,8 +96,7 @@ func (s *StatsStore) TotalActiveParticipants(filters *StatsFilters) int { From("IR_Run_Participants as rp"). Join("IR_Incident AS i ON i.ID = rp.IncidentID"). Where("i.EndAt = 0"). - Where("rp.IsParticipant = true"). - Where(sq.Expr("rp.UserId NOT IN (SELECT UserId FROM Bots)")) + Where("rp.IsParticipant = true") query = applyFilters(query, filters) @@ -316,8 +315,7 @@ func (s *StatsStore) ActiveParticipantsPerDayLastXDays(x int, filters *StatsFilt q = q. From("IR_Incident as i"). - InnerJoin("ChannelMemberHistory as cmh ON i.ChannelId = cmh.ChannelId"). - Where(sq.Expr("cmh.UserId NOT IN (SELECT UserId FROM Bots)")) + InnerJoin("ChannelMemberHistory as cmh ON i.ChannelId = cmh.ChannelId") q = applyFilters(q, filters) counts, err := s.performQueryForXCols(q, x) diff --git a/server/sqlstore/stats_test.go b/server/sqlstore/stats_test.go index ec0a1ed5a0..bf05c5d390 100644 --- a/server/sqlstore/stats_test.go +++ b/server/sqlstore/stats_test.go @@ -196,41 +196,41 @@ func TestTotalInProgressPlaybookRuns(t *testing.T) { } addUsersToRuns(t, store, []userInfo{bob, lucy, phil}, []string{playbookRuns[0].ID, playbookRuns[1].ID, playbookRuns[2].ID, playbookRuns[3].ID, playbookRuns[5].ID, playbookRuns[6].ID, playbookRuns[7].ID, playbookRuns[8].ID}) - addUsersToRuns(t, store, []userInfo{bob, quincy}, []string{playbookRuns[4].ID}) - addUsersToRuns(t, store, []userInfo{john}, []string{playbookRuns[0].ID}) + addUsersToRuns(t, store, []userInfo{bob, quincy, bot1}, []string{playbookRuns[4].ID}) + addUsersToRuns(t, store, []userInfo{john, bot2}, []string{playbookRuns[0].ID}) addUsersToRuns(t, store, []userInfo{jane}, []string{playbookRuns[0].ID, playbookRuns[1].ID}) t.Run(driverName+" Active Participants - team1", func(t *testing.T) { result := statsStore.TotalActiveParticipants(&StatsFilters{ TeamID: team1id, }) - assert.Equal(t, 5, result) + assert.Equal(t, 6, result) }) t.Run(driverName+" Active Participants - team2", func(t *testing.T) { result := statsStore.TotalActiveParticipants(&StatsFilters{ TeamID: team2id, }) - assert.Equal(t, 4, result) + assert.Equal(t, 5, result) }) t.Run(driverName+" Active Participants, playbook1", func(t *testing.T) { result := statsStore.TotalActiveParticipants(&StatsFilters{ PlaybookID: "playbook1", }) - assert.Equal(t, 5, result) + assert.Equal(t, 6, result) }) t.Run(driverName+" Active Participants, playbook2", func(t *testing.T) { result := statsStore.TotalActiveParticipants(&StatsFilters{ PlaybookID: "playbook2", }) - assert.Equal(t, 4, result) + assert.Equal(t, 5, result) }) t.Run(driverName+" Active Participants, all", func(t *testing.T) { result := statsStore.TotalActiveParticipants(&StatsFilters{}) - assert.Equal(t, 6, result) + assert.Equal(t, 8, result) }) t.Run(driverName+" In-progress Playbook Runs - team1", func(t *testing.T) {