diff options
author | George Goldberg <george@gberg.me> | 2017-02-24 17:33:59 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-24 17:33:59 +0000 |
commit | f182d196fffc9da89ad63bdbd7bbb2e41da3146e (patch) | |
tree | 45d8aaad6ffc335dd40db225f2775d3204fcc812 /store | |
parent | ba028ed74b69bd1dd902344663fbf8ba4f1dfb87 (diff) | |
download | chat-f182d196fffc9da89ad63bdbd7bbb2e41da3146e.tar.gz chat-f182d196fffc9da89ad63bdbd7bbb2e41da3146e.tar.bz2 chat-f182d196fffc9da89ad63bdbd7bbb2e41da3146e.zip |
PLT-5070: Server side component of Telemetry. (#5514)
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_channel_store.go | 27 | ||||
-rw-r--r-- | store/sql_channel_store_test.go | 92 | ||||
-rw-r--r-- | store/sql_user_store.go | 39 | ||||
-rw-r--r-- | store/sql_user_store_test.go | 67 | ||||
-rw-r--r-- | store/store.go | 3 |
5 files changed, 228 insertions, 0 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index e7501ae69..068692074 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -1362,6 +1362,32 @@ func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) S return storeChannel } +func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType AND DeleteAt > 0" + + if len(teamId) > 0 { + query += " AND TeamId = :TeamId" + } + + v, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId, "ChannelType": channelType}) + if err != nil { + result.Err = model.NewLocAppError("SqlChannelStore.AnalyticsDeletedTypeCount", "store.sql_channel.analytics_deleted_type_count.app_error", nil, err.Error()) + } else { + result.Data = v + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) ExtraUpdateByUser(userId string, time int64) StoreChannel { storeChannel := make(StoreChannel, 1) @@ -1546,6 +1572,7 @@ func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) Sto result.Err = model.NewLocAppError("SqlChannelStore.GetMembersByIds", "store.sql_channel.get_members_by_ids.app_error", nil, "channelId="+channelId+" "+err.Error()) } else { result.Data = &members + } storeChannel <- result diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index 3075498f7..a43b97706 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -1401,3 +1401,95 @@ func TestChannelStoreGetMembersByIds(t *testing.T) { t.Fatal("empty user ids - should have failed") } } + +func TestChannelStoreAnalyticsDeletedTypeCount(t *testing.T) { + Setup() + + o1 := model.Channel{} + o1.TeamId = model.NewId() + o1.DisplayName = "ChannelA" + o1.Name = "a" + model.NewId() + "b" + o1.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&o1)) + + o2 := model.Channel{} + o2.TeamId = model.NewId() + o2.DisplayName = "Channel2" + o2.Name = "a" + model.NewId() + "b" + o2.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&o2)) + + p3 := model.Channel{} + p3.TeamId = model.NewId() + p3.DisplayName = "Channel3" + p3.Name = "a" + model.NewId() + "b" + p3.Type = model.CHANNEL_PRIVATE + Must(store.Channel().Save(&p3)) + + u1 := &model.User{} + u1.Email = model.NewId() + u1.Nickname = model.NewId() + Must(store.User().Save(u1)) + + u2 := &model.User{} + u2.Email = model.NewId() + u2.Nickname = model.NewId() + Must(store.User().Save(u2)) + + var d4 *model.Channel + if result := <-store.Channel().CreateDirectChannel(u1.Id, u2.Id); result.Err != nil { + t.Fatalf(result.Err.Error()) + } else { + d4 = result.Data.(*model.Channel) + } + + var openStartCount int64 + if result := <-store.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil { + t.Fatal(result.Err.Error()) + } else { + openStartCount = result.Data.(int64) + } + + var privateStartCount int64 + if result := <-store.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil { + t.Fatal(result.Err.Error()) + } else { + privateStartCount = result.Data.(int64) + } + + var directStartCount int64 + if result := <-store.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil { + t.Fatal(result.Err.Error()) + } else { + directStartCount = result.Data.(int64) + } + + Must(store.Channel().Delete(o1.Id, model.GetMillis())) + Must(store.Channel().Delete(o2.Id, model.GetMillis())) + Must(store.Channel().Delete(p3.Id, model.GetMillis())) + Must(store.Channel().Delete(d4.Id, model.GetMillis())) + + if result := <-store.Channel().AnalyticsDeletedTypeCount("", "O"); result.Err != nil { + t.Fatal(result.Err.Error()) + } else { + if result.Data.(int64) != openStartCount+2 { + t.Fatalf("Wrong open channel deleted count.") + } + } + + if result := <-store.Channel().AnalyticsDeletedTypeCount("", "P"); result.Err != nil { + t.Fatal(result.Err.Error()) + } else { + if result.Data.(int64) != privateStartCount+1 { + t.Fatalf("Wrong private channel deleted count.") + } + } + + if result := <-store.Channel().AnalyticsDeletedTypeCount("", "D"); result.Err != nil { + t.Fatal(result.Err.Error()) + } else { + if result.Data.(int64) != directStartCount+1 { + t.Fatalf("Wrong direct channel deleted count.") + } + } +} diff --git a/store/sql_user_store.go b/store/sql_user_store.go index 68c1ffec7..a2a9c8347 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -1438,3 +1438,42 @@ func (us SqlUserStore) performSearch(searchQuery string, term string, options ma return result } + +func (us SqlUserStore) AnalyticsGetInactiveUsersCount() StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if count, err := us.GetReplica().SelectInt("SELECT COUNT(Id) FROM Users WHERE DeleteAt > 0"); err != nil { + result.Err = model.NewLocAppError("SqlUserStore.AnalyticsGetInactiveUsersCount", "store.sql_user.analytics_get_inactive_users_count.app_error", nil, err.Error()) + } else { + result.Data = count + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (us SqlUserStore) AnalyticsGetSystemAdminCount() StoreChannel { + + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if count, err := us.GetReplica().SelectInt("SELECT count(*) FROM Users WHERE Roles LIKE :Roles and DeleteAt = 0", map[string]interface{}{"Roles": "%system_admin%"}); err != nil { + result.Err = model.NewLocAppError("SqlUserStore.AnalyticsGetSystemAdminCount", "store.sql_user.analytics_get_system_admin_count.app_error", nil, err.Error()) + } else { + result.Data = count + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} diff --git a/store/sql_user_store_test.go b/store/sql_user_store_test.go index f7236659b..c46a32ec1 100644 --- a/store/sql_user_store_test.go +++ b/store/sql_user_store_test.go @@ -1477,3 +1477,70 @@ func TestUserStoreSearch(t *testing.T) { } } } + +func TestUserStoreAnalyticsGetInactiveUsersCount(t *testing.T) { + Setup() + + u1 := &model.User{} + u1.Email = model.NewId() + Must(store.User().Save(u1)) + + var count int64 + + if result := <-store.User().AnalyticsGetInactiveUsersCount(); result.Err != nil { + t.Fatal(result.Err) + } else { + count = result.Data.(int64) + } + + u2 := &model.User{} + u2.Email = model.NewId() + u2.DeleteAt = model.GetMillis() + Must(store.User().Save(u2)) + + if result := <-store.User().AnalyticsGetInactiveUsersCount(); result.Err != nil { + t.Fatal(result.Err) + } else { + newCount := result.Data.(int64) + if count != newCount-1 { + t.Fatal("Expected 1 more inactive users but found otherwise.", count, newCount) + } + } +} + +func TestUserStoreAnalyticsGetSystemAdminCount(t *testing.T) { + Setup() + + var countBefore int64 + if result := <-store.User().AnalyticsGetSystemAdminCount(); result.Err != nil { + t.Fatal(result.Err) + } else { + countBefore = result.Data.(int64) + } + + u1 := model.User{} + u1.Email = model.NewId() + u1.Username = model.NewId() + u1.Roles = "system_user system_admin" + + u2 := model.User{} + u2.Email = model.NewId() + u2.Username = model.NewId() + + if err := (<-store.User().Save(&u1)).Err; err != nil { + t.Fatal("couldn't save user", err) + } + + if err := (<-store.User().Save(&u2)).Err; err != nil { + t.Fatal("couldn't save user", err) + } + + if result := <-store.User().AnalyticsGetSystemAdminCount(); result.Err != nil { + t.Fatal(result.Err) + } else { + // We expect to find 1 more system admin than there was at the start of this test function. + if count := result.Data.(int64); count != countBefore+1 { + t.Fatal("Did not get the expected number of system admins. Expected, got: ", countBefore+1, count) + } + } +} diff --git a/store/store.go b/store/store.go index 752b6cc28..34a709568 100644 --- a/store/store.go +++ b/store/store.go @@ -129,6 +129,7 @@ type ChannelStore interface { SearchInTeam(teamId string, term string) StoreChannel SearchMore(userId string, teamId string, term string) StoreChannel GetMembersByIds(channelId string, userIds []string) StoreChannel + AnalyticsDeletedTypeCount(teamId string, channelType string) StoreChannel } type PostStore interface { @@ -193,6 +194,8 @@ type UserStore interface { Search(teamId string, term string, options map[string]bool) StoreChannel SearchInChannel(channelId string, term string, options map[string]bool) StoreChannel SearchNotInChannel(teamId string, channelId string, term string, options map[string]bool) StoreChannel + AnalyticsGetInactiveUsersCount() StoreChannel + AnalyticsGetSystemAdminCount() StoreChannel } type SessionStore interface { |