forked from y-scope/yscope-log-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWorker.ts
143 lines (132 loc) · 4.7 KB
/
MainWorker.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import dayjs from "dayjs";
import dayjsTimezone from "dayjs/plugin/timezone";
import dayjsUtc from "dayjs/plugin/utc";
import {LOG_LEVEL} from "../typings/logs";
import {
ChunkResults,
MainWorkerReqMessage,
WORKER_REQ_CODE,
WORKER_RESP_CODE,
WorkerResp,
} from "../typings/worker";
import {EXPORT_LOGS_CHUNK_SIZE} from "../utils/config";
import LogFileManager from "./LogFileManager";
/* eslint-disable import/no-named-as-default-member */
dayjs.extend(dayjsUtc);
dayjs.extend(dayjsTimezone);
/* eslint-enable import/no-named-as-default-member */
/**
* Manager for the currently opened log file.
*/
let LOG_FILE_MANAGER : null | LogFileManager = null;
/**
* Sends a response to the renderer.
*
* @param code
* @param args
*/
const postResp = <T extends WORKER_RESP_CODE>(
code: T,
args: WorkerResp<T>
) => {
postMessage({code, args});
};
/**
* Post a response of a query chunk.
*
* @param chunkResults
*/
const chunkResultsHandler = (chunkResults: ChunkResults) => {
postResp(WORKER_RESP_CODE.CHUNK_RESULT, chunkResults);
};
// eslint-disable-next-line no-warning-comments
// TODO: Break this function up into smaller functions.
// eslint-disable-next-line max-lines-per-function,max-statements
onmessage = async (ev: MessageEvent<MainWorkerReqMessage>) => {
const {code, args} = ev.data;
console.log(`[Renderer -> MainWorker] code=${code}: args=${JSON.stringify(args)}`);
try {
switch (code) {
case WORKER_REQ_CODE.EXPORT_LOG: {
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 (decodedEventIdx < LOG_FILE_MANAGER.numEvents) {
postResp(
WORKER_RESP_CODE.CHUNK_DATA,
LOG_FILE_MANAGER.loadChunk(decodedEventIdx)
);
decodedEventIdx += EXPORT_LOGS_CHUNK_SIZE;
}
break;
}
case WORKER_REQ_CODE.LOAD_FILE: {
LOG_FILE_MANAGER = await LogFileManager.create(
args.fileSrc,
args.pageSize,
args.decoderOptions,
chunkResultsHandler
);
postResp(WORKER_RESP_CODE.LOG_FILE_INFO, {
fileName: LOG_FILE_MANAGER.fileName,
numEvents: LOG_FILE_MANAGER.numEvents,
});
postResp(
WORKER_RESP_CODE.PAGE_DATA,
LOG_FILE_MANAGER.loadPage(args.cursor)
);
break;
}
case WORKER_REQ_CODE.LOAD_PAGE:
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);
}
postResp(
WORKER_RESP_CODE.PAGE_DATA,
LOG_FILE_MANAGER.loadPage(args.cursor)
);
break;
case WORKER_REQ_CODE.QUERY_LOG:
if (null === LOG_FILE_MANAGER) {
throw new Error("Log file manager hasn't been initialized");
}
if (
"string" !== typeof args.searchString ||
"boolean" !== typeof args.isRegex ||
"boolean" !== typeof args.isCaseSensitive
) {
throw new Error("Invalid arguments for QUERY_LOG");
}
LOG_FILE_MANAGER.startQuery(
args.searchString,
args.isRegex,
args.isCaseSensitive
);
break;
default:
console.error(`Unexpected ev.data: ${JSON.stringify(ev.data)}`);
break;
}
} catch (e) {
console.error(e);
if (e instanceof Error) {
postResp(WORKER_RESP_CODE.NOTIFICATION, {
logLevel: LOG_LEVEL.ERROR,
message: e.message,
});
} else {
postResp(WORKER_RESP_CODE.NOTIFICATION, {
logLevel: LOG_LEVEL.FATAL,
message: "An error occurred in the worker that cannot be serialized. " +
`Check the browser console for more details. Type: ${typeof e}`,
});
}
}
};