forked from krujos/usagereport-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
365 lines (330 loc) · 10.2 KB
/
models.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package models
import (
"bytes"
"database/sql"
"fmt"
"log"
"strconv"
"strings"
"time"
_ "github.com/mattn/go-sqlite3"
)
type Org struct {
Name string
MemoryQuota int
MemoryUsage int
Spaces Spaces
}
type Space struct {
Name string
Apps Apps
Services Services
}
//App representation
type App struct {
Actual int
Desire int
RAM int
}
//Service representation
type Service struct {
Label string
ServicePlan string
}
type SpaceStats struct {
Name string
DeployedAppsCount int
RunningAppsCount int
StoppedAppsCount int
DeployedAppInstancesCount int
RunningAppInstancesCount int
StoppedAppInstancesCount int
ServicesCount int
ConsumedMemory int
}
type OrgStats struct {
Name string
MemoryQuota int
MemoryUsage int
Spaces Spaces
DeployedAppsCount int
RunningAppsCount int
StoppedAppsCount int
DeployedAppInstancesCount int
RunningAppInstancesCount int
StoppedAppInstancesCount int
ServicesCount int
}
type Orgs []Org
type Spaces []Space
type Apps []App
type Services []Service
type Report struct {
Orgs Orgs
}
func (org *Org) InstancesCount() int {
instancesCount := 0
for _, space := range org.Spaces {
instancesCount += space.InstancesCount()
SCSCount := space.ServiceInstancesCount("p-spring-cloud-services")
SCDFCount := space.ServiceInstancesCount("p-dataflow-servers")
instancesCount += SCSCount + (SCDFCount*3)
}
return instancesCount
}
func (org *Org) RunningAppsCount() int {
instancesCount := 0
for _, space := range org.Spaces {
instancesCount += space.RunningAppsCount()
}
return instancesCount
}
func (org *Org) RunningInstancesCount() int {
instancesCount := 0
for _, space := range org.Spaces {
instancesCount += space.RunningInstancesCount()
SCSCount := space.ServiceInstancesCount("p-spring-cloud-services")
SCDFCount := space.ServiceInstancesCount("p-dataflow-servers")
instancesCount += SCSCount + (SCDFCount*3)
}
return instancesCount
}
func (org *Org) AppsCount() int {
appsCount := 0
for _, space := range org.Spaces {
appsCount += len(space.Apps)
}
return appsCount
}
func (org *Org) ServicesCount() int {
servicesCount := 0
for _, space := range org.Spaces {
servicesCount += len(space.Services)
SCSCount := space.ServiceInstancesCount("p-spring-cloud-services")
SCDFCount := space.ServiceInstancesCount("p-dataflow-servers")
servicesCount -= (SCSCount + SCDFCount)
}
return servicesCount
}
func (space *Space) ConsumedMemory() int {
consumed := 0
for _, app := range space.Apps {
consumed += int(app.Actual * app.RAM)
}
return consumed
}
func (space *Space) RunningAppsCount() int {
runningAppsCount := 0
for _, app := range space.Apps {
if (app.Actual > 0) {
runningAppsCount++
}
}
return runningAppsCount
}
func (space *Space) InstancesCount() int {
instancesCount := 0
for _, app := range space.Apps {
instancesCount += int(app.Desire)
}
return instancesCount
}
func (space *Space) RunningInstancesCount() int {
runningInstancesCount := 0
for _, app := range space.Apps {
runningInstancesCount += int(app.Actual)
}
return runningInstancesCount
}
func (space *Space) ServicesCount() int {
servicesCount := len(space.Services)
return servicesCount
}
func (space *Space) ServiceInstancesCount(serviceType string) int {
boundedServiceInstancesCount := 0
for _, service := range space.Services {
if(strings.Contains(service.Label,serviceType)) {
boundedServiceInstancesCount++
}
}
return boundedServiceInstancesCount
}
func (spaces Spaces) Stats (c chan SpaceStats, skipSIcount bool) {
for _, space := range spaces {
SCSCount := space.ServiceInstancesCount("p-spring-cloud-services")
SCDFCount := space.ServiceInstancesCount("p-dataflow-servers")
lApps := len(space.Apps)
rApps := space.RunningAppsCount()
sApps := lApps-rApps
lAIs := space.InstancesCount()
lAIs += (SCSCount + (SCDFCount*3))
rAIs := space.RunningInstancesCount()
rAIs += (SCSCount + (SCDFCount*3))
sAIs := lAIs-rAIs
siCount := space.ServicesCount()
siCount -= (SCSCount + SCDFCount)
rAIConsumedMemory := (space.ConsumedMemory()+(SCSCount*1024)+(SCDFCount*3*1024))
if(skipSIcount) {
siCount = 0
}
c <- SpaceStats{
Name: space.Name,
DeployedAppsCount: lApps,
RunningAppsCount: rApps,
StoppedAppsCount: sApps,
DeployedAppInstancesCount: lAIs,
RunningAppInstancesCount: rAIs,
StoppedAppInstancesCount: sAIs,
ServicesCount: siCount,
ConsumedMemory: rAIConsumedMemory,
}
}
close(c)
}
func (orgs Orgs) Stats (c chan OrgStats) {
for _, org := range orgs {
lApps := org.AppsCount()
rApps := org.RunningAppsCount()
sApps := lApps-rApps
lAIs := org.InstancesCount()
rAIs := org.RunningInstancesCount()
sAIs := lAIs-rAIs
c <- OrgStats{
Name: org.Name,
MemoryQuota: org.MemoryQuota,
MemoryUsage: org.MemoryUsage,
Spaces: org.Spaces,
DeployedAppsCount: lApps,
RunningAppsCount: rApps,
StoppedAppsCount: sApps,
DeployedAppInstancesCount: lAIs,
RunningAppInstancesCount: rAIs,
StoppedAppInstancesCount: sAIs,
ServicesCount: org.ServicesCount(),
}
}
close(c)
}
func (report *Report) String() string {
var response bytes.Buffer
totalApps := 0
totalInstances := 0
totalRunningApps := 0
totalRunningInstances := 0
totalServiceInstances := 0
chOrgStats := make(chan OrgStats, len(report.Orgs))
go report.Orgs.Stats(chOrgStats)
for orgStats := range chOrgStats {
response.WriteString(fmt.Sprintf("Org %s is consuming %d MB of %d MB.\n",
orgStats.Name, orgStats.MemoryUsage, orgStats.MemoryQuota))
chSpaceStats := make(chan SpaceStats, len(orgStats.Spaces))
go orgStats.Spaces.Stats(chSpaceStats,orgStats.Name == "p-spring-cloud-services")
for spaceState := range chSpaceStats {
response.WriteString(
fmt.Sprintf("\tSpace %s is consuming %d MB memory (%d%%) of org quota.\n",
spaceState.Name, spaceState.ConsumedMemory, (100 * spaceState.ConsumedMemory / orgStats.MemoryQuota)))
response.WriteString(
fmt.Sprintf("\t\t%d apps: %d running %d stopped\n", spaceState.DeployedAppsCount,
spaceState.RunningAppsCount, spaceState.StoppedAppsCount))
response.WriteString(
fmt.Sprintf("\t\t%d app instances: %d running, %d stopped\n", spaceState.DeployedAppInstancesCount,
spaceState.RunningAppInstancesCount, spaceState.StoppedAppInstancesCount))
response.WriteString(
fmt.Sprintf("\t\t%d service instances of type Service Suite\n", spaceState.ServicesCount))
}
totalApps += orgStats.DeployedAppsCount
totalInstances += orgStats.DeployedAppInstancesCount
totalRunningApps += orgStats.RunningAppsCount
totalRunningInstances += orgStats.RunningAppInstancesCount
totalServiceInstances += orgStats.ServicesCount
}
response.WriteString(
fmt.Sprintf("You have deployed %d apps across %d org(s), with a total of %d app instances configured. You are currently running %d apps with %d app instances and using %d service instances of type Service Suite.\n",
totalApps, len(report.Orgs), totalInstances, totalRunningApps, totalRunningInstances, totalServiceInstances))
return response.String()
}
func (report *Report) CSV(apiep string) string {
var rows = [][]string{}
var csv bytes.Buffer
var headers = []string{"Env", "ReportDate", "OrgName", "SpaceName", "SpaceMemoryUsed", "OrgMemoryQuota", "AppsDeployed", "AppsRunning", "AppInstancesConfigured", "AppInstancesRunning", "TotalServiceInstancesDeployed", "RabbitMQServiceInstanceDeployed", "RedisServiceInstanceDeployed", "MySQLServiceInstanceDeployed", "SpringCloudServiceInstanceDeployed", "SpringCloudDataFlowServerInstanceDeployed"}
rows = append(rows, headers)
db, err := sql.Open("sqlite3", "./usagereport.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
db.SetMaxOpenConns(1)
sqlStmt := `
CREATE TABLE IF NOT EXISTS trueupreport(api_ep TEXT, report_date DATE, org_name TEXT, space_name TEXT, space_memory_used NUMERIC, org_memory_quota NUMERIC, apps_deployed NUMERIC, apps_running NUMERIC, app_instances_configured NUMERIC, app_instances_running NUMERIC, total_service_instances_deployed NUMERIC, rabbitmq_service_instance_deployed NUMERIC, redis_service_instance_deployed NUMERIC, mysql_service_instance_deployed NUMERIC, spring_cloud_service_instance_deployed NUMERIC, spring_cloud_dataflow_server_instance_deployed NUMERIC);
CREATE UNIQUE INDEX IF NOT EXISTS trueupreportidx ON trueupreport(api_ep,report_date,org_name,space_name);
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("INSERT INTO trueupreport VALUES (?, CURRENT_DATE, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
for _, org := range report.Orgs {
for _, space := range org.Spaces {
if (org.Name == "p-spring-cloud-services" || org.Name == "p-dataflow") {
continue
}
appsDeployed := len(space.Apps)
SCSCount := space.ServiceInstancesCount("p-spring-cloud-services")
SCDFCount := space.ServiceInstancesCount("p-dataflow-servers")
col0 := apiep
col1 := time.Now().Format("2006-01-02")
col2 := org.Name
col3 := space.Name
col4 := strconv.Itoa(space.ConsumedMemory()+(SCSCount*1024)+(SCDFCount*3*1024))
col5 := strconv.Itoa(org.MemoryQuota)
col6 := strconv.Itoa(appsDeployed)
col7 := strconv.Itoa(space.RunningAppsCount())
col8 := strconv.Itoa(space.InstancesCount())
col9 := strconv.Itoa(space.RunningInstancesCount()+SCSCount+(SCDFCount*3))
col10 := strconv.Itoa(space.ServicesCount()-(SCSCount + SCDFCount))
col11 := strconv.Itoa(space.ServiceInstancesCount("rabbit"))
col12 := strconv.Itoa(space.ServiceInstancesCount("redis"))
col13 := strconv.Itoa(space.ServiceInstancesCount("mysql"))
col14 := strconv.Itoa(SCSCount)
col15 := strconv.Itoa(SCDFCount*3)
spaceResult := []string{
col0,
col1,
col2,
col3,
col4,
col5,
col6,
col7,
col8,
col9,
col10,
col11,
col12,
col13,
col14,
col15,
}
rows = append(rows, spaceResult)
_, err = stmt.Exec(col0,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15)
if err != nil {
log.Fatal(err)
}
}
}
tx.Commit()
//database.Close()
for i := range rows {
csv.WriteString(strings.Join(rows[i], ", "))
csv.WriteString("\n")
}
return csv.String()
}