Skip to content

Commit

Permalink
Move blob & numChunks to LogExportManager; Move downloadDecompressedL…
Browse files Browse the repository at this point in the history
…ogs to utils folder
  • Loading branch information
Henry8192 committed Sep 16, 2024
1 parent b9ab9dd commit da3f0be
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
26 changes: 13 additions & 13 deletions new-log-viewer/src/contexts/StateContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
useState,
} from "react";

import LogExportManager from "../services/LogExportManager";
import {Nullable} from "../typings/common";
import {CONFIG_KEY} from "../typings/config";
import {SEARCH_PARAM_NAMES} from "../typings/url";
Expand Down Expand Up @@ -146,30 +147,24 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
const numPagesRef = useRef<number>(STATE_DEFAULT.numPages);
const pageNumRef = useRef<Nullable<number>>(STATE_DEFAULT.pageNum);
const receivedNumChunksRef = useRef<number>(0);
const logExportManagerRef = useRef<LogExportManager>(new LogExportManager());

const mainWorkerRef = useRef<null|Worker>(null);

logExportManagerRef.current.setNumChunks(Math.ceil(numEvents / EXPORT_LOGS_CHUNK_SIZE));
console.error("beforeDownload", logExportManagerRef.current.getNumChunks());

const handleMainWorkerResp = useCallback((ev: MessageEvent<MainWorkerRespMessage>) => {
const {code, args} = ev.data;

// Create a file blob and push the data inside
const blob = new Blob();
const url = URL.createObjectURL(blob);
const numChunks = Math.ceil(numEvents / EXPORT_LOGS_CHUNK_SIZE);

console.log(`[MainWorker -> Renderer] code=${code}`);
switch (code) {
case WORKER_RESP_CODE.CHUNK_DATA:
receivedNumChunksRef.current += 1;
logExportManagerRef.current.appendChunkData(args.logs);

// If all chunks are received, trigger the download of the file
if (numChunks === receivedNumChunksRef.current) {
const link = document.createElement("a");
link.href = url;
link.download = `${fileName}-exported-${new Date().toISOString()
.replace(/[:.]/g, "-")}.log`;
link.click();
URL.revokeObjectURL(url);
if (logExportManagerRef.current.getNumChunks() === receivedNumChunksRef.current) {
logExportManagerRef.current.download(fileName);
}
break;
case WORKER_RESP_CODE.LOG_FILE_INFO:
Expand Down Expand Up @@ -202,6 +197,11 @@ const StateContextProvider = ({children}: StateContextProviderProps) => {
return;
}
receivedNumChunksRef.current = 0;

// FIXME: uncomment the line below to observe the error

Check warning on line 201 in new-log-viewer/src/contexts/StateContextProvider.tsx

View workflow job for this annotation

GitHub Actions / lint-check

Unexpected 'fixme' comment: 'FIXME: uncomment the line below to...'
// logExportManagerRef.current.reset(Math.ceil(numEvents / EXPORT_LOGS_CHUNK_SIZE));
console.error("numEvents", numEvents);
console.error(logExportManagerRef.current.getNumChunks());
workerPostReq(
mainWorkerRef.current,
WORKER_REQ_CODE.EXPORT_LOG,
Expand Down
42 changes: 42 additions & 0 deletions new-log-viewer/src/services/LogExportManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import downloadDecompressedLogs from "../utils/downloadDecompressedLogs";


class LogExportManager {
private blob: Blob;

private numChunks: number;

constructor (numChunks?: number) {
this.blob = new Blob();
this.numChunks = numChunks ?? 0;
}

getBlob (): Blob {
return this.blob;
}

appendChunkData (chunkData: string) {
this.blob = new Blob([this.blob,
chunkData], {type: "text/plain"});
}

getNumChunks (): number {
return this.numChunks;
}

setNumChunks (numChunks: number) {
this.numChunks = numChunks;
}

download (fileName: string) {
// FIXME: eslint complains about this format

Check warning on line 32 in new-log-viewer/src/services/LogExportManager.ts

View workflow job for this annotation

GitHub Actions / lint-check

Unexpected 'fixme' comment: 'FIXME: eslint complains about this...'
downloadDecompressedLogs({blob: this.blob, fileName});

Check failure on line 33 in new-log-viewer/src/services/LogExportManager.ts

View workflow job for this annotation

GitHub Actions / lint-check

Unexpected mix of shorthand and non-shorthand properties
}

reset (numChunks?: number) {
this.blob = new Blob();
this.numChunks = numChunks ?? 0;
}
}

export default LogExportManager;
3 changes: 3 additions & 0 deletions new-log-viewer/src/services/MainWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ onmessage = async (ev: MessageEvent<MainWorkerReqMessage>) => {
if (null === LOG_FILE_MANAGER) {
throw new Error("Log file manager hasn't been initialized");
}
if ("undefined" !== typeof args.decoderOptions) {
LOG_FILE_MANAGER.setDecoderOptions(args.decoderOptions);
}

let decodedEventIdx = 0;
while (LOG_FILE_MANAGER.numEvents > decodedEventIdx) {
Expand Down
23 changes: 23 additions & 0 deletions new-log-viewer/src/utils/downloadDecompressedLogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface downloadDecompressedLogsProps {
blob: Blob,
fileName: string,
}

/**

Check warning on line 6 in new-log-viewer/src/utils/downloadDecompressedLogs.ts

View workflow job for this annotation

GitHub Actions / lint-check

Missing JSDoc block description
*
* @param blob.blob
* @param blob
* @param fileName

Check warning on line 10 in new-log-viewer/src/utils/downloadDecompressedLogs.ts

View workflow job for this annotation

GitHub Actions / lint-check

@param "fileName" does not match an existing function parameter
* @param blob.fileName
*/
const downloadDecompressedLogs = ({blob, fileName}: downloadDecompressedLogsProps) => {
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = `${fileName}-exported-${new Date().toISOString()
.replace(/[:.]/g, "-")}.log`;
link.click();
URL.revokeObjectURL(url);
};

export default downloadDecompressedLogs;

0 comments on commit da3f0be

Please sign in to comment.