From 7ac97273855e5e68d9e4b10e96d5466201fb8871 Mon Sep 17 00:00:00 2001 From: Florian Orben Date: Sat, 28 Nov 2015 14:56:30 +0100 Subject: PLT-1035: Remove last data point in graphs on #statistics page --- api/admin_test.go | 8 +++++++ store/sql_post_store.go | 17 +++++++++++---- store/sql_post_store_test.go | 5 +++-- utils/time.go | 23 ++++++++++++++++++++ utils/time_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 utils/time.go create mode 100644 utils/time_test.go diff --git a/api/admin_test.go b/api/admin_test.go index 0db5caa4c..0a1682a99 100644 --- a/api/admin_test.go +++ b/api/admin_test.go @@ -235,6 +235,10 @@ func TestGetPostCount(t *testing.T) { post1 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"} post1 = Client.Must(Client.CreatePost(post1)).Data.(*model.Post) + // manually update creation time, since it's always set to 0 upon saving and we only retrieve posts < today + Srv.Store.(*store.SqlStore).GetMaster().Exec("UPDATE Posts SET CreateAt = :CreateAt WHERE ChannelId = :ChannelId", + map[string]interface{}{"ChannelId": channel1.Id, "CreateAt": utils.MillisFromTime(utils.Yesterday())}) + if _, err := Client.GetAnalytics(team.Id, "post_counts_day"); err == nil { t.Fatal("Shouldn't have permissions") } @@ -276,6 +280,10 @@ func TestUserCountsWithPostsByDay(t *testing.T) { post1 := &model.Post{ChannelId: channel1.Id, Message: "a" + model.NewId() + "a"} post1 = Client.Must(Client.CreatePost(post1)).Data.(*model.Post) + // manually update creation time, since it's always set to 0 upon saving and we only retrieve posts < today + Srv.Store.(*store.SqlStore).GetMaster().Exec("UPDATE Posts SET CreateAt = :CreateAt WHERE ChannelId = :ChannelId", + map[string]interface{}{"ChannelId": channel1.Id, "CreateAt": utils.MillisFromTime(utils.Yesterday())}) + if _, err := Client.GetAnalytics(team.Id, "user_counts_with_posts_day"); err == nil { t.Fatal("Shouldn't have permissions") } diff --git a/store/sql_post_store.go b/store/sql_post_store.go index cc596074f..1831eb23c 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -807,6 +807,7 @@ func (s SqlPostStore) AnalyticsUserCountsWithPostsByDay(teamId string) StoreChan WHERE Posts.ChannelId = Channels.Id AND Channels.TeamId = :TeamId + AND Posts.CreateAt <= :EndTime ORDER BY Name DESC) AS t1 GROUP BY Name ORDER BY Name DESC @@ -825,17 +826,20 @@ func (s SqlPostStore) AnalyticsUserCountsWithPostsByDay(teamId string) StoreChan WHERE Posts.ChannelId = Channels.Id AND Channels.TeamId = :TeamId + AND Posts.CreateAt <= :EndTime ORDER BY Name DESC) AS t1 GROUP BY Name ORDER BY Name DESC LIMIT 30` } + end := utils.MillisFromTime(utils.EndOfDay(utils.Yesterday())) + var rows model.AnalyticsRows _, err := s.GetReplica().Select( &rows, query, - map[string]interface{}{"TeamId": teamId, "Time": model.GetMillis() - 1000*60*60*24*31}) + map[string]interface{}{"TeamId": teamId, "EndTime": end}) if err != nil { result.Err = model.NewAppError("SqlPostStore.AnalyticsUserCountsWithPostsByDay", "We couldn't get user counts with posts", err.Error()) } else { @@ -867,7 +871,8 @@ func (s SqlPostStore) AnalyticsPostCountsByDay(teamId string) StoreChannel { WHERE Posts.ChannelId = Channels.Id AND Channels.TeamId = :TeamId - AND Posts.CreateAt >:Time) AS t1 + AND Posts.CreateAt <= :EndTime + AND Posts.CreateAt >= :StartTime) AS t1 GROUP BY Name ORDER BY Name DESC LIMIT 30` @@ -885,17 +890,21 @@ func (s SqlPostStore) AnalyticsPostCountsByDay(teamId string) StoreChannel { WHERE Posts.ChannelId = Channels.Id AND Channels.TeamId = :TeamId - AND Posts.CreateAt > :Time) AS t1 + AND Posts.CreateAt <= :EndTime + AND Posts.CreateAt >= :StartTime) AS t1 GROUP BY Name ORDER BY Name DESC LIMIT 30` } + end := utils.MillisFromTime(utils.EndOfDay(utils.Yesterday())) + start := utils.MillisFromTime(utils.StartOfDay(utils.Yesterday().AddDate(0, 0, -31))) + var rows model.AnalyticsRows _, err := s.GetReplica().Select( &rows, query, - map[string]interface{}{"TeamId": teamId, "Time": model.GetMillis() - 1000*60*60*24*31}) + map[string]interface{}{"TeamId": teamId, "StartTime": start, "EndTime": end}) if err != nil { result.Err = model.NewAppError("SqlPostStore.AnalyticsPostCountsByDay", "We couldn't get post counts by day", err.Error()) } else { diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index d9b087ea7..12b50cad3 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) func TestPostStoreSave(t *testing.T) { @@ -776,7 +777,7 @@ func TestUserCountsWithPostsByDay(t *testing.T) { o1 := &model.Post{} o1.ChannelId = c1.Id o1.UserId = model.NewId() - o1.CreateAt = model.GetMillis() + o1.CreateAt = utils.MillisFromTime(utils.Yesterday()) o1.Message = "a" + model.NewId() + "b" o1 = Must(store.Post().Save(o1)).(*model.Post) @@ -836,7 +837,7 @@ func TestPostCountsByDay(t *testing.T) { o1 := &model.Post{} o1.ChannelId = c1.Id o1.UserId = model.NewId() - o1.CreateAt = model.GetMillis() + o1.CreateAt = utils.MillisFromTime(utils.Yesterday()) o1.Message = "a" + model.NewId() + "b" o1 = Must(store.Post().Save(o1)).(*model.Post) diff --git a/utils/time.go b/utils/time.go new file mode 100644 index 000000000..7d5afdf8f --- /dev/null +++ b/utils/time.go @@ -0,0 +1,23 @@ +package utils + +import ( + "time" +) + +func MillisFromTime(t time.Time) int64 { + return t.UnixNano() / int64(time.Millisecond) +} + +func StartOfDay(t time.Time) time.Time { + year, month, day := t.Date() + return time.Date(year, month, day, 0, 0, 0, 0, t.Location()) +} + +func EndOfDay(t time.Time) time.Time { + year, month, day := t.Date() + return time.Date(year, month, day, 23, 59, 59, 999999999, t.Location()) +} + +func Yesterday() time.Time { + return time.Now().AddDate(0, 0, -1) +} diff --git a/utils/time_test.go b/utils/time_test.go new file mode 100644 index 000000000..7d65046bf --- /dev/null +++ b/utils/time_test.go @@ -0,0 +1,50 @@ +// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package utils + +import ( + "testing" + "time" +) + +var format = "2006-01-02 15:04:05.000000000" + +func TestMillisFromTime(t *testing.T) { + input, _ := time.Parse(format, "2015-01-01 12:34:00.000000000") + actual := MillisFromTime(input) + expected := int64(1420115640000) + + if actual != expected { + t.Fatalf("TestMillisFromTime failed, %v=%v", expected, actual) + } +} + +func TestYesterday(t *testing.T) { + actual := Yesterday() + expected := time.Now().AddDate(0, 0, -1) + + if actual.Year() != expected.Year() || actual.Day() != expected.Day() || actual.Month() != expected.Month() { + t.Fatalf("TestYesterday failed, %v=%v", expected, actual) + } +} + +func TestStartOfDay(t *testing.T) { + input, _ := time.Parse(format, "2015-01-01 12:34:00.000000000") + actual := StartOfDay(input) + expected, _ := time.Parse(format, "2015-01-01 00:00:00.000000000") + + if actual != expected { + t.Fatalf("TestStartOfDay failed, %v=%v", expected, actual) + } +} + +func TestEndOfDay(t *testing.T) { + input, _ := time.Parse(format, "2015-01-01 12:34:00.000000000") + actual := EndOfDay(input) + expected, _ := time.Parse(format, "2015-01-01 23:59:59.999999999") + + if actual != expected { + t.Fatalf("TestEndOfDay failed, %v=%v", expected, actual) + } +} -- cgit v1.2.3-1-g7c22