Refactor and modularize analytics on the client

Этот коммит содержится в:
JoramWilander
2016-02-25 12:32:46 -05:00
родитель 8aa4e28932
Коммит 8239c68cf3
28 изменённых файлов: 1416 добавлений и 1076 удалений

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

@@ -151,18 +151,49 @@ func (s SqlCommandStore) PermanentDeleteByUser(userId string) StoreChannel {
return storeChannel
}
func (s SqlCommandStore) Update(hook *model.Command) StoreChannel {
func (s SqlCommandStore) Update(cmd *model.Command) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
result := StoreResult{}
hook.UpdateAt = model.GetMillis()
cmd.UpdateAt = model.GetMillis()
if _, err := s.GetMaster().Update(hook); err != nil {
result.Err = model.NewLocAppError("SqlCommandStore.Update", "store.sql_command.save.update.app_error", nil, "id="+hook.Id+", "+err.Error())
if _, err := s.GetMaster().Update(cmd); err != nil {
result.Err = model.NewLocAppError("SqlCommandStore.Update", "store.sql_command.save.update.app_error", nil, "id="+cmd.Id+", "+err.Error())
} else {
result.Data = hook
result.Data = cmd
}
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}
func (s SqlCommandStore) AnalyticsCommandCount(teamId string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
result := StoreResult{}
query :=
`SELECT
COUNT(*)
FROM
Commands
WHERE
DeleteAt = 0`
if len(teamId) > 0 {
query += " AND TeamId = :TeamId"
}
if c, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId}); err != nil {
result.Err = model.NewLocAppError("SqlCommandStore.AnalyticsCommandCount", "store.sql_command.analytics_command_count.app_error", nil, err.Error())
} else {
result.Data = c
}
storeChannel <- result

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

@@ -153,3 +153,31 @@ func TestCommandStoreUpdate(t *testing.T) {
t.Fatal(r2.Err)
}
}
func TestCommandCount(t *testing.T) {
Setup()
o1 := &model.Command{}
o1.CreatorId = model.NewId()
o1.Method = model.COMMAND_METHOD_POST
o1.TeamId = model.NewId()
o1.URL = "http://nowhere.com/"
o1 = (<-store.Command().Save(o1)).Data.(*model.Command)
if r1 := <-store.Command().AnalyticsCommandCount(""); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) == 0 {
t.Fatal("should be at least 1 command")
}
}
if r2 := <-store.Command().AnalyticsCommandCount(o1.TeamId); r2.Err != nil {
t.Fatal(r2.Err)
} else {
if r2.Data.(int64) != 1 {
t.Fatal("should be 1 command")
}
}
}

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

@@ -947,7 +947,7 @@ func (s SqlPostStore) AnalyticsPostCount(teamId string, mustHaveFile bool, mustH
result := StoreResult{}
query :=
`SELECT
`SELECT
COUNT(Posts.Id) AS Value
FROM
Posts,

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

@@ -255,3 +255,32 @@ func (me SqlSessionStore) UpdateDeviceId(id, deviceId string) StoreChannel {
return storeChannel
}
func (me SqlSessionStore) AnalyticsSessionCount(teamId string) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
result := StoreResult{}
query :=
`SELECT
COUNT(*)
FROM
Sessions`
if len(teamId) > 0 {
query += " WHERE TeamId = :TeamId"
}
if c, err := me.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId}); err != nil {
result.Err = model.NewLocAppError("SqlSessionStore.AnalyticsSessionCount", "store.sql_session.analytics_session_count.app_error", nil, err.Error())
} else {
result.Data = c
}
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}

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

@@ -200,3 +200,28 @@ func TestSessionStoreUpdateLastActivityAt(t *testing.T) {
}
}
func TestSessionCount(t *testing.T) {
Setup()
s1 := model.Session{}
s1.UserId = model.NewId()
s1.TeamId = model.NewId()
Must(store.Session().Save(&s1))
if r1 := <-store.Session().AnalyticsSessionCount(""); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) == 0 {
t.Fatal("should have at least 1 session")
}
}
if r2 := <-store.Session().AnalyticsSessionCount(s1.TeamId); r2.Err != nil {
t.Fatal(r2.Err)
} else {
if r2.Data.(int64) != 1 {
t.Fatal("should have 1 session")
}
}
}

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

@@ -317,3 +317,22 @@ func (s SqlTeamStore) PermanentDelete(teamId string) StoreChannel {
return storeChannel
}
func (s SqlTeamStore) AnalyticsTeamCount() StoreChannel {
storeChannel := make(StoreChannel)
go func() {
result := StoreResult{}
if c, err := s.GetReplica().SelectInt("SELECT COUNT(*) FROM Teams WHERE DeleteAt = 0", map[string]interface{}{}); err != nil {
result.Err = model.NewLocAppError("SqlTeamStore.AnalyticsTeamCount", "store.sql_team.analytics_team_count.app_error", nil, err.Error())
} else {
result.Data = c
}
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}

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

@@ -261,3 +261,23 @@ func TestDelete(t *testing.T) {
t.Fatal(r1.Err)
}
}
func TestTeamCount(t *testing.T) {
Setup()
o1 := model.Team{}
o1.DisplayName = "DisplayName"
o1.Name = "a" + model.NewId() + "b"
o1.Email = model.NewId() + "@nowhere.com"
o1.Type = model.TEAM_OPEN
o1.AllowTeamListing = true
Must(store.Team().Save(&o1))
if r1 := <-store.Team().AnalyticsTeamCount(); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) == 0 {
t.Fatal("should be at least 1 team")
}
}
}

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

@@ -55,6 +55,7 @@ type TeamStore interface {
GetAllTeamListing() StoreChannel
GetByInviteId(inviteId string) StoreChannel
PermanentDelete(teamId string) StoreChannel
AnalyticsTeamCount() StoreChannel
}
type ChannelStore interface {
@@ -141,6 +142,7 @@ type SessionStore interface {
UpdateLastActivityAt(sessionId string, time int64) StoreChannel
UpdateRoles(userId string, roles string) StoreChannel
UpdateDeviceId(id string, deviceId string) StoreChannel
AnalyticsSessionCount(teamId string) StoreChannel
}
type AuditStore interface {
@@ -196,6 +198,7 @@ type CommandStore interface {
Delete(commandId string, time int64) StoreChannel
PermanentDeleteByUser(userId string) StoreChannel
Update(hook *model.Command) StoreChannel
AnalyticsCommandCount(teamId string) StoreChannel
}
type PreferenceStore interface {