Skip to content

Commit

Permalink
basic JSON output support (#75)
Browse files Browse the repository at this point in the history
* lets see how well this gets jsonified

* bump to 2.10.0 since we're adding json support

* commenting on report type

* summary report description

* json output example
  • Loading branch information
aegershman authored Nov 27, 2019
1 parent 6f8acd0 commit 5c6f0c6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ cf usage-report -o voyager
cf usage-report -o voyager -o tenzing [-o orgName...]

# report using different formats
cf usage-report -o voyager -o tenzing --format string
cf usage-report -o voyager -o tenzing --format table
cf usage-report -o voyager -o tenzing --format string
cf usage-report -o voyager -o tenzing --format json
```

`--format table`:
Expand Down Expand Up @@ -61,6 +62,14 @@ org tenzing is consuming 0 MB of 83968 MB
across 2 org(s), you have 45 billable AIs, 37 are canonical AIs (30 running, 7 stopped), 8 are SCS instances
```

`--format json`:

This example is going to be a bit cluttered, so it's recommended using `jq` to parse the output. Then it could be used for parsing and interacting with other systems, like `cf-mgmt`:

```json
{"SummaryReport":{"OrgReports":[{"AppInstancesCount":37,"AppsCount":28,"BillableAppInstancesCount":40,"BillableServicesCount":13,"MemoryQuota":83968,"MemoryUsage":60416,"Name":"voyager","RunningAppInstancesCount":30,"RunningAppsCount":21,"ServicesCount":16,"ServicesSuiteForPivotalPlatformCount":7,"SpringCloudServicesCount":3,"StoppedAppInstancesCount":7,"StoppedAppsCount":7,"SpaceReports":[{"AppInstancesCount":18,"AppsCount":16,"BillableAppInstancesCount":20,"BillableServicesCount":7,"MemoryQuota":-1,"MemoryUsage":25600,"Name":"dev","RunningAppInstancesCount":13,"RunningAppsCount":11,"ServicesCount":9,"ServicesSuiteForPivotalPlatformCount":4,"SpringCloudServicesCount":2,"StoppedAppInstancesCount":5,"StoppedAppsCount":5},{"AppInstancesCount":19,"AppsCount":12,"BillableAppInstancesCount":20,"BillableServicesCount":6,"MemoryQuota":-1,"MemoryUsage":34816,"Name":"test","RunningAppInstancesCount":17,"RunningAppsCount":10,"ServicesCount":7,"ServicesSuiteForPivotalPlatformCount":3,"SpringCloudServicesCount":1,"StoppedAppInstancesCount":2,"StoppedAppsCount":2}]},{"AppInstancesCount":0,"AppsCount":21,"BillableAppInstancesCount":5,"BillableServicesCount":18,"MemoryQuota":83968,"MemoryUsage":0,"Name":"tenzing","RunningAppInstancesCount":0,"RunningAppsCount":0,"ServicesCount":23,"ServicesSuiteForPivotalPlatformCount":9,"SpringCloudServicesCount":5,"StoppedAppInstancesCount":0,"StoppedAppsCount":21,"SpaceReports":[{"AppInstancesCount":0,"AppsCount":8,"BillableAppInstancesCount":2,"BillableServicesCount":6,"MemoryQuota":-1,"MemoryUsage":0,"Name":"dev","RunningAppInstancesCount":0,"RunningAppsCount":0,"ServicesCount":8,"ServicesSuiteForPivotalPlatformCount":3,"SpringCloudServicesCount":2,"StoppedAppInstancesCount":0,"StoppedAppsCount":8},{"AppInstancesCount":0,"AppsCount":9,"BillableAppInstancesCount":2,"BillableServicesCount":6,"MemoryQuota":-1,"MemoryUsage":0,"Name":"test","RunningAppInstancesCount":0,"RunningAppsCount":0,"ServicesCount":8,"ServicesSuiteForPivotalPlatformCount":3,"SpringCloudServicesCount":2,"StoppedAppInstancesCount":0,"StoppedAppsCount":9},{"AppInstancesCount":0,"AppsCount":4,"BillableAppInstancesCount":1,"BillableServicesCount":6,"MemoryQuota":-1,"MemoryUsage":0,"Name":"integration","RunningAppInstancesCount":0,"RunningAppsCount":0,"ServicesCount":7,"ServicesSuiteForPivotalPlatformCount":3,"SpringCloudServicesCount":1,"StoppedAppInstancesCount":0,"StoppedAppsCount":4}]}],"AppInstancesCount":37,"AppsCount":49,"BillableAppInstancesCount":45,"BillableServicesCount":31,"MemoryQuota":167936,"MemoryUsage":60416,"Name":"voyagertenzing","RunningAppInstancesCount":30,"RunningAppsCount":21,"ServicesCount":39,"ServicesSuiteForPivotalPlatformCount":16,"SpringCloudServicesCount":8,"StoppedAppInstancesCount":7,"StoppedAppsCount":28},"Format":"json"}
```

## use in pivotal licensing

This plugin's usefulness for reporting things Pivotal's licensing on AI/SI packs and such is definitely a work in progress
Expand Down
1 change: 1 addition & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tasks:
# TODO wasteful; maybe ability to specify multiple formats on a single run...?
- cf usage-report -o voyager -o tenzing --format string
- cf usage-report -o voyager -o tenzing --format table
- cf usage-report -o voyager -o tenzing --format json
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (cmd *UsageReportCmd) GetMetadata() plugin.PluginMetadata {
Name: "cf-usage-report-plugin",
Version: plugin.VersionType{
Major: 2,
Minor: 9,
Build: 2,
Minor: 10,
Build: 0,
},
Commands: []plugin.Command{
{
Expand Down
5 changes: 4 additions & 1 deletion models/report.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

// Report -
// Report should be present in all reports at all levels,
// e.g. summary level, org level, space level, etc.
// this allows us to calculate common data at discrete layers and be
// confident it'll be available when presenting the data
type Report struct {
AppInstancesCount int
AppsCount int
Expand Down
4 changes: 2 additions & 2 deletions models/summary_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package models
import "bytes"

// SummaryReport holds an aggregated view of multiple OrgReports
// It effectively serves as the entrypoint into aggregating the data
// in preparation for it being presented
type SummaryReport struct {
OrgReports []OrgReport
orgsRef []Org
Report
}

Expand All @@ -17,7 +18,6 @@ func NewSummaryReport(orgs []Org) *SummaryReport {
}

self := &SummaryReport{
orgsRef: orgs,
OrgReports: orgReports,
}

Expand Down
11 changes: 10 additions & 1 deletion presenters/json_presenter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package presenters

import (
"encoding/json"
"fmt"

log "github.com/sirupsen/logrus"
)

func (p *Presenter) asJSON() {
log.Fatalln("uhhh... TODO. You probably shouldn't have used interfaces for the reporters, you should 've used structs. But it made sense at the time.")
j, err := json.Marshal(p)
if err != nil {
log.Fatalln(err)
}

// TODO maybe there's a cleaner way of outputting this than just a println
fmt.Println(string(j))
}

0 comments on commit 5c6f0c6

Please sign in to comment.