-
Notifications
You must be signed in to change notification settings - Fork 9
/
console.go
222 lines (188 loc) · 4.99 KB
/
console.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package renderwait
import (
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
"time"
"github.com/kr/text"
"github.com/morikuni/aec"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/text/timefmt"
"namespacelabs.dev/foundation/schema/orchestration"
)
const minWaitTime = 1 * time.Millisecond
type consRenderer struct {
ch chan *orchestration.Event
done chan struct{}
flushLog io.Writer
setSticky func(string)
}
type blockState struct {
Category string
Title string
Stage orchestration.Event_Stage
AlreadyExisted bool
StartTime time.Time
CommittedTime time.Time
EndTime time.Time
WaitStatus []*orchestration.Event_WaitStatus
WaitDetails string
}
func (rwb consRenderer) Ch() chan *orchestration.Event { return rwb.ch }
func (rwb consRenderer) Wait(ctx context.Context) error {
select {
case <-rwb.done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (rwb consRenderer) Loop(ctx context.Context) {
defer close(rwb.done) // Signal parent we're done.
resourceState := map[string]*blockState{} // Key: ResourceId
ids := []string{}
for {
select {
case <-ctx.Done():
return
case ev, ok := <-rwb.ch:
if !ok {
fmt.Fprint(rwb.flushLog, render(resourceState, ids, true))
rwb.setSticky("")
return
}
stage := ev.Stage
// Backwards compatibility.
if stage == orchestration.Event_UNKNOWN_STAGE {
if ev.Ready == orchestration.Event_READY {
stage = orchestration.Event_DONE
} else {
stage = orchestration.Event_WAITING
}
}
var timestamp time.Time
if ev.Timestamp != nil {
timestamp = ev.Timestamp.AsTime()
} else {
timestamp = time.Now()
}
if _, has := resourceState[ev.ResourceId]; !has {
ids = append(ids, ev.ResourceId)
sort.Strings(ids)
resourceState[ev.ResourceId] = &blockState{
Category: ev.Category,
Title: title(ev),
Stage: stage,
StartTime: timestamp,
}
}
resourceState[ev.ResourceId].AlreadyExisted = ev.AlreadyExisted
if stage >= orchestration.Event_COMMITTED && resourceState[ev.ResourceId].CommittedTime.IsZero() {
resourceState[ev.ResourceId].CommittedTime = timestamp
}
if stage >= orchestration.Event_DONE && resourceState[ev.ResourceId].EndTime.IsZero() {
resourceState[ev.ResourceId].EndTime = timestamp
}
resourceState[ev.ResourceId].Stage = stage
resourceState[ev.ResourceId].WaitStatus = ev.WaitStatus
if stage != orchestration.Event_DONE && ev.WaitDetails != "" {
resourceState[ev.ResourceId].WaitDetails = ev.WaitDetails
}
rwb.setSticky(render(resourceState, ids, false))
}
}
}
func title(ev *orchestration.Event) string {
title := ev.ResourceLabel
if title == "" {
title = ev.Scope
if title == "" {
title = ev.ResourceId
}
}
return title
}
func render(m map[string]*blockState, ids []string, flush bool) string {
var b bytes.Buffer
if flush {
fmt.Fprintln(&b)
}
cats := map[string]struct{}{}
for _, bs := range m {
cats[bs.Category] = struct{}{}
}
sortedCats := maps.Keys(cats)
slices.Sort(sortedCats)
perCat := map[string][]*blockState{}
for _, id := range ids {
x := m[id]
perCat[x.Category] = append(perCat[x.Category], x)
}
for k, cat := range sortedCats {
if k > 0 {
fmt.Fprintln(&b)
}
blocks := perCat[cat]
if len(blocks) == 0 {
continue
}
fmt.Fprintf(&b, " %s:\n\n", cat)
for _, blk := range blocks {
var ready bool
var took string
if blk.AlreadyExisted && blk.Stage != orchestration.Event_DONE {
took = box("Waiting for previous deployment ...", mergeWaitStatus(blk.WaitStatus))
} else if blk.AlreadyExisted {
ready = true
took = "(no update required)"
} else if blk.Stage == orchestration.Event_DONE {
ready = true
took = fmt.Sprintf("took %v", timefmt.Format(blk.EndTime.Sub(blk.CommittedTime)))
if d := blk.CommittedTime.Sub(blk.StartTime); d >= minWaitTime {
took += fmt.Sprintf(" (waited %v)", timefmt.Format(d))
}
} else if blk.Stage == orchestration.Event_RUNNING {
took = "Running"
} else {
took = mergeWaitStatus(blk.WaitStatus)
if took == "" {
took = "Waiting..."
}
}
fmt.Fprintf(&b, " %s %s %s\n", icon(ready), blk.Title, aec.LightBlackF.Apply(took))
if details := blk.WaitDetails; !ready && details != "" {
fmt.Fprint(text.NewIndentWriter(&b, []byte(" ")), details)
}
}
}
if flush {
fmt.Fprintln(&b)
}
return b.String()
}
func icon(ready bool) string {
if ready {
return "[✓]"
}
return "[ ]"
}
func mergeWaitStatus(status []*orchestration.Event_WaitStatus) string {
var st []string
for _, s := range status {
st = append(st, s.Description)
}
return strings.Join(st, "; ")
}
func box(a, b string) string {
if b == "" {
return a
}
return fmt.Sprintf("%s (%s)", a, b)
}