forked from basho/riak_cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathriak_cs_storage_console.erl
More file actions
155 lines (141 loc) · 5.29 KB
/
riak_cs_storage_console.erl
File metadata and controls
155 lines (141 loc) · 5.29 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
%% ---------------------------------------------------------------------
%%
%% Copyright (c) 2007-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% ---------------------------------------------------------------------
%% @doc These functions are used by the riak-cs-storage command line script.
-module(riak_cs_storage_console).
-export([
batch/1,
status/1,
pause/1,
resume/1,
cancel/1
]).
-define(SAFELY(Code, Description),
try
Code
catch
Type:Reason ->
io:format("~s failed:~n ~p:~p~n",
[Description, Type, Reason]),
error
end).
-define(SCRIPT_NAME, "riak-cs-storage").
-define(BATCH_OPTIONS, [{recalc, $r, "recalc", boolean,
"recalculate all users for this period"
" (default=false)"}]).
%% @doc Kick off a batch of storage calculation, unless one is already
%% in progress.
batch(Opts) ->
?SAFELY(
case getopt:parse(?BATCH_OPTIONS, Opts) of
{ok, {Parsed, _Extra}} ->
case riak_cs_storage_d:start_batch(Parsed) of
ok ->
io:format("Batch storage calculation started.~n"),
ok;
{error, already_calculating} ->
io:format("Error: A batch storage calculation"
" is already in progress.~n"),
error
end;
{error, {OptReason, OptMessage}} ->
io:format("Error: ~p: ~p~n", [OptReason, OptMessage]),
getopt:usage(?BATCH_OPTIONS, lists:flatten([?SCRIPT_NAME, " batch"])),
error
end,
"Starting batch storage calculation").
%% @doc Find out what the storage daemon is up to.
status(_Opts) ->
?SAFELY(
begin
{ok, {State, Details}} = riak_cs_storage_d:status(),
print_state(State),
print_details(Details)
end,
"Checking storage calculation status").
print_state(idle) ->
io:format("There is no storage calculation in progress~n");
print_state(calculating) ->
io:format("A storage calculation is in progress~n");
print_state(paused) ->
io:format("A storage calculation is current paused~n").
cancel(_Opts) ->
?SAFELY(
case riak_cs_storage_d:cancel_batch() of
ok ->
io:format("The calculation was canceled.~n");
{error, no_batch} ->
io:format("No storage calculation was running.~n")
end,
"Canceling the storage calculation").
pause(_Opts) ->
?SAFELY(
case riak_cs_storage_d:pause_batch() of
ok ->
io:format("The calculation was paused.~n");
{error, no_batch} ->
io:format("No storage calculation was running.~n")
end,
"Pausing the storage calculation").
resume(_Opts) ->
?SAFELY(
case riak_cs_storage_d:resume_batch() of
ok ->
io:format("The calculation was resumed.~n");
{error, no_batch} ->
io:format("No calcluation was running.~n")
end,
"Resuming the storage calcluation").
%% @doc Pretty-print the status returned from the storage daemon.
print_details(Details) ->
[ begin
{HumanName, HumanValue} = human_detail(K, V),
io:format(" ~s: ~s~n", [HumanName, HumanValue])
end
|| {K, V} <- Details ].
human_detail(schedule, Schedule) ->
Human = case Schedule of
[] -> "none defined";
_ ->
%% convert the list of tuples to a comma-separated
%% stringy thing
string:join([io_lib:format("~2..0b~2..0b", [H, M])
|| {H, M} <- Schedule],
",")
end,
{"Schedule", Human};
human_detail(last, Time) ->
{"Last run started at", human_time(Time)};
human_detail(next, Time) ->
{"Next run scheduled for", human_time(Time)};
human_detail(current, Time) ->
{"Current run started at", human_time(Time)};
human_detail(elapsed, Elapsed) ->
{"Elapsed time of current run", integer_to_list(Elapsed)};
human_detail(users_done, Count) ->
{"Users completed in current run", integer_to_list(Count)};
human_detail(users_skipped, Count) ->
{"Users skipped in current run", integer_to_list(Count)};
human_detail(users_left, Count) ->
{"Users left in current run", integer_to_list(Count)};
human_detail(Name, Value) ->
%% anything not to bomb if something was added
{io_lib:format("~p", [Name]), io_lib:format("~p", [Value])}.
human_time(undefined) -> "unknown/never";
human_time(Datetime) -> rts:iso8601(Datetime).