Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 80 lines (70 sloc) 3.076 kb
577ac9a @rzezeski Add visibility into transfers/handoff
rzezeski authored
1 %% -------------------------------------------------------------------
2 %%
3 %% Copyright (c) 2007-2012 Basho Technologies, Inc. All Rights Reserved.
4 %%
5 %% This file is provided to you under the Apache License,
6 %% Version 2.0 (the "License"); you may not use this file
7 %% except in compliance with the License. You may obtain
8 %% a copy of the License at
9 %%
10 %% http://www.apache.org/licenses/LICENSE-2.0
11 %%
12 %% Unless required by applicable law or agreed to in writing,
13 %% software distributed under the License is distributed on an
14 %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 %% KIND, either express or implied. See the License for the
16 %% specific language governing permissions and limitations
17 %% under the License.
18 %%
19 %% -------------------------------------------------------------------
20
21 %% @doc Functions for formatting data.
22
23 -module(riak_core_format).
24 -export([fmt/2,
25 human_size_fmt/2,
26 human_time_fmt/2]).
27
28 %% @doc Created a string `Str' based on the format string `FmtStr' and
29 %% list of args `Args'.
30 -spec fmt(string(), list()) -> Str::string().
31 fmt(FmtStr, Args) ->
32 lists:flatten(io_lib:format(FmtStr, Args)).
33
34 %% @doc Create a human friendly string `Str' for number of bytes
35 %% `Bytes' and format based on format string `Fmt'.
36 -spec human_size_fmt(string(), non_neg_integer()) -> Str::string().
37 human_size_fmt(Fmt, Bytes) ->
38 Fmt2 = Fmt ++ " ~s",
39 {Value, Units} = human_size(Bytes, ["B","KB","MB","GB","TB","PB"]),
40 fmt(Fmt2, [Value, Units]).
41
42 %% @doc Create a human friendly string `Str' for the given time in
43 %% microseconds `Micros'. Format according to format string
44 %% `Fmt'.
45 -spec human_time_fmt(string(), non_neg_integer()) -> Str::string().
46 human_time_fmt(Fmt, Micros) ->
47 Fmt2 = Fmt ++ " ~s",
48 {Value, Units} = human_time(Micros),
49 fmt(Fmt2, [Value, Units]).
50
51 %%%===================================================================
52 %%% Private
53 %%%===================================================================
54
55 %% @private
56 %%
57 %% @doc Formats a byte size into a human-readable size with units.
58 %% Thanks StackOverflow:
59 %% http://stackoverflow.com/questions/2163691/simpler-way-to-format-bytesize-in-a-human-readable-way
60 -spec human_size(non_neg_integer(), list()) -> iolist().
61 human_size(S, [_|[_|_] = L]) when S >= 1024 -> human_size(S/1024, L);
62 human_size(S, [M|_]) ->
63 {float(S), M}.
64
65 %% @private
66 %%
67 %% @doc Given a number of `Micros' returns a human friendly time
68 %% duration in the form of `{Value, Units}'.
69 -spec human_time(non_neg_integer()) -> {Value::number(), Units::string()}.
70 human_time(Micros) ->
71 human_time(Micros, {1000, "us"}, [{1000, "ms"}, {1000, "s"}, {60, "min"}, {60, "hr"}, {24, "d"}]).
72
73 -spec human_time(non_neg_integer(), {pos_integer(), string()},
74 [{pos_integer(), string()}]) ->
75 {number(), string()}.
76 human_time(T, {Divisor, Unit}, Units) when T < Divisor orelse Units == [] ->
77 {float(T), Unit};
78 human_time(T, {Divisor, _}, [Next|Units]) ->
79 human_time(T / Divisor, Next, Units).
Something went wrong with that request. Please try again.