/
instancespage.go
128 lines (103 loc) · 3.57 KB
/
instancespage.go
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
package pages
import (
"fmt"
"strings"
"github.com/swartzrock/ecsview/cmd/ecsview"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/thoas/go-funk"
"github.com/swartzrock/ecsview/cmd/aws"
"github.com/swartzrock/ecsview/cmd/ui"
"github.com/swartzrock/ecsview/cmd/utils"
)
var latestEcsAgentVersion *string
// Returns a page that displays the container instances in a cluster
func NewInstancesPage() *ClusterDetailsPage {
instancesTable := tview.NewTable()
instancesTable.
SetBorders(true).
SetBorder(true).
SetTitle(" 📦 ECS Instances ")
instancesTableInfo := &ui.TableInfo{
Table: instancesTable,
Alignment: []int{ui.L, ui.L, ui.L, ui.L, ui.L, ui.L, ui.L, ui.L, ui.L, ui.L},
Expansions: []int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Selectable: true,
}
ui.AddTableConfigData(instancesTableInfo, 0, [][]string{
{"#", "Instance Id ▾", "Status", "Type", "ECS Agent", "Registered", "Tasks", "CPU", "Memory"},
}, tcell.ColorYellow)
return &ClusterDetailsPage{
"Instances",
instancesTableInfo,
instancesPageRenderer(instancesTableInfo),
}
}
func instancesPageRenderer(tableInfo *ui.TableInfo) func(*ecsview.ClusterData) {
return func(e *ecsview.ClusterData) {
renderInstancesTable(tableInfo, e)
}
}
func renderInstancesTable(tableInfo *ui.TableInfo, ecsData *ecsview.ClusterData) {
if latestEcsAgentVersion == nil {
latestEcsAgentVersion, _ = aws.GetLatestECSAgentVersion()
}
ui.TruncTableRows(tableInfo.Table, 1)
if len(ecsData.Containers) == 0 {
return
}
data := funk.Map(ecsData.Containers, func(instance *aws.EcsContainer) []string {
agentVersion := *instance.VersionInfo.AgentVersion
if latestEcsAgentVersion == nil {
agentVersion = agentVersion + " ❓"
} else if agentVersion == *latestEcsAgentVersion {
agentVersion = agentVersion + " ✅"
} else {
agentVersion = agentVersion + " ⚠️"
}
meterWidth := 5
cpuMeter := ""
memoryMeter := ""
usage := instance.GetStats()
if usage != nil {
cpuMeter = utils.BuildAsciiMeterCurrentTotal(usage.CpuUsed, usage.CpuTotal, meterWidth)
memoryMeter = utils.BuildAsciiMeterCurrentTotal(usage.MemoryUsed, usage.MemoryTotal, meterWidth)
}
instanceType := "n/a"
instanceTypeAttribute := instance.GetAttribute("ecs.instance-type")
if instanceTypeAttribute != nil {
instanceType = *instanceTypeAttribute
}
taskCount := utils.I64ToString(*instance.RunningTasksCount)
if *instance.PendingTasksCount > 0 {
taskCount = fmt.Sprintf("%s (%d pending)", taskCount, *instance.PendingTasksCount)
}
if taskCount != "0" {
tasks := make([]string, 0)
for _, task := range ecsData.Tasks {
if task.ContainerInstanceArn != nil && *task.ContainerInstanceArn == *instance.ContainerInstanceArn {
tasks = append(tasks, aws.ShortenTaskDefArn(task.TaskDefinitionArn))
}
}
instanceTasks := strings.Join(tasks, ",")
taskCount = fmt.Sprintf("%s: %s", taskCount, utils.TakeLeft(instanceTasks, 40))
}
return []string{
*instance.Ec2InstanceId,
utils.LowerTitle(*instance.Status),
instanceType,
agentVersion,
utils.FormatLocalDate(*instance.RegisteredAt),
taskCount,
cpuMeter,
memoryMeter,
}
}).([][]string)
data = PrependRowNumColumn(data)
ui.AddTableConfigData(tableInfo, 1, data, tcell.ColorWhite)
instanceIdStyle := tcell.StyleDefault.Bold(true).Foreground(tcell.ColorWhite)
ui.SetColumnStyle(tableInfo.Table, 1, 1, instanceIdStyle)
usageMeterStyle := tcell.StyleDefault.Foreground(tcell.ColorDarkCyan)
ui.SetColumnStyle(tableInfo.Table, 7, 1, usageMeterStyle)
ui.SetColumnStyle(tableInfo.Table, 8, 1, usageMeterStyle)
}