-
Notifications
You must be signed in to change notification settings - Fork 288
/
HUD.tsx
672 lines (609 loc) · 19.5 KB
/
HUD.tsx
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import React, { Component } from "react"
import AppController from "./AppController"
import NoMatch from "./NoMatch"
import Sidebar from "./Sidebar"
import SidebarAccount from "./SidebarAccount"
import SidebarResources, { SidebarItem } from "./SidebarResources"
import Statusbar, { StatusItem } from "./Statusbar"
import LogPane from "./LogPane"
import HeroScreen from "./HeroScreen"
import ResourceInfo from "./ResourceInfo"
import K8sViewPane from "./K8sViewPane"
import PathBuilder from "./PathBuilder"
import { matchPath } from "react-router"
import { Route, Switch, RouteComponentProps } from "react-router-dom"
import { History, UnregisterCallback } from "history"
import { counter, pathToTag } from "./analytics"
import SecondaryNav from "./SecondaryNav"
import SocketBar from "./SocketBar"
import "./HUD.scss"
import {
LogLine,
ResourceView,
ShowFatalErrorModal,
SnapshotHighlight,
SocketState,
ShowErrorModal,
} from "./types"
import { logLinesFromString } from "./logs"
import HudState from "./HudState"
import AlertPane from "./AlertPane"
import AnalyticsNudge from "./AnalyticsNudge"
import NotFound from "./NotFound"
import { numberOfAlerts } from "./alerts"
import Features from "./feature"
import ShareSnapshotModal from "./ShareSnapshotModal"
import FatalErrorModal from "./FatalErrorModal"
import * as _ from "lodash"
import FacetsPane from "./FacetsPane"
import HUDLayout from "./HUDLayout"
import LogStore from "./LogStore"
import { traceNav } from "./trace"
import ErrorModal from "./ErrorModal"
import { debounce, Cancelable } from "lodash"
type HudProps = {
history: History
incr: counter
}
// The Main HUD view, as specified in
// https://docs.google.com/document/d/1VNIGfpC4fMfkscboW0bjYYFJl07um_1tsFrbN-Fu3FI/edit#heading=h.l8mmnclsuxl1
class HUD extends Component<HudProps, HudState> {
// The root of the HUD view, without the slash.
private pathBuilder: PathBuilder
private controller: AppController
private history: History
private unlisten: UnregisterCallback
private incrHighlight: (() => void) & Cancelable
flush: () => void
constructor(props: HudProps) {
super(props)
props.incr("ui.web.init", { ua: window.navigator.userAgent })
this.pathBuilder = new PathBuilder(
window.location.host,
window.location.pathname
)
this.controller = new AppController(this.pathBuilder, this)
this.history = props.history
this.unlisten = this.history.listen((location, _) => {
let tags = { type: pathToTag(location.pathname) }
props.incr("ui.web.navigation", tags)
this.handleClearHighlight()
let selection = document.getSelection()
selection && selection.removeAllRanges()
})
this.state = {
view: {
resources: [],
needsAnalyticsNudge: false,
fatalError: undefined,
runningTiltBuild: {
version: "",
date: "",
dev: false,
},
suggestedTiltVersion: "",
versionSettings: { checkUpdates: true },
featureFlags: {},
tiltCloudUsername: "",
tiltCloudSchemeHost: "",
tiltCloudTeamID: "",
},
isSidebarClosed: false,
snapshotLink: "",
showSnapshotModal: false,
showFatalErrorModal: ShowFatalErrorModal.Default,
snapshotHighlight: undefined,
socketState: SocketState.Closed,
showErrorModal: ShowErrorModal.Default,
error: undefined,
}
this.toggleSidebar = this.toggleSidebar.bind(this)
this.handleClearHighlight = this.handleClearHighlight.bind(this)
this.handleSetHighlight = this.handleSetHighlight.bind(this)
this.handleOpenModal = this.handleOpenModal.bind(this)
this.incrHighlight = debounce(
() => props.incr("ui.web.highlight", {}),
3000 /* ms */
)
this.flush = this.incrHighlight.flush
}
componentDidMount() {
if (process.env.NODE_ENV === "test") {
// we don't want to run any bootstrapping code in the test environment
return
}
if (this.pathBuilder.isSnapshot()) {
this.controller.setStateFromSnapshot()
} else {
this.controller.createNewSocket()
}
}
componentWillUnmount() {
this.controller.dispose()
this.unlisten()
}
setAppState<K extends keyof HudState>(state: Pick<HudState, K>) {
this.setState(prevState => {
let newState = _.clone(state) as any
newState.logStore = prevState.logStore ?? new LogStore()
let newLogList = newState.view?.logList
if (newLogList) {
let fromCheckpoint = newLogList.fromCheckpoint ?? 0
if (fromCheckpoint > 0) {
newState.logStore.append(newLogList)
} else if (fromCheckpoint === 0) {
// if the fromCheckpoint is 0 or undefined, create a brand new log store.
newState.logStore = new LogStore()
newState.logStore.append(newLogList)
}
}
return newState
})
}
setHistoryLocation(path: string) {
this.props.history.replace(path)
}
toggleSidebar() {
this.setState(prevState => {
return {
isSidebarClosed: !prevState.isSidebarClosed,
}
})
}
path(relPath: string) {
return this.pathBuilder.path(relPath)
}
snapshotFromState(state: HudState): Proto.webviewSnapshot {
let view = _.cloneDeep(state.view ?? null)
if (view && state.logStore) {
view.logList = state.logStore.toLogList()
}
return {
view: view,
isSidebarClosed: !!state.isSidebarClosed,
path: this.props.history.location.pathname,
snapshotHighlight: _.cloneDeep(state.snapshotHighlight),
}
}
sendSnapshot(snapshot: Proto.webviewSnapshot) {
let url = `//${window.location.host}/api/snapshot/new`
if (!snapshot.view) {
return
}
let body = JSON.stringify(snapshot)
// TODO(dmiller): we need to figure out a way to get human readable error messages from the server
fetch(url, {
method: "post",
body: body,
})
.then(res => {
res
.json()
.then((value: Proto.webviewUploadSnapshotResponse) => {
this.setState({
snapshotLink: value.url ? value.url : "",
})
})
.catch(err => {
console.error(err)
this.setAppState({
showSnapshotModal: false,
error: "Error decoding JSON response",
})
})
})
.catch(err => {
console.error(err)
this.setAppState({
showSnapshotModal: false,
error: "Error posting snapshot",
})
})
}
private getFeatures(): Features {
if (this.state.view) {
return new Features(this.state.view.featureFlags)
}
return new Features({})
}
handleSetHighlight(highlight: SnapshotHighlight) {
this.incrHighlight()
this.setState({
snapshotHighlight: highlight,
})
}
handleClearHighlight() {
this.setState({
snapshotHighlight: undefined,
})
}
private handleOpenModal() {
this.setState({ showSnapshotModal: true })
}
render() {
let view = this.state.view
let needsNudge = view?.needsAnalyticsNudge ?? false
let resources = view?.resources ?? []
if (!resources?.length) {
return <HeroScreen message={"Loading…"} />
}
let statusItems = resources.map(res => new StatusItem(res))
let runningBuild = view?.runningTiltBuild
let suggestedVersion = view?.suggestedTiltVersion
const versionSettings = view?.versionSettings
const checkUpdates = versionSettings?.checkUpdates ?? true
let shareSnapshotModal = this.renderShareSnapshotModal(view)
let fatalErrorModal = this.renderFatalErrorModal(view)
let errorModal = this.renderErrorModal()
let statusbar = (
<Statusbar
items={statusItems}
alertsUrl={this.path("/alerts")}
runningBuild={runningBuild}
suggestedVersion={suggestedVersion}
checkVersion={checkUpdates}
/>
)
let hudClasses = ["HUD"]
if (this.pathBuilder.isSnapshot()) {
hudClasses.push("is-snapshot")
}
let matchTrace = matchPath(String(this.props.history.location.pathname), {
path: this.path("/r/:name/trace/:span"),
})
let matchTraceParams: any = matchTrace?.params
let isTwoLevelHeader = !!matchTraceParams?.span
return (
<div className={hudClasses.join(" ")}>
<AnalyticsNudge needsNudge={needsNudge} />
<SocketBar state={this.state.socketState} />
{fatalErrorModal}
{errorModal}
{shareSnapshotModal}
{this.renderSidebarSwitch()}
{statusbar}
<HUDLayout
header={this.renderHUDHeader()}
isSidebarClosed={!!this.state.isSidebarClosed}
isTwoLevelHeader={isTwoLevelHeader}
>
{this.renderMainPaneSwitch()}
</HUDLayout>
</div>
)
}
renderHUDHeader() {
return (
<>
{this.renderResourceInfo()}
{this.renderSecondaryNav()}
</>
)
}
renderResourceInfo() {
let match = matchPath(String(this.props.history.location.pathname), {
path: this.path("/r/:name"),
})
let params: any = match?.params
let name = params?.name
let view = this.state.view
let resources = view?.resources ?? []
let selectedResource = resources?.find(r => r.name === name)
let endpoints = selectedResource?.endpoints ?? []
let podID = selectedResource?.podID ?? ""
let podStatus =
(selectedResource?.k8sResourceInfo &&
selectedResource?.k8sResourceInfo.podStatus) ||
""
let showSnapshot =
this.getFeatures().isEnabled("snapshots") &&
!this.pathBuilder.isSnapshot()
let snapshotHighlight = this.state.snapshotHighlight || null
return (
<ResourceInfo
endpoints={endpoints}
podID={podID}
podStatus={podStatus}
showSnapshotButton={showSnapshot}
highlight={snapshotHighlight}
handleOpenModal={this.handleOpenModal}
/>
)
}
renderSecondaryNav() {
let view = this.state.view
let resources = view?.resources ?? []
let secondaryNavRoute = (
t: ResourceView,
props: RouteComponentProps<any>
) => {
let name = props.match.params?.name ?? ""
let span = props.match.params?.span ?? ""
let numAlerts = 0
let logUrl = name === "" ? this.path("/") : this.path(`/r/${name}`)
let alertsUrl =
name === "" ? this.path("/alerts") : this.path(`/r/${name}/alerts`)
let facetsUrl = name !== "" ? this.path(`/r/${name}/facets`) : null
let currentTraceNav =
span && this.state.logStore
? traceNav(this.state.logStore, this.pathBuilder, span)
: null
if (name) {
let selectedResource = resources.find(r => r.name === name)
if (selectedResource) {
numAlerts = numberOfAlerts(selectedResource)
}
} else {
numAlerts = resources
.map(r => numberOfAlerts(r))
.reduce((sum, current) => sum + current, 0)
}
return (
<SecondaryNav
logUrl={logUrl}
alertsUrl={alertsUrl}
resourceView={t}
numberOfAlerts={numAlerts}
facetsUrl={facetsUrl}
traceNav={currentTraceNav}
/>
)
}
return (
<Switch>
<Route
path={this.path("/r/:name/alerts")}
render={secondaryNavRoute.bind(null, ResourceView.Alerts)}
/>
<Route
path={this.path("/r/:name/facets")}
render={secondaryNavRoute.bind(null, ResourceView.Facets)}
/>
<Route
path={this.path("/r/:name/trace/:span")}
render={secondaryNavRoute.bind(null, ResourceView.Trace)}
/>
<Route
path={this.path("/r/:name")}
render={secondaryNavRoute.bind(null, ResourceView.Log)}
/>
<Route
path={this.path("/alerts")}
render={secondaryNavRoute.bind(null, ResourceView.Alerts)}
/>
<Route render={secondaryNavRoute.bind(null, ResourceView.Log)} />
</Switch>
)
}
renderSidebarSwitch() {
let view = this.state.view
let resources = view?.resources || []
let sidebarItems = resources.map(res => new SidebarItem(res))
let isSidebarClosed = !!this.state.isSidebarClosed
let tiltCloudUsername = view?.tiltCloudUsername || null
let tiltCloudSchemeHost = view?.tiltCloudSchemeHost || ""
let tiltCloudTeamID = view?.tiltCloudTeamID || null
let tiltCloudTeamName = view?.tiltCloudTeamName || null
let isSnapshot = this.pathBuilder.isSnapshot()
let sidebarRoute = (t: ResourceView, props: RouteComponentProps<any>) => {
let name = props.match.params.name
return (
<Sidebar isClosed={isSidebarClosed} toggleSidebar={this.toggleSidebar}>
<SidebarAccount
tiltCloudUsername={tiltCloudUsername}
tiltCloudSchemeHost={tiltCloudSchemeHost}
tiltCloudTeamID={tiltCloudTeamID}
tiltCloudTeamName={tiltCloudTeamName}
isSnapshot={isSnapshot}
/>
<SidebarResources
selected={name}
items={sidebarItems}
resourceView={t}
pathBuilder={this.pathBuilder}
/>
</Sidebar>
)
}
return (
<Switch>
<Route
path={this.path("/r/:name/alerts")}
render={sidebarRoute.bind(null, ResourceView.Alerts)}
/>
<Route
path={this.path("/r/:name/facets")}
render={sidebarRoute.bind(null, ResourceView.Facets)}
/>
<Route
path={this.path("/alerts")}
render={sidebarRoute.bind(null, ResourceView.Alerts)}
/>
<Route
path={this.path("/r/:name")}
render={sidebarRoute.bind(null, ResourceView.Log)}
/>
<Route render={sidebarRoute.bind(null, ResourceView.Log)} />
</Switch>
)
}
renderMainPaneSwitch() {
let logStore = this.state.logStore ?? null
let view = this.state.view
let resources = (view && view.resources) || []
let snapshotHighlight = this.state.snapshotHighlight || null
let isSnapshot = this.pathBuilder.isSnapshot()
let traceRoute = (props: RouteComponentProps<any>) => {
let name = props.match.params?.name ?? ""
let span = props.match.params?.span ?? ""
let r = resources.find(r => r.name === name)
if (r === undefined) {
return <Route component={NotFound} />
}
let logLines: LogLine[] = []
if (span && logStore) {
logLines = logStore.traceLog(span)
}
return (
<LogPane
logLines={logLines}
showManifestPrefix={false}
manifestName={name}
handleSetHighlight={this.handleSetHighlight}
handleClearHighlight={this.handleClearHighlight}
highlight={snapshotHighlight}
isSnapshot={isSnapshot}
/>
)
}
let logsRoute = (props: RouteComponentProps<any>) => {
let name = props.match.params?.name ?? ""
let r = resources.find(r => r.name === name)
if (r === undefined) {
return <Route component={NotFound} />
}
let logLines: LogLine[] = []
if (name && logStore) {
logLines = logStore.manifestLog(name)
}
return (
<LogPane
logLines={logLines}
showManifestPrefix={false}
manifestName={name}
handleSetHighlight={this.handleSetHighlight}
handleClearHighlight={this.handleClearHighlight}
highlight={snapshotHighlight}
isSnapshot={isSnapshot}
/>
)
}
let errorRoute = (props: RouteComponentProps<any>): React.ReactNode => {
let name = props.match.params ? props.match.params.name : ""
let er = resources.find(r => r.name === name)
if (!er) {
return <Route component={NotFound} />
}
return (
<AlertPane
pathBuilder={this.pathBuilder}
resources={[er]}
logStore={logStore}
/>
)
}
let facetsRoute = (props: RouteComponentProps<any>): React.ReactNode => {
let name = props.match.params ? props.match.params.name : ""
let fr = resources.find(r => r.name === name)
if (!fr) {
return <Route component={NotFound} />
}
return <FacetsPane resource={fr} logStore={logStore} />
}
let allLogsRoute = () => {
let allLogs: LogLine[] = []
if (logStore) {
allLogs = logStore.allLog()
} else if (view?.log) {
allLogs = logLinesFromString(
"ERROR: Tilt Server and client protocol mismatch. This happens in dev mode if you have a new client talking to an old Tilt binary. Please re-compile Tilt"
)
}
return (
<LogPane
logLines={allLogs}
showManifestPrefix={true}
manifestName={""}
handleSetHighlight={this.handleSetHighlight}
handleClearHighlight={this.handleClearHighlight}
highlight={this.state.snapshotHighlight}
isSnapshot={isSnapshot}
/>
)
}
return (
<Switch>
<Route exact path={this.path("/")} render={allLogsRoute} />
<Route
exact
path={this.path("/alerts")}
render={() => (
<AlertPane
pathBuilder={this.pathBuilder}
resources={resources}
logStore={logStore}
/>
)}
/>
<Route exact path={this.path("/r/:name")} render={logsRoute} />
<Route
exact
path={this.path("/r/:name/trace/:span")}
render={traceRoute}
/>
<Route
exact
path={this.path("/r/:name/k8s")}
render={() => <K8sViewPane />}
/>
<Route exact path={this.path("/r/:name/alerts")} render={errorRoute} />
<Route exact path={this.path("/r/:name/facets")} render={facetsRoute} />
<Route component={NoMatch} />
</Switch>
)
}
renderShareSnapshotModal(view: Proto.webviewView | null) {
let handleClose = () =>
this.setState({ showSnapshotModal: false, snapshotLink: "" })
let handleSendSnapshot = () =>
this.sendSnapshot(this.snapshotFromState(this.state))
let tiltCloudUsername = (view && view.tiltCloudUsername) || null
let tiltCloudSchemeHost = (view && view.tiltCloudSchemeHost) || ""
let tiltCloudTeamID = (view && view.tiltCloudTeamID) || null
let highlightedLines = this.state.snapshotHighlight
? Math.abs(
parseInt(this.state.snapshotHighlight.endingLogID, 10) -
parseInt(this.state.snapshotHighlight.beginningLogID, 10)
) + 1
: null
return (
<ShareSnapshotModal
handleSendSnapshot={handleSendSnapshot}
handleClose={handleClose}
snapshotUrl={this.state.snapshotLink}
tiltCloudUsername={tiltCloudUsername}
tiltCloudSchemeHost={tiltCloudSchemeHost}
tiltCloudTeamID={tiltCloudTeamID}
isOpen={this.state.showSnapshotModal}
highlightedLines={highlightedLines}
/>
)
}
renderFatalErrorModal(view: Proto.webviewView | null) {
let error = view && view.fatalError
let handleClose = () =>
this.setState({ showFatalErrorModal: ShowFatalErrorModal.Hide })
return (
<FatalErrorModal
error={error}
showFatalErrorModal={this.state.showFatalErrorModal}
handleClose={handleClose}
/>
)
}
renderErrorModal() {
return (
<ErrorModal
error={this.state.error}
showErrorModal={this.state.showErrorModal}
handleClose={() =>
this.setState({
showErrorModal: ShowErrorModal.Default,
error: undefined,
})
}
/>
)
}
}
export default HUD