Skip to content

Commit

Permalink
enhancements on reward per month
Browse files Browse the repository at this point in the history
  • Loading branch information
tenmoves authored and Neylix committed Jan 31, 2023
1 parent ba126bc commit a83f265
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 43 deletions.
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

0 comments on commit a83f265

Please sign in to comment.