-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathusage.go
More file actions
101 lines (89 loc) · 2.81 KB
/
Copy pathusage.go
File metadata and controls
101 lines (89 loc) · 2.81 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
package usage
import (
"fmt"
"time"
"github.com/duneanalytics/cli/cmdutil"
"github.com/duneanalytics/cli/output"
"github.com/duneanalytics/duneapi-client-go/models"
"github.com/spf13/cobra"
)
// NewUsageCmd returns the top-level "usage" command.
func NewUsageCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "usage",
Short: "Show credit and resource usage for the authenticated Dune account",
Long: "Show current-period credit usage, storage consumption, and billing period\n" +
"boundaries for the authenticated user or team.\n\n" +
"Displays:\n" +
" - Private queries and dashboards count\n" +
" - Storage bytes used vs. allowed\n" +
" - Credits used and included per billing period\n\n" +
"Optionally filter by date range with --start-date and --end-date.\n\n" +
"Examples:\n" +
" dune usage\n" +
" dune usage --start-date 2025-01-01 --end-date 2025-06-01\n" +
" dune usage --output json",
RunE: runUsage,
}
cmd.Flags().String("start-date", "", "filter usage from this date (YYYY-MM-DD format)")
cmd.Flags().String("end-date", "", "filter usage up to this date (YYYY-MM-DD format)")
output.AddFormatFlag(cmd, "text")
return cmd
}
func runUsage(cmd *cobra.Command, _ []string) error {
client := cmdutil.ClientFromCmd(cmd)
startDate, _ := cmd.Flags().GetString("start-date")
endDate, _ := cmd.Flags().GetString("end-date")
if err := validateDateFlag(startDate, "start-date"); err != nil {
return err
}
if err := validateDateFlag(endDate, "end-date"); err != nil {
return err
}
var (
resp *models.UsageResponse
err error
)
if startDate != "" || endDate != "" {
resp, err = client.GetUsageForDates(startDate, endDate)
} else {
resp, err = client.GetUsage()
}
if err != nil {
return err
}
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
default:
fmt.Fprintf(w, "Private Queries: %d\n", resp.PrivateQueries)
fmt.Fprintf(w, "Private Dashboards: %d\n", resp.PrivateDashboards)
fmt.Fprintf(w, "Storage Used: %s / %s\n",
output.FormatBytes(resp.BytesUsed), output.FormatBytes(resp.BytesAllowed))
if len(resp.BillingPeriods) > 0 {
fmt.Fprintln(w)
columns := []string{"START DATE", "END DATE", "CREDITS USED", "CREDITS INCLUDED"}
rows := make([][]string, len(resp.BillingPeriods))
for i, bp := range resp.BillingPeriods {
rows[i] = []string{
bp.StartDate,
bp.EndDate,
fmt.Sprintf("%.2f", bp.CreditsUsed),
fmt.Sprintf("%d", bp.CreditsIncluded),
}
}
output.PrintTable(w, columns, rows)
}
return nil
}
}
func validateDateFlag(value, name string) error {
if value == "" {
return nil
}
if _, err := time.Parse("2006-01-02", value); err != nil {
return fmt.Errorf("invalid --%s: expected YYYY-MM-DD format", name)
}
return nil
}