-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stats adding multiple times same summary (#1141)
Do not store each summary stats as only last one is used
- Loading branch information
Showing
2 changed files
with
84 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule Migration_1_2_4 do | ||
@moduledoc """ | ||
Erase stats file to keep only values of last beacon summary | ||
""" | ||
|
||
alias Archethic.DB.EmbeddedImpl | ||
|
||
def run() do | ||
db_path = EmbeddedImpl.db_path() | ||
filepath = Path.join(db_path, "stats") | ||
filepath_backup = Path.join(db_path, "stats.backup") | ||
|
||
# make a backup | ||
:ok = File.rename(filepath, filepath_backup) | ||
|
||
fd = File.open!(filepath_backup) | ||
|
||
{last_update, tps, nb_transactions, burned_fees} = load_from_file(fd) | ||
|
||
File.write!( | ||
filepath, | ||
<<DateTime.to_unix(last_update)::32, tps::float-64, nb_transactions::64, burned_fees::64>> | ||
) | ||
|
||
# remove backup | ||
File.rm(filepath_backup) | ||
end | ||
|
||
defp load_from_file(fd, acc \\ {0.0, 0, 0}) do | ||
# Read each stats entry 28 bytes: 4(timestamp) + 8(tps) + 8(nb transactions) + 8(burned_fees) | ||
case :file.read(fd, 28) do | ||
{:ok, <<timestamp::32, tps::float-64, nb_transactions::64, burned_fees::64>>} -> | ||
{_, prev_nb_transactions, _} = acc | ||
|
||
load_from_file( | ||
fd, | ||
{DateTime.from_unix!(timestamp), tps, prev_nb_transactions + nb_transactions, | ||
burned_fees} | ||
) | ||
|
||
:eof -> | ||
acc | ||
end | ||
end | ||
end |