forked from basho/riak_cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriak_cs_diags.erl
More file actions
132 lines (107 loc) · 4.59 KB
/
riak_cs_diags.erl
File metadata and controls
132 lines (107 loc) · 4.59 KB
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
-module(riak_cs_diags).
-behaviour(gen_server).
-include("riak_cs.hrl").
%% API
-export([start_link/0,
print_manifests/2,
print_manifest/3]).
-define(INDENT_LEVEL, 4).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {}).
-spec start_link() -> {ok, pid()} | {error, term()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-spec print_manifests(binary() | string(), binary() | string()) -> ok.
print_manifests(Bucket, Key) when is_list(Bucket), is_list(Key) ->
print_manifests(list_to_binary(Bucket), list_to_binary(Key));
print_manifests(Bucket, Key) ->
Manifests = gen_server:call(?MODULE, {get_manifests, Bucket, Key}),
Rows = manifest_rows(orddict_values(Manifests)),
table:print(manifest_table_spec(), Rows).
-spec print_manifest(binary() | string(), binary() | string(), binary() | string()) -> ok.
print_manifest(Bucket, Key, Uuid) when is_list(Bucket), is_list(Key) ->
print_manifest(list_to_binary(Bucket), list_to_binary(Key), Uuid);
print_manifest(Bucket, Key, Uuid) when is_list(Uuid) ->
print_manifest(Bucket, Key, mochihex:to_bin(Uuid));
print_manifest(Bucket, Key, Uuid) ->
Manifests = gen_server:call(?MODULE, {get_manifests, Bucket, Key}),
{ok, Manifest} = orddict:find(Uuid, Manifests),
io:format("\n~s", [pr(Manifest)]).
-spec pr(tuple()) -> iolist().
pr(Record) ->
pr(Record, 0).
-spec pr(tuple(), non_neg_integer()) -> iolist().
pr(Record, Indent) ->
{'$lager_record', RecordName, Zipped} = lager:pr(Record, ?MODULE),
["\n", spaces(Indent), "#", atom_to_list(RecordName),
"\n", spaces(Indent), "--------------------\n",
[print_field(Field, Indent) || Field <- Zipped]].
print_field({_, undefined}, _) ->
"";
print_field({uuid, Uuid}, Indent) when is_binary(Uuid) ->
print_field({uuid, mochihex:to_hex(Uuid)}, Indent);
print_field({content_md5, Value}, Indent) when is_binary(Value) ->
print_field({content_md5, mochihex:to_hex(Value)}, Indent);
print_field({upload_id, Value}, Indent) when is_binary(Value) ->
print_field({upload_id, mochihex:to_hex(Value)}, Indent);
print_field({part_id, Value}, Indent) when is_binary(Value) ->
print_field({part_id, mochihex:to_hex(Value)}, Indent);
print_field({acl, Value}, Indent) ->
io_lib:format("~s~s = ~s\n\n", [spaces(Indent), acl, pr(Value, Indent + 1)]);
print_field({props, Props}, Indent) ->
io_lib:format("~s~s = ~s\n\n", [spaces(Indent), multipart,
print_multipart_manifest(Props, Indent)]);
print_field({parts, Parts}, Indent) ->
io_lib:format("~s~s = ~s\n\n", [spaces(Indent), parts,
[pr(P, Indent + 1) || P <- Parts]]);
print_field({Key, Value}, Indent) ->
io_lib:format("~s~s = ~p\n", [spaces(Indent), Key, Value]).
orddict_values(Dict) ->
[Val || {_, Val} <- orddict:to_list(Dict)].
spaces(Num) ->
[" " || _ <- lists:seq(1, Num*?INDENT_LEVEL)].
%% ====================================================================
%% Table Specifications and Record to Row conversions
%% ====================================================================
manifest_table_spec() ->
[{state, 20}, {deleted, 8}, {mp, 6}, {created, 28}, {uuid, 36},
{write_start_time, 23}, {delete_marked_time, 23}].
manifest_rows(Manifests) ->
[ [M?MANIFEST.state, deleted(M?MANIFEST.props),
riak_cs_mp_utils:is_multipart_manifest(M),
M?MANIFEST.created, mochihex:to_hex(M?MANIFEST.uuid),
M?MANIFEST.write_start_time, M?MANIFEST.delete_marked_time] || M <- Manifests].
print_multipart_manifest(Props, Indent) ->
case lists:keyfind(multipart, 1, Props) of
{multipart, MpManifest} ->
pr(MpManifest, Indent + 1);
_ ->
""
end.
deleted(Props) ->
lists:keymember(deleted, 1, Props).
%% ====================================================================
%% gen_server callbacks
%% ====================================================================
init([]) ->
{ok, #state{}}.
handle_call({get_manifests, Bucket, Key}, _From, State) ->
{ok, Pid} = riak_cs_utils:riak_connection(),
try
{ok, _, Manifests} = riak_cs_manifest:get_manifests(Pid, Bucket, Key),
{reply, Manifests, State}
catch _:_=E ->
{reply, {error, E}, State}
after
riak_cs_utils:close_riak_connection(Pid)
end.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.