Skip to content

refactor: clean up WorkspaceBuildLogs types #4738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions site/src/components/WorkspaceBuildLogs/WorkspaceBuildLogs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ProvisionerJobLog } from "api/typesGenerated"
import { groupLogsByStage } from "./WorkspaceBuildLogs"

describe("groupLogsByStage", () => {
it("should group them by stage", () => {
const input: ProvisionerJobLog[] = [
{
id: "1",
created_at: "oct 13",
log_source: "provisioner",
log_level: "debug",
stage: "build",
output: "test",
},
{
id: "2",
created_at: "oct 13",
log_source: "provisioner",
log_level: "debug",
stage: "cleanup",
output: "test",
},
{
id: "3",
created_at: "oct 13",
log_source: "provisioner",
log_level: "debug",
stage: "cleanup",
output: "done",
},
]

const actual = groupLogsByStage(input)

expect(actual["cleanup"].length).toBe(2)
})
})
16 changes: 8 additions & 8 deletions site/src/components/WorkspaceBuildLogs/WorkspaceBuildLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ const Language = {
}

type Stage = ProvisionerJobLog["stage"]
type LogsGroupedByStage = Record<Stage, ProvisionerJobLog[]>
type GroupLogsByStageFn = (logs: ProvisionerJobLog[]) => LogsGroupedByStage

const groupLogsByStage = (logs: ProvisionerJobLog[]) => {
const logsByStage: Record<Stage, ProvisionerJobLog[]> = {}
export const groupLogsByStage: GroupLogsByStageFn = (logs) => {
const logsByStage: LogsGroupedByStage = {}

for (const log of logs) {
// If there is no log in the stage record, add an empty array
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (logsByStage[log.stage] === undefined) {
logsByStage[log.stage] = []
if (log.stage in logsByStage) {
logsByStage[log.stage].push(log)
} else {
logsByStage[log.stage] = [log]
}

logsByStage[log.stage].push(log)
}

return logsByStage
Expand Down