/
app_usage.go
246 lines (209 loc) · 8.43 KB
/
app_usage.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/cloudfoundry-community/go-cfclient"
"github.com/jszwec/csvutil"
"github.com/labstack/echo"
"github.com/palantir/stacktrace"
"github.com/parnurzeal/gorequest"
)
//AppUsage array of orgs usage
type AppUsage struct {
Orgs []OrgAppUsage `json:"orgs" csv:"orgs"`
}
//OrgAppUsage Single org usage
type OrgAppUsage struct {
OrganizationGUID string `json:"organization_guid" csv:"organization_guid"`
OrgName string `json:"organization_name" csv:"organization_name"`
PeriodStart time.Time `json:"period_start" csv:"period_start"`
PeriodEnd time.Time `json:"period_end" csv:"period_end"`
AppUsages []struct {
SpaceGUID string `json:"space_guid" csv:"space_guid"`
SpaceName string `json:"space_name" csv:"space_name"`
AppName string `json:"app_name" csv:"app_name"`
AppGUID string `json:"app_guid" csv:"app_guid"`
InstanceCount int `json:"instance_count" csv:"instance_count"`
MemoryInMbPerInstance int `json:"memory_in_mb_per_instance" csv:"memory_in_mb_per_instance"`
DurationInSeconds int `json:"duration_in_seconds" csv:"duration_in_seconds"`
} `json:"app_usages" csv:"app_usages"`
}
// FlattenAppUsage flattened data for simple response with repeated org info
type FlattenAppUsage struct {
Orgs []FlattenOrgAppUsage `json:"app_usages" csv:"app_usages"`
}
// FlattenOrgAppUsage flattened data for simple response usage
type FlattenOrgAppUsage struct {
OrganizationGUID string `json:"organization_guid" csv:"organization_guid"`
OrgName string `json:"organization_name" csv:"organization_name"`
PeriodStart time.Time `json:"period_start" csv:"period_start"`
PeriodEnd time.Time `json:"period_end" csv:"period_end"`
SpaceGUID string `json:"space_guid" csv:"space_guid"`
SpaceName string `json:"space_name" csv:"space_name"`
AppName string `json:"app_name" csv:"app_name"`
AppGUID string `json:"app_guid" csv:"app_guid"`
InstanceCount int `json:"instance_count" csv:"instance_count"`
MemoryInMbPerInstance int `json:"memory_in_mb_per_instance" csv:"memory_in_mb_per_instance"`
DurationInSeconds int `json:"duration_in_seconds" csv:"duration_in_seconds"`
}
// handles report formatting if CSV is specified
func appReportFormatter(c echo.Context, usageReport *FlattenAppUsage) error {
var format = strings.ToLower(c.QueryParam("format"))
if format == "csv" {
fmt.Println("csv output requested")
b, err := csvutil.Marshal(usageReport.Orgs)
if err != nil {
fmt.Println("error:", err)
}
return c.String(http.StatusOK, string(b))
} else {
return c.JSON(http.StatusOK, usageReport)
}
}
// AppUsageReportByRange handle a start and end date in the call
// /app-usage?start=2017-11-01&end=2017-11-03
func AppUsageReportByRange(c echo.Context) error {
// format the date range
fmt.Println("Start date is '" + c.QueryParam("start") + "'")
start, err := time.Parse(dateFormat, c.QueryParam("start"))
if err != nil {
return stacktrace.Propagate(err, "Improper start date provided in the URL")
}
end, err := time.Parse(dateFormat, c.QueryParam("end"))
if err != nil {
return stacktrace.Propagate(err, "Improper end date provided in the URL")
}
// format the start and end string
dateRange := GenDateRange(start, end)
fmt.Println("Date range is ", dateRange)
// Generate the report for all orgs
usageReport, err := GenAppUsageReport(cfClient, dateRange)
if err != nil {
return stacktrace.Propagate(err, "Couldn't get app usage report for yesterday")
}
// return report
return appReportFormatter(c, usageReport)
}
// AppUsageReportForToday handles the static nature of Apptio's Datalink
// in order to gather app usage data for the previous day
func AppUsageReportForToday(c echo.Context) error {
// format the date range
dateToday := time.Now().Local()
// format the start and end string
dateRange := GenDateRange(dateToday, dateToday)
fmt.Println("Date range is ", dateRange)
// Generate the report for all orgs
usageReport, err := GenAppUsageReport(cfClient, dateRange)
if err != nil {
return stacktrace.Propagate(err, "Couldn't get app usage report for yesterday")
}
// return report
return appReportFormatter(c, usageReport)
}
// AppUsageReportForYesterday handles the static nature of Apptio's Datalink
// in order to gather app usage data for the previous day
func AppUsageReportForYesterday(c echo.Context) error {
// format the date range
dateToday := time.Now().Local()
dateYesterday := dateToday.AddDate(0, 0, -1)
// format the start and end string
dateRange := GenDateRange(dateYesterday, dateYesterday)
fmt.Println("Date range is ", dateRange)
// Generate the report for all orgs
usageReport, err := GenAppUsageReport(cfClient, dateRange)
if err != nil {
return stacktrace.Propagate(err, "Couldn't get app usage report for yesterday")
}
// return report
return appReportFormatter(c, usageReport)
}
// AppUsageReportForMonth handles the app-usage call validating the date
// and executing the report creation
func AppUsageReportForMonth(c echo.Context) error {
// first day of month and today's date
dateToday := time.Now().Local()
currentYear, currentMonth, _ := dateToday.Date()
currentLocation := dateToday.Location()
firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
// format the start and end string
dateRange := GenDateRange(firstOfMonth, dateToday)
fmt.Println("Date range is ", dateRange)
// Generate the report for all orgs
usageReport, err := GenAppUsageReport(cfClient, dateRange)
if err != nil {
return stacktrace.Propagate(err, "Couldn't get app usage report for yesterday")
}
// return report
return appReportFormatter(c, usageReport)
}
// GenAppUsageReport pulls the entire report together
func GenAppUsageReport(client *cfclient.Client, dateRange string) (*FlattenAppUsage, error) {
// get a list of orgs within the foundation
orgs, err := client.ListOrgs()
if err != nil {
return nil, stacktrace.Propagate(err, "Failed getting list of orgs using client: %v", client)
}
report := AppUsage{}
token, err := client.GetToken()
if err != nil {
return nil, stacktrace.Propagate(err, "Failed getting token using client: %v", client)
}
// loop through orgs and get app usage report for each
for _, org := range orgs {
orgUsage, err := AppUsageForOrg(token, org, dateRange)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed getting app usage for org: "+org.Name)
}
orgUsage.OrgName = org.Name
report.Orgs = append(report.Orgs, *orgUsage)
}
// flatten the complexity of report for ease of consumption
flatReport, err := GetFlattenedAppOutput(&report)
if err != nil {
return nil, stacktrace.Propagate(err, "Couldn't get app usage report")
}
return &flatReport, nil
}
// AppUsageForOrg queries apps manager app_usages API for the orgs app usage information
func AppUsageForOrg(token string, org cfclient.Org, dateRange string) (*OrgAppUsage, error) {
usageAPI := os.Getenv("CF_USAGE_API")
target := &OrgAppUsage{}
request := gorequest.New()
resp, _, err := request.Get(usageAPI+"/organizations/"+org.Guid+"/app_usages?"+dateRange).
Set("Authorization", token).TLSClientConfig(&tls.Config{InsecureSkipVerify: cfSkipSsl}).
EndStruct(&target)
if err != nil {
return nil, stacktrace.Propagate(err[0], "Failed to get app usage report %v", org)
}
if resp.StatusCode != 200 {
return nil, stacktrace.NewError("Failed getting app usage report %v", resp)
}
return target, nil
}
// GetFlattenedAppOutput convert formatting to flattened output
func GetFlattenedAppOutput(usageReport *AppUsage) (FlattenAppUsage, error) {
var flatUsage FlattenAppUsage
for _, orgs := range usageReport.Orgs {
for _, app := range orgs.AppUsages {
appusage := FlattenOrgAppUsage{
OrganizationGUID: orgs.OrganizationGUID,
OrgName: orgs.OrgName,
PeriodStart: orgs.PeriodStart,
PeriodEnd: orgs.PeriodEnd,
SpaceGUID: app.SpaceGUID,
SpaceName: app.SpaceName,
AppName: app.AppName,
AppGUID: app.AppGUID,
InstanceCount: app.InstanceCount,
MemoryInMbPerInstance: app.MemoryInMbPerInstance,
DurationInSeconds: app.DurationInSeconds,
}
flatUsage.Orgs = append(flatUsage.Orgs, appusage)
}
}
return flatUsage, nil
}