2007-11-21 20:03:53 +00:00
|
|
|
/*
|
2008-01-01 17:20:20 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
2007-11-21 20:03:53 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-02-26 21:08:51 +00:00
|
|
|
* $Id$
|
2007-11-21 20:03:53 +00:00
|
|
|
*/
|
|
|
|
|
2008-05-23 18:30:49 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#include "platform.h" /* tr_sessionGetConfigDir() */
|
2008-08-11 19:05:02 +00:00
|
|
|
#include "stats.h"
|
2007-11-21 20:03:53 +00:00
|
|
|
#include "utils.h" /* tr_buildPath */
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-11-26 18:55:36 +00:00
|
|
|
struct tr_stats_handle
|
|
|
|
{
|
|
|
|
tr_session_stats single;
|
2008-02-04 19:54:47 +00:00
|
|
|
tr_session_stats old;
|
2007-11-26 18:55:36 +00:00
|
|
|
time_t startTime;
|
|
|
|
};
|
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
static char*
|
2008-06-12 16:25:36 +00:00
|
|
|
getOldFilename( const tr_handle * handle, char * buf, size_t buflen )
|
2008-04-18 16:23:59 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
|
|
|
|
"stats.benc",
|
|
|
|
NULL );
|
2008-04-18 16:23:59 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-06-12 16:25:36 +00:00
|
|
|
static char*
|
|
|
|
getFilename( const tr_handle * handle, char * buf, size_t buflen )
|
|
|
|
{
|
|
|
|
tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
|
|
|
|
"stats.json",
|
|
|
|
NULL );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
static void
|
2008-04-18 16:23:59 +00:00
|
|
|
loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
int loaded = FALSE;
|
2008-04-18 16:23:59 +00:00
|
|
|
char filename[MAX_PATH_LENGTH];
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc top;
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2008-04-18 16:23:59 +00:00
|
|
|
getFilename( handle, filename, sizeof(filename) );
|
2008-06-12 16:25:36 +00:00
|
|
|
loaded = !tr_bencLoadJSONFile( filename, &top );
|
|
|
|
if( !loaded ) {
|
|
|
|
getOldFilename( handle, filename, sizeof(filename) );
|
|
|
|
loaded = !tr_bencLoadFile( filename, &top );
|
|
|
|
}
|
|
|
|
if( loaded )
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2008-04-13 22:31:07 +00:00
|
|
|
int64_t i;
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2008-04-13 22:31:07 +00:00
|
|
|
if( tr_bencDictFindInt( &top, "downloaded-bytes", &i ) )
|
|
|
|
setme->downloadedBytes = (uint64_t) i;
|
|
|
|
if( tr_bencDictFindInt( &top, "files-added", &i ) )
|
|
|
|
setme->filesAdded = (uint64_t) i;
|
|
|
|
if( tr_bencDictFindInt( &top, "seconds-active", &i ) )
|
|
|
|
setme->secondsActive = (uint64_t) i;
|
2008-05-23 18:30:49 +00:00
|
|
|
if( tr_bencDictFindInt( &top, "session-count", &i ) )
|
|
|
|
setme->sessionCount = (uint64_t) i;
|
|
|
|
if( tr_bencDictFindInt( &top, "uploaded-bytes", &i ) )
|
|
|
|
setme->uploadedBytes = (uint64_t) i;
|
2007-11-21 20:03:53 +00:00
|
|
|
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-26 18:55:36 +00:00
|
|
|
static void
|
2008-04-14 11:52:50 +00:00
|
|
|
saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
|
|
|
char filename[MAX_PATH_LENGTH];
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc top;
|
2007-11-26 18:55:36 +00:00
|
|
|
|
2008-04-03 21:38:32 +00:00
|
|
|
tr_bencInitDict( &top, 5 );
|
2008-04-18 16:23:59 +00:00
|
|
|
tr_bencDictAddInt( &top, "downloaded-bytes", s->downloadedBytes );
|
|
|
|
tr_bencDictAddInt( &top, "files-added", s->filesAdded );
|
|
|
|
tr_bencDictAddInt( &top, "seconds-active", s->secondsActive );
|
2008-05-23 18:30:49 +00:00
|
|
|
tr_bencDictAddInt( &top, "session-count", s->sessionCount );
|
|
|
|
tr_bencDictAddInt( &top, "uploaded-bytes", s->uploadedBytes );
|
2007-11-26 18:55:36 +00:00
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
getFilename( handle, filename, sizeof(filename) );
|
2008-04-29 18:22:26 +00:00
|
|
|
tr_deepLog( __FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename );
|
2008-06-12 16:25:36 +00:00
|
|
|
tr_bencSaveJSONFile( filename, &top );
|
2007-11-26 18:55:36 +00:00
|
|
|
|
2007-11-26 19:10:53 +00:00
|
|
|
tr_bencFree( &top );
|
2007-11-26 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_statsInit( tr_handle * handle )
|
|
|
|
{
|
2007-11-26 18:55:36 +00:00
|
|
|
struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
|
2008-04-05 20:12:11 +00:00
|
|
|
loadCumulativeStats( handle, &stats->old );
|
2008-02-04 19:54:47 +00:00
|
|
|
stats->single.sessionCount = 1;
|
|
|
|
stats->startTime = time( NULL );
|
2007-11-26 18:55:36 +00:00
|
|
|
handle->sessionStats = stats;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-26 18:55:36 +00:00
|
|
|
tr_statsClose( tr_handle * handle )
|
2007-11-21 20:03:53 +00:00
|
|
|
{
|
2008-02-04 19:54:47 +00:00
|
|
|
tr_session_stats cumulative;
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_sessionGetCumulativeStats( handle, &cumulative );
|
2008-04-05 20:12:11 +00:00
|
|
|
saveCumulativeStats( handle, &cumulative );
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2007-11-26 18:55:36 +00:00
|
|
|
tr_free( handle->sessionStats );
|
|
|
|
handle->sessionStats = NULL;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2008-02-04 19:54:47 +00:00
|
|
|
static struct tr_stats_handle *
|
|
|
|
getStats( const tr_handle * handle )
|
|
|
|
{
|
2008-05-23 18:30:49 +00:00
|
|
|
return handle->sessionStats;
|
2008-02-04 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-22 03:01:14 +00:00
|
|
|
static void
|
|
|
|
updateRatio( tr_session_stats * setme )
|
|
|
|
{
|
2008-02-04 19:54:47 +00:00
|
|
|
setme->ratio = tr_getRatio( setme->uploadedBytes,
|
|
|
|
setme->downloadedBytes );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addStats( tr_session_stats * setme,
|
|
|
|
const tr_session_stats * a,
|
|
|
|
const tr_session_stats * b )
|
|
|
|
{
|
|
|
|
setme->uploadedBytes = a->uploadedBytes + b->uploadedBytes;
|
|
|
|
setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
|
|
|
|
setme->filesAdded = a->filesAdded + b->filesAdded;
|
|
|
|
setme->sessionCount = a->sessionCount + b->sessionCount;
|
|
|
|
setme->secondsActive = a->secondsActive + b->secondsActive;
|
|
|
|
updateRatio( setme );
|
2007-12-22 03:01:14 +00:00
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
void
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_sessionGetStats( const tr_handle * handle,
|
2007-11-21 20:03:53 +00:00
|
|
|
tr_session_stats * setme )
|
|
|
|
{
|
2008-02-04 19:54:47 +00:00
|
|
|
const struct tr_stats_handle * stats = getStats( handle );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( stats );
|
2007-11-26 18:55:36 +00:00
|
|
|
*setme = stats->single;
|
2008-02-04 19:54:47 +00:00
|
|
|
setme->secondsActive = time( NULL ) - stats->startTime;
|
2007-12-22 03:01:14 +00:00
|
|
|
updateRatio( setme );
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_sessionGetCumulativeStats( const tr_handle * handle,
|
2007-11-21 20:03:53 +00:00
|
|
|
tr_session_stats * setme )
|
|
|
|
{
|
2008-05-23 18:30:49 +00:00
|
|
|
const struct tr_stats_handle * stats = getStats( handle );
|
2008-02-04 19:54:47 +00:00
|
|
|
tr_session_stats current;
|
2008-08-11 19:05:02 +00:00
|
|
|
assert( stats );
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_sessionGetStats( handle, ¤t );
|
2008-05-23 18:30:49 +00:00
|
|
|
addStats( setme, &stats->old, ¤t );
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
2008-04-22 14:07:42 +00:00
|
|
|
void
|
2008-05-22 20:44:41 +00:00
|
|
|
tr_sessionClearStats( tr_handle * handle )
|
2008-04-22 14:07:42 +00:00
|
|
|
{
|
|
|
|
tr_session_stats zero;
|
|
|
|
zero.uploadedBytes = 0;
|
|
|
|
zero.downloadedBytes = 0;
|
|
|
|
zero.ratio = TR_RATIO_NA;
|
|
|
|
zero.filesAdded = 0;
|
|
|
|
zero.sessionCount = 0;
|
|
|
|
zero.secondsActive = 0;
|
|
|
|
handle->sessionStats->single = handle->sessionStats->old = zero;
|
|
|
|
|
|
|
|
handle->sessionStats->startTime = time( NULL );
|
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
|
|
|
|
{
|
2008-05-23 18:30:49 +00:00
|
|
|
struct tr_stats_handle * s;
|
|
|
|
if(( s = getStats( handle )))
|
|
|
|
s->single.uploadedBytes += bytes;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
|
|
|
|
{
|
2008-05-23 18:30:49 +00:00
|
|
|
struct tr_stats_handle * s;
|
|
|
|
if(( s = getStats( handle )))
|
|
|
|
s->single.downloadedBytes += bytes;
|
2007-11-26 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-26 20:37:07 +00:00
|
|
|
tr_statsFileCreated( tr_handle * handle )
|
2007-11-26 18:55:36 +00:00
|
|
|
{
|
2008-05-23 18:30:49 +00:00
|
|
|
struct tr_stats_handle * s;
|
|
|
|
if(( s = getStats( handle )))
|
|
|
|
s->single.filesAdded++;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|