forked from y-scope/yscope-log-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClpIrDecoder.ts
147 lines (122 loc) · 4.34 KB
/
ClpIrDecoder.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
144
145
146
147
import clpFfiJsModuleInit, {ClpStreamReader} from "clp-ffi-js";
import {Dayjs} from "dayjs";
import {Nullable} from "../../typings/common";
import {
Decoder,
DecodeResult,
DecoderOptions,
FilteredLogEventMap,
LogEventCount,
} from "../../typings/decoders";
import {Formatter} from "../../typings/formatters";
import {JsonObject} from "../../typings/js";
import {LogLevelFilter} from "../../typings/logs";
import YscopeFormatter from "../formatters/YscopeFormatter";
import {postFormatPopup} from "../MainWorker";
import {
convertToDayjsTimestamp,
isJsonObject,
} from "./JsonlDecoder/utils";
enum CLP_IR_STREAM_TYPE {
STRUCTURED = "structured",
UNSTRUCTURED = "unstructured",
}
class ClpIrDecoder implements Decoder {
#streamReader: ClpStreamReader;
readonly #streamType: CLP_IR_STREAM_TYPE;
#formatter: Nullable<Formatter> = null;
constructor (
streamType: CLP_IR_STREAM_TYPE,
streamReader: ClpStreamReader,
decoderOptions: DecoderOptions
) {
this.#streamType = streamType;
this.#streamReader = streamReader;
if (streamType === CLP_IR_STREAM_TYPE.STRUCTURED) {
this.#formatter = new YscopeFormatter({formatString: decoderOptions.formatString});
if (0 === decoderOptions.formatString.length) {
postFormatPopup();
}
}
}
/**
* Creates a new ClpIrDecoder instance.
* NOTE: `decoderOptions` only affects decode results if the stream type is
* {@link CLP_IR_STREAM_TYPE.STRUCTURED}.
*
* @param dataArray The input data array to be passed to the decoder.
* @param decoderOptions
* @return The created ClpIrDecoder instance.
*/
static async create (
dataArray: Uint8Array,
decoderOptions: DecoderOptions
): Promise<ClpIrDecoder> {
const module = await clpFfiJsModuleInit();
const streamReader = new module.ClpStreamReader(dataArray, decoderOptions);
const streamType = streamReader.getIrStreamType() === module.IrStreamType.STRUCTURED ?
CLP_IR_STREAM_TYPE.STRUCTURED :
CLP_IR_STREAM_TYPE.UNSTRUCTURED;
return new ClpIrDecoder(streamType, streamReader, decoderOptions);
}
getEstimatedNumEvents (): number {
return this.#streamReader.getNumEventsBuffered();
}
getFilteredLogEventMap (): FilteredLogEventMap {
return this.#streamReader.getFilteredLogEventMap();
}
setLogLevelFilter (logLevelFilter: LogLevelFilter): boolean {
this.#streamReader.filterLogEvents(logLevelFilter);
return true;
}
build (): LogEventCount {
return {
numInvalidEvents: 0,
numValidEvents: this.#streamReader.deserializeStream(),
};
}
setFormatterOptions (options: DecoderOptions): boolean {
this.#formatter = new YscopeFormatter({formatString: options.formatString});
return true;
}
decodeRange (
beginIdx: number,
endIdx: number,
useFilter: boolean
): Nullable<DecodeResult[]> {
const results: DecodeResult[] =
this.#streamReader.decodeRange(beginIdx, endIdx, useFilter);
if (null === this.#formatter) {
if (this.#streamType === CLP_IR_STREAM_TYPE.STRUCTURED) {
// eslint-disable-next-line no-warning-comments
// TODO: Revisit when we allow displaying structured logs without a formatter.
console.error("Formatter is not set for structured logs.");
}
return results;
}
for (const r of results) {
const [
message,
timestamp,
level,
] = r;
const dayJsTimestamp: Dayjs = convertToDayjsTimestamp(timestamp);
let fields: JsonObject = {};
try {
fields = JSON.parse(message) as JsonObject;
if (false === isJsonObject(fields)) {
throw new Error("Unexpected non-object.");
}
} catch (e) {
console.error(e, message);
}
r[0] = this.#formatter.formatLogEvent({
fields: fields,
level: level,
timestamp: dayJsTimestamp,
});
}
return results;
}
}
export default ClpIrDecoder;