summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
Diffstat (limited to 'store')
-rw-r--r--store/sql_command_store.go41
-rw-r--r--store/sql_command_store_test.go28
-rw-r--r--store/sql_post_store.go2
-rw-r--r--store/sql_session_store.go30
-rw-r--r--store/sql_session_store_test.go26
-rw-r--r--store/sql_team_store.go19
-rw-r--r--store/sql_team_store_test.go20
-rw-r--r--store/store.go3
8 files changed, 163 insertions, 6 deletions
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..337ad16e6 100644
--- a/store/sql_session_store.go
+++ b/store/sql_session_store.go
@@ -255,3 +255,33 @@ 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
+ WHERE ExpiresAt > :Time`
+
+ if len(teamId) > 0 {
+ query += " AND TeamId = :TeamId"
+ }
+
+ 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
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
diff --git a/store/sql_session_store_test.go b/store/sql_session_store_test.go
index 34d3128a6..506695f0e 100644
--- a/store/sql_session_store_test.go
+++ b/store/sql_session_store_test.go
@@ -200,3 +200,29 @@ func TestSessionStoreUpdateLastActivityAt(t *testing.T) {
}
}
+
+func TestSessionCount(t *testing.T) {
+ Setup()
+
+ 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 {
+ 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 {