4
4
* This file is licensed by the GPL version 2. Works owned by the
5
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.
6
* so that the bulk of its code can remain under the MIT license.
7
7
* This exemption does not extend to derived works not owned by
8
8
* the Transmission project.
10
* $Id: stats.c 6489 2008-08-11 19:05:00Z charles $
10
* $Id: stats.c 6896 2008-10-14 03:03:29Z charles $
13
13
#include <assert.h>
25
25
struct tr_stats_handle
27
tr_session_stats single;
27
tr_session_stats single;
33
getOldFilename( const tr_handle * handle, char * buf, size_t buflen )
33
getOldFilename( const tr_handle * handle )
35
tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
35
return tr_buildPath( tr_sessionGetConfigDir( handle ), "stats.benc", NULL );
42
getFilename( const tr_handle * handle, char * buf, size_t buflen )
39
getFilename( const tr_handle * handle )
44
tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
41
return tr_buildPath( tr_sessionGetConfigDir( handle ), "stats.json", NULL );
51
loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
45
loadCumulativeStats( const tr_handle * handle,
46
tr_session_stats * setme )
54
char filename[MAX_PATH_LENGTH];
57
getFilename( handle, filename, sizeof(filename) );
52
filename = getFilename( handle );
58
53
loaded = !tr_bencLoadJSONFile( filename, &top );
60
getOldFilename( handle, filename, sizeof(filename) );
58
filename = getOldFilename( handle );
61
59
loaded = !tr_bencLoadFile( filename, &top );
83
saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
83
saveCumulativeStats( const tr_handle * handle,
84
const tr_session_stats * s )
85
char filename[MAX_PATH_LENGTH];
88
89
tr_bencInitDict( &top, 5 );
92
93
tr_bencDictAddInt( &top, "session-count", s->sessionCount );
93
94
tr_bencDictAddInt( &top, "uploaded-bytes", s->uploadedBytes );
95
getFilename( handle, filename, sizeof(filename) );
96
filename = getFilename( handle );
96
97
tr_deepLog( __FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename );
97
98
tr_bencSaveJSONFile( filename, &top );
99
101
tr_bencFree( &top );
107
109
tr_statsInit( tr_handle * handle )
109
111
struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
110
113
loadCumulativeStats( handle, &stats->old );
111
114
stats->single.sessionCount = 1;
112
115
stats->startTime = time( NULL );
117
120
tr_statsClose( tr_handle * handle )
119
122
tr_session_stats cumulative;
120
124
tr_sessionGetCumulativeStats( handle, &cumulative );
121
125
saveCumulativeStats( handle, &cumulative );
145
addStats( tr_session_stats * setme,
149
addStats( tr_session_stats * setme,
146
150
const tr_session_stats * a,
147
151
const tr_session_stats * b )
158
tr_sessionGetStats( const tr_handle * handle,
159
tr_session_stats * setme )
162
tr_sessionGetStats( const tr_handle * handle,
163
tr_session_stats * setme )
161
165
const struct tr_stats_handle * stats = getStats( handle );
163
168
*setme = stats->single;
164
169
setme->secondsActive = time( NULL ) - stats->startTime;
169
tr_sessionGetCumulativeStats( const tr_handle * handle,
170
tr_session_stats * setme )
174
tr_sessionGetCumulativeStats( const tr_handle * handle,
175
tr_session_stats * setme )
172
177
const struct tr_stats_handle * stats = getStats( handle );
173
tr_session_stats current;
178
tr_session_stats current;
175
181
tr_sessionGetStats( handle, ¤t );
176
182
addStats( setme, &stats->old, ¤t );
199
tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
206
tr_statsAddUploaded( tr_handle * handle,
201
209
struct tr_stats_handle * s;
202
if(( s = getStats( handle )))
211
if( ( s = getStats( handle ) ) )
203
212
s->single.uploadedBytes += bytes;
207
tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
216
tr_statsAddDownloaded( tr_handle * handle,
209
219
struct tr_stats_handle * s;
210
if(( s = getStats( handle )))
221
if( ( s = getStats( handle ) ) )
211
222
s->single.downloadedBytes += bytes;