feat(cli): add daily_cost to `coder ls` (#9200)

This commit is contained in:
Ammar Bandukwala 2023-08-19 12:56:08 -05:00 committed by GitHub
parent e95557890a
commit 6b8102cf4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 4 deletions

View File

@ -30,6 +30,7 @@ type workspaceListRow struct {
Outdated bool `json:"-" table:"outdated"`
StartsAt string `json:"-" table:"starts at"`
StopsAfter string `json:"-" table:"stops after"`
DailyCost string `json:"-" table:"daily cost"`
}
func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]codersdk.User, workspace codersdk.Workspace) workspaceListRow {
@ -68,6 +69,7 @@ func workspaceListRowFromWorkspace(now time.Time, usersByID map[uuid.UUID]coders
Outdated: workspace.Outdated,
StartsAt: autostartDisplay,
StopsAfter: autostopDisplay,
DailyCost: strconv.Itoa(int(workspace.LatestBuild.DailyCost)),
}
}
@ -78,7 +80,19 @@ func (r *RootCmd) list() *clibase.Cmd {
searchQuery string
displayWorkspaces []workspaceListRow
formatter = cliui.NewOutputFormatter(
cliui.TableFormat([]workspaceListRow{}, nil),
cliui.TableFormat(
[]workspaceListRow{},
[]string{
"workspace",
"template",
"status",
"healthy",
"last built",
"outdated",
"starts at",
"stops after",
},
),
cliui.JSONFormat(),
)
)

View File

@ -11,7 +11,7 @@ Aliases: ls
-c, --column string-array (default: workspace,template,status,healthy,last built,outdated,starts at,stops after)
Columns to display in table output. Available columns: workspace,
template, status, healthy, last built, outdated, starts at, stops
after.
after, daily cost.
-o, --output string (default: table)
Output format. Available formats: table, json.

2
docs/cli/list.md generated
View File

@ -31,7 +31,7 @@ Specifies whether all workspaces will be listed or not.
| Type | <code>string-array</code> |
| Default | <code>workspace,template,status,healthy,last built,outdated,starts at,stops after</code> |
Columns to display in table output. Available columns: workspace, template, status, healthy, last built, outdated, starts at, stops after.
Columns to display in table output. Available columns: workspace, template, status, healthy, last built, outdated, starts at, stops after, daily cost.
### -o, --output

View File

@ -404,7 +404,7 @@ func (p *Server) acquireJob(ctx context.Context) {
runner.Options{
Updater: p,
QuotaCommitter: p,
Logger: p.opts.Logger,
Logger: p.opts.Logger.Named("runner"),
Filesystem: p.opts.Filesystem,
WorkDirectory: p.opts.WorkDirectory,
Provisioner: provisioner,

View File

@ -964,6 +964,9 @@ func (r *Runner) buildWorkspace(ctx context.Context, stage string, req *sdkproto
}
func (r *Runner) commitQuota(ctx context.Context, resources []*sdkproto.Resource) *proto.FailedJob {
r.logger.Debug(ctx, "committing quota",
slog.F("resources", resources),
)
cost := sumDailyCost(resources)
if cost == 0 {
return nil