source: trunk/libtransmission/stats.c @ 5666

Last change on this file since 5666 was 5666, checked in by charles, 14 years ago

add statistics "reset" button

  • Property svn:keywords set to Date Rev Author Id
File size: 4.9 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: stats.c 5666 2008-04-22 14:07:42Z charles $
11 */
12
13#include "transmission.h"
14#include "bencode.h"
15#include "platform.h" /* tr_getConfigDir() */
16#include "utils.h" /* tr_buildPath */
17
18/***
19****
20***/
21
22struct tr_stats_handle
23{
24    tr_session_stats single;
25    tr_session_stats old;
26    time_t startTime;
27};
28
29static char*
30getFilename( const tr_handle * handle, char * buf, size_t buflen )
31{
32    tr_buildPath( buf, buflen, tr_getConfigDir(handle), "stats.benc", NULL );
33    return buf;
34}
35
36static void
37loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
38{
39    char filename[MAX_PATH_LENGTH];
40    tr_benc top;
41
42    getFilename( handle, filename, sizeof(filename) );
43    if( !tr_bencLoadFile( filename, &top ) )
44    {
45        int64_t i;
46
47        if( tr_bencDictFindInt( &top, "uploaded-bytes", &i ) )
48            setme->uploadedBytes = (uint64_t) i;
49
50        if( tr_bencDictFindInt( &top, "downloaded-bytes", &i ) )
51            setme->downloadedBytes = (uint64_t) i;
52
53        if( tr_bencDictFindInt( &top, "files-added", &i ) )
54            setme->filesAdded = (uint64_t) i;
55
56        if( tr_bencDictFindInt( &top, "session-count", &i ) )
57            setme->sessionCount = (uint64_t) i;
58
59        if( tr_bencDictFindInt( &top, "seconds-active", &i ) )
60            setme->secondsActive = (uint64_t) i;
61
62        tr_bencFree( &top );
63    }
64}
65
66static void
67saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
68{
69    char filename[MAX_PATH_LENGTH];
70    tr_benc top;
71
72    tr_bencInitDict( &top, 5 );
73    tr_bencDictAddInt( &top, "uploaded-bytes",   s->uploadedBytes );
74    tr_bencDictAddInt( &top, "downloaded-bytes", s->downloadedBytes );
75    tr_bencDictAddInt( &top, "files-added",      s->filesAdded );
76    tr_bencDictAddInt( &top, "session-count",    s->sessionCount );
77    tr_bencDictAddInt( &top, "seconds-active",   s->secondsActive );
78
79    getFilename( handle, filename, sizeof(filename) );
80    tr_bencSaveFile( filename, &top );
81
82    tr_bencFree( &top );
83}
84
85/***
86****
87***/
88
89void
90tr_statsInit( tr_handle * handle )
91{
92    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
93    loadCumulativeStats( handle, &stats->old );
94    stats->single.sessionCount = 1;
95    stats->startTime = time( NULL );
96    handle->sessionStats = stats;
97}
98
99void
100tr_statsClose( tr_handle * handle )
101{
102    tr_session_stats cumulative;
103    tr_getCumulativeSessionStats( handle, &cumulative );
104    saveCumulativeStats( handle, &cumulative );
105
106    tr_free( handle->sessionStats );
107    handle->sessionStats = NULL;
108}
109
110static struct tr_stats_handle *
111getStats( const tr_handle * handle )
112{
113    static struct tr_stats_handle nullObject;
114
115    return handle && handle->sessionStats
116        ? handle->sessionStats
117        : &nullObject;
118}
119
120/***
121****
122***/
123
124static void
125updateRatio( tr_session_stats * setme )
126{
127    setme->ratio = tr_getRatio( setme->uploadedBytes,
128                                setme->downloadedBytes );
129}
130
131static void
132addStats( tr_session_stats       * setme,
133          const tr_session_stats * a,
134          const tr_session_stats * b )
135{
136    setme->uploadedBytes   = a->uploadedBytes   + b->uploadedBytes;
137    setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
138    setme->filesAdded      = a->filesAdded      + b->filesAdded;
139    setme->sessionCount    = a->sessionCount    + b->sessionCount;
140    setme->secondsActive   = a->secondsActive   + b->secondsActive;
141    updateRatio( setme );
142}
143
144void
145tr_getSessionStats( const tr_handle   * handle,
146                    tr_session_stats  * setme )
147{
148    const struct tr_stats_handle * stats = getStats( handle );
149    *setme = stats->single;
150    setme->secondsActive = time( NULL ) - stats->startTime;
151    updateRatio( setme );
152}
153
154void
155tr_getCumulativeSessionStats( const tr_handle   * handle,
156                              tr_session_stats  * setme )
157{
158    tr_session_stats current;
159    tr_getSessionStats( handle, &current );
160    addStats( setme, &getStats(handle)->old, &current );
161}
162
163void
164tr_clearSessionStats( tr_handle * handle )
165{
166    tr_session_stats zero;
167    zero.uploadedBytes = 0;
168    zero.downloadedBytes = 0;
169    zero.ratio = TR_RATIO_NA;
170    zero.filesAdded = 0;
171    zero.sessionCount = 0;
172    zero.secondsActive = 0;
173    handle->sessionStats->single = handle->sessionStats->old = zero;
174
175    handle->sessionStats->startTime = time( NULL );
176}
177
178/**
179***
180**/
181
182void
183tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
184{
185    getStats(handle)->single.uploadedBytes += bytes;
186}
187
188void
189tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
190{
191    getStats(handle)->single.downloadedBytes += bytes;
192}
193
194void
195tr_statsFileCreated( tr_handle * handle )
196{
197    getStats(handle)->single.filesAdded++;
198}
Note: See TracBrowser for help on using the repository browser.