-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsummary.go
291 lines (275 loc) · 7.71 KB
/
summary.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
package main
import (
"encoding/json"
"fmt"
"github.com/briandowns/spinner"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"os"
"time"
)
func GetAccountSummary(*cli.Context) error {
_ = PromptEnvDetails()
url := GetUrl(migrationReq.Environment, MigratorService, "discover/summary/async", migrationReq.Account)
return handleSummary(url)
}
func GetAppSummary(*cli.Context) error {
_ = PromptEnvDetails()
if len(migrationReq.AppId) == 0 {
migrationReq.AppId = TextInput("Please provide the application ID - ")
}
url := GetUrlWithQueryParams(migrationReq.Environment, MigratorService, "discover/summary/async", map[string]string{
AccountIdentifier: migrationReq.Account,
"appId": migrationReq.AppId,
})
return handleSummary(url)
}
func handleSummary(url string) error {
resp, err := Get(url, migrationReq.Auth)
if err != nil {
log.Fatal("Failed to fetch account summary", err)
}
resource, err := getResource(resp.Resource)
if err != nil || len(resource.RequestId) == 0 {
log.Fatal("Failed to fetch account summary", err)
return err
}
reqId := resource.RequestId
log.Infof("The request id is - %s", reqId)
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Processing"
s.Start()
for {
time.Sleep(time.Second)
url := GetUrlWithQueryParams(migrationReq.Environment, MigratorService, "discover/summary/async-result", map[string]string{
AccountIdentifier: migrationReq.Account,
"requestId": reqId,
})
resp, err := Get(url, migrationReq.Auth)
if err != nil {
s.Stop()
log.Fatal("Failed to fetch account summary", err)
}
resource, err := getResource(resp.Resource)
if err != nil {
s.Stop()
log.Fatal("Failed to fetch account summary", err)
}
if resource.Status == "ERROR" {
s.Stop()
log.Fatal("Failed to fetch account summary", err)
}
if resource.Status == "DONE" {
s.Stop()
summary, err := getSummary(resource)
if err != nil {
s.Stop()
log.Fatal("Failed to fetch account summary", err)
}
renderSummary(summary.Summary)
break
}
}
return nil
}
func getResource(data interface{}) (resource Resource, err error) {
byteData, err := json.Marshal(data)
if err != nil {
return
}
err = json.Unmarshal(byteData, &resource)
if err != nil {
return
}
return
}
func getSummary(resource Resource) (summary SummaryResponse, err error) {
byteData, err := json.Marshal(resource.ResponsePayload)
if err != nil {
return
}
err = json.Unmarshal(byteData, &summary)
if err != nil {
return
}
return
}
func renderSummary(summary map[string]EntitySummary) {
if len(summary["ACCOUNT"].Name) > 0 {
fmt.Printf("Account Name - %s\n\n", summary["ACCOUNT"].Name)
}
renderTable("Summary", map[string]interface{}{
"Applications": summary[Application].Count,
"Pipelines": summary[Pipeline].Count,
"Workflows": summary[Workflow].Count,
"Environments": summary[Environment].Count,
"Services": summary[Service].Count,
"Connectors": summary[Connector].Count,
"Secrets": summary[Secret].Count,
})
for k, v := range summary {
switch k {
case Infrastructure:
renderMultipleSummaries(k, 2, v.Count, []SubSummary{{
Title: "Deployment Types",
Data: mapToArray(v.DeploymentTypeSummary),
}, {
Title: "Cloud Providers",
Data: mapToArray(v.CloudProviderTypeSummary),
}})
case Workflow:
renderMultipleSummaries(k, 3, v.Count, []SubSummary{{
Title: "Workflow Types",
Data: mapToArrayWithDefaultStatus(v.TypeSummary, "SUPPORTED"),
}, {
Title: "Steps",
Data: toArray(v.StepsSummary),
}})
case Service:
renderMultipleSummaries(k, 3, v.Count, []SubSummary{{
Title: "Service Types",
Data: toArray(v.DeploymentsSummary),
}, {
Title: "Artifact Types",
Data: toArray(v.ArtifactsSummary),
}})
case ApplicationManifest:
renderMultipleSummaries(k, 2, v.Count, []SubSummary{{
Title: "Types",
Data: mapToArray(v.KindSummary),
}, {
Title: "Store",
Data: mapToArray(v.StoreSummary),
}})
case Connector:
v.Count = v.Count - v.TypesSummary["STRING"].Count
delete(v.TypeSummary, "STRING")
delete(v.TypesSummary, "STRING")
renderSummaryWithCountAndStatus(k, v.Count, v.TypesSummary)
case Account:
case Application:
case Pipeline:
case Secret:
case Environment:
case WorkflowExecution:
default:
renderSummaryWithCount(k, v.Count, v.TypeSummary)
}
}
}
func renderMultipleSummaries(title string, cols int, count int64, summaries []SubSummary) {
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
t := table.NewWriter()
header := fmt.Sprintf("%s (%d)", title, count)
t.AppendHeader(makeArray(cols, header), rowConfigAutoMerge)
for _, summary := range summaries {
if len(summary.Data) > 0 {
var rows []table.Row
for _, v := range summary.Data {
rows = append(rows, v)
}
t.SetOutputMirror(os.Stdout)
t.AppendRow(makeArray(cols, summary.Title), rowConfigAutoMerge)
t.AppendSeparator()
t.AppendRows(rows)
t.AppendSeparator()
t.SetStyle(table.StyleLight)
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, AlignHeader: text.AlignCenter},
})
}
}
t.Render()
}
func renderSummaryWithCount(title string, count int64, data map[string]int64) {
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
if len(data) > 0 {
t := table.NewWriter()
header := fmt.Sprintf("%s (%d)", title, count)
t.AppendHeader(table.Row{header, header}, rowConfigAutoMerge)
var rows []table.Row
for k, v := range data {
rows = append(rows, table.Row{k, v})
}
t.SetOutputMirror(os.Stdout)
t.AppendRows(rows)
t.AppendSeparator()
t.SetStyle(table.StyleLight)
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, AlignHeader: text.AlignCenter},
})
t.Render()
}
}
func renderSummaryWithCountAndStatus(title string, count int64, data map[string]SummaryDetails) {
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
if len(data) > 0 {
t := table.NewWriter()
header := fmt.Sprintf("%s (%d)", title, count)
t.AppendHeader(table.Row{header, header, header}, rowConfigAutoMerge)
var rows []table.Row
for k, v := range data {
rows = append(rows, table.Row{k, v.Count, v.Status})
}
t.SetOutputMirror(os.Stdout)
t.AppendRows(rows)
t.AppendSeparator()
t.SetStyle(table.StyleLight)
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, AlignHeader: text.AlignCenter},
})
t.Render()
}
}
func renderTable(title string, data map[string]interface{}) {
rowConfigAutoMerge := table.RowConfig{AutoMerge: true}
if len(data) > 0 {
var rows []table.Row
for k, v := range data {
rows = append(rows, table.Row{k, v})
}
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{title, title}, rowConfigAutoMerge)
t.AppendRows(rows)
t.AppendSeparator()
t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, AlignHeader: text.AlignCenter},
})
t.SetStyle(table.StyleLight)
t.Render()
}
}
func mapToArray(dict map[string]int64) [][]interface{} {
var data [][]interface{}
for k, v := range dict {
data = append(data, []interface{}{k, v})
}
return data
}
func mapToArrayWithDefaultStatus(dict map[string]int64, status string) [][]interface{} {
var data [][]interface{}
for k, v := range dict {
data = append(data, []interface{}{k, v, status})
}
return data
}
type SubSummary struct {
Title string
Data [][]interface{}
}
func makeArray(size int, defaultValue interface{}) []interface{} {
var data []interface{}
for i := 0; i < size; i++ {
data = append(data, defaultValue)
}
return data
}
func toArray(summaries map[string]SummaryDetails) (data [][]interface{}) {
for k, v := range summaries {
data = append(data, []interface{}{k, v.Count, v.Status})
}
return
}