/
ServiceLogs.tsx
58 lines (53 loc) · 2.13 KB
/
ServiceLogs.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
import { Timestamp } from "@bufbuild/protobuf";
import { ServiceInfo } from "enclave-manager-sdk/build/api_container_service_pb";
import { DateTime } from "luxon";
import { useEffect, useState } from "react";
import { useKurtosisClient } from "../../../../../client/enclaveManager/KurtosisClientContext";
import { LogLineProps } from "../../../../../components/enclaves/logs/LogLine";
import { LogViewer } from "../../../../../components/enclaves/logs/LogViewer";
import { isDefined, stringifyError } from "../../../../../utils";
import { EnclaveFullInfo } from "../../../types";
const serviceLogLineToLogLineProps = (lines: string[], timestamp?: Timestamp): LogLineProps[] => {
return lines.map((line) => ({
message: line,
timestamp: isDefined(timestamp) ? DateTime.fromJSDate(timestamp?.toDate()) : undefined,
}));
};
type ServiceLogsProps = {
enclave: EnclaveFullInfo;
service: ServiceInfo;
};
export const ServiceLogs = ({ enclave, service }: ServiceLogsProps) => {
const kurtosisClient = useKurtosisClient();
const [logLines, setLogLines] = useState<LogLineProps[]>([]);
useEffect(() => {
let cancelled = false;
const abortController = new AbortController();
(async () => {
setLogLines([]);
try {
for await (const lineGroup of await kurtosisClient.getServiceLogs(abortController, enclave, [service])) {
if (cancelled) {
return;
}
const lineGroupForService = lineGroup.serviceLogsByServiceUuid[service.serviceUuid];
if (!isDefined(lineGroupForService)) {
continue;
}
const parsedLines = serviceLogLineToLogLineProps(lineGroupForService.line, lineGroupForService.timestamp);
setLogLines((logLines) => [...logLines, ...parsedLines]);
}
} catch (error: any) {
if (cancelled) {
return;
}
setLogLines((logLines) => [...logLines, { message: `Error: ${stringifyError(error)}`, status: "error" }]);
}
})();
return () => {
cancelled = true;
abortController.abort();
};
}, [enclave, service, kurtosisClient]);
return <LogViewer logLines={logLines} />;
};