From bba3bbd9f3e250cca0ce705e664382fe3ad6e78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 24 Aug 2018 09:53:44 +0200 Subject: MM-11572: Force correct order on messages generated in the bulk (#9244) --- cmd/mattermost/commands/sampledata.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'cmd') diff --git a/cmd/mattermost/commands/sampledata.go b/cmd/mattermost/commands/sampledata.go index 0983ab0df..ed550bf6b 100644 --- a/cmd/mattermost/commands/sampledata.go +++ b/cmd/mattermost/commands/sampledata.go @@ -59,6 +59,15 @@ func randomPastTime(seconds int) int64 { return (today.Unix() * 1000) - int64(rand.Intn(seconds*1000)) } +func sortedRandomDates(size int) []int64 { + dates := make([]int64, size) + for i := 0; i < size; i++ { + dates[i] = randomPastTime(50000) + } + sort.Slice(dates, func(a, b int) bool { return dates[a] < dates[b] }) + return dates +} + func randomEmoji() string { emojis := []string{"+1", "-1", "heart", "blush"} return emojis[rand.Intn(len(emojis))] @@ -274,8 +283,10 @@ func sampleDataCmdF(command *cobra.Command, args []string) error { for team, channels := range teamsAndChannels { for _, channel := range channels { + dates := sortedRandomDates(postsPerChannel) + for i := 0; i < postsPerChannel; i++ { - postLine := createPost(team, channel, allUsers) + postLine := createPost(team, channel, allUsers, dates[i]) encoder.Encode(postLine) } } @@ -286,8 +297,10 @@ func sampleDataCmdF(command *cobra.Command, args []string) error { user2 := allUsers[rand.Intn(len(allUsers))] channelLine := createDirectChannel([]string{user1, user2}) encoder.Encode(channelLine) + + dates := sortedRandomDates(postsPerDirectChannel) for j := 0; j < postsPerDirectChannel; j++ { - postLine := createDirectPost([]string{user1, user2}) + postLine := createDirectPost([]string{user1, user2}, dates[j]) encoder.Encode(postLine) } } @@ -303,8 +316,10 @@ func sampleDataCmdF(command *cobra.Command, args []string) error { } channelLine := createDirectChannel(users) encoder.Encode(channelLine) + + dates := sortedRandomDates(postsPerGroupChannel) for j := 0; j < postsPerGroupChannel; j++ { - postLine := createDirectPost(users) + postLine := createDirectPost(users, dates[j]) encoder.Encode(postLine) } } @@ -529,9 +544,9 @@ func createChannel(idx int, teamName string) app.LineImportData { } } -func createPost(team string, channel string, allUsers []string) app.LineImportData { +func createPost(team string, channel string, allUsers []string, createAt int64) app.LineImportData { message := randomMessage(allUsers) - create_at := randomPastTime(50000) + create_at := createAt user := allUsers[rand.Intn(len(allUsers))] // Some messages are flagged by an user @@ -589,9 +604,9 @@ func createDirectChannel(members []string) app.LineImportData { } } -func createDirectPost(members []string) app.LineImportData { +func createDirectPost(members []string, createAt int64) app.LineImportData { message := randomMessage(members) - create_at := randomPastTime(50000) + create_at := createAt user := members[rand.Intn(len(members))] // Some messages are flagged by an user -- cgit v1.2.3-1-g7c22