-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.go
99 lines (79 loc) · 2.24 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"strings"
"time"
common "github.com/apiheat/akamai-cli-common"
edgegrid "github.com/apiheat/go-edgegrid"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func cmdReport(c *cli.Context) error {
if c.String("start") == "" && c.String("end") == "" {
log.Info("[" + strings.Title(appName) + "]::Report will be generated for last 24h")
}
if c.String("cp-code") == "all" {
return reportAllCpCodes(c)
}
return reportCpCode(c)
}
func reportAllCpCodes(c *cli.Context) error {
log.Info("[" + strings.Title(appName) + "]::Report will be generated for all CP Codes")
start, end := prepareTimeframes(c)
requestOptions := edgegrid.ReportOptions{
TypeOfReport: c.String("type"),
Interval: c.String("interval"),
Start: start,
End: end,
}
body := edgegrid.ReportingBodyAll{
ObjectType: "cpcode",
ObjectIds: "all",
Metrics: common.StringToStringsArr(c.String("metrics")),
}
report, _ := apiClient.Reporting.GenerateReport(body, requestOptions)
fmt.Println(report)
return nil
}
func reportCpCode(c *cli.Context) error {
start, end := prepareTimeframes(c)
requestOptions := edgegrid.ReportOptions{
TypeOfReport: c.String("type"),
Interval: c.String("interval"),
Start: start,
End: end,
}
body := edgegrid.ReportingBody{
ObjectType: "cpcode",
ObjectIds: common.StringToStringsArr(c.String("cp-code")),
Metrics: common.StringToStringsArr(c.String("metrics")),
}
report, err := apiClient.Reporting.GenerateReport(body, requestOptions)
common.ErrorCheck(err)
common.PrintJSON(report.Body)
return nil
}
// prepareTimeframes return start and end time for our query against OpsGenie
func prepareTimeframes(c *cli.Context) (start, end time.Time) {
var (
r time.Duration
)
switch c.String("interval") {
case "FIVE_MINUTES":
r = 5 * time.Minute
case "HOUR":
r = 1 * time.Hour
}
if c.String("start") == "" && c.String("end") == "" {
start = time.Now().AddDate(0, 0, -1).Round(r)
end = time.Now().Round(r)
if c.String("interval") == "DAY" {
start = specificDate(start)
end = specificDate(end)
}
}
return start, end
}
func specificDate(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
}