Skip to content

Commit

Permalink
refactor logs to download
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 committed Dec 4, 2023
1 parent 0b4b334 commit b2ce2d9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 56 deletions.
72 changes: 51 additions & 21 deletions enclave-manager/web/src/components/CopyLogsButton.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,93 @@
import { useState } from "react";import streamsaver from "streamsaver";
import { useKurtosisClient } from "../../../client/enclaveManager/KurtosisClientContext";
import { EnclaveFullInfo } from "../../../emui/enclaves/types";
import { DownloadButton } from "../../CopyButton";
import { useKurtosisClient } from "../client/enclaveManager/KurtosisClientContext";
import { EnclaveFullInfo } from "../emui/enclaves/types";
import { CopyButton } from "./CopyButton";
import {ServiceInfo} from "enclave-manager-sdk/build/api_container_service_pb";
import {isDefined, stripAnsi} from "../utils";
import {LogLineMessage} from "./enclaves/logs/types";
import { Timestamp } from "@bufbuild/protobuf";
import {DateTime} from "luxon";

type DownloadLogsButtonProps = {
type CopyLogsButtonProps = {
logsFileName:string,
enclave:EnclaveFullInfo,
service?:ServiceInfo,
logsToDownload: LogLineMessage[];
};

export const DownloadLogsButton = ({ enclave, service }: DownloadLogsButtonProps) => {
export const CopyLogsButton = ({ logsFileName, enclave, service, logsToDownload }: CopyLogsButtonProps) => {
const kurtosisClient = useKurtosisClient();
const [isLoading, setIsLoading] = useState(false);
const [logLinesToDownload, setLogLinesToDownload] = useState(propsLogLines);
const [logLinesToDownload, setLogLinesToDownload] = useState(logsToDownload);

const handleDownloadClick = async () => {
const serviceLogLineToLogLineMessage = (lines: string[], timestamp?: Timestamp): LogLineMessage[] => {
return lines.map((line) => ({
message: line,
timestamp: isDefined(timestamp) ? DateTime.fromJSDate(timestamp?.toDate()) : undefined,
}));
};

const handleCopyLogsClick = async () => {
setIsLoading(true);
const abortController = new AbortController();
const writableStream = streamsaver.createWriteStream(logsFileName || "logs.txt");
const writer = writableStream.getWriter();

if (service) {
if (service){
console.log("pulling logs")
const abortController = new AbortController();
for await (const lineGroup of await kurtosisClient.getServiceLogs(abortController, enclave, [service], false, 0, true)) {
const lineGroupForService = lineGroup.serviceLogsByServiceUuid[service.serviceUuid];
if (!isDefined(lineGroupForService)) continue;
const parsedLogLines = serviceLogLineToLogLineMessage(lineGroupForService.line, lineGroupForService.timestamp);
console.log("writing logs")
setLogLinesToDownload((logLinesToDownload) => [...logLinesToDownload, ...parsedLogLines]);
}
} else {
setLogLinesToDownload(() => [...logLines])

try {
console.log("downloading logs")
await writer.write(logLinesToDownload.map(({message}) => message)
.filter(isDefined)
.map(stripAnsi)
.join("\n"));
} catch(err) {
console.error(err)
}
await writer.close();
}

try {
console.log("downloading logs")
await writer.write(logLinesToDownload.map(({message}) => message)
.filter(isDefined)
.map(stripAnsi)
.join("\n"));
} catch(err) {
console.error(err)
if(logsToDownload) {
try {
console.log("copying logs")
await writer.write(getLogsValue())
} catch(err) {
console.error(err)
}
await writer.close();
}
await writer.close();
console.log("finished downloading logs")
setLogLinesToDownload(() => [])
setIsLoading(false);
};

const getLogsValue = () => {
return logsToDownload
.map(({ message }) => message)
.filter(isDefined)
.map(stripAnsi)
.join("\n");
};

return (
<CopyButton
contentName={"logs"}
valueToCopy={getLogsValue}
size={"sm"}
isDisabled={logLines.length === 0}
// isDisabled={logLinesToDownload.length === 0}
isIconButton
aria-label={"Copy logs"}
color={"gray.100"}
isLoading={isLoading}
onClick={handleCopyLogsClick}
/>
);
};
56 changes: 29 additions & 27 deletions enclave-manager/web/src/components/DownloadLogsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { useState } from "react";import streamsaver from "streamsaver";
import { useKurtosisClient } from "../client/enclaveManager/KurtosisClientContext";
import { EnclaveFullInfo } from "../emui/enclaves/types";
import { DownloadButton } from "./DownloadButton";
import {ServiceInfo} from "enclave-manager-sdk/build/api_container_service_pb";
import {ServiceInfo, StreamedDataChunk} from "enclave-manager-sdk/build/api_container_service_pb";
import {isDefined, stripAnsi} from "../utils";
import {Timestamp} from "@bufbuild/protobuf/dist/esm";
import { Timestamp } from "@bufbuild/protobuf";
import {LogLineMessage} from "./enclaves/logs/types";
import {DateTime} from "luxon";
import {Simulate} from "react-dom/test-utils";
import canPlayThrough = Simulate.canPlayThrough;

type DownloadLogsButtonProps = {
logsFileName:string,
Expand All @@ -18,7 +20,7 @@ type DownloadLogsButtonProps = {
export const DownloadLogsButton = ({ logsFileName, enclave, service, logsToDownload }: DownloadLogsButtonProps) => {
const kurtosisClient = useKurtosisClient();
const [isLoading, setIsLoading] = useState(false);
const [logLinesToDownload, setLogLinesToDownload] = useState(logsToDownload);
// const [logLinesToDownload, setLogLinesToDownload] = useState(logsToDownload);

const serviceLogLineToLogLineMessage = (lines: string[], timestamp?: Timestamp): LogLineMessage[] => {
return lines.map((line) => ({
Expand All @@ -27,7 +29,7 @@ export const DownloadLogsButton = ({ logsFileName, enclave, service, logsToDownl
}));
};

const handleDownloadClick = async () => {
const handleDownloadLogsClick = async () => {
setIsLoading(true);
const writableStream = streamsaver.createWriteStream(logsFileName || "logs.txt");
const writer = writableStream.getWriter();
Expand All @@ -36,30 +38,30 @@ export const DownloadLogsButton = ({ logsFileName, enclave, service, logsToDownl
console.log("pulling logs")
const abortController = new AbortController();
for await (const lineGroup of await kurtosisClient.getServiceLogs(abortController, enclave, [service], false, 0, true)) {
const lineGroupForService = lineGroup.serviceLogsByServiceUuid[service.serviceUuid];
if (!isDefined(lineGroupForService)) continue;
const parsedLogLines = serviceLogLineToLogLineMessage(lineGroupForService.line, lineGroupForService.timestamp);
console.log("writing logs")
setLogLinesToDownload((logLinesToDownload) => [...logLinesToDownload, ...parsedLogLines]);
try {
console.log(lineGroup)
const lineGroupForService = lineGroup.serviceLogsByServiceUuid[service.serviceUuid];
if (!isDefined(lineGroupForService)) {
console.error("smth went wrong")
continue;
}
const parsedLogLines = serviceLogLineToLogLineMessage(lineGroupForService.line, lineGroupForService.timestamp);
console.log("downloading logs")
const logBlob = new Blob([parsedLogLines.map(({message}) => message)
.filter(isDefined)
.map(stripAnsi)
.join("\n")])
await writer.write(logBlob);
} catch(err){
console.error(err)
}
}

try {
console.log("downloading logs")
await writer.write(logLinesToDownload.map(({message}) => message)
.filter(isDefined)
.map(stripAnsi)
.join("\n"));
} catch(err) {
console.error(err)
}
await writer.close();
}


if(logsToDownload) {

} else if (logsToDownload){
console.log("downloading logs")
await writer.write(getLogsValue())
}

await writer.close();
console.log("finished downloading logs")
setIsLoading(false);
};
Expand All @@ -76,12 +78,12 @@ export const DownloadLogsButton = ({ logsFileName, enclave, service, logsToDownl
<DownloadButton
size={"sm"}
fileName={logsFileName || `logs.txt`}
isDisabled={logLinesToDownload.length === 0}
// isDisabled={logsToDownload.length === 0}
isIconButton
aria-label={"Download logs"}
color={"gray.100"}
isLoading={isLoading}
onClick={handleDownloadClick()}
onClick={handleDownloadLogsClick}
/>
);
};
14 changes: 6 additions & 8 deletions enclave-manager/web/src/components/enclaves/logs/LogViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { LogLineMessage } from "./types";
import { normalizeLogText } from "./utils";
import {EnclaveFullInfo} from "../../../emui/enclaves/types";
import {ServiceInfo} from "enclave-manager-sdk/build/api_container_service_pb";
import {CopyLogsButton} from "../../CopyLogsButton";

type LogViewerProps = {
logLines: LogLineMessage[];
Expand Down Expand Up @@ -184,14 +185,11 @@ export const LogViewer = ({
</FormLabel>
</FormControl>
<ButtonGroup>
<CopyButton
contentName={"logs"}
valueToCopy={getLogsValue}
size={"sm"}
isDisabled={logLines.length === 0}
isIconButton
aria-label={"Copy logs"}
color={"gray.100"}
<CopyLogsButton
logsFileName={logsFileName || "logs.txt"}
enclave={enclave}
service={service}
logsToDownload={logLines}
/>
<DownloadLogsButton
logsFileName={logsFileName || "logs.txt"}
Expand Down

0 comments on commit b2ce2d9

Please sign in to comment.