|
| 1 | +defmodule AeMdw.Stats do |
| 2 | + @moduledoc """ |
| 3 | + Context module for dealing with Stats. |
| 4 | + """ |
| 5 | + |
| 6 | + alias AeMdw.Blocks |
| 7 | + alias AeMdw.Collection |
| 8 | + alias AeMdw.Db.Format |
| 9 | + alias AeMdw.Db.Model |
| 10 | + alias AeMdw.Mnesia |
| 11 | + |
| 12 | + require Model |
| 13 | + |
| 14 | + @type stat() :: map() |
| 15 | + @type sum_stat() :: map() |
| 16 | + @type cursor() :: binary() | nil |
| 17 | + |
| 18 | + @typep height() :: Blocks.height() |
| 19 | + @typep direction() :: Mnesia.direction() |
| 20 | + @typep limit() :: Mnesia.limit() |
| 21 | + @typep range() :: {:gen, Range.t()} | nil |
| 22 | + |
| 23 | + @table Model.Stat |
| 24 | + @sum_table Model.SumStat |
| 25 | + |
| 26 | + @spec fetch_stats(direction(), range(), cursor(), limit()) :: {[stat()], cursor()} |
| 27 | + def fetch_stats(direction, range, cursor, limit) do |
| 28 | + {:ok, {last_gen, -1}} = Mnesia.last_key(AeMdw.Db.Model.Block) |
| 29 | + |
| 30 | + range_scope = deserialize_scope(range) |
| 31 | + |
| 32 | + cursor_scope = |
| 33 | + case deserialize_cursor(cursor) do |
| 34 | + nil -> nil |
| 35 | + cursor when direction == :forward -> {cursor, cursor + limit + 1} |
| 36 | + cursor -> {cursor, cursor - limit - 1} |
| 37 | + end |
| 38 | + |
| 39 | + global_scope = if direction == :forward, do: {1, last_gen}, else: {last_gen, 1} |
| 40 | + |
| 41 | + case intersect_scopes([range_scope, cursor_scope, global_scope], direction) do |
| 42 | + {:ok, first, last} -> |
| 43 | + {gens, next_cursor} = Collection.paginate(first..last, limit) |
| 44 | + |
| 45 | + {render_stats(gens), serialize_cursor(next_cursor)} |
| 46 | + |
| 47 | + :error -> |
| 48 | + {[], nil} |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + @spec fetch_sum_stats(direction(), range(), cursor(), limit()) :: {[sum_stat()], cursor()} |
| 53 | + def fetch_sum_stats(direction, range, cursor, limit) do |
| 54 | + {:ok, {last_gen, -1}} = Mnesia.last_key(AeMdw.Db.Model.Block) |
| 55 | + |
| 56 | + range_scope = deserialize_scope(range) |
| 57 | + |
| 58 | + cursor_scope = |
| 59 | + case deserialize_cursor(cursor) do |
| 60 | + nil -> nil |
| 61 | + cursor when direction == :forward -> {cursor, cursor + limit + 1} |
| 62 | + cursor -> {cursor, cursor - limit - 1} |
| 63 | + end |
| 64 | + |
| 65 | + global_scope = if direction == :forward, do: {0, last_gen}, else: {last_gen, 0} |
| 66 | + |
| 67 | + case intersect_scopes([range_scope, cursor_scope, global_scope], direction) do |
| 68 | + {:ok, first, last} -> |
| 69 | + {gens, next_cursor} = Collection.paginate(first..last, limit) |
| 70 | + |
| 71 | + {render_sum_stats(gens), serialize_cursor(next_cursor)} |
| 72 | + |
| 73 | + :error -> |
| 74 | + {[], nil} |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + @spec fetch_stat!(height()) :: stat() |
| 79 | + def fetch_stat!(height), do: render_stat(Mnesia.fetch!(@table, height)) |
| 80 | + |
| 81 | + @spec fetch_sum_stat!(height()) :: sum_stat() |
| 82 | + def fetch_sum_stat!(height), do: render_sum_stat(Mnesia.fetch!(@sum_table, height)) |
| 83 | + |
| 84 | + defp intersect_scopes(scopes, direction) do |
| 85 | + scopes |
| 86 | + |> Enum.reject(&is_nil/1) |
| 87 | + |> Enum.reduce(fn |
| 88 | + {first, last}, {acc_first, acc_last} when direction == :forward -> |
| 89 | + {max(first, acc_first), min(last, acc_last)} |
| 90 | + |
| 91 | + {first, last}, {acc_first, acc_last} -> |
| 92 | + {min(first, acc_first), max(last, acc_last)} |
| 93 | + end) |
| 94 | + |> case do |
| 95 | + {first, last} when direction == :forward and first <= last -> {:ok, first, last} |
| 96 | + {_first, _last} when direction == :forward -> :error |
| 97 | + {first, last} when direction == :backward and first >= last -> {:ok, first, last} |
| 98 | + {_first, _last} when direction == :backward -> :error |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + defp render_stats(gens), do: Enum.map(gens, &fetch_stat!/1) |
| 103 | + |
| 104 | + defp render_sum_stats(gens), do: Enum.map(gens, &fetch_sum_stat!/1) |
| 105 | + |
| 106 | + defp render_stat(stat), do: Format.to_map(stat, @table) |
| 107 | + |
| 108 | + defp render_sum_stat(sum_stat), do: Format.to_map(sum_stat, @sum_table) |
| 109 | + |
| 110 | + defp serialize_cursor(nil), do: nil |
| 111 | + |
| 112 | + defp serialize_cursor(gen), do: Integer.to_string(gen) |
| 113 | + |
| 114 | + defp deserialize_cursor(nil), do: nil |
| 115 | + |
| 116 | + defp deserialize_cursor(cursor_bin) do |
| 117 | + case Integer.parse(cursor_bin) do |
| 118 | + {n, ""} when n >= 0 -> n |
| 119 | + {_n, _rest} -> nil |
| 120 | + :error -> nil |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + defp deserialize_scope(nil), do: nil |
| 125 | + |
| 126 | + defp deserialize_scope({:gen, %Range{first: first_gen, last: last_gen}}), |
| 127 | + do: {first_gen, last_gen} |
| 128 | +end |
0 commit comments