<commit_before>%% @author Northscale <<EMAIL>>
%% @copyright 2009 NorthScale, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(stats_collector).

-include_lib("eunit/include/eunit.hrl").

-include("ns_common.hrl").
-include("ns_stats.hrl").

-behaviour(gen_server).

-define(STATS_TIMER, 1000).

-define(l2r(KeyName),
        l2r(KeyName, V, Rec) ->
               Rec#stat_entry{KeyName=list_to_integer(V)}).



%% API
-export([start_link/1]).

-record(state, {bucket, counters}).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2,
         handle_info/2, terminate/2, code_change/3]).

start_link(Bucket) ->
    gen_server:start_link(?MODULE, Bucket, []).

init(Bucket) ->
    ns_pubsub:subscribe(ns_tick_event),
    {ok, #state{bucket=Bucket}}.

handle_call(unhandled, unhandled, unhandled) ->
    unhandled.

handle_cast(unhandled, unhandled) ->
    unhandled.

handle_info({tick, TS}, #state{bucket=Bucket, counters=Counters} = State) ->
    case catch ns_memcached:stats(Bucket) of
        {ok, Stats} ->
            TS1 = latest_tick(TS),
            {Entry, NewCounters} = parse_stats(TS1, Stats, Counters),
            case Counters of % Don't send event with undefined values
                undefined ->
                    ok;
                _ ->
                    gen_event:notify(ns_stats_event, {stats, Bucket, Entry})
            end,
            {noreply, State#state{counters=NewCounters}};
        _ ->
            {noreply, State}
    end;
handle_info(_Msg, State) -> % Don't crash on delayed responses to calls
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.


%% Internal functions
latest_tick(TS) ->
    latest_tick(TS, 0).


latest_tick(TS, NumDropped) -><commit_msg>Dump local memcached's stats to log once/min<commit_after>%% @author Northscale <<EMAIL>>
%% @copyright 2009 NorthScale, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(stats_collector).

-include_lib("eunit/include/eunit.hrl").

-include("ns_common.hrl").
-include("ns_stats.hrl").

-behaviour(gen_server).

-define(STATS_TIMER, 1000). % How often in ms we collect stats
-define(LOG_FREQ, 100).     % Dump every n collections to the log
-define(WIDTH, 30).         % Width of the key part of the formatted logs

-define(l2r(KeyName),
        l2r(KeyName, V, Rec) ->
               Rec#stat_entry{KeyName=list_to_integer(V)}).



%% API
-export([start_link/1]).

-record(state, {bucket, counters, count=?LOG_FREQ}).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2,
         handle_info/2, terminate/2, code_change/3]).

start_link(Bucket) ->
    gen_server:start_link(?MODULE, Bucket, []).

init(Bucket) ->
    ns_pubsub:subscribe(ns_tick_event),
    {ok, #state{bucket=Bucket}}.

handle_call(unhandled, unhandled, unhandled) ->
    unhandled.

handle_cast(unhandled, unhandled) ->
    unhandled.

handle_info({tick, TS}, #state{bucket=Bucket, counters=Counters} = State) ->
    case catch ns_memcached:stats(Bucket) of
        {ok, Stats} ->
            TS1 = latest_tick(TS),
            {Entry, NewCounters} = parse_stats(TS1, Stats, Counters),
            case Counters of % Don't send event with undefined values
                undefined ->
                    ok;
                _ ->
                    gen_event:notify(ns_stats_event, {stats, Bucket, Entry})
            end,
            Count = case State#state.count of
                        ?LOG_FREQ ->
                            ?log_info("Stats for bucket ~p:~n~s",
                                      [Bucket, format_stats(Stats)]),
                            1;
                        C ->
                            C + 1
                    end,
            {noreply, State#state{counters=NewCounters, count=Count}};
        _ ->
            {noreply, State}
    end;
handle_info(_Msg, State) -> % Don't crash on delayed responses to calls
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.


%% Internal functions
format_stats(Stats) ->
    erlang:list_to_binary(
      [[K, lists:duplicate(?WIDTH - byte_size(K), $\s), V, $\n]
       || {K, V} <- lists:sort(Stats)]).


latest_tick(TS) ->
    latest_tick(TS, 0).


latest_tick(TS, NumDropped) ->
{'fraction_non_alphanumeric': 0.12936997569639186, 'fraction_numerical': 0.010282295756216114, 'mean_word_length': 2.7178596247394022, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '886928', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1647, 'n_words': 442}