<commit_before>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92 -*-
%% ex: ts=4 sw=4 et
%%% @copyright Copyright 2012-2012 Opscode Inc.
%%% @doc
%%% Node health statistics
%%% @end

-module(pushy_node_stats).

-record(metric, {node_pid :: pid(),
                 avg=down_threshold() * 2 :: float(),
                 interval_start :: pos_integer(),
                 heartbeats=1 :: pos_integer()}).

%% These two weights must total to 1.0
-define(NOW_WEIGHT, (1.0/decay_window())).
-define(HISTORY_WEIGHT, 1.0-?NOW_WEIGHT).

-define(MEGA, 1000000). %% because I can't count zeros reliably

-export([init/0,
         heartbeat/1,
         scan/0]).

-spec init() -> atom() | ets:tid().
init() ->
    ets:new(?MODULE, [set, public, named_table, {keypos, 2},
                      {write_concurrency, true}, {read_concurrency, true}]).

-spec heartbeat(pid()) -> ok | should_die.
heartbeat(NodePid) ->
    case ets:lookup(?MODULE, NodePid) of
        [] ->
            ets:insert_new(?MODULE, #metric{node_pid=NodePid}),
            ok;
        [Node] ->
            Node1 = hb(Node),
            case evaluate_node_health(Node1) of
                {reset, Node2} ->
                    ets:insert(?MODULE, reset(Node2)),
                    ok;
                {ok, Node2} ->
                    ets:insert(?MODULE, Node2),
                    ok;
                {should_die, _Node2} -><commit_msg>Fix bad initialization. Parens are needed to fix order of operations bug.<commit_after>%% -*- erlang-indent-level: 4;indent-tabs-mode: nil; fill-column: 92 -*-
%% ex: ts=4 sw=4 et
%%% @copyright Copyright 2012-2012 Opscode Inc.
%%% @doc
%%% Node health statistics
%%% @end

-module(pushy_node_stats).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-record(metric, {node_pid :: pid(),
                 avg=down_threshold() * 2 :: float(),
                 interval_start=now_as_int() :: pos_integer(),
                 heartbeats=1 :: pos_integer()}).

%% These two weights must total to 1.0
-define(NOW_WEIGHT, (1.0/decay_window())).
-define(HISTORY_WEIGHT, (1.0-?NOW_WEIGHT)).

-define(MEGA, 1000000). %% because I can't count zeros reliably

-export([init/0,
         heartbeat/1,
         scan/0]).

-spec init() -> atom() | ets:tid().
init() ->
    ets:new(?MODULE, [set, public, named_table, {keypos, 2},
                      {write_concurrency, true}, {read_concurrency, true}]).

-spec heartbeat(pid()) -> ok | should_die.
heartbeat(NodePid) ->
    case ets:lookup(?MODULE, NodePid) of
        [] ->
            ets:insert_new(?MODULE, #metric{node_pid=NodePid}),
            ok;
        [Node] ->
            Node1 = hb(Node),
            case evaluate_node_health(Node1) of
                {reset, Node2} ->
                    ets:insert(?MODULE, reset(Node2)),
                    ok;
                {ok, Node2} ->
                    ets:insert(?MODULE, Node2),
                    ok;
                {should_die, _Node2} ->
{'fraction_non_alphanumeric': 0.1626890756302521, 'fraction_numerical': 0.02621848739495798, 'mean_word_length': 2.096774193548387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2869199', 'n_tokens_mistral': 1040, 'n_tokens_neox': 949, 'n_words': 210}