This commit is contained in:
@@ -8,8 +8,9 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec save(#node_metric{}) -> ok.
|
||||
save(Metric) ->
|
||||
mnesia:dirty_write(Metric),
|
||||
ok.
|
||||
try mnesia:dirty_write(Metric)
|
||||
catch _:_ -> ok
|
||||
end.
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Возвращает метрики за период, отсортированные по времени.
|
||||
|
||||
@@ -269,15 +269,21 @@ apply_updates(Sub, Updates) ->
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Разбор ISO8601 строки в формат `calendar:datetime()`.
|
||||
%%% Поддерживает миллисекунды и таймзону.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec parse_iso_datetime(binary() | term()) -> calendar:datetime() | term().
|
||||
parse_iso_datetime(Bin) when is_binary(Bin) ->
|
||||
try
|
||||
[DateStr, TimeStr] = string:split(Bin, "T"),
|
||||
TimeStrNoZ = string:trim(TimeStr, trailing, "Z"),
|
||||
[YearStr, MonthStr, DayStr] = string:split(DateStr, "-", all),
|
||||
[HourStr, MinuteStr, SecondStr] = string:split(TimeStrNoZ, ":", all),
|
||||
[DatePart, TimePart] = string:split(Bin, "T"),
|
||||
[YearStr, MonthStr, DayStr] = string:split(DatePart, "-", all),
|
||||
TimeNoZ = string:trim(TimePart, trailing, "Z"),
|
||||
[HourStr, MinuteStr, SecondPart] = string:split(TimeNoZ, ":", all),
|
||||
% удаляем миллисекунды, если они есть
|
||||
SecondStr = case string:split(SecondPart, ".") of
|
||||
[Sec, _] -> Sec;
|
||||
_ -> SecondPart
|
||||
end,
|
||||
Year = binary_to_integer(list_to_binary(YearStr)),
|
||||
Month = binary_to_integer(list_to_binary(MonthStr)),
|
||||
Day = binary_to_integer(list_to_binary(DayStr)),
|
||||
@@ -357,6 +363,7 @@ count_trial_subscriptions() ->
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Платные подписки (trial_used = true), истекающие в течение Days дней.
|
||||
%%% Принимает меры к преобразованию expires_at, если оно сохранено как строка.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec get_ending_paid_subscriptions(Days :: non_neg_integer()) -> [#subscription{}].
|
||||
@@ -366,6 +373,11 @@ get_ending_paid_subscriptions(Days) ->
|
||||
Match = #subscription{status = active, trial_used = true, _ = '_'},
|
||||
ActivePaid = mnesia:dirty_match_object(Match),
|
||||
lists:filter(fun(#subscription{expires_at = Exp}) ->
|
||||
ExpSec = calendar:datetime_to_gregorian_seconds(Exp),
|
||||
ExpDateTime = case Exp of
|
||||
{{_,_,_}, {_,_,_}} -> Exp; % уже datetime
|
||||
Bin when is_binary(Bin) -> parse_iso_datetime(Bin);
|
||||
_ -> Exp
|
||||
end,
|
||||
ExpSec = calendar:datetime_to_gregorian_seconds(ExpDateTime),
|
||||
ExpSec >= NowSec andalso ExpSec =< ThresholdSec
|
||||
end, ActivePaid).
|
||||
end, ActivePaid).
|
||||
Reference in New Issue
Block a user