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

tensorchord / envd / 13090064785

01 Feb 2025 03:44PM UTC coverage: 41.601% (-1.3%) from 42.886%
13090064785

push

github

web-flow
feat: use moby context by default (#1964)

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

1 of 1 new or added line in 1 file covered. (100.0%)

158 existing lines in 10 files now uncovered.

5020 of 12067 relevant lines covered (41.6%)

160.04 hits per line

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

86.44
/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,719✔
53
        if p.last == nil {
1,742✔
54
                p.last = make(map[string]lastStatus)
23✔
55
        }
23✔
56

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

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

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

78
                if p.notFirst {
1,729✔
79
                        fmt.Fprintln(p.w, "")
853✔
80
                } else {
876✔
81
                        p.notFirst = true
23✔
82
                }
23✔
83

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

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

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

381✔
104
                        if last, ok := p.last[s.ID]; ok && s.Completed == nil {
434✔
105
                                var progressDelta float64
53✔
106
                                if s.Total > 0 {
65✔
107
                                        progressDelta = float64(s.Current-last.Current) / float64(s.Total)
12✔
108
                                }
12✔
109
                                timeDelta := s.Timestamp.Sub(last.Timestamp)
53✔
110
                                if progressDelta < minProgressDelta && timeDelta < minTimeDelta {
99✔
111
                                        doPrint = false
46✔
112
                                }
46✔
113
                        }
114

115
                        if !doPrint {
427✔
116
                                continue
46✔
117
                        }
118

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

335✔
124
                        var bytes string
335✔
125
                        if s.Total != 0 {
429✔
126
                                bytes = fmt.Sprintf(" %.2f / %.2f", units.Bytes(s.Current), units.Bytes(s.Total))
94✔
127
                        } else if s.Current != 0 {
354✔
128
                                bytes = fmt.Sprintf(" %.2f", units.Bytes(s.Current))
19✔
129
                        }
19✔
130
                        var tm string
335✔
131
                        endTime := s.Timestamp
335✔
132
                        if s.Completed != nil {
611✔
133
                                endTime = *s.Completed
276✔
134
                        }
276✔
135
                        if s.Started != nil {
670✔
136
                                diff := endTime.Sub(*s.Started).Seconds()
335✔
137
                                if diff > 0.01 {
524✔
138
                                        tm = fmt.Sprintf(" %.1fs", diff)
189✔
139
                                }
189✔
140
                        }
141
                        if s.Completed != nil {
611✔
142
                                tm += " done"
276✔
143
                        } else {
335✔
144
                                isOpenStatus = true
59✔
145
                        }
59✔
146
                        fmt.Fprintf(p.w, "#%d %s%s%s\n", v.index, s.ID, bytes, tm)
335✔
147
                }
148
        }
149
        v.statusUpdates = map[string]struct{}{}
1,719✔
150

1,719✔
151
        for _, w := range v.warnings[v.warningIdx:] {
1,719✔
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 {
10,218✔
157
                if i == 0 {
9,158✔
158
                        l = l[v.logsOffset:]
659✔
159
                }
659✔
160
                fmt.Fprintf(p.w, "%s", l)
8,499✔
161
                if i != len(v.logs)-1 || !v.logsPartial {
16,745✔
162
                        fmt.Fprintln(p.w, "")
8,246✔
163
                }
8,246✔
164
                if v.logsBuffer == nil {
8,566✔
165
                        v.logsBuffer = ring.New(logsBufferSize)
67✔
166
                }
67✔
167
                v.logsBuffer.Value = l
8,499✔
168
                if !v.logsPartial {
12,123✔
169
                        v.logsBuffer = v.logsBuffer.Next()
3,624✔
170
                }
3,624✔
171
        }
172

173
        if len(v.logs) > 0 {
2,378✔
174
                if v.logsPartial {
912✔
175
                        v.logs = v.logs[len(v.logs)-1:]
253✔
176
                        v.logsOffset = len(v.logs[0])
253✔
177
                } else {
659✔
178
                        v.logs = nil
406✔
179
                        v.logsOffset = 0
406✔
180
                }
406✔
181
        }
182

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

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

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

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

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

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

253
        current := p.current
3,164✔
254

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

260
        for _, dgst := range sortCompleted(t, completed) {
4,023✔
261
                if dgst != current {
1,566✔
262
                        p.printVtx(t, dgst)
707✔
263
                }
707✔
264
        }
265

266
        if len(rest) == 0 {
5,395✔
267
                if current != "" {
4,451✔
268
                        if v := t.byDigest[current]; v.isStarted() && !v.isCompleted() {
4,371✔
269
                                return
2,151✔
270
                        }
2,151✔
271
                }
272
                // make any open vertex active
273
                for dgst, v := range t.byDigest {
2,879✔
274
                        if v.isStarted() && !v.isCompleted() && v.ProgressGroup == nil && !v.hidden {
2,800✔
275
                                p.printVtx(t, dgst)
1✔
276
                                return
1✔
277
                        }
1✔
278
                }
279
                return
79✔
280
        }
281

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

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

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

321
        // show items that were hidden
322
        for dgst := range rest {
1,748✔
323
                if stats[dgst].overLimit {
975✔
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() {
773✔
UNCOV
331
                p.printVtx(t, max)
×
UNCOV
332
                return
×
UNCOV
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