|
5 | 5 | "flag"
|
6 | 6 | "fmt"
|
7 | 7 | "os"
|
| 8 | + "reflect" |
8 | 9 | "strconv"
|
| 10 | + "strings" |
9 | 11 | "time"
|
10 | 12 |
|
11 | 13 | "github.com/demisto/tools/client"
|
|
16 | 18 | password = flag.String("p", "", "Password to login to the server")
|
17 | 19 | server = flag.String("s", "", "Demisto server URL")
|
18 | 20 | filter = flag.String("f", "status:=2 and closed:>"+defaultStartDate(), "Filter for the mttr query")
|
| 21 | + group = flag.String("g", "owner", "Which field to group by") |
19 | 22 | output = flag.String("o", "mttr.csv", "Output csv file path")
|
20 | 23 | )
|
21 | 24 |
|
@@ -79,22 +82,50 @@ func main() {
|
79 | 82 | continue
|
80 | 83 | }
|
81 | 84 | delta := incidents.Data[i].Closed.Sub(incidents.Data[i].Created)
|
82 |
| - owner := incidents.Data[i].OwnerID |
83 |
| - if owner == "" { |
84 |
| - owner = "dbot" |
| 85 | + field := incidents.Data[i].OwnerID |
| 86 | + if *group == "owner" { |
| 87 | + if field == "" { |
| 88 | + field = "dbot" |
| 89 | + } |
| 90 | + } else { |
| 91 | + found := false |
| 92 | + val := reflect.ValueOf(incidents.Data[i]) |
| 93 | + typ := val.Type() |
| 94 | + for i := 0; i < typ.NumField(); i++ { |
| 95 | + tag := typ.Field(i).Tag |
| 96 | + jsonTag := tag.Get("json") |
| 97 | + if jsonTag == *group || strings.Title(*group) == typ.Field(i).Name { |
| 98 | + field = val.FieldByName(typ.Field(i).Name).String() |
| 99 | + found = true |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + if !found { |
| 104 | + for k, v := range incidents.Data[i].CustomFields { |
| 105 | + if k == *group { |
| 106 | + field = fmt.Sprintf("%v", v) |
| 107 | + found = true |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + // If not found then it will default to owner |
| 113 | + if !found { |
| 114 | + field = "" |
| 115 | + } |
85 | 116 | }
|
86 |
| - if _, ok := mttr[owner]; ok { |
87 |
| - mttr[owner]["incidents"] = mttr[owner]["incidents"] + 1 |
88 |
| - mttr[owner]["total"] = mttr[owner]["total"] + int64(delta) |
| 117 | + if _, ok := mttr[field]; ok { |
| 118 | + mttr[field]["incidents"] = mttr[field]["incidents"] + 1 |
| 119 | + mttr[field]["total"] = mttr[field]["total"] + int64(delta) |
89 | 120 | } else {
|
90 |
| - mttr[owner] = map[string]int64{"incidents": 1, "total": int64(delta)} |
| 121 | + mttr[field] = map[string]int64{"incidents": 1, "total": int64(delta)} |
91 | 122 | }
|
92 | 123 | }
|
93 | 124 | f, err := os.Create(*output)
|
94 | 125 | check(err)
|
95 | 126 | defer f.Close()
|
96 | 127 | w := csv.NewWriter(f)
|
97 |
| - w.Write([]string{"Analyst", "Incidents", "MTTR"}) |
| 128 | + w.Write([]string{strings.Title(*group), "Incidents", "MTTR"}) |
98 | 129 | for k, v := range mttr {
|
99 | 130 | w.Write([]string{k, strconv.FormatInt(v["incidents"], 10), strconv.FormatInt(int64(time.Duration(v["total"]/v["incidents"]).Minutes()), 10)})
|
100 | 131 | }
|
|
0 commit comments