/
app_details.go
141 lines (129 loc) · 3.22 KB
/
app_details.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
package app
import (
"encoding/json"
"fmt"
"github.com/appbaseio/abc/appbase/common"
"github.com/appbaseio/abc/appbase/session"
"github.com/appbaseio/abc/appbase/spinner"
"github.com/olekukonko/tablewriter"
"net/http"
"os"
"strconv"
"time"
)
// Permission represents an app permission object
type Permission struct {
Description string `json:"description"`
Username string `json:"username"`
Password string `json:"password"`
}
type metricsBucket struct {
// DocCount int64 `json:"doc_count"`
APICalls map[string]json.Number `json:"apiCalls"`
DateAsStr string `json:"key_as_string"`
}
type metricsBuckets struct {
Buckets []metricsBucket `json:"buckets"`
}
type metricsOverall struct {
NumDocs int64 `json:"numDocs"`
Storage int `json:"storage"`
}
type metricsBody struct {
Month metricsBuckets `json:"month"`
Overall metricsOverall `json:"overall"`
}
type respBodyPerms struct {
Body []Permission `json:"body"`
}
type respBodyMetrics struct {
Body metricsBody `json:"body"`
}
// ShowAppMetrics ...
func ShowAppMetrics(app string) error {
spinner.StartText("Fetching app metrics")
defer spinner.Stop()
// show metrics
fmt.Println()
req, err := http.NewRequest("GET", common.AccAPIURL+"/app/"+app+"/metrics", nil)
if err != nil {
return err
}
resp, err := session.SendRequest(req)
if err != nil {
return err
}
spinner.Stop()
// decode
var res respBodyMetrics
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&res)
if err != nil {
return err
}
// output
fmt.Printf("Storage(KB): %d\n", common.SizeInKB(res.Body.Overall.Storage))
fmt.Printf("Records: %d\n", res.Body.Overall.NumDocs)
// table
var callCount int64
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Date", "API Calls"})
latest := res.Body.Month.Buckets[common.Max(0, len(res.Body.Month.Buckets)-30):len(res.Body.Month.Buckets)]
for _, bucket := range latest {
if bucket.APICalls["value"] == "0" {
continue
}
table.Append([]string{
getHumanDate(bucket.DateAsStr), common.JSONNumberToString(bucket.APICalls["value"]),
})
callCount = callCount + common.JSONNumberToInt(bucket.APICalls["value"])
}
table.SetFooter([]string{"Total",
strconv.FormatInt(callCount, 10),
})
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.Render()
return nil
}
// ShowAppPerms ...
func ShowAppPerms(app string) error {
spinner.StartText("Fetching app credentials")
fmt.Println()
permissions, err := GetAppPerms(app)
if err != nil {
return err
}
spinner.Stop()
// output
for _, p := range permissions {
fmt.Printf("%s%s:%s\n", common.ColonPad(p.Description, 20),
p.Username, p.Password)
}
return nil
}
// GetAppPerms ...
func GetAppPerms(app string) ([]Permission, error) {
req, err := http.NewRequest("GET", common.AccAPIURL+"/app/"+app+"/permissions", nil)
if err != nil {
return nil, err
}
resp, err := session.SendRequest(req)
if err != nil {
return nil, err
}
// decode
var res respBodyPerms
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&res)
if err != nil {
return nil, err
}
return res.Body, nil
}
func getHumanDate(date string) string {
t, err := time.Parse("2006-01-02", date[0:10])
if err != nil {
return date
}
return t.Format("2006-Jan-02")
}