From fde9ae0bee01547f1be587c0ad50afd8d54b35de Mon Sep 17 00:00:00 2001 From: alankan886 Date: Fri, 7 May 2021 00:04:48 -0400 Subject: [PATCH 1/3] Change two dates args to a number of days --- README.md | 4 ++-- server/command/command.go | 36 +++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 32726690f3..83881f8b81 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,9 @@ To quickly test Mattermost Incident Collaboration, use the following test comman * An example command looks like: `/incident test create-incident 6utgh6qg7p8ndeef9edc583cpc 2020-11-23 PR-Testing` -- `/incident test bulk-data [ongoing] [ended] [start date] [end date] [seed]` - Provide a number of ongoing and ended incidents, a start and end date, and an optional random seed. The command creates the given number of ongoing and ended incidents, with creation dates randomly between the start and end dates. The seed may be used to reproduce the same outcome on multiple invocations. Incident names are generated randomly. +- `/incident test bulk-data [ongoing] [ended] [days] [seed]` - Provide a number of ongoing and ended incidents, a number of days, and an optional random seed. The command creates the given number of ongoing and ended incidents, with creation dates randomly between n days ago and the day when the command was issued. The seed may be used to reproduce the same outcome on multiple invocations. Incident names are generated randomly. - * An example command looks like: `/incident test bulk-data 10 3 2020-01-31 2020-11-22 2` + * An example command looks like: `/incident test bulk-data 10 3 342 2` ## Contributing diff --git a/server/command/command.go b/server/command/command.go index c59a7a1d8c..dca03b565c 100644 --- a/server/command/command.go +++ b/server/command/command.go @@ -130,7 +130,7 @@ func getAutocompleteData(addTestCommands bool) *model.AutocompleteData { testCreate.AddTextArgument("Name of the incident", "Incident name", "") test.AddCommand(testCreate) - testData := model.NewAutocompleteData("bulk-data", "[ongoing] [ended] [begin] [end] [seed]", "Generate random test data in bulk") + testData := model.NewAutocompleteData("bulk-data", "[ongoing] [ended] [days] [seed]", "Generate random test data in bulk") testData.AddTextArgument("An integer indicating how many ongoing incidents will be generated.", "Number of ongoing incidents", "") testData.AddTextArgument("An integer indicating how many ended incidents will be generated.", "Number of ended incidents", "") testData.AddTextArgument("Date in format 2020-01-31", "First possible creation date", "") @@ -1117,8 +1117,8 @@ func (r *Runner) actionTestCreate(params []string) { } func (r *Runner) actionTestData(params []string) { - if len(params) < 4 { - r.postCommandResponse("`/incident test bulk-data` expects at least 4 arguments: [ongoing] [ended] [begin] [end]. Optionally, a fifth argument can be added: [seed].") + if len(params) < 3 { + r.postCommandResponse("`/incident test bulk-data` expects at least 3 arguments: [ongoing] [ended] [days]. Optionally, a fourth argument can be added: [seed].") return } @@ -1134,23 +1134,34 @@ func (r *Runner) actionTestData(params []string) { return } - begin, err := time.ParseInLocation("2006-01-02", params[2], time.Now().Location()) + days, err := strconv.Atoi((params[2])) if err != nil { - r.postCommandResponse(fmt.Sprintf("The provided value for the first possible date, '%s', is not a valid date. It needs to be in the format 2020-01-31.", params[2])) + r.postCommandResponse(fmt.Sprintf("The provided value for days, '%s', is not an integer.", params[2])) + return + } + + if days < 1 { + r.postCommandResponse(fmt.Sprintf("The provided value for days, '%d', is not greater than 0.", days)) return } - end, err := time.ParseInLocation("2006-01-02", params[3], time.Now().Location()) + begin, err := time.ParseInLocation("2006-01-02", time.Now().AddDate(0, 0, -days).Format("2006-01-02"), time.Now().Location()) if err != nil { - r.postCommandResponse(fmt.Sprintf("The provided value for the last possible date, '%s', is not a valid date. It needs to be in the format 2020-01-31.", params[3])) + r.warnUserAndLogErrorf("unable to parse in location on time.Now(): %v", err) + return + } + + end, err := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Now().Location()) + if err != nil { + r.warnUserAndLogErrorf("unable to parse in location on time.Now(): %v", err) return } seed := time.Now().Unix() - if len(params) > 4 { - parsedSeed, err := strconv.ParseInt(params[4], 10, 0) + if len(params) > 3 { + parsedSeed, err := strconv.ParseInt(params[3], 10, 0) if err != nil { - r.postCommandResponse(fmt.Sprintf("The provided value for the random seed, '%s', is not an integer.", params[4])) + r.postCommandResponse(fmt.Sprintf("The provided value for the random seed, '%s', is not an integer.", params[3])) return } @@ -1265,11 +1276,6 @@ func (r *Runner) generateTestData(numActiveIncidents, numEndedIncidents int, beg return } - if !end.After(begin) { - r.postCommandResponse("`end` must be a later date than `begin`") - return - } - timestamps := make([]int64, 0, numIncidents) for i := 0; i < numIncidents; i++ { timestamp := rand.Int63n(endMillis-beginMillis) + beginMillis From 2c7e4c7b6783e90d202038cfb4fa268689413f12 Mon Sep 17 00:00:00 2001 From: alankan886 Date: Fri, 7 May 2021 19:44:32 -0400 Subject: [PATCH 2/3] Improve error message for begin --- server/command/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/command/command.go b/server/command/command.go index dca03b565c..8b942738c6 100644 --- a/server/command/command.go +++ b/server/command/command.go @@ -1147,7 +1147,7 @@ func (r *Runner) actionTestData(params []string) { begin, err := time.ParseInLocation("2006-01-02", time.Now().AddDate(0, 0, -days).Format("2006-01-02"), time.Now().Location()) if err != nil { - r.warnUserAndLogErrorf("unable to parse in location on time.Now(): %v", err) + r.warnUserAndLogErrorf("unable to parse in location on time.Now().AddDate: %v", err) return } From befec73b419645033575f67be77b0f396f76b04b Mon Sep 17 00:00:00 2001 From: alankan886 Date: Thu, 13 May 2021 22:58:24 -0400 Subject: [PATCH 3/3] Update testData args and clean begin and end --- server/command/command.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/server/command/command.go b/server/command/command.go index 8b942738c6..02a96e90f4 100644 --- a/server/command/command.go +++ b/server/command/command.go @@ -133,8 +133,7 @@ func getAutocompleteData(addTestCommands bool) *model.AutocompleteData { testData := model.NewAutocompleteData("bulk-data", "[ongoing] [ended] [days] [seed]", "Generate random test data in bulk") testData.AddTextArgument("An integer indicating how many ongoing incidents will be generated.", "Number of ongoing incidents", "") testData.AddTextArgument("An integer indicating how many ended incidents will be generated.", "Number of ended incidents", "") - testData.AddTextArgument("Date in format 2020-01-31", "First possible creation date", "") - testData.AddTextArgument("Date in format 2020-01-31", "Last possible creation date", "") + testData.AddTextArgument("An integer n. The incidents generated will have a start date between n days ago and today.", "Range of days for the incident start date", "") testData.AddTextArgument("An integer in case you need random, but reproducible, results", "Random seed (optional)", "") test.AddCommand(testData) @@ -1145,17 +1144,8 @@ func (r *Runner) actionTestData(params []string) { return } - begin, err := time.ParseInLocation("2006-01-02", time.Now().AddDate(0, 0, -days).Format("2006-01-02"), time.Now().Location()) - if err != nil { - r.warnUserAndLogErrorf("unable to parse in location on time.Now().AddDate: %v", err) - return - } - - end, err := time.ParseInLocation("2006-01-02", time.Now().Format("2006-01-02"), time.Now().Location()) - if err != nil { - r.warnUserAndLogErrorf("unable to parse in location on time.Now(): %v", err) - return - } + begin := time.Now().AddDate(0, 0, -days) + end := time.Now() seed := time.Now().Unix() if len(params) > 3 {