From 0e6678534ea889508b2b71979a0c81d10a50cd94 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Thu, 24 Sep 2020 14:02:04 +0200 Subject: [PATCH] Add report user command --- cmd/report.go | 33 +++++++++++++++++++++++++++++++++ cmd/root.go | 1 + model/campaign.go | 5 +++-- model/campaign_reports.go | 30 ++++++++++++++++++++++++++++++ model/ticket.go | 4 ++-- 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 cmd/report.go create mode 100644 model/campaign_reports.go diff --git a/cmd/report.go b/cmd/report.go new file mode 100644 index 0000000..968a85b --- /dev/null +++ b/cmd/report.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "git.ctrlz.es/mgdelacroix/campaigner/app" +) + +func UsersReportCmd() *cobra.Command { + return &cobra.Command{ + Use: "users", + Short: "A users report", + Args: cobra.NoArgs, + Run: withApp(userReportCmdF), + } +} + +func ReportCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "report", + Short: "Generates reports on campaign information", + } + + cmd.AddCommand( + UserReportCmd(), + ) + + return cmd +} + +func userReportCmdF(a *app.App, cmd *cobra.Command, _ []string) { + a.Campaign.PrintUserReport() +} diff --git a/cmd/root.go b/cmd/root.go index d339238..5775ddf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -51,6 +51,7 @@ func RootCmd() *cobra.Command { PublishCmd(), PullCmd(), SyncCmd(), + ReportCmd(), ) return cmd diff --git a/model/campaign.go b/model/campaign.go index a6dc35b..9b94fde 100644 --- a/model/campaign.go +++ b/model/campaign.go @@ -62,8 +62,9 @@ func (c *Campaign) PrintStatus() { totalPublishedGithub++ if t.IsAssigned() { totalAssigned++ - } else if t.IsClosed() { - totalClosed++ + if t.IsClosed() { + totalClosed++ + } } } } diff --git a/model/campaign_reports.go b/model/campaign_reports.go new file mode 100644 index 0000000..9cf4257 --- /dev/null +++ b/model/campaign_reports.go @@ -0,0 +1,30 @@ +package model + +import ( + "fmt" + "os" + "text/tabwriter" +) + +func (c *Campaign) PrintUserReport() { + userTickets := map[string]int{} + for _, ticket := range c.Tickets { + user := ticket.GithubAssignee + if user != "" { + if count, ok := userTickets[user]; ok { + userTickets[user] = count+1 + } else { + userTickets[user] = 1 + } + } + } + + fmt.Println("User report:\n") + w := tabwriter.NewWriter(os.Stdout, 3, 0, 3, ' ', tabwriter.AlignRight) + fmt.Fprintln(w, "GitHub username\tTickets closed\t") + for user, count := range userTickets { + fmt.Fprintf(w, "%s\t%d\t\n", user, count) + } + w.Flush() + fmt.Println() +} diff --git a/model/ticket.go b/model/ticket.go index 64af85e..071898f 100644 --- a/model/ticket.go +++ b/model/ticket.go @@ -24,11 +24,11 @@ func (t *Ticket) IsPublishedGithub() bool { } func (t *Ticket) IsAssigned() bool { - return t.IsPublishedGithub() && t.GithubAssignee != "" && t.GithubStatus == "open" + return t.IsPublishedGithub() && t.GithubAssignee != "" } func (t *Ticket) IsClosed() bool { - return t.IsPublishedGithub() && t.GithubAssignee != "" && t.GithubStatus == "closed" + return t.IsAssigned() && t.GithubStatus == "closed" } func (t *Ticket) PrintStatus() {