-
Notifications
You must be signed in to change notification settings - Fork 290
/
view.go
302 lines (248 loc) · 7.58 KB
/
view.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package view
import (
"strings"
"time"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/pkg/model"
"github.com/windmilleng/tilt/pkg/model/logstore"
)
const LogLineCount = 50
const TiltfileResourceName = "(Tiltfile)"
type ResourceInfoView interface {
resourceInfoView()
RuntimeSpanID() logstore.SpanID
RuntimeStatus() model.RuntimeStatus
Status() string
}
type DCResourceInfo struct {
ConfigPaths []string
ContainerStatus string
ContainerID container.ID
SpanID logstore.SpanID
StartTime time.Time
RunStatus model.RuntimeStatus
}
func NewDCResourceInfo(configPaths []string, status string, cID container.ID, spanID logstore.SpanID, startTime time.Time, runtimeStatus model.RuntimeStatus) DCResourceInfo {
return DCResourceInfo{
ConfigPaths: configPaths,
ContainerStatus: status,
ContainerID: cID,
SpanID: spanID,
StartTime: startTime,
RunStatus: runtimeStatus,
}
}
var _ ResourceInfoView = DCResourceInfo{}
func (DCResourceInfo) resourceInfoView() {}
func (dcInfo DCResourceInfo) RuntimeSpanID() logstore.SpanID { return dcInfo.SpanID }
func (dcInfo DCResourceInfo) Status() string {
return strings.Title(dcInfo.ContainerStatus)
}
func (dcInfo DCResourceInfo) RuntimeStatus() model.RuntimeStatus { return dcInfo.RunStatus }
type K8sResourceInfo struct {
PodName string
PodCreationTime time.Time
PodUpdateStartTime time.Time
PodStatus string
PodRestarts int
SpanID logstore.SpanID
RunStatus model.RuntimeStatus
}
var _ ResourceInfoView = K8sResourceInfo{}
func (K8sResourceInfo) resourceInfoView() {}
func (k8sInfo K8sResourceInfo) RuntimeSpanID() logstore.SpanID { return k8sInfo.SpanID }
func (k8sInfo K8sResourceInfo) Status() string { return k8sInfo.PodStatus }
func (k8sInfo K8sResourceInfo) RuntimeStatus() model.RuntimeStatus { return k8sInfo.RunStatus }
// This TiltfileResourceInfo is consistent with how the web UI models the tiltfile.
type TiltfileResourceInfo struct {
}
var _ ResourceInfoView = TiltfileResourceInfo{}
func (TiltfileResourceInfo) resourceInfoView() {}
func (TiltfileResourceInfo) RuntimeSpanID() logstore.SpanID { return "unknown" }
func (TiltfileResourceInfo) Status() string { return "" }
func (TiltfileResourceInfo) RuntimeStatus() model.RuntimeStatus {
return model.RuntimeStatusNotApplicable
}
type YAMLResourceInfo struct {
K8sResources []string
}
var _ ResourceInfoView = YAMLResourceInfo{}
func (YAMLResourceInfo) resourceInfoView() {}
func (yamlInfo YAMLResourceInfo) RuntimeSpanID() logstore.SpanID { return "unknown" }
func (yamlInfo YAMLResourceInfo) Status() string { return "" }
func (yamlInfo YAMLResourceInfo) RuntimeStatus() model.RuntimeStatus {
return model.RuntimeStatusNotApplicable
}
type LocalResourceInfo struct {
status model.RuntimeStatus
pid int
spanID model.LogSpanID
}
func NewLocalResourceInfo(status model.RuntimeStatus, pid int, spanID model.LogSpanID) LocalResourceInfo {
return LocalResourceInfo{status: status, pid: pid, spanID: spanID}
}
var _ ResourceInfoView = LocalResourceInfo{}
func (lri LocalResourceInfo) resourceInfoView() {}
func (lri LocalResourceInfo) RuntimeSpanID() logstore.SpanID { return lri.spanID }
func (lri LocalResourceInfo) Status() string { return string(lri.status) }
func (lri LocalResourceInfo) RuntimeStatus() model.RuntimeStatus { return lri.status }
type Resource struct {
Name model.ManifestName
DirectoriesWatched []string
PathsWatched []string
LastDeployTime time.Time
TriggerMode model.TriggerMode
BuildHistory []model.BuildRecord
CurrentBuild model.BuildRecord
PendingBuildReason model.BuildReason
PendingBuildEdits []string
PendingBuildSince time.Time
Endpoints []string
ResourceInfo ResourceInfoView
// If a pod had to be killed because it was crashing, we keep the old log around
// for a little while.
CrashLog model.Log
IsTiltfile bool
}
func (r Resource) DockerComposeTarget() DCResourceInfo {
switch info := r.ResourceInfo.(type) {
case DCResourceInfo:
return info
default:
return DCResourceInfo{}
}
}
func (r Resource) DCInfo() DCResourceInfo {
ret, _ := r.ResourceInfo.(DCResourceInfo)
return ret
}
func (r Resource) IsDC() bool {
_, ok := r.ResourceInfo.(DCResourceInfo)
return ok
}
func (r Resource) K8sInfo() K8sResourceInfo {
ret, _ := r.ResourceInfo.(K8sResourceInfo)
return ret
}
func (r Resource) IsK8s() bool {
_, ok := r.ResourceInfo.(K8sResourceInfo)
return ok
}
func (r Resource) YAMLInfo() YAMLResourceInfo {
ret, _ := r.ResourceInfo.(YAMLResourceInfo)
return ret
}
func (r Resource) IsYAML() bool {
_, ok := r.ResourceInfo.(YAMLResourceInfo)
return ok
}
func (r Resource) LastBuild() model.BuildRecord {
if len(r.BuildHistory) == 0 {
return model.BuildRecord{}
}
return r.BuildHistory[0]
}
func (r Resource) DefaultCollapse() bool {
autoExpand := false
if k8sInfo, ok := r.ResourceInfo.(K8sResourceInfo); ok {
autoExpand = k8sInfo.PodRestarts > 0 || k8sInfo.PodStatus == "CrashLoopBackOff" || k8sInfo.PodStatus == "Error"
}
if r.IsDC() && r.DockerComposeTarget().RuntimeStatus() == model.RuntimeStatusError {
autoExpand = true
}
autoExpand = autoExpand ||
r.LastBuild().Error != nil ||
!r.CrashLog.Empty() ||
r.LastBuild().WarningCount > 0 ||
r.LastBuild().Reason.Has(model.BuildReasonFlagCrash) ||
r.CurrentBuild.Reason.Has(model.BuildReasonFlagCrash) ||
r.PendingBuildReason.Has(model.BuildReasonFlagCrash)
return !autoExpand
}
func (r Resource) IsCollapsed(rv ResourceViewState) bool {
return rv.CollapseState.IsCollapsed(r.DefaultCollapse())
}
// Snapshot of the current view that's not expressed in the underlying model state.
//
// This includes things like the current selection, warning messages,
// narration messages, etc.
//
// Client should always hold this as a value struct, and copy it
// whenever they need to mutate something.
type View struct {
LogReader logstore.Reader
Resources []Resource
IsProfiling bool
FatalError error
}
func (v View) TiltfileErrorMessage() string {
for _, res := range v.Resources {
if res.Name == TiltfileResourceName {
err := res.LastBuild().Error
if err != nil {
return err.Error()
}
return ""
}
}
return ""
}
func (v View) Resource(n model.ManifestName) (Resource, bool) {
for _, res := range v.Resources {
if res.Name == n {
return res, true
}
}
return Resource{}, false
}
type ViewState struct {
ShowNarration bool
NarrationMessage string
Resources []ResourceViewState
ProcessedLogs logstore.Checkpoint
AlertMessage string
TabState TabState
SelectedIndex int
TiltLogState TiltLogState
}
type TabState int
const (
TabAllLog TabState = iota
TabBuildLog
TabRuntimeLog
)
type CollapseState int
const (
CollapseAuto = iota
CollapseYes
CollapseNo
)
func (c CollapseState) IsCollapsed(defaultCollapse bool) bool {
switch c {
case CollapseYes:
return true
case CollapseNo:
return false
default: // CollapseAuto
return defaultCollapse
}
}
func (vs *ViewState) CycleViewLogState() {
states := []TiltLogState{TiltLogShort, TiltLogHalfScreen, TiltLogFullScreen}
for i := range states {
if states[i] == vs.TiltLogState {
vs.TiltLogState = states[(i+1)%len(states)]
return
}
}
vs.TiltLogState = TiltLogFullScreen
}
type TiltLogState int
const (
TiltLogShort TiltLogState = iota
TiltLogHalfScreen
TiltLogFullScreen
)
type ResourceViewState struct {
CollapseState CollapseState
}