Skip to content

Commit

Permalink
feat!: Add support for hierarchical and auto-generated keys for log f…
Browse files Browse the repository at this point in the history
…iltering. (#62)

Co-authored-by: davemarco <83603688+davemarco@users.noreply.github.com>
  • Loading branch information
LinZhihao-723 and davemarco authored Feb 28, 2025
1 parent 4299d4c commit 737c9d5
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 98 deletions.
3 changes: 2 additions & 1 deletion src/clp_ffi_js/ir/StreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ EMSCRIPTEN_BINDINGS(ClpStreamReader) {
emscripten::register_type<clp_ffi_js::ir::DataArrayTsType>("Uint8Array");
emscripten::register_type<clp_ffi_js::ir::LogLevelFilterTsType>("number[] | null");
emscripten::register_type<clp_ffi_js::ir::ReaderOptions>(
"{logLevelKey: string, timestampKey: string} | null"
"{logLevelKey: {isAutoGenerated: boolean; parts: string[];} | null;"
" timestampKey: {isAutoGenerated: boolean; parts: string[];} | null;}"
);

// JS types used as outputs
Expand Down
43 changes: 40 additions & 3 deletions src/clp_ffi_js/ir/StructuredIrStreamReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstddef>
#include <format>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <system_error>
Expand All @@ -11,6 +12,7 @@
#include <clp/Array.hpp>
#include <clp/ErrorCode.hpp>
#include <clp/ffi/ir_stream/Deserializer.hpp>
#include <clp/ffi/SchemaTree.hpp>
#include <clp/ir/types.hpp>
#include <clp/TraceableException.hpp>
#include <emscripten/bind.h>
Expand All @@ -27,6 +29,8 @@
namespace clp_ffi_js::ir {
namespace {
constexpr std::string_view cEmptyJsonStr{"{}"};
constexpr std::string_view cFilterOptionIsAutoGeneratedKey{"isAutoGenerated"};
constexpr std::string_view cFilterOptionPartsKey{"parts"};
constexpr std::string_view cReaderOptionsLogLevelKey{"logLevelKey"};
constexpr std::string_view cReaderOptionsTimestampKey{"timestampKey"};
constexpr std::string_view cMergedKvPairsAutoGeneratedKey{"auto-generated"};
Expand All @@ -39,12 +43,39 @@ constexpr std::string_view cMergedKvPairsUserGeneratedKey{"user-generated"};
* @param json
* @return Serialized JSON.
*/
auto dump_json_with_replace(nlohmann::json const& json) -> std::string;
[[nodiscard]] auto dump_json_with_replace(nlohmann::json const& json) -> std::string;

/**
* @param filter_option The JavaScript object representing a filter option.
* @param leaf_node_type The type of the leaf node for the schema tree full branch.
* @return A schema tree full branch constructed based on the provided filter option and leaf node
* type.
* @return A schema tree full branch constructed based on the provided reader option.
* @return std::nullopt if `option` is `null`.
*/
[[nodiscard]] auto get_schema_tree_full_branch_from_filter_option(
emscripten::val const& filter_option,
clp::ffi::SchemaTree::Node::Type leaf_node_type
) -> std::optional<StructuredIrUnitHandler::SchemaTreeFullBranch>;

auto dump_json_with_replace(nlohmann::json const& json) -> std::string {
return json.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
}

auto get_schema_tree_full_branch_from_filter_option(
emscripten::val const& filter_option,
clp::ffi::SchemaTree::Node::Type leaf_node_type
) -> std::optional<StructuredIrUnitHandler::SchemaTreeFullBranch> {
if (filter_option.isNull() || filter_option.isUndefined()) {
return std::nullopt;
}
return StructuredIrUnitHandler::SchemaTreeFullBranch{
filter_option[cFilterOptionIsAutoGeneratedKey.data()].as<bool>(),
emscripten::vecFromJSArray<std::string>(filter_option[cFilterOptionPartsKey.data()]),
leaf_node_type
};
}

EMSCRIPTEN_BINDINGS(ClpStructuredIrStreamReader) {
emscripten::constant(
"MERGED_KV_PAIRS_AUTO_GENERATED_KEY",
Expand All @@ -67,8 +98,14 @@ auto StructuredIrStreamReader::create(
*zstd_decompressor,
StructuredIrUnitHandler{
deserialized_log_events,
reader_options[cReaderOptionsLogLevelKey.data()].as<std::string>(),
reader_options[cReaderOptionsTimestampKey.data()].as<std::string>()
get_schema_tree_full_branch_from_filter_option(
reader_options[cReaderOptionsLogLevelKey.data()],
clp::ffi::SchemaTree::Node::Type::Str
),
get_schema_tree_full_branch_from_filter_option(
reader_options[cReaderOptionsTimestampKey.data()],
clp::ffi::SchemaTree::Node::Type::Int
)
}
)};
if (result.has_error()) {
Expand Down
Loading

0 comments on commit 737c9d5

Please sign in to comment.