Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancements on reward per month #877

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/archethic/reward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ defmodule Archethic.Reward do
defp number_of_reward_occurences_per_month() do
datetime = NaiveDateTime.utc_now()

key =
case {datetime.month, Date.leap_year?(datetime)} do
{2, true} -> 0
{month_num, _} -> month_num
end
key = Utils.get_key_from_date(datetime)

Map.get(@number_of_occurences_per_month_for_a_year, key)
end
Expand Down
86 changes: 48 additions & 38 deletions lib/archethic/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -814,59 +814,69 @@ defmodule Archethic.Utils do

iex> Utils.number_of_possible_reward_occurences_per_month_for_a_year("0 0 2 * * * *")
%{
0 => 29,
1 => 31,
2 => 28,
3 => 31,
4 => 30,
5 => 31,
6 => 30,
7 => 31,
8 => 31,
9 => 30,
10 => 31,
11 => 30,
12 => 31
0 => 31,
1 => 30,
2 => 29,
3 => 28
}
"""
@spec number_of_possible_reward_occurences_per_month_for_a_year(String.t()) :: map
def number_of_possible_reward_occurences_per_month_for_a_year(
interval \\ Application.get_env(:archethic, RewardScheduler)[:interval]
) do
normal_year = [
months = [
# 31 days
~N[2023-01-01 00:00:00.000000],
~N[2023-02-01 00:00:00.000000],
~N[2023-03-01 00:00:00.000000],
# 30 days
~N[2023-04-01 00:00:00.000000],
~N[2023-05-01 00:00:00.000000],
~N[2023-06-01 00:00:00.000000],
~N[2023-07-01 00:00:00.000000],
~N[2023-08-01 00:00:00.000000],
~N[2023-09-01 00:00:00.000000],
~N[2023-10-01 00:00:00.000000],
~N[2023-11-01 00:00:00.000000],
~N[2023-12-01 00:00:00.000000]
# 29 days
~N[2024-02-01 00:00:00.000000],
# 28 days
~N[2023-02-01 00:00:00.000000]
]

leap_year_february = ~N[2024-02-01 00:00:00.000000]

months = normal_year ++ [leap_year_february]

months
|> Task.async_stream(fn date ->
key =
if Date.leap_year?(date) do
0
else
date.month
end

{key, number_of_reward_occurences_per_month(interval, date)}
end)
|> Task.async_stream(
fn date ->
key = get_key_from_date(date)

{key, number_of_reward_occurences_per_month(interval, date)}
end,
timeout: 10_000
)
|> Stream.map(fn {:ok, v} -> v end)
|> Map.new()
end

@doc """
Return the key for a given date

## Examples

iex> Utils.get_key_from_date(~N[2023-01-01 00:00:00.000000])
0

iex> Utils.get_key_from_date(~N[2023-04-01 00:00:00.000000])
1

iex> Utils.get_key_from_date(~N[2023-02-01 00:00:00.000000])
3

iex> Utils.get_key_from_date(~N[2024-02-01 00:00:00.000000])
2
"""
def get_key_from_date(date) do
# January March May July August October December have 31 days
# April June September November have 30 days
# February has 28 days or 29 days in a leap year
case {Date.leap_year?(date), date.month} do
{_, month} when month in [1, 3, 5, 7, 8, 10, 12] -> 0
{_, month} when month in [4, 6, 9, 11] -> 1
{true, 2} -> 2
{false, 2} -> 3
end
end

@doc """
Return the number of occurences for the cron job over the month

Expand Down