summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
author=Corey Hulen <corey@hulen.com>2016-01-25 11:23:55 -0500
committer=Corey Hulen <corey@hulen.com>2016-01-25 11:23:55 -0500
commitea71731f838fc010cfc7511c09875184d1b2396b (patch)
tree7841e6908a42badb5171760426d2ca5898a76bda /store
parent6b534f1b0dae2614ec267a82f1c4dc1b096b7b1c (diff)
parent5478ea34e436109ece417c3704a1fa36d3aba4a5 (diff)
downloadchat-ea71731f838fc010cfc7511c09875184d1b2396b.tar.gz
chat-ea71731f838fc010cfc7511c09875184d1b2396b.tar.bz2
chat-ea71731f838fc010cfc7511c09875184d1b2396b.zip
merging
Diffstat (limited to 'store')
-rw-r--r--store/sql_channel_store.go27
-rw-r--r--store/sql_channel_store_test.go106
-rw-r--r--store/sql_post_store.go52
-rw-r--r--store/sql_user_store.go27
-rw-r--r--store/store.go1
5 files changed, 187 insertions, 26 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go
index 4585647de..7400df8d2 100644
--- a/store/sql_channel_store.go
+++ b/store/sql_channel_store.go
@@ -582,7 +582,16 @@ func (s SqlChannelStore) GetMemberCount(channelId string) StoreChannel {
go func() {
result := StoreResult{}
- count, err := s.GetReplica().SelectInt("SELECT count(*) FROM ChannelMembers WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId})
+ count, err := s.GetReplica().SelectInt(`
+ SELECT
+ count(*)
+ FROM
+ ChannelMembers,
+ Users
+ WHERE
+ ChannelMembers.UserId = Users.Id
+ AND ChannelMembers.ChannelId = :ChannelId
+ AND Users.DeleteAt = 0`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
result.Err = model.NewAppError("SqlChannelStore.GetMemberCount", "We couldn't get the channel member count", "channel_id="+channelId+", "+err.Error())
} else {
@@ -869,15 +878,13 @@ func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) S
go func() {
result := StoreResult{}
- v, err := s.GetReplica().SelectInt(
- `SELECT
- COUNT(Id) AS Value
- FROM
- Channels
- WHERE
- TeamId = :TeamId
- AND Type = :ChannelType`,
- map[string]interface{}{"TeamId": teamId, "ChannelType": channelType})
+ query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType"
+
+ 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.NewAppError("SqlChannelStore.AnalyticsTypeCount", "We couldn't get channel type counts", err.Error())
} else {
diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go
index 8b22fbb7a..a3b0c2286 100644
--- a/store/sql_channel_store_test.go
+++ b/store/sql_channel_store_test.go
@@ -750,3 +750,109 @@ func TestChannelStoreIncrementMentionCount(t *testing.T) {
t.Fatal("failed to update")
}
}
+
+func TestGetMemberCount(t *testing.T) {
+ Setup()
+
+ teamId := model.NewId()
+
+ c1 := model.Channel{
+ TeamId: teamId,
+ DisplayName: "Channel1",
+ Name: "a" + model.NewId() + "b",
+ Type: model.CHANNEL_OPEN,
+ }
+ Must(store.Channel().Save(&c1))
+
+ c2 := model.Channel{
+ TeamId: teamId,
+ DisplayName: "Channel2",
+ Name: "a" + model.NewId() + "b",
+ Type: model.CHANNEL_OPEN,
+ }
+ Must(store.Channel().Save(&c2))
+
+ t.Logf("c1.Id = %v", c1.Id)
+
+ u1 := model.User{
+ TeamId: teamId,
+ Email: model.NewId(),
+ DeleteAt: 0,
+ }
+ Must(store.User().Save(&u1))
+
+ m1 := model.ChannelMember{
+ ChannelId: c1.Id,
+ UserId: u1.Id,
+ NotifyProps: model.GetDefaultChannelNotifyProps(),
+ }
+ Must(store.Channel().SaveMember(&m1))
+
+ if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil {
+ t.Fatal("failed to get member count: %v", result.Err)
+ } else if result.Data.(int64) != 1 {
+ t.Fatal("got incorrect member count %v", result.Data)
+ }
+
+ u2 := model.User{
+ TeamId: teamId,
+ Email: model.NewId(),
+ DeleteAt: 0,
+ }
+ Must(store.User().Save(&u2))
+
+ m2 := model.ChannelMember{
+ ChannelId: c1.Id,
+ UserId: u2.Id,
+ NotifyProps: model.GetDefaultChannelNotifyProps(),
+ }
+ Must(store.Channel().SaveMember(&m2))
+
+ if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil {
+ t.Fatal("failed to get member count: %v", result.Err)
+ } else if result.Data.(int64) != 2 {
+ t.Fatal("got incorrect member count %v", result.Data)
+ }
+
+ // make sure members of other channels aren't counted
+ u3 := model.User{
+ TeamId: teamId,
+ Email: model.NewId(),
+ DeleteAt: 0,
+ }
+ Must(store.User().Save(&u3))
+
+ m3 := model.ChannelMember{
+ ChannelId: c2.Id,
+ UserId: u3.Id,
+ NotifyProps: model.GetDefaultChannelNotifyProps(),
+ }
+ Must(store.Channel().SaveMember(&m3))
+
+ if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil {
+ t.Fatal("failed to get member count: %v", result.Err)
+ } else if result.Data.(int64) != 2 {
+ t.Fatal("got incorrect member count %v", result.Data)
+ }
+
+ // make sure inactive users aren't counted
+ u4 := model.User{
+ TeamId: teamId,
+ Email: model.NewId(),
+ DeleteAt: 10000,
+ }
+ Must(store.User().Save(&u4))
+
+ m4 := model.ChannelMember{
+ ChannelId: c1.Id,
+ UserId: u4.Id,
+ NotifyProps: model.GetDefaultChannelNotifyProps(),
+ }
+ Must(store.Channel().SaveMember(&m4))
+
+ if result := <-store.Channel().GetMemberCount(c1.Id); result.Err != nil {
+ t.Fatal("failed to get member count: %v", result.Err)
+ } else if result.Data.(int64) != 2 {
+ t.Fatal("got incorrect member count %v", result.Data)
+ }
+}
diff --git a/store/sql_post_store.go b/store/sql_post_store.go
index 40dca9930..e332858e4 100644
--- a/store/sql_post_store.go
+++ b/store/sql_post_store.go
@@ -805,9 +805,13 @@ func (s SqlPostStore) AnalyticsUserCountsWithPostsByDay(teamId string) StoreChan
FROM
Posts, Channels
WHERE
- Posts.ChannelId = Channels.Id
- AND Channels.TeamId = :TeamId
- AND Posts.CreateAt <= :EndTime
+ Posts.ChannelId = Channels.Id`
+
+ if len(teamId) > 0 {
+ query += " AND Channels.TeamId = :TeamId"
+ }
+
+ query += ` AND Posts.CreateAt <= :EndTime
ORDER BY Name DESC) AS t1
GROUP BY Name
ORDER BY Name DESC
@@ -824,9 +828,13 @@ func (s SqlPostStore) AnalyticsUserCountsWithPostsByDay(teamId string) StoreChan
FROM
Posts, Channels
WHERE
- Posts.ChannelId = Channels.Id
- AND Channels.TeamId = :TeamId
- AND Posts.CreateAt <= :EndTime
+ Posts.ChannelId = Channels.Id`
+
+ if len(teamId) > 0 {
+ query += " AND Channels.TeamId = :TeamId"
+ }
+
+ query += ` AND Posts.CreateAt <= :EndTime
ORDER BY Name DESC) AS t1
GROUP BY Name
ORDER BY Name DESC
@@ -869,9 +877,13 @@ func (s SqlPostStore) AnalyticsPostCountsByDay(teamId string) StoreChannel {
FROM
Posts, Channels
WHERE
- Posts.ChannelId = Channels.Id
- AND Channels.TeamId = :TeamId
- AND Posts.CreateAt <= :EndTime
+ Posts.ChannelId = Channels.Id`
+
+ if len(teamId) > 0 {
+ query += " AND Channels.TeamId = :TeamId"
+ }
+
+ query += ` AND Posts.CreateAt <= :EndTime
AND Posts.CreateAt >= :StartTime) AS t1
GROUP BY Name
ORDER BY Name DESC
@@ -888,9 +900,13 @@ func (s SqlPostStore) AnalyticsPostCountsByDay(teamId string) StoreChannel {
FROM
Posts, Channels
WHERE
- Posts.ChannelId = Channels.Id
- AND Channels.TeamId = :TeamId
- AND Posts.CreateAt <= :EndTime
+ Posts.ChannelId = Channels.Id`
+
+ if len(teamId) > 0 {
+ query += " AND Channels.TeamId = :TeamId"
+ }
+
+ query += ` AND Posts.CreateAt <= :EndTime
AND Posts.CreateAt >= :StartTime) AS t1
GROUP BY Name
ORDER BY Name DESC
@@ -924,16 +940,20 @@ func (s SqlPostStore) AnalyticsPostCount(teamId string) StoreChannel {
go func() {
result := StoreResult{}
- v, err := s.GetReplica().SelectInt(
+ query :=
`SELECT
COUNT(Posts.Id) AS Value
FROM
Posts,
Channels
WHERE
- Posts.ChannelId = Channels.Id
- AND Channels.TeamId = :TeamId`,
- map[string]interface{}{"TeamId": teamId})
+ Posts.ChannelId = Channels.Id`
+
+ if len(teamId) > 0 {
+ query += " AND Channels.TeamId = :TeamId"
+ }
+
+ v, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.AnalyticsPostCount", "We couldn't get post counts", err.Error())
} else {
diff --git a/store/sql_user_store.go b/store/sql_user_store.go
index 0f73f73c3..efd8b7f33 100644
--- a/store/sql_user_store.go
+++ b/store/sql_user_store.go
@@ -600,3 +600,30 @@ func (us SqlUserStore) PermanentDelete(userId string) StoreChannel {
return storeChannel
}
+
+func (us SqlUserStore) AnalyticsUniqueUserCount(teamId string) StoreChannel {
+
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ query := "SELECT COUNT(DISTINCT Email) FROM Users"
+
+ if len(teamId) > 0 {
+ query += " WHERE TeamId = :TeamId"
+ }
+
+ v, err := us.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId})
+ if err != nil {
+ result.Err = model.NewAppError("SqlUserStore.AnalyticsUniqueUserCount", "We couldn't get the unique user count", err.Error())
+ } else {
+ result.Data = v
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
diff --git a/store/store.go b/store/store.go
index 3a865d52a..8a362bc8f 100644
--- a/store/store.go
+++ b/store/store.go
@@ -126,6 +126,7 @@ type UserStore interface {
GetTotalActiveUsersCount() StoreChannel
GetSystemAdminProfiles() StoreChannel
PermanentDelete(userId string) StoreChannel
+ AnalyticsUniqueUserCount(teamId string) StoreChannel
}
type SessionStore interface {