This is an automated email from the ASF dual-hosted git repository. iilyak pushed a commit to branch couch-stats-resource-tracker-v3-rebase-http-2 in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit e5de047c31280ffa3ed29a1de6e99241c615f4f5 Author: ILYA Khlopotov <[email protected]> AuthorDate: Wed Jun 25 08:20:25 2025 -0700 Move JSON conversion to csrt_entry --- src/couch_stats/src/csrt_entry.erl | 84 ++++++++++++++++++++++- src/couch_stats/src/csrt_util.erl | 85 +----------------------- src/couch_stats/test/eunit/csrt_server_tests.erl | 4 +- 3 files changed, 86 insertions(+), 87 deletions(-) diff --git a/src/couch_stats/src/csrt_entry.erl b/src/couch_stats/src/csrt_entry.erl index b92217f41..0c13ac1d9 100644 --- a/src/couch_stats/src/csrt_entry.erl +++ b/src/couch_stats/src/csrt_entry.erl @@ -22,11 +22,21 @@ record_info/0 ]). +%% JSON Conversion API +-export([ + convert_type/1, + convert_pidref/1, + convert_pid/1, + convert_ref/1, + convert_string/1, + to_json/1 +]). + -spec value(rctx_field(), #rctx{}) -> any(). value(pid_ref, #rctx{pid_ref = Val}) -> Val; value(nonce, #rctx{nonce = Val}) -> Val; -value(type, #rctx{type = Val}) -> csrt_util:convert_type(Val); +value(type, #rctx{type = Val}) -> convert_type(Val); value(dbname, #rctx{dbname = Val}) -> Val; value(username, #rctx{username = Val}) -> Val; value(db_open, #rctx{db_open = Val}) -> Val; @@ -154,3 +164,75 @@ record_info() -> field_idx => Idx, size => Size }. + +-spec to_json(Rctx :: rctx()) -> map(). +to_json(#rctx{} = Rctx) -> + #{ + updated_at => convert_string(csrt_util:tutc(Rctx#rctx.updated_at)), + started_at => convert_string(csrt_util:tutc(Rctx#rctx.started_at)), + pid_ref => convert_pidref(Rctx#rctx.pid_ref), + nonce => convert_string(Rctx#rctx.nonce), + dbname => convert_string(Rctx#rctx.dbname), + username => convert_string(Rctx#rctx.username), + db_open => Rctx#rctx.db_open, + docs_read => Rctx#rctx.docs_read, + docs_written => Rctx#rctx.docs_written, + js_filter => Rctx#rctx.js_filter, + js_filtered_docs => Rctx#rctx.js_filtered_docs, + rows_read => Rctx#rctx.rows_read, + type => convert_type(Rctx#rctx.type), + get_kp_node => Rctx#rctx.get_kp_node, + get_kv_node => Rctx#rctx.get_kv_node, + %% "Example to extend CSRT" + %% write_kp_node => Rctx#rctx.write_kp_node, + %% write_kv_node => Rctx#rctx.write_kv_node, + changes_returned => Rctx#rctx.changes_returned, + ioq_calls => Rctx#rctx.ioq_calls + }. + +%% +%% Conversion API for outputting JSON +%% + +-spec convert_type(T) -> binary() | null when + T :: #coordinator{} | #rpc_worker{} | undefined. +convert_type(#coordinator{method = Verb0, path = Path, mod = M0, func = F0}) -> + M = atom_to_binary(M0), + F = atom_to_binary(F0), + Verb = atom_to_binary(Verb0), + <<"coordinator-{", M/binary, ":", F/binary, "}:", Verb/binary, ":", Path/binary>>; +convert_type(#rpc_worker{mod = M0, func = F0, from = From0}) -> + M = atom_to_binary(M0), + F = atom_to_binary(F0), + %% Technically From is a PidRef data type from Pid, but different Ref for fabric + From = convert_pidref(From0), + <<"rpc_worker-{", From/binary, "}:", M/binary, ":", F/binary>>; +convert_type(undefined) -> + null. + +-spec convert_pidref(PidRef) -> binary() | null when + PidRef :: {A :: pid(), B :: reference()} | undefined. +convert_pidref({Parent0, ParentRef0}) -> + Parent = convert_pid(Parent0), + ParentRef = convert_ref(ParentRef0), + <<Parent/binary, ":", ParentRef/binary>>; +%%convert_pidref(null) -> +%% null; +convert_pidref(undefined) -> + null. + +-spec convert_pid(Pid :: pid()) -> binary(). +convert_pid(Pid) when is_pid(Pid) -> + list_to_binary(pid_to_list(Pid)). + +-spec convert_ref(Ref :: reference()) -> binary(). +convert_ref(Ref) when is_reference(Ref) -> + list_to_binary(ref_to_list(Ref)). + +-spec convert_string(Str :: string() | binary() | undefined) -> binary() | null. +convert_string(undefined) -> + null; +convert_string(Str) when is_list(Str) -> + list_to_binary(Str); +convert_string(Bin) when is_binary(Bin) -> + Bin. diff --git a/src/couch_stats/src/csrt_util.erl b/src/couch_stats/src/csrt_util.erl index cc4efb856..2e976f12c 100644 --- a/src/couch_stats/src/csrt_util.erl +++ b/src/couch_stats/src/csrt_util.erl @@ -26,16 +26,6 @@ tutc/1 ]). -%% JSON Conversion API --export([ - convert_type/1, - convert_pidref/1, - convert_pid/1, - convert_ref/1, - convert_string/1, - to_json/1 -]). - %% Delta API -export([ add_delta/2, @@ -56,8 +46,7 @@ %% Extra niceties and testing facilities -export([ set_fabric_init_p/2, - set_fabric_init_p/3, - rctx_record_info/0 + set_fabric_init_p/3 ]). -include_lib("couch_stats_resource_tracker.hrl"). @@ -153,78 +142,6 @@ make_dt(A, B, Unit) when is_integer(A) andalso is_integer(B) andalso B > A -> 1 end. -%% -%% Conversion API for outputting JSON -%% - --spec convert_type(T) -> binary() | null when - T :: #coordinator{} | #rpc_worker{} | undefined. -convert_type(#coordinator{method = Verb0, path = Path, mod = M0, func = F0}) -> - M = atom_to_binary(M0), - F = atom_to_binary(F0), - Verb = atom_to_binary(Verb0), - <<"coordinator-{", M/binary, ":", F/binary, "}:", Verb/binary, ":", Path/binary>>; -convert_type(#rpc_worker{mod = M0, func = F0, from = From0}) -> - M = atom_to_binary(M0), - F = atom_to_binary(F0), - %% Technically From is a PidRef data type from Pid, but different Ref for fabric - From = convert_pidref(From0), - <<"rpc_worker-{", From/binary, "}:", M/binary, ":", F/binary>>; -convert_type(undefined) -> - null. - --spec convert_pidref(PidRef) -> binary() | null when - PidRef :: {A :: pid(), B :: reference()} | undefined. -convert_pidref({Parent0, ParentRef0}) -> - Parent = convert_pid(Parent0), - ParentRef = convert_ref(ParentRef0), - <<Parent/binary, ":", ParentRef/binary>>; -%%convert_pidref(null) -> -%% null; -convert_pidref(undefined) -> - null. - --spec convert_pid(Pid :: pid()) -> binary(). -convert_pid(Pid) when is_pid(Pid) -> - list_to_binary(pid_to_list(Pid)). - --spec convert_ref(Ref :: reference()) -> binary(). -convert_ref(Ref) when is_reference(Ref) -> - list_to_binary(ref_to_list(Ref)). - --spec convert_string(Str :: string() | binary() | undefined) -> binary() | null. -convert_string(undefined) -> - null; -convert_string(Str) when is_list(Str) -> - list_to_binary(Str); -convert_string(Bin) when is_binary(Bin) -> - Bin. - --spec to_json(Rctx :: rctx()) -> map(). -to_json(#rctx{} = Rctx) -> - #{ - updated_at => convert_string(tutc(Rctx#rctx.updated_at)), - started_at => convert_string(tutc(Rctx#rctx.started_at)), - pid_ref => convert_pidref(Rctx#rctx.pid_ref), - nonce => convert_string(Rctx#rctx.nonce), - dbname => convert_string(Rctx#rctx.dbname), - username => convert_string(Rctx#rctx.username), - db_open => Rctx#rctx.db_open, - docs_read => Rctx#rctx.docs_read, - docs_written => Rctx#rctx.docs_written, - js_filter => Rctx#rctx.js_filter, - js_filtered_docs => Rctx#rctx.js_filtered_docs, - rows_read => Rctx#rctx.rows_read, - type => convert_type(Rctx#rctx.type), - get_kp_node => Rctx#rctx.get_kp_node, - get_kv_node => Rctx#rctx.get_kv_node, - %% "Example to extend CSRT" - %% write_kp_node => Rctx#rctx.write_kp_node, - %% write_kv_node => Rctx#rctx.write_kv_node, - changes_returned => Rctx#rctx.changes_returned, - ioq_calls => Rctx#rctx.ioq_calls - }. - -spec add_delta(T :: term(), Delta :: maybe_delta()) -> term_delta(). add_delta(T, undefined) -> T; diff --git a/src/couch_stats/test/eunit/csrt_server_tests.erl b/src/couch_stats/test/eunit/csrt_server_tests.erl index 89da7b4b3..3a1e09875 100644 --- a/src/couch_stats/test/eunit/csrt_server_tests.erl +++ b/src/couch_stats/test/eunit/csrt_server_tests.erl @@ -497,7 +497,7 @@ pdbg(Str, Args) -> ?DEBUG_ENABLED andalso ?debugFmt(Str, Args). convert_pidref({_, _} = PidRef) -> - csrt_util:convert_pidref(PidRef); + csrt_entry:convert_pidref(PidRef); convert_pidref(PidRef) when is_binary(PidRef) -> PidRef; convert_pidref(false) -> @@ -517,7 +517,7 @@ rctx_assert(Rctx, Asserts0) -> }, Updates = #{ pid_ref => fun convert_pidref/1, - nonce => fun csrt_util:convert_string/1 + nonce => fun csrt_entry:convert_string/1 }, Asserts = maps:merge( DefaultAsserts,
