Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit de3b7a6

Browse files
committed
Add environment status to "envs ls table"
1 parent 11d24a7 commit de3b7a6

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

coder-sdk/env.go

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ import (
1111

1212
// Environment describes a Coder environment
1313
type Environment struct {
14-
ID string `json:"id" tab:"-"`
15-
Name string `json:"name"`
16-
ImageID string `json:"image_id" tab:"-"`
17-
ImageTag string `json:"image_tag"`
18-
OrganizationID string `json:"organization_id" tab:"-"`
19-
UserID string `json:"user_id" tab:"-"`
20-
LastBuiltAt time.Time `json:"last_built_at" tab:"-"`
21-
CPUCores float32 `json:"cpu_cores"`
22-
MemoryGB int `json:"memory_gb"`
23-
DiskGB int `json:"disk_gb"`
24-
GPUs int `json:"gpus"`
25-
Updating bool `json:"updating"`
14+
ID string `json:"id" tab:"-"`
15+
Name string `json:"name"`
16+
ImageID string `json:"image_id" tab:"-"`
17+
ImageTag string `json:"image_tag"`
18+
OrganizationID string `json:"organization_id" tab:"-"`
19+
UserID string `json:"user_id" tab:"-"`
20+
LastBuiltAt time.Time `json:"last_built_at" tab:"-"`
21+
CPUCores float32 `json:"cpu_cores"`
22+
MemoryGB int `json:"memory_gb"`
23+
DiskGB int `json:"disk_gb"`
24+
GPUs int `json:"gpus"`
25+
Updating bool `json:"updating"`
26+
LatestStat EnvironmentStat `json:"latest_stat" tab:"Status"`
2627
RebuildMessages []struct {
2728
Text string `json:"text"`
2829
Required bool `json:"required"`
@@ -34,6 +35,33 @@ type Environment struct {
3435
AutoOffThreshold xjson.Duration `json:"auto_off_threshold" tab:"-"`
3536
}
3637

38+
type EnvironmentStat struct {
39+
Time time.Time `json:"time"`
40+
LastOnline time.Time `json:"last_online"`
41+
ContainerStatus EnvironmentStatus `json:"container_status"`
42+
StatError string `json:"stat_error"`
43+
CPUUsage float32 `json:"cpu_usage"`
44+
MemoryTotal int64 `json:"memory_total"`
45+
MemoryUsage float32 `json:"memory_usage"`
46+
DiskTotal int64 `json:"disk_total"`
47+
DiskUsed int64 `json:"disk_used"`
48+
}
49+
50+
func (e EnvironmentStat) String() string {
51+
return string(e.ContainerStatus)
52+
}
53+
54+
// EnvironmentStatus refers to the states of an environment.
55+
type EnvironmentStatus string
56+
57+
const (
58+
EnvironmentCreating EnvironmentStatus = "CREATING"
59+
EnvironmentOff EnvironmentStatus = "OFF"
60+
EnvironmentOn EnvironmentStatus = "ON"
61+
EnvironmentFailed EnvironmentStatus = "FAILED"
62+
EnvironmentUnknown EnvironmentStatus = "UNKNOWN"
63+
)
64+
3765
// CreateEnvironmentRequest is used to configure a new environment
3866
type CreateEnvironmentRequest struct {
3967
Name string `json:"name"`

internal/x/xtabwriter/tabwriter.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func StructFieldNames(data interface{}) string {
4141
if shouldHideField(field) {
4242
continue
4343
}
44-
s.WriteString(fmt.Sprintf("%s\t", field.Name))
44+
s.WriteString(fmt.Sprintf("%s\t", fieldName(field)))
4545
}
4646
return s.String()
4747
}
@@ -72,6 +72,14 @@ func WriteTable(length int, each func(i int) interface{}) error {
7272
return nil
7373
}
7474

75+
func fieldName(f reflect.StructField) string {
76+
custom, ok := f.Tag.Lookup(structFieldTagKey)
77+
if ok {
78+
return custom
79+
}
80+
return f.Name
81+
}
82+
7583
func shouldHideField(f reflect.StructField) bool {
7684
return f.Tag.Get(structFieldTagKey) == "-"
7785
}

0 commit comments

Comments
 (0)