PLT-5070: Server side component of Telemetry. (#5514)

Этот коммит содержится в:
George Goldberg
2017-02-24 17:33:59 +00:00
коммит произвёл GitHub
родитель ba028ed74b
Коммит f182d196ff
13 изменённых файлов: 861 добавлений и 354 удалений

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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.")
}
}
}

Просмотреть файл

@@ -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
}

Просмотреть файл

@@ -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)
}
}
}

Просмотреть файл

@@ -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 {