- [ cons([N, <<"io">> , <<"bytes_in">> ], IOBytesIn , Ts)
- , cons([N, <<"io">> , <<"bytes_out">>], IOBytesOut , Ts)
- , cons([N, <<"context_switches">> ], ContextSwitches, Ts)
- , cons([N, <<"reductions">> ], Reductions , Ts)
- , cons([N, <<"run_queue">> ], RunQueue , Ts)
- | of_memory(Memory, NodeID, Ts)
- ]
- ++ of_ets(ETS, NodeID, Ts)
- ++ of_processes(Processes, NodeID, Ts).
+ Msgs =
+ [ cons([N, <<"io">> , <<"bytes_in">> ], IOBytesIn , Ts)
+ , cons([N, <<"io">> , <<"bytes_out">>], IOBytesOut , Ts)
+ , cons([N, <<"context_switches">> ], ContextSwitches, Ts)
+ , cons([N, <<"reductions">> ], Reductions , Ts)
+ , cons([N, <<"run_queue">> ], RunQueue , Ts)
+ | of_memory(Memory, NodeID, Ts)
+ ]
+ ++ of_ets(ETS, NodeID, Ts)
+ ++ of_processes(Processes, NodeID, Ts),
+ lists:map(fun path_prefix_schema_version/1, Msgs).
+
+-spec to_iolist(t()) ->
+ iolist().
+to_iolist(
+ ?T
+ { path = Path
+ , value = Value
+ , timestamp = Timestamp
+ }
+) ->
+ PathIOList = path_to_iolist(Path),
+ ValueBin = integer_to_binary(Value),
+ TimestampInt = timestamp_to_integer(Timestamp),
+ TimestampBin = integer_to_binary(TimestampInt),
+ [PathIOList, <<" ">>, ValueBin, <<" ">>, TimestampBin].
+
+-spec path_to_iolist([binary()]) ->
+ iolist().
+path_to_iolist(Path) ->
+ interleave(Path, <<".">>).
+
+-spec node_id_to_bin(node()) ->
+ binary().
+node_id_to_bin(NodeID) ->
+ NodeIDBin = atom_to_binary(NodeID, utf8),
+ re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).
+
+%% ============================================================================
+%% Helpers
+%% ============================================================================
+
+-spec path_prefix_schema_version(t()) ->
+ t().
+path_prefix_schema_version(?T{}=T) ->
+ path_prefix(T, ?SCHEMA_VERSION).
+
+-spec path_prefix(t(), binary()) ->
+ t().
+path_prefix(?T{path=Path}=T, <<Prefix/binary>>) ->
+ T?T{path = [Prefix | Path]}.
+
+-spec interleave([A], A) ->
+ [A].
+interleave([], _) ->
+ [];
+interleave([X], _) ->
+ [X];
+interleave([X|Xs], Sep) ->
+ [X, Sep | interleave(Xs, Sep)].
+
+-spec timestamp_to_integer(erlang:timestamp()) ->
+ non_neg_integer().
+timestamp_to_integer({Megaseconds, Seconds, _}) ->
+ Megaseconds * 1000000 + Seconds.