Etop.Utils (Etop v0.7.0)
Utility helpers for Etop.
Link to this section Summary
Functions
Center a string in the given length.
Run a short, but heavy load on the system.
Round a number and convert to a string.
Returns the server's local naive datetime with the microsecond field truncated to the given precision (:microsecond, :millisecond or :second).
Pad (leading) the given string with spaces for the given length.
Pad (trailing) the given string with spaces for the given length.
Runs the run_load/0 num times, sleeping for 1 second between them.
Scale a number into xb unit with label.
Scale a number into xb unit with label.
Configurable sort.
Get the server's timezone offset in seconds.
Link to this section Functions
center(item, len, char \\ " ")
Specs
Center a string in the given length.
Return a string of length >= the given length with the given string centered.
The returned string is padded (leading and trailing) with the given padding (default " ")
Examples
iex> Etop.Utils.center("Test", 8)
" Test "
iex> Etop.Utils.center('Test', 7, "-")
"-Test--"
iex> Etop.Utils.center("test", 2)
"test" creat_load(count)
create_load()
create_load(count, load)
Run a short, but heavy load on the system.
Runs a tight loop for 1 = 1..5M, i * 10 + 4.
float_to_string(size, rnd)
Specs
Round a number and convert to a string.
iex> Etop.Utils.float_to_string(1.125, 2)
"1.13"
iex> Etop.Utils.float_to_string(1.125, 1)
"1.1"
iex> Etop.Utils.float_to_string(1.5, 0)
"2"
iex> Etop.Utils.float_to_string(100, 0)
"100" local_time(datetime \\ NaiveDateTime.utc_now(), precision \\ :second)
Specs
local_time(DateTime.t() | NaiveDateTime.t(), atom()) :: NaiveDateTime.t()
Returns the server's local naive datetime with the microsecond field truncated to the given precision (:microsecond, :millisecond or :second).
Arguments
- datetime (default utc_now)
- precision (default :second)
Examples
iex> datetime = Etop.Utils.local_time()
iex> datetime.year >= 2020
true
iex> datetime = Etop.Utils.local_time(:millisecond)
iex> elem(datetime.microsecond, 1)
3
iex> datetime = NaiveDateTime.utc_now()
iex> expected = NaiveDateTime.add(datetime, Etop.Utils.timezone_offset())
iex> Etop.Utils.local_time(datetime) == %{expected | microsecond: {0, 0}}
true
iex> datetime = NaiveDateTime.utc_now()
iex> expected = NaiveDateTime.add(datetime, Etop.Utils.timezone_offset())
iex> Etop.Utils.local_time(datetime, :microsecond) == expected
true pad(string, len, char \\ " ")
Specs
Pad (leading) the given string with spaces for the given length.
Examples
iex> Etop.Utils.pad("Test", 8)
" Test"
iex> Etop.Utils.pad("Test", 2)
"Test"
iex> Etop.Utils.pad(100, 4, "0")
"0100" pad_t(string, len, char \\ " ")
Specs
Pad (trailing) the given string with spaces for the given length.
Examples
iex> Etop.Utils.pad_t("Test", 8)
"Test "
iex> Etop.Utils.pad_t("Test", 2)
"Test"
iex> Etop.Utils.pad_t(10.1, 5, "0")
"10.10" run_load(num \\ 10, opts \\ [])
Runs the run_load/0 num times, sleeping for 1 second between them.
size_string_b(size, rnd \\ 2)
Specs
Scale a number into xb unit with label.
Examples
iex> Etop.Utils.size_string_b(100.123)
"100.12B"
iex> Etop.Utils.size_string_b(10.5, 0)
"11B"
iex> Etop.Utils.size_string_b(1500)
"1.46KB" size_string_kb(size, rnd \\ 2)
Specs
Scale a number into xb unit with label.
Examples
iex> Etop.Utils.size_string_kb(0.253)
"0.25KB"
iex> Etop.Utils.size_string_kb(0.253, 1)
"0.3KB"
iex> Etop.Utils.size_string_kb(1500)
"1.46MB"
iex> Etop.Utils.size_string_kb(1024 * 1024 * 3)
"3.0GB"
iex> Etop.Utils.size_string_kb(1024 * 1024 * 1024 * 2.5)
"2.5TB"
iex> Etop.Utils.size_string_kb(1024 * 1024 * 1024 * 1024 * 1.5, 0)
"2PB"
iex> Etop.Utils.size_string_kb(1024 * 1024 * 1024 * 1024 * 1024, 0)
"1EB" sort(list, field, opts \\ [])
Configurable sort.
Arguments
list- the enumerable to be sorted.field(:reductions_diff) - the field to be sorted on.field_fn(fn field -> &elem(&1, 1)[field] end) - function to get the field.sorter_fn(&>/2) -> Sort comparator (default descending)
Examples
iex> data = [one: %{a: 3, b: 2}, two: %{a: 1, b: 3}]
iex> Etop.Utils.sort(data, :b)
[two: %{a: 1, b: 3}, one: %{a: 3, b: 2}]
iex> data = [one: %{a: 3, b: 2}, two: %{a: 1, b: 3}]
iex> Etop.Utils.sort(data, :a, sorter: &<=/2)
[two: %{a: 1, b: 3}, one: %{a: 3, b: 2}]
iex> data = [%{a: 1, b: 2}, %{a: 2, b: 3}]
iex> Etop.Utils.sort(data, :a, mapper: & &1[:a])
[%{a: 2, b: 3}, %{a: 1, b: 2}]
iex> data = [x: %{a: 1, b: 1}, z: %{a: 2, b: 0}, y: %{a: 1, b: 2}]
iex> Etop.Utils.sort(data, :a, secondary: :b)
[z: %{a: 2, b: 0}, y: %{a: 1, b: 2}, x: %{a: 1, b: 1}]
iex> data = [w: %{a: 1, b: 3}, x: %{a: 1, b: 1}, z: %{a: 2, b: 0}, y: %{a: 1, b: 2}]
iex> data |> Etop.Utils.sort(:a, secondary: :b, mapper: &elem(&1, 1)) |> Keyword.keys()
[:z, :w, :y, :x] timezone_offset()
Specs
timezone_offset() :: integer()
Get the server's timezone offset in seconds.