From 84ec8fd9dc898b06a8b9729a1498087e26724ac7 Mon Sep 17 00:00:00 2001 From: Kevin Lynx Date: Thu, 11 Jul 2013 22:42:20 +0800 Subject: [PATCH] add torrent download stats in hash reader, to debug something --- src/hash_reader/hash_reader.erl | 2 ++ src/hash_reader/hash_reader_stats.erl | 28 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/hash_reader/hash_reader.erl b/src/hash_reader/hash_reader.erl index c3b80c1..a6af551 100644 --- a/src/hash_reader/hash_reader.erl +++ b/src/hash_reader/hash_reader.erl @@ -46,9 +46,11 @@ handle_info({got_torrent, failed, _Hash}, State) -> Conn = db_conn(State), try_next_download(Conn), ?T(?FMT("got torrent failed ~s", [_Hash])), + hash_reader_stats:handle_download_failed(), {noreply, State#state{downloading = D - 1}}; handle_info({got_torrent, ok, Hash, Content}, State) -> + hash_reader_stats:handle_download_ok(), Conn = db_conn(State), true = is_binary(Content), % save the torrent file diff --git a/src/hash_reader/hash_reader_stats.erl b/src/hash_reader/hash_reader_stats.erl index 38bfae8..cfb655a 100644 --- a/src/hash_reader/hash_reader_stats.erl +++ b/src/hash_reader/hash_reader_stats.erl @@ -17,8 +17,12 @@ handle_update/0, handle_insert/0, handle_used_cache/0, + handle_download_ok/0, + handle_download_failed/0, dump/0]). --record(state, {tref, count, start, name, cache_used = 0, updated = 0, inserted = 0}). +-record(state, {tref, count, start, name, cache_used = 0, + updated = 0, inserted = 0, + download_ok = 0, download_failed = 0}). -define(STATS_INTERVAL, 10*60*1000). -define(TEXT(Fmt, Arg), lists:flatten(io_lib:format(Fmt, Arg))). @@ -43,6 +47,12 @@ handle_insert() -> handle_used_cache() -> gen_server:cast(srv_name(), inc_cache_used). +handle_download_ok() -> + gen_server:cast(srv_name(), inc_download_ok). + +handle_download_failed() -> + gen_server:cast(srv_name(), inc_download_failed). + srv_name() -> ?MODULE. @@ -81,6 +91,14 @@ handle_cast(inc_inserted, State) -> handle_cast(inc_cache_used, State) -> #state{cache_used = C} = State, {noreply, State#state{cache_used = C + 1}}; + +handle_cast(inc_download_ok, State) -> + #state{download_ok = D} = State, + {noreply, State#state{download_ok = D + 1}}; + +handle_cast(inc_download_failed, State) -> + #state{download_failed = D} = State, + {noreply, State#state{download_failed = D + 1}}; handle_cast(stop, State) -> {stop, normal, State}. @@ -112,7 +130,9 @@ date_string() -> [Year, Month, Day, Hour, Min, Sec])). format_stats(State) -> - #state{count = C, start = Start, cache_used = Cache, updated = U, inserted = I} = State, + #state{count = C, start = Start, cache_used = Cache, + download_ok = DO, download_failed = DF, + updated = U, inserted = I} = State, {Day, {H, M, S}} = stats_time(Start), Mins = Day * 24 * 60 + H * 60 + M, TotalMins = if Mins > 0 -> Mins; true -> 1 end, @@ -124,6 +144,8 @@ format_stats(State) -> ?TEXT(" Reader count ~p~n", [C]) ++ ?TEXT(" Process speed ~p req/min~n", [Speed]) ++ ?TEXT(" Download torrents speed ~p tor/min~n", [I div TotalMins]) ++ + ?TEXT(" Download success ~p~n", [DO]) ++ + ?TEXT(" Download failed ~p~n", [DF]) ++ ?TEXT(" Updated ~p~n", [U]) ++ ?TEXT(" Inserted ~p~n", [I]) ++ ?TEXT(" Inserted percentage ~.2f%~n", [InsertPercent]) ++ @@ -145,5 +167,3 @@ format_download_stats() -> ?TEXT(" Download speed ~p tor/secs~n", [TorSpeed]) ++ ?TEXT(" Current wait requests ~p~n", [CurrentReqCount]). - -