Skip to content

Commit

Permalink
fix: some optimizations for dashboard status processing
Browse files Browse the repository at this point in the history
  • Loading branch information
starpit committed Apr 9, 2023
1 parent a216fb0 commit 555ccc3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ export default class Live {
/** Model of the lines of output */
private readonly events = new Heap<Event>((a, b) => {
if (a.line === b.line) {
return a.timestamp - b.timestamp
// later timestamps get higher priority
return b.timestamp - a.timestamp
}

const stateDiff = a.stateRank - b.stateRank
if (stateDiff !== 0) {
return stateDiff
} else {
return a.timestamp - b.timestamp
// later timestamps get higher priority
return b.timestamp - a.timestamp
}
})

Expand All @@ -83,6 +85,7 @@ export default class Live {
if (tail.kind === "logs") {
// handle a log line
this.pushLineAndPublish(stripColors(data), cb)
return
}

// otherwise, treat it as an event
Expand Down Expand Up @@ -129,11 +132,7 @@ export default class Live {
return
}

// inform the UI that we have updates
cb({
events: this.pushEvent(data, metric, timestamp),
workers: Object.values(this.workers),
})
this.pushEvent(data, metric, timestamp)
}

if (name === "*") {
Expand All @@ -143,6 +142,12 @@ export default class Live {
// this event affects a specific worker
update(name)
}

// inform the UI that we have updates
cb({
events: this.importantEvents(),
workers: Object.values(this.workers),
})
}
}
})
Expand All @@ -153,10 +158,14 @@ export default class Live {

/** @return the most important events, to be shown in the UI */
private importantEvents() {
return this.events
.toArray()
.slice(0, this.opts.events || 8)
.sort((a, b) => a.timestamp - b.timestamp)
if (this.opts.events === 0) {
return []
} else {
return this.events
.toArray()
.slice(0, this.opts.events || 8) // 8 highest priority
.sort((a, b) => a.timestamp - b.timestamp) // sorted by time
}
}

/** Replace any timestamps with a placeholder, so that the UI can use a "5m ago" style */
Expand All @@ -182,6 +191,7 @@ export default class Live {
}

private readonly lookup: Record<string, Event> = {}

/** Add `line` to our heap `this.events` */
private pushEvent(line: string, metric: WorkerState, timestamp: number) {
const key = this.prepareLineForUI(line)
Expand All @@ -207,11 +217,11 @@ export default class Live {
}
}

if (this.opts.events === 0) {
/* if (this.opts.events === 0) {
return []
} else {
return this.importantEvents()
}
} */
}

/** This helps us parse out a [W5] style prefix for loglines, so we can intuit the worker id of the log line */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ export function waitTillExists(filepath: string, okIf404 = true) {
watcher.on("error", closeAndReject)

// oof, we need to give up, at some point
const timeoutSeconds = process.env.FILE_WAIT_TIMEOUT ? parseInt(process.env.FILE_WAIT_TIMEOUT, 10) : 5
setTimeout(() => {
if (okIf404) {
closeAndResolve(false)
} else {
if (!okIf404) {
const timeoutSeconds = process.env.FILE_WAIT_TIMEOUT ? parseInt(process.env.FILE_WAIT_TIMEOUT, 10) : 5
setTimeout(() => {
closeAndReject(new Error(`Could not find ${filepath} after ${timeoutSeconds} seconds`))
}
}, timeoutSeconds * 1000)
}, timeoutSeconds * 1000)
}
})
}

Expand Down

0 comments on commit 555ccc3

Please sign in to comment.