From 8239c68cf323e4bb20007d2b456336becead273d Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Thu, 25 Feb 2016 12:32:46 -0500 Subject: Refactor and modularize analytics on the client --- store/sql_command_store.go | 41 ++++++++++++++++++++++++++++++++++++----- store/sql_command_store_test.go | 28 ++++++++++++++++++++++++++++ store/sql_post_store.go | 2 +- store/sql_session_store.go | 29 +++++++++++++++++++++++++++++ store/sql_session_store_test.go | 25 +++++++++++++++++++++++++ store/sql_team_store.go | 19 +++++++++++++++++++ store/sql_team_store_test.go | 20 ++++++++++++++++++++ store/store.go | 3 +++ 8 files changed, 161 insertions(+), 6 deletions(-) (limited to 'store') diff --git a/store/sql_command_store.go b/store/sql_command_store.go index 760235e10..074a6e588 100644 --- a/store/sql_command_store.go +++ b/store/sql_command_store.go @@ -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 diff --git a/store/sql_command_store_test.go b/store/sql_command_store_test.go index b4610d4aa..644ebc9ae 100644 --- a/store/sql_command_store_test.go +++ b/store/sql_command_store_test.go @@ -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") + } + } +} diff --git a/store/sql_post_store.go b/store/sql_post_store.go index 6a614b6a7..3346534ab 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -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, diff --git a/store/sql_session_store.go b/store/sql_session_store.go index 8dccc0770..6fe71db17 100644 --- a/store/sql_session_store.go +++ b/store/sql_session_store.go @@ -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 +} diff --git a/store/sql_session_store_test.go b/store/sql_session_store_test.go index 34d3128a6..9b430eb30 100644 --- a/store/sql_session_store_test.go +++ b/store/sql_session_store_test.go @@ -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") + } + } +} diff --git a/store/sql_team_store.go b/store/sql_team_store.go index 86ab9ac04..1893268c8 100644 --- a/store/sql_team_store.go +++ b/store/sql_team_store.go @@ -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 +} diff --git a/store/sql_team_store_test.go b/store/sql_team_store_test.go index 7dc31cbe2..743ef053f 100644 --- a/store/sql_team_store_test.go +++ b/store/sql_team_store_test.go @@ -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") + } + } +} diff --git a/store/store.go b/store/store.go index 397601543..b041cfa25 100644 --- a/store/store.go +++ b/store/store.go @@ -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 { -- cgit v1.2.3-1-g7c22 From e09a18db310f403eac632272351bcb6ef9282717 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Fri, 26 Feb 2016 11:27:45 -0500 Subject: Don't count expired sessions --- store/sql_session_store.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'store') diff --git a/store/sql_session_store.go b/store/sql_session_store.go index 6fe71db17..337ad16e6 100644 --- a/store/sql_session_store.go +++ b/store/sql_session_store.go @@ -266,13 +266,14 @@ func (me SqlSessionStore) AnalyticsSessionCount(teamId string) StoreChannel { `SELECT COUNT(*) FROM - Sessions` + Sessions + WHERE ExpiresAt > :Time` if len(teamId) > 0 { - query += " WHERE TeamId = :TeamId" + query += " AND TeamId = :TeamId" } - if c, err := me.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId}); err != nil { + if c, err := me.GetReplica().SelectInt(query, map[string]interface{}{"Time": model.GetMillis(), "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 -- cgit v1.2.3-1-g7c22 From fbb71cab4b4c0289c1f071f9fa2e61b0b0237ece Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Mon, 29 Feb 2016 08:08:11 -0500 Subject: Fix unit test for session counting --- store/sql_session_store_test.go | 1 + 1 file changed, 1 insertion(+) (limited to 'store') diff --git a/store/sql_session_store_test.go b/store/sql_session_store_test.go index 9b430eb30..506695f0e 100644 --- a/store/sql_session_store_test.go +++ b/store/sql_session_store_test.go @@ -207,6 +207,7 @@ func TestSessionCount(t *testing.T) { s1 := model.Session{} s1.UserId = model.NewId() s1.TeamId = model.NewId() + s1.ExpiresAt = model.GetMillis() + 100000 Must(store.Session().Save(&s1)) if r1 := <-store.Session().AnalyticsSessionCount(""); r1.Err != nil { -- cgit v1.2.3-1-g7c22