forked from reanahub/reana-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquota_show.go
More file actions
378 lines (334 loc) · 8.92 KB
/
Copy pathquota_show.go
File metadata and controls
378 lines (334 loc) · 8.92 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
This file is part of REANA.
Copyright (C) 2022, 2025, 2026 CERN.
REANA is free software; you can redistribute it and/or modify it
under the terms of the MIT License; see LICENSE file for more details.
*/
package cmd
import (
"encoding/json"
"fmt"
"io"
"reanahub/reana-client-go/client"
"reanahub/reana-client-go/client/operations"
"reanahub/reana-client-go/pkg/config"
"reanahub/reana-client-go/pkg/displayer"
"reanahub/reana-client-go/pkg/validator"
"strings"
"time"
"github.com/jedib0t/go-pretty/v6/text"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const quotaShowDesc = `
Show user quota.
The ` + "``quota-show``" + ` command displays quota usage for the user.
Examples:
$ reana-client quota-show --resource disk --report limit
$ reana-client quota-show --resource disk --report usage
$ reana-client quota-show --resource disk
$ reana-client quota-show --resources
`
type quotaResource struct {
Health string `json:"health"`
Limit *quotaResourceStat `json:"limit"`
QuotaPeriodMonths *int64 `json:"quota_period_months"`
QuotaPeriodStartAt *string `json:"quota_period_start_at"`
Usage *quotaResourceStat `json:"usage"`
Stats map[string]quotaResourceStat `json:"-"`
}
type quotaResourceStat struct {
HumanReadable string `json:"human_readable"`
Raw float64 `json:"raw"`
}
type quotaShowOptions struct {
token string
report string
resource string
showResources bool
humanReadable bool
unspecifiedReport bool
}
type quotaPeriodInfo struct {
PeriodMonths *int64
StartDate string
EndDate string
}
// newQuotaShowCmd creates a command to show user quota.
func newQuotaShowCmd() *cobra.Command {
o := "aShowOptions{}
cmd := &cobra.Command{
Use: "quota-show",
Short: "Show user quota.",
Long: quotaShowDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := validator.ValidateAtLeastOne(
cmd.Flags(), []string{"resource", "resources"},
); err != nil {
return fmt.Errorf("%s\n%s", err.Error(), cmd.UsageString())
}
if cmd.Flags().Changed("report") {
if err := validator.ValidateChoice(
o.report, config.QuotaReports, "report",
); err != nil {
return err
}
} else {
o.unspecifiedReport = true
}
return o.run(cmd)
},
}
f := cmd.Flags()
f.StringVarP(
&o.token,
"access-token",
"t",
"",
"Access token of the current user.",
)
f.StringVarP(
&o.report,
"report",
"",
"",
"Specify quota report type. e.g. limit, usage.",
)
f.StringVarP(
&o.resource,
"resource",
"",
"",
"Specify quota resource. e.g. disk, memory.",
)
f.BoolVarP(
&o.showResources,
"resources",
"",
false,
"Print available resources.",
)
f.BoolVarP(
&o.humanReadable,
"human-readable",
"h",
false,
"Show disk size in human readable format.",
)
// Remove -h shorthand
cmd.PersistentFlags().BoolP("help", "", false, "Help for quota-show")
return cmd
}
func (o *quotaShowOptions) run(cmd *cobra.Command) error {
quotaParams := operations.NewGetYouParams()
quotaParams.SetAccessToken(&o.token)
api, err := client.ApiClient()
if err != nil {
return err
}
quotaResp, err := api.Operations.GetYou(quotaParams)
if err != nil {
return err
}
quotaResources, err := parseQuotaInfo(quotaResp.Payload.Quota)
if err != nil {
return err
}
var availableResources []string
for resourceName := range quotaResources {
availableResources = append(availableResources, resourceName)
}
if o.showResources {
cmd.Println(strings.Join(availableResources, "\n"))
return nil
}
resource, isValidResource := quotaResources[o.resource]
if !isValidResource {
return fmt.Errorf(
"resource '%s' is not valid\nAvailable resources are '%s'",
o.resource,
strings.Join(availableResources, "', '"),
)
}
report, reportExists := resource.Stats[o.report]
if o.unspecifiedReport {
displayQuotaResourceUsage(
resource.Health,
resource.Stats["usage"], resource.Stats["limit"],
formatQuotaPeriodWindow(resource),
o.humanReadable, cmd.OutOrStdout(),
)
} else if !reportExists || report.Raw <= 0 {
cmd.Printf("No %s.\n", o.report)
} else if o.humanReadable {
msg := report.HumanReadable
if o.resource == "cpu" {
if window := formatQuotaPeriodWindow(resource); window != "" {
msg = fmt.Sprintf("%s in the period from %s", msg, window)
}
}
cmd.Println(msg)
} else {
cmd.Printf("%.0f\n", report.Raw)
}
return nil
}
// NOTE: Keep this month-boundary arithmetic in sync with
// reana_db/utils.py (_add_months), reana-client's _add_months helper,
// and reana-ui's quota period window calculation.
func addMonths(dt time.Time, months int) time.Time {
year := dt.Year() + int((int(dt.Month())-1+months)/12)
month := time.Month((int(dt.Month())-1+months)%12 + 1)
lastDay := time.Date(
year,
month+1,
0,
dt.Hour(),
dt.Minute(),
dt.Second(),
dt.Nanosecond(),
dt.Location(),
).Day()
day := dt.Day()
if day > lastDay {
day = lastDay
}
return time.Date(
year,
month,
day,
dt.Hour(),
dt.Minute(),
dt.Second(),
dt.Nanosecond(),
dt.Location(),
)
}
func formatQuotaPeriodDate(dt time.Time) string {
return dt.Format("2006-01-02")
}
func getQuotaPeriodDateRange(resource quotaResource) (string, string) {
if resource.QuotaPeriodMonths == nil || resource.QuotaPeriodStartAt == nil {
return "", ""
}
startAt, err := time.Parse(time.RFC3339Nano, *resource.QuotaPeriodStartAt)
if err != nil {
log.Debugf(
"Could not parse server datetime %q as RFC3339: %v",
*resource.QuotaPeriodStartAt,
err,
)
return "", ""
}
endAt := addMonths(startAt, int(*resource.QuotaPeriodMonths))
return formatQuotaPeriodDate(startAt), formatQuotaPeriodDate(endAt)
}
func formatQuotaPeriodWindow(resource quotaResource) string {
startDate, endDate := getQuotaPeriodDateRange(resource)
if startDate == "" || endDate == "" {
return ""
}
return fmt.Sprintf("%s to %s", startDate, endDate)
}
func buildCPUQuotaPeriodInfo(
quotaResources map[string]quotaResource,
) quotaPeriodInfo {
cpuResource, ok := quotaResources["cpu"]
if !ok {
return quotaPeriodInfo{}
}
periodMonths := int64(0)
if cpuResource.QuotaPeriodMonths != nil &&
*cpuResource.QuotaPeriodMonths > 0 {
periodMonths = *cpuResource.QuotaPeriodMonths
}
startDate, endDate := getQuotaPeriodDateRange(cpuResource)
return quotaPeriodInfo{
PeriodMonths: &periodMonths,
StartDate: startDate,
EndDate: endDate,
}
}
// displayQuotaResourceUsage displays the resource usage of the quotas, using its usage and limit.
func displayQuotaResourceUsage(
health string,
usage, limit quotaResourceStat,
periodWindow string,
humanReadable bool,
out io.Writer,
) {
var limitInfo string
var includeHealthColor bool
if limit.Raw > 0 {
percentage := fmt.Sprintf("%.0f%%", (usage.Raw/limit.Raw)*100)
if humanReadable {
limitInfo = fmt.Sprintf(
"out of %s used (%s)",
limit.HumanReadable,
percentage,
)
} else {
limitInfo = fmt.Sprintf("out of %.0f used (%s)", limit.Raw, percentage)
}
includeHealthColor = health != ""
} else {
limitInfo = "used"
includeHealthColor = false
}
color := text.Reset
if includeHealthColor {
color = displayer.ResourceHealthToColor[health]
}
var usageMsg string
if humanReadable {
usageMsg = usage.HumanReadable
} else {
usageMsg = fmt.Sprintf("%.0f", usage.Raw)
}
usageMsg = fmt.Sprintf("%s %s", usageMsg, limitInfo)
if periodWindow != "" {
// NOTE: This splice relies on the formatted usage message ending with
// a trailing parenthetical when percentage output is present.
if strings.HasSuffix(usageMsg, ")") {
usageMsg = strings.TrimSuffix(usageMsg, ")") +
fmt.Sprintf(" in the period from %s)", periodWindow)
} else {
usageMsg = fmt.Sprintf("%s in the period from %s", usageMsg, periodWindow)
}
}
usageMsg += "\n"
displayer.PrintColorable(usageMsg, out, color)
}
// parseQuotaInfo parses the quota payload to a map of quotaResource values, with the resource names as keys.
// Necessary because all the resources implement different structs in the swagger API.
func parseQuotaInfo(
quotaBody *operations.GetYouOKBodyQuota,
) (map[string]quotaResource, error) {
var rawResourcesInfo map[string]json.RawMessage
quotaBinary, err := quotaBody.MarshalBinary()
if err != nil {
return nil, err
}
err = json.Unmarshal(quotaBinary, &rawResourcesInfo)
if err != nil {
return nil, err
}
quotaResources := make(map[string]quotaResource)
for resourceName, rawInfo := range rawResourcesInfo {
var resourceInfo quotaResource
err = json.Unmarshal(rawInfo, &resourceInfo)
if err != nil {
return nil, err
}
resourceInfo.Stats = map[string]quotaResourceStat{}
if resourceInfo.Limit != nil {
resourceInfo.Stats["limit"] = *resourceInfo.Limit
}
if resourceInfo.Usage != nil {
resourceInfo.Stats["usage"] = *resourceInfo.Usage
}
quotaResources[resourceName] = resourceInfo
}
return quotaResources, nil
}