• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tensorchord / envd / 13784149040

11 Mar 2025 08:57AM UTC coverage: 42.539% (-0.02%) from 42.559%
13784149040

push

github

web-flow
feat: emb the whole template/*.envd, avoid random map key order (#1995)

Signed-off-by: Keming <kemingyang@tensorchord.ai>

14 of 24 new or added lines in 1 file covered. (58.33%)

2 existing lines in 1 file now uncovered.

5160 of 12130 relevant lines covered (42.54%)

147.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.32
/pkg/progress/progressui/printer.go
1
// Copyright 2023 The envd Authors
2
// Copyright 2023 The buildkit Authors
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//      http://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15

16
package progressui
17

18
import (
19
        "container/ring"
20
        "context"
21
        "fmt"
22
        "io"
23
        "os"
24
        "sort"
25
        "strings"
26
        "time"
27

28
        "github.com/opencontainers/go-digest"
29
        "github.com/tonistiigi/units"
30
)
31

32
const antiFlicker = 5 * time.Second
33
const maxDelay = 10 * time.Second
34
const minTimeDelta = 5 * time.Second
35
const minProgressDelta = 0.05 // %
36

37
const logsBufferSize = 10
38

39
type lastStatus struct {
40
        Current   int64
41
        Timestamp time.Time
42
}
43

44
type textMux struct {
45
        w         io.Writer
46
        current   digest.Digest
47
        last      map[string]lastStatus
48
        notFirst  bool
49
        nextIndex int
50
}
51

52
func (p *textMux) printVtx(t *trace, dgst digest.Digest) {
1,536✔
53
        if p.last == nil {
1,561✔
54
                p.last = make(map[string]lastStatus)
25✔
55
        }
25✔
56

57
        v, ok := t.byDigest[dgst]
1,536✔
58
        if !ok {
1,536✔
59
                return
×
60
        }
×
61

62
        if v.index == 0 {
2,384✔
63
                p.nextIndex++
848✔
64
                v.index = p.nextIndex
848✔
65
        }
848✔
66

67
        if dgst != p.current {
2,440✔
68
                if p.current != "" {
937✔
69
                        old := t.byDigest[p.current]
33✔
70
                        if old.logsPartial {
33✔
UNCOV
71
                                fmt.Fprintln(p.w, "")
×
UNCOV
72
                        }
×
73
                        old.logsOffset = 0
33✔
74
                        old.count = 0
33✔
75
                        fmt.Fprintf(p.w, "#%d ...\n", old.index)
33✔
76
                }
77

78
                if p.notFirst {
1,783✔
79
                        fmt.Fprintln(p.w, "")
879✔
80
                } else {
904✔
81
                        p.notFirst = true
25✔
82
                }
25✔
83

84
                if os.Getenv("PROGRESS_NO_TRUNC") == "0" {
904✔
85
                        fmt.Fprintf(p.w, "#%d %s\n", v.index, limitString(v.Name, 72))
×
86
                } else {
904✔
87
                        fmt.Fprintf(p.w, "#%d %s\n", v.index, v.Name)
904✔
88
                }
904✔
89
        }
90

91
        if len(v.events) != 0 {
1,536✔
92
                v.logsOffset = 0
×
93
        }
×
94
        for _, ev := range v.events {
1,536✔
95
                fmt.Fprintf(p.w, "#%d %s\n", v.index, ev)
×
96
        }
×
97
        v.events = v.events[:0]
1,536✔
98

1,536✔
99
        isOpenStatus := false // remote cache loading can currently produce status updates without active vertex
1,536✔
100
        for _, s := range v.statuses {
2,347✔
101
                if _, ok := v.statusUpdates[s.ID]; ok {
1,210✔
102
                        doPrint := true
399✔
103

399✔
104
                        if last, ok := p.last[s.ID]; ok && s.Completed == nil {
440✔
105
                                var progressDelta float64
41✔
106
                                if s.Total > 0 {
42✔
107
                                        progressDelta = float64(s.Current-last.Current) / float64(s.Total)
1✔
108
                                }
1✔
109
                                timeDelta := s.Timestamp.Sub(last.Timestamp)
41✔
110
                                if progressDelta < minProgressDelta && timeDelta < minTimeDelta {
81✔
111
                                        doPrint = false
40✔
112
                                }
40✔
113
                        }
114

115
                        if !doPrint {
439✔
116
                                continue
40✔
117
                        }
118

119
                        p.last[s.ID] = lastStatus{
359✔
120
                                Timestamp: s.Timestamp,
359✔
121
                                Current:   s.Current,
359✔
122
                        }
359✔
123

359✔
124
                        var bytes string
359✔
125
                        if s.Total != 0 {
425✔
126
                                bytes = fmt.Sprintf(" %.2f / %.2f", units.Bytes(s.Current), units.Bytes(s.Total))
66✔
127
                        } else if s.Current != 0 {
380✔
128
                                bytes = fmt.Sprintf(" %.2f", units.Bytes(s.Current))
21✔
129
                        }
21✔
130
                        var tm string
359✔
131
                        endTime := s.Timestamp
359✔
132
                        if s.Completed != nil {
663✔
133
                                endTime = *s.Completed
304✔
134
                        }
304✔
135
                        if s.Started != nil {
718✔
136
                                diff := endTime.Sub(*s.Started).Seconds()
359✔
137
                                if diff > 0.01 {
551✔
138
                                        tm = fmt.Sprintf(" %.1fs", diff)
192✔
139
                                }
192✔
140
                        }
141
                        if s.Completed != nil {
663✔
142
                                tm += " done"
304✔
143
                        } else {
359✔
144
                                isOpenStatus = true
55✔
145
                        }
55✔
146
                        fmt.Fprintf(p.w, "#%d %s%s%s\n", v.index, s.ID, bytes, tm)
359✔
147
                }
148
        }
149
        v.statusUpdates = map[string]struct{}{}
1,536✔
150

1,536✔
151
        for _, w := range v.warnings[v.warningIdx:] {
1,536✔
152
                fmt.Fprintf(p.w, "#%d WARN: %s\n", v.index, w.Short)
×
153
                v.warningIdx++
×
154
        }
×
155

156
        for i, l := range v.logs {
8,781✔
157
                if i == 0 {
7,703✔
158
                        l = l[v.logsOffset:]
458✔
159
                }
458✔
160
                fmt.Fprintf(p.w, "%s", l)
7,245✔
161
                if i != len(v.logs)-1 || !v.logsPartial {
14,250✔
162
                        fmt.Fprintln(p.w, "")
7,005✔
163
                }
7,005✔
164
                if v.logsBuffer == nil {
7,292✔
165
                        v.logsBuffer = ring.New(logsBufferSize)
47✔
166
                }
47✔
167
                v.logsBuffer.Value = l
7,245✔
168
                if !v.logsPartial {
9,746✔
169
                        v.logsBuffer = v.logsBuffer.Next()
2,501✔
170
                }
2,501✔
171
        }
172

173
        if len(v.logs) > 0 {
1,994✔
174
                if v.logsPartial {
698✔
175
                        v.logs = v.logs[len(v.logs)-1:]
240✔
176
                        v.logsOffset = len(v.logs[0])
240✔
177
                } else {
458✔
178
                        v.logs = nil
218✔
179
                        v.logsOffset = 0
218✔
180
                }
218✔
181
        }
182

183
        p.current = dgst
1,536✔
184
        if v.isCompleted() && !isOpenStatus {
2,407✔
185
                p.current = ""
871✔
186
                v.count = 0
871✔
187

871✔
188
                if v.Error != "" {
871✔
189
                        if v.logsPartial {
×
190
                                fmt.Fprintln(p.w, "")
×
191
                        }
×
192
                        if strings.HasSuffix(v.Error, context.Canceled.Error()) {
×
193
                                fmt.Fprintf(p.w, "#%d CANCELED\n", v.index)
×
194
                        } else {
×
195
                                fmt.Fprintf(p.w, "#%d ERROR: %s\n", v.index, v.Error)
×
196
                        }
×
197
                } else if v.Cached {
1,346✔
198
                        fmt.Fprintf(p.w, "#%d CACHED\n", v.index)
475✔
199
                } else {
871✔
200
                        tm := ""
396✔
201
                        var ivals []interval
396✔
202
                        for _, ival := range v.intervals {
831✔
203
                                ivals = append(ivals, ival)
435✔
204
                        }
435✔
205
                        ivals = mergeIntervals(ivals)
396✔
206
                        if len(ivals) > 0 {
792✔
207
                                var dt float64
396✔
208
                                for _, ival := range ivals {
831✔
209
                                        dt += ival.duration().Seconds()
435✔
210
                                }
435✔
211
                                tm = fmt.Sprintf(" %.1fs", dt)
396✔
212
                        }
213
                        fmt.Fprintf(p.w, "#%d DONE%s\n", v.index, tm)
396✔
214
                }
215
        }
216

217
        delete(t.updates, dgst)
1,536✔
218
}
219

220
func sortCompleted(t *trace, m map[digest.Digest]struct{}) []digest.Digest {
2,804✔
221
        out := make([]digest.Digest, 0, len(m))
2,804✔
222
        for k := range m {
3,676✔
223
                out = append(out, k)
872✔
224
        }
872✔
225
        sort.Slice(out, func(i, j int) bool {
4,413✔
226
                vtxi := t.byDigest[out[i]]
1,609✔
227
                vtxj := t.byDigest[out[j]]
1,609✔
228
                return vtxi.mostRecentInterval().stop.Before(*vtxj.mostRecentInterval().stop)
1,609✔
229
        })
1,609✔
230
        return out
2,804✔
231
}
232

233
func (p *textMux) print(t *trace) {
2,804✔
234
        completed := map[digest.Digest]struct{}{}
2,804✔
235
        rest := map[digest.Digest]struct{}{}
2,804✔
236

2,804✔
237
        for dgst := range t.updates {
4,467✔
238
                v, ok := t.byDigest[dgst]
1,663✔
239
                if !ok {
1,663✔
240
                        continue
×
241
                }
242
                if v.ProgressGroup != nil || v.hidden {
1,663✔
243
                        // skip vtxs in a group (they are merged into a single vtx) and hidden ones
×
244
                        continue
×
245
                }
246
                if v.isCompleted() {
2,535✔
247
                        completed[dgst] = struct{}{}
872✔
248
                } else {
1,663✔
249
                        rest[dgst] = struct{}{}
791✔
250
                }
791✔
251
        }
252

253
        current := p.current
2,804✔
254

2,804✔
255
        // items that have completed need to be printed first
2,804✔
256
        if _, ok := completed[current]; ok {
2,938✔
257
                p.printVtx(t, current)
134✔
258
        }
134✔
259

260
        for _, dgst := range sortCompleted(t, completed) {
3,676✔
261
                if dgst != current {
1,610✔
262
                        p.printVtx(t, dgst)
738✔
263
                }
738✔
264
        }
265

266
        if len(rest) == 0 {
4,879✔
267
                if current != "" {
4,137✔
268
                        if v := t.byDigest[current]; v.isStarted() && !v.isCompleted() {
4,072✔
269
                                return
2,010✔
270
                        }
2,010✔
271
                }
272
                // make any open vertex active
273
                for dgst, v := range t.byDigest {
2,065✔
274
                        if v.isStarted() && !v.isCompleted() && v.ProgressGroup == nil && !v.hidden {
2,003✔
275
                                p.printVtx(t, dgst)
3✔
276
                                return
3✔
277
                        }
3✔
278
                }
279
                return
62✔
280
        }
281

282
        // now print the active one
283
        if _, ok := rest[current]; ok {
1,249✔
284
                p.printVtx(t, current)
520✔
285
        }
520✔
286

287
        stats := map[digest.Digest]*vtxStat{}
729✔
288
        now := time.Now()
729✔
289
        sum := 0.0
729✔
290
        var max digest.Digest
729✔
291
        if current != "" {
1,400✔
292
                rest[current] = struct{}{}
671✔
293
        }
671✔
294
        for dgst := range rest {
1,671✔
295
                v, ok := t.byDigest[dgst]
942✔
296
                if !ok {
942✔
297
                        continue
×
298
                }
299
                if v.lastBlockTime == nil {
942✔
300
                        // shouldn't happen, but not worth crashing over
×
301
                        continue
×
302
                }
303
                tm := now.Sub(*v.lastBlockTime)
942✔
304
                speed := float64(v.count) / tm.Seconds()
942✔
305
                overLimit := tm > maxDelay && dgst != current
942✔
306
                stats[dgst] = &vtxStat{blockTime: tm, speed: speed, overLimit: overLimit}
942✔
307
                sum += speed
942✔
308
                if overLimit || max == "" || stats[max].speed < speed {
1,731✔
309
                        max = dgst
789✔
310
                }
789✔
311
        }
312
        for dgst := range stats {
1,671✔
313
                stats[dgst].share = stats[dgst].speed / sum
942✔
314
        }
942✔
315

316
        if _, ok := completed[current]; ok || current == "" {
870✔
317
                p.printVtx(t, max)
141✔
318
                return
141✔
319
        }
141✔
320

321
        // show items that were hidden
322
        for dgst := range rest {
1,291✔
323
                if stats[dgst].overLimit {
703✔
324
                        p.printVtx(t, dgst)
×
325
                        return
×
326
                }
×
327
        }
328

329
        // fair split between vertices
330
        if 1.0/(1.0-stats[current].share)*antiFlicker.Seconds() < stats[current].blockTime.Seconds() {
588✔
331
                p.printVtx(t, max)
×
332
                return
×
333
        }
×
334
}
335

336
type vtxStat struct {
337
        blockTime time.Duration
338
        speed     float64
339
        share     float64
340
        overLimit bool
341
}
342

343
func limitString(s string, l int) string {
×
344
        if len(s) > l {
×
345
                return s[:l] + "..."
×
346
        }
×
347
        return s
×
348
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc