-
Notifications
You must be signed in to change notification settings - Fork 11
feat(sfa): Add API for decoding all log events. #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import {LogEvent} from "./LogEvent.js"; | ||
| import {getModule} from "./module.js"; | ||
| import type {FileInfo} from "./types.js"; | ||
|
|
||
|
|
@@ -65,6 +66,18 @@ class ClpArchiveReader { | |
| return this.#getWasmReader().getFileInfos(); | ||
| } | ||
|
|
||
| /** | ||
| * Decodes all log events in global log-event-index order. | ||
| * | ||
| * @return Decoded log events. | ||
| * @throws {Error} If the reader has been closed. | ||
| */ | ||
| decodeAll (): LogEvent[] { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as discussed offline - can we add coverage for this new public WASM path? something like const events = reader.decodeAll();
expect(events).toHaveLength(Number(CLP_JSON_TEST_LOG_FILES_EXPECTED_EVENT_COUNT));
events.forEach((event, index) => {
expect(event).toBeInstanceOf(LogEvent);
expect(event.logEventIdx).toBe(BigInt(index));
expect(typeof event.timestamp).toBe("bigint");
expect(typeof event.message).toBe("string");
});
expect(reader.decodeAll()).toEqual(events); |
||
| return this.#getWasmReader() | ||
| .decodeAll() | ||
| .map((rawEvent) => new LogEvent(rawEvent)); | ||
| } | ||
|
|
||
| /** | ||
| * Releases the underlying WASM resources. After calling this method, the reader is no longer | ||
| * usable and any subsequent method calls will throw. | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||||||||||
| import type { | ||||||||||||||
| JsonObject, | ||||||||||||||
| JsonValue, | ||||||||||||||
| RawLogEvent, | ||||||||||||||
| } from "./types.js"; | ||||||||||||||
| import {isJsonObject} from "./utils.js"; | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Single log event from a CLP archive. | ||||||||||||||
| */ | ||||||||||||||
| class LogEvent implements RawLogEvent { | ||||||||||||||
| /** | ||||||||||||||
| * Global log event index. | ||||||||||||||
| */ | ||||||||||||||
| declare readonly logEventIdx: bigint; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Epoch timestamp. | ||||||||||||||
| */ | ||||||||||||||
|
Comment on lines
+18
to
+20
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would this be more clear?
Suggested change
|
||||||||||||||
| declare readonly timestamp: bigint; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Serialized message string. | ||||||||||||||
| */ | ||||||||||||||
| declare readonly message: string; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * @param rawEvent Raw log event interface returned by the WASM binding. | ||||||||||||||
| */ | ||||||||||||||
| constructor (rawEvent: RawLogEvent) { | ||||||||||||||
| Object.assign(this, rawEvent); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Parses the serialized message as a JSON object. | ||||||||||||||
| * | ||||||||||||||
| * @return The parsed object, or null if parsing fails or produces a non-object. | ||||||||||||||
| */ | ||||||||||||||
| getKvPairs (): Readonly<JsonObject> | null { | ||||||||||||||
| try { | ||||||||||||||
| const kvPairs = JSON.parse(this.message) as JsonValue; | ||||||||||||||
| if (false === isJsonObject(kvPairs)) { | ||||||||||||||
| return null; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return kvPairs; | ||||||||||||||
| } catch { | ||||||||||||||
| return null; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| export {LogEvent}; | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -66,13 +66,40 @@ auto SfaReader::get_file_infos() const -> FileInfoArrayTsType { | |||||||||||||||
| } | ||||||||||||||||
| return FileInfoArrayTsType{file_infos}; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| auto SfaReader::decode_all() -> LogEventArrayTsType { | ||||||||||||||||
| auto decoded_result{m_reader.decode_all()}; | ||||||||||||||||
| if (decoded_result.has_error()) { | ||||||||||||||||
| auto const error{decoded_result.error()}; | ||||||||||||||||
| auto const err_msg{fmt::format( | ||||||||||||||||
| "Failed to decode SFA archive: {} - {}.", | ||||||||||||||||
| error.category().name(), | ||||||||||||||||
| error.message() | ||||||||||||||||
| )}; | ||||||||||||||||
| SPDLOG_ERROR("{}", err_msg); | ||||||||||||||||
| throw std::runtime_error{err_msg}; | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we use
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| auto decoded_events{emscripten::val::array()}; | ||||||||||||||||
| for (auto const& event : decoded_result.value()) { | ||||||||||||||||
| auto entry{emscripten::val::object()}; | ||||||||||||||||
| entry.set("logEventIdx", emscripten::val(event.get_log_event_idx())); | ||||||||||||||||
| entry.set("timestamp", emscripten::val(event.get_timestamp())); | ||||||||||||||||
| entry.set("message", emscripten::val(event.get_message())); | ||||||||||||||||
| decoded_events.call<void>("push", entry); | ||||||||||||||||
| } | ||||||||||||||||
| return LogEventArrayTsType{decoded_events}; | ||||||||||||||||
| } | ||||||||||||||||
| } // namespace clp_ffi_js::sfa | ||||||||||||||||
|
|
||||||||||||||||
| EMSCRIPTEN_BINDINGS(SfaReader) { | ||||||||||||||||
| emscripten::register_type<clp_ffi_js::sfa::FileInfoArrayTsType>( | ||||||||||||||||
| "Array<{fileName: string, logEventIdxStart: bigint, logEventIdxEnd: bigint, " | ||||||||||||||||
| "logEventCount: bigint}>" | ||||||||||||||||
| ); | ||||||||||||||||
| emscripten::register_type<clp_ffi_js::sfa::LogEventArrayTsType>( | ||||||||||||||||
| "Array<{logEventIdx: bigint, timestamp: bigint, message: string}>" | ||||||||||||||||
| ); | ||||||||||||||||
|
|
||||||||||||||||
| emscripten::class_<clp_ffi_js::sfa::SfaReader>("ClpSfaReader") | ||||||||||||||||
| .constructor( | ||||||||||||||||
|
|
@@ -81,5 +108,6 @@ EMSCRIPTEN_BINDINGS(SfaReader) { | |||||||||||||||
| ) | ||||||||||||||||
| .function("getEventCount", &clp_ffi_js::sfa::SfaReader::get_event_count) | ||||||||||||||||
| .function("getFileNames", &clp_ffi_js::sfa::SfaReader::get_file_names) | ||||||||||||||||
| .function("getFileInfos", &clp_ffi_js::sfa::SfaReader::get_file_infos); | ||||||||||||||||
| .function("getFileInfos", &clp_ffi_js::sfa::SfaReader::get_file_infos) | ||||||||||||||||
| .function("decodeAll", &clp_ffi_js::sfa::SfaReader::decode_all); | ||||||||||||||||
| } | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.