focused_opps/cmd/counts.go
2024-07-17 18:33:08 -05:00

119 lines
3.2 KiB
Go

/*
Copyright © 2024 Peter Morton <Peter.Morton@verint.com>
*/
package cmd
import (
"encoding/json"
"fmt"
"os"
"strconv"
log "github.com/sirupsen/logrus"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"verint.com/focusedopps/internal/client"
"verint.com/focusedopps/internal/opps"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "count",
Short: "Counts the focused opps for each Solution Consultant",
Long: `Counts the focused opps for each Solution Consultant from the DFE Focused Opps Lists site.`,
PreRun: toggleDebug,
RunE: func(cmd *cobra.Command, args []string) error {
var counters map[int]opps.Counter = make(map[int]opps.Counter)
var scs map[int]string = make(map[int]string)
scs[4] = "Peter Morton"
scs[60] = "Jim Gross"
scs[122] = "Megan Brown"
scs[237] = "Shelley Rose"
scs[78] = "Amber Nash"
scs[172] = "Robin Zukowski"
scs[158] = "Ramzi Banna"
scs[385] = "David Young"
scs[52] = "Brian Cash"
scs[140] = "Scott Dorris"
scs[29] = "Dean Vivian"
scs[143] = "Mike Leyva"
scs[186] = "Paul Tessier"
scs[107] = "Jason Valdina"
scs[227] = "Adriano Oliveira"
for key := range scs {
counters[key] = opps.Counter{Inactive: 0, Won: 0, Open: 0, Closed: 0}
}
token := viper.GetString("token")
var fetchAgain = true
var next = ""
for fetchAgain {
collectionJSON, err := client.GetOpportunities(&client.Config{Token: token}, next)
if err != nil {
return err
}
var data opps.Result
if err := json.Unmarshal([]byte(collectionJSON), &data); err != nil {
return err
}
next = data.Data.Next
if next == "" {
fetchAgain = false
}
log.WithFields(log.Fields{"next": fmt.Sprintf("%+v", next)}).Debug()
for _, opp := range data.Data.Opportunities {
for _, scID := range opp.SolutionConsultants.Results {
if tmp, ok := counters[scID]; ok {
switch {
case opp.Status == "Inactive":
tmp.Inactive++
case opp.Status == "Won":
tmp.Won++
case opp.Status == "Open":
tmp.Open++
case opp.Status == "Lost/Other":
tmp.Closed++
}
counters[scID] = tmp
} else {
scs[scID] = "unknown(" + strconv.Itoa(scID) + "), see " + opp.Title
counters[scID] = opps.Counter{Inactive: 0, Won: 0, Open: 0, Closed: 0}
}
}
}
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Solution Consultant", "Inactive", "Won", "Open", "Lost/Other"})
for key, val := range counters {
t.AppendRow([]interface{}{scs[key], val.Inactive, val.Won, val.Open, val.Closed})
}
t.Render()
return nil
},
}
func init() {
rootCmd.AddCommand(listCmd)
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().StringVarP(&filter, "filter", "f", "", "Filter properties using a query. For example --filter \"redaction\"")
// listCmd.Flags().BoolVarP(&notrunc, "notrunc", "", false, "Prevent truncation of values")
}