|
1 | 1 | defmodule AeMdw.Sync.AsyncTasks.UpdateTxStats do |
2 | 2 | @moduledoc """ |
3 | | - Async work to update tx count, fee and stats without blocking sync. |
| 3 | + Temporary module to get rid of pending tasks. |
4 | 4 | """ |
5 | 5 | @behaviour AeMdw.Sync.AsyncTasks.Work |
6 | 6 |
|
7 | | - alias AeMdw.Db.Model |
8 | | - alias AeMdw.Db.WriteMutation |
9 | | - alias AeMdw.Log |
10 | | - alias AeMdw.Db.RocksDbCF |
11 | | - alias AeMdw.Db.State |
12 | | - |
13 | | - alias AeMdw.Sync.AsyncStoreServer |
14 | | - |
15 | | - require Model |
16 | | - require Logger |
17 | | - |
18 | | - @microsecs 1_000_000 |
19 | | - @seconds_per_day 24 * 3_600 |
20 | | - |
21 | 7 | @spec process(args :: list(), done_fn :: fun()) :: :ok |
22 | | - def process([started_at], done_fn) do |
23 | | - state = State.mem_state() |
24 | | - |
25 | | - state |
26 | | - |> State.get(Model.Stat, :tx_stats) |
27 | | - |> case do |
28 | | - {:ok, Model.stat(payload: {old_started_at, _old_stats})} when old_started_at > started_at -> |
29 | | - done_fn.() |
30 | | - :ok |
31 | | - |
32 | | - {:ok, _old_stats} -> |
33 | | - update_stats(state, started_at, done_fn) |
34 | | - |
35 | | - :not_found -> |
36 | | - update_stats(state, started_at, done_fn) |
37 | | - end |
38 | | - end |
39 | | - |
40 | | - defp update_stats(state, started_at, done_fn) do |
41 | | - {time_delta, :ok} = |
42 | | - :timer.tc(fn -> |
43 | | - tx_stats = |
44 | | - calculate_fees(state, started_at) |
45 | | - |
46 | | - write_mutation = |
47 | | - WriteMutation.new( |
48 | | - Model.Stat, |
49 | | - Model.stat(index: :tx_stats, payload: tx_stats) |
50 | | - ) |
51 | | - |
52 | | - AsyncStoreServer.write_mutations( |
53 | | - [write_mutation], |
54 | | - done_fn |
55 | | - ) |
56 | | - end) |
57 | | - |
58 | | - Log.info("[update_tx_stats] after #{time_delta / @microsecs}s") |
| 8 | + def process(_args, done_fn) do |
| 9 | + _task = Task.async(done_fn) |
59 | 10 |
|
60 | 11 | :ok |
61 | 12 | end |
62 | | - |
63 | | - defp calculate_fees(state, started_at) do |
64 | | - time_24hs_ago = started_at - @seconds_per_day * 1_000 |
65 | | - |
66 | | - with {:ok, {_time, tx_index_24hs_ago}} <- State.next(state, Model.Time, {time_24hs_ago, -1}), |
67 | | - {:ok, last_tx_index} <- State.prev(state, Model.Tx, nil), |
68 | | - time_48hs_ago <- time_24hs_ago - @seconds_per_day * 1_000, |
69 | | - {:ok, {_time, tx_index_48hs_ago}} <- State.next(state, Model.Time, {time_48hs_ago, -1}), |
70 | | - txs_count_24hs when txs_count_24hs > 0 <- last_tx_index - tx_index_24hs_ago + 1, |
71 | | - txs_count_48hs <- tx_index_24hs_ago - tx_index_48hs_ago, |
72 | | - trend <- Float.round((txs_count_24hs - txs_count_48hs) / txs_count_24hs, 2), |
73 | | - average_tx_fees_24hs when average_tx_fees_24hs > 0 <- |
74 | | - average_tx_fees(tx_index_24hs_ago, last_tx_index), |
75 | | - average_tx_fees_48hs <- average_tx_fees(tx_index_48hs_ago, tx_index_24hs_ago), |
76 | | - fee_trend <- |
77 | | - Float.round((average_tx_fees_24hs - average_tx_fees_48hs) / average_tx_fees_24hs, 2) do |
78 | | - {started_at, |
79 | | - {{txs_count_24hs, Float.to_string(trend)}, |
80 | | - {Float.to_string(average_tx_fees_24hs), Float.to_string(fee_trend)}}} |
81 | | - else |
82 | | - _error -> |
83 | | - {started_at, {{0, "0.0"}, {"0.0", "0.0"}}} |
84 | | - end |
85 | | - end |
86 | | - |
87 | | - defp average_tx_fees(start_txi, end_txi) do |
88 | | - txs_count = end_txi - start_txi + 1 |
89 | | - |
90 | | - if txs_count != 0 do |
91 | | - Model.Tx |
92 | | - |> RocksDbCF.stream(key_boundary: {start_txi, end_txi}) |
93 | | - |> Enum.reduce(0, fn Model.tx(fee: fee), acc -> |
94 | | - acc + fee |
95 | | - end) |
96 | | - |> then(&(&1 / txs_count)) |
97 | | - else |
98 | | - 0 |
99 | | - end |
100 | | - end |
101 | 13 | end |
0 commit comments