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

Add environment status to "envs ls" #108

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add environment status to "envs ls" command
  • Loading branch information
cmoog committed Aug 31, 2020
commit 648f647965e375c1090700b095b934db095cf62a
54 changes: 42 additions & 12 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (

// Environment describes a Coder environment
type Environment struct {
ID string `json:"id" tab:"-"`
Name string `json:"name"`
ImageID string `json:"image_id" tab:"-"`
ImageTag string `json:"image_tag"`
OrganizationID string `json:"organization_id" tab:"-"`
UserID string `json:"user_id" tab:"-"`
LastBuiltAt time.Time `json:"last_built_at" tab:"-"`
CPUCores float32 `json:"cpu_cores"`
MemoryGB int `json:"memory_gb"`
DiskGB int `json:"disk_gb"`
GPUs int `json:"gpus"`
Updating bool `json:"updating"`
ID string `json:"id" tab:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It would be neat to align the tags.

Name string `json:"name"`
ImageID string `json:"image_id" tab:"-"`
ImageTag string `json:"image_tag"`
OrganizationID string `json:"organization_id" tab:"-"`
UserID string `json:"user_id" tab:"-"`
LastBuiltAt time.Time `json:"last_built_at" tab:"-"`
CPUCores float32 `json:"cpu_cores"`
MemoryGB int `json:"memory_gb"`
DiskGB int `json:"disk_gb"`
GPUs int `json:"gpus"`
Updating bool `json:"updating"`
LatestStat EnvironmentStat `json:"latest_stat" tab:"Status"`
RebuildMessages []struct {
Text string `json:"text"`
Required bool `json:"required"`
Expand All @@ -34,6 +35,35 @@ type Environment struct {
AutoOffThreshold xjson.Duration `json:"auto_off_threshold" tab:"-"`
}

// EnvironmentStat represents the state of an environment
type EnvironmentStat struct {
Time time.Time `json:"time"`
LastOnline time.Time `json:"last_online"`
ContainerStatus EnvironmentStatus `json:"container_status"`
StatError string `json:"stat_error"`
CPUUsage float32 `json:"cpu_usage"`
MemoryTotal int64 `json:"memory_total"`
MemoryUsage float32 `json:"memory_usage"`
DiskTotal int64 `json:"disk_total"`
DiskUsed int64 `json:"disk_used"`
}

func (e EnvironmentStat) String() string {
return string(e.ContainerStatus)
}

// EnvironmentStatus refers to the states of an environment.
type EnvironmentStatus string

// The following represent the possible environment container states
const (
EnvironmentCreating EnvironmentStatus = "CREATING"
EnvironmentOff EnvironmentStatus = "OFF"
EnvironmentOn EnvironmentStatus = "ON"
EnvironmentFailed EnvironmentStatus = "FAILED"
EnvironmentUnknown EnvironmentStatus = "UNKNOWN"
)

// CreateEnvironmentRequest is used to configure a new environment
type CreateEnvironmentRequest struct {
Name string `json:"name"`
Expand Down
12 changes: 10 additions & 2 deletions internal/x/xtabwriter/tabwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func StructValues(data interface{}) string {
if shouldHideField(v.Type().Field(i)) {
continue
}
s.WriteString(fmt.Sprintf("%v\t", v.Field(i).Interface()))
fmt.Fprintf(s, "%v\t", v.Field(i).Interface())
}
return s.String()
}
Expand All @@ -41,7 +41,7 @@ func StructFieldNames(data interface{}) string {
if shouldHideField(field) {
continue
}
s.WriteString(fmt.Sprintf("%s\t", field.Name))
fmt.Fprintf(s, "%s\t", fieldName(field))
}
return s.String()
}
Expand Down Expand Up @@ -72,6 +72,14 @@ func WriteTable(length int, each func(i int) interface{}) error {
return nil
}

func fieldName(f reflect.StructField) string {
custom, ok := f.Tag.Lookup(structFieldTagKey)
if ok {
return custom
}
return f.Name
}

func shouldHideField(f reflect.StructField) bool {
return f.Tag.Get(structFieldTagKey) == "-"
}