Skip to content

Commit b00d622

Browse files
authored
Merge pull request #96 from dockersamples/improve-startup-log-output
Improve log output in extension while starting a Labspace
2 parents 18e789b + e780393 commit b00d622

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

dd-extension/src/DockerContext.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
useState,
55
useEffect,
66
useCallback,
7+
useMemo,
78
} from "react";
89
import Spinner from "react-bootstrap/Spinner";
910

1011
import { parse } from "yaml";
12+
import { LogProcessor } from "./logProcessor.js";
1113

1214
const CATALOGS = [
1315
"https://raw.githubusercontent.com/dockersamples/awesome-labspaces/refs/heads/main/catalog.yaml",
@@ -30,6 +32,8 @@ export function DockerContextProvider({ children }) {
3032
const [launchLog, setLaunchLog] = useState("");
3133
const [forceRefreshCount, setForceRefreshCount] = useState(0);
3234

35+
const logProcessor = useMemo(() => new LogProcessor(), []);
36+
3337
useEffect(() => {
3438
Promise.all(
3539
CATALOGS.map((url) =>
@@ -92,6 +96,7 @@ export function DockerContextProvider({ children }) {
9296
const startLabspace = useCallback(
9397
(location) => {
9498
console.log(`Starting Labspace with location ${location}`);
99+
logProcessor.reset();
95100
setLaunchLog("");
96101
setStartingLabspace(location);
97102

@@ -110,7 +115,13 @@ export function DockerContextProvider({ children }) {
110115
stream: {
111116
onOutput(data) {
112117
const newData = data.stdout ? data.stdout : data.stderr;
113-
setLaunchLog((l) => l + newData);
118+
let result;
119+
newData.split("\n").forEach((line) => {
120+
if (line.trim() === "") return;
121+
122+
result = logProcessor.processLine(line.trim());
123+
});
124+
setLaunchLog(result);
114125
},
115126
onClose(exitCode) {
116127
setHasLabspace(true);

dd-extension/src/logProcessor.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export class LogProcessor {
2+
constructor() {
3+
this.lines = [];
4+
this.identifierToLineMapping = new Map();
5+
6+
this.downloadPattern = /^(.+?)\s+(Pull|Download|Extracting)/;
7+
this.resourceCreationPattern = /(Container|Volume)\s+([a-z0-9-]+)\s+/;
8+
}
9+
10+
reset() {
11+
this.lines = [];
12+
this.identifierToLineMapping = new Map();
13+
}
14+
15+
processLine(line) {
16+
const downloadMatch = line.match(this.downloadPattern);
17+
18+
if (downloadMatch) {
19+
const identifier = downloadMatch[1].trim();
20+
21+
if (this.identifierToLineMapping.has(identifier)) {
22+
this.lines[this.identifierToLineMapping.get(identifier)] = line;
23+
} else {
24+
this.identifierToLineMapping.set(identifier, this.lines.length);
25+
this.lines.push(line);
26+
}
27+
} else if (this.resourceCreationPattern.test(line)) {
28+
const identifier = line.match(this.resourceCreationPattern)[2];
29+
if (this.identifierToLineMapping.has(identifier)) {
30+
this.lines[this.identifierToLineMapping.get(identifier)] = line;
31+
} else {
32+
this.identifierToLineMapping.set(identifier, this.lines.length);
33+
this.lines.push(line);
34+
}
35+
} else {
36+
this.lines.push(line);
37+
}
38+
39+
return this.lines.join("\n");
40+
}
41+
}

0 commit comments

Comments
 (0)