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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h> /* memset */
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "bencode.h"
|
|
|
|
#include "platform.h" /* tr_getPrefsDirectory */
|
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
static void
|
|
|
|
parseCumulativeStats( tr_session_stats * setme,
|
|
|
|
const uint8_t * content,
|
|
|
|
size_t len )
|
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc top;
|
2007-11-21 20:03:53 +00:00
|
|
|
|
|
|
|
if( !tr_bencLoad( content, len, &top, NULL ) )
|
|
|
|
{
|
2008-02-26 21:58:58 +00:00
|
|
|
const tr_benc * val;
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2007-11-26 20:20:46 +00:00
|
|
|
if(( val = tr_bencDictFindType( &top, "uploaded-bytes", TYPE_INT )))
|
|
|
|
setme->uploadedBytes = (uint64_t) tr_bencGetInt( val );
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2007-11-26 20:20:46 +00:00
|
|
|
if(( val = tr_bencDictFindType( &top, "downloaded-bytes", TYPE_INT )))
|
|
|
|
setme->downloadedBytes = (uint64_t) tr_bencGetInt( val );
|
2007-11-21 20:03:53 +00:00
|
|
|
|
|
|
|
if(( val = tr_bencDictFindType( &top, "files-added", TYPE_INT )))
|
|
|
|
setme->filesAdded = (uint64_t) tr_bencGetInt( val );
|
|
|
|
|
|
|
|
if(( val = tr_bencDictFindType( &top, "session-count", TYPE_INT )))
|
|
|
|
setme->sessionCount = (uint64_t) tr_bencGetInt( val );
|
|
|
|
|
|
|
|
if(( val = tr_bencDictFindType( &top, "seconds-active", TYPE_INT )))
|
|
|
|
setme->secondsActive = (uint64_t) tr_bencGetInt( val );
|
|
|
|
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-26 18:55:36 +00:00
|
|
|
static char*
|
|
|
|
getFilename( char * buf, size_t buflen )
|
|
|
|
{
|
|
|
|
tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "stats.benc", NULL );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2007-11-21 20:03:53 +00:00
|
|
|
static void
|
|
|
|
loadCumulativeStats( tr_session_stats * setme )
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
uint8_t * content;
|
2007-11-26 18:55:36 +00:00
|
|
|
char filename[MAX_PATH_LENGTH];
|
2007-11-21 20:03:53 +00:00
|
|
|
|
2007-11-26 18:55:36 +00:00
|
|
|
getFilename( filename, sizeof(filename) );
|
|
|
|
content = tr_loadFile( filename, &len );
|
2007-11-21 20:03:53 +00:00
|
|
|
if( content != NULL )
|
|
|
|
parseCumulativeStats( setme, content, len );
|
|
|
|
|
|
|
|
tr_free( content );
|
|
|
|
}
|
|
|
|
|
2007-11-26 18:55:36 +00:00
|
|
|
static void
|
|
|
|
saveCumulativeStats( const tr_session_stats * stats )
|
|
|
|
{
|
|
|
|
FILE * fp;
|
|
|
|
char * str;
|
|
|
|
char filename[MAX_PATH_LENGTH];
|
|
|
|
int len;
|
2008-02-26 21:58:58 +00:00
|
|
|
tr_benc top;
|
2007-11-26 18:55:36 +00:00
|
|
|
|
|
|
|
tr_bencInit( &top, TYPE_DICT );
|
|
|
|
tr_bencDictReserve( &top, 5 );
|
2007-11-26 20:20:46 +00:00
|
|
|
tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), stats->uploadedBytes );
|
|
|
|
tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), stats->downloadedBytes );
|
2007-11-26 18:55:36 +00:00
|
|
|
tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), stats->filesAdded );
|
|
|
|
tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), stats->sessionCount );
|
|
|
|
tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), stats->secondsActive );
|
|
|
|
|
|
|
|
str = tr_bencSave( &top, &len );
|
|
|
|
getFilename( filename, sizeof(filename) );
|
|
|
|
fp = fopen( filename, "wb+" );
|
|
|
|
fwrite( str, 1, len, fp );
|
|
|
|
fclose( fp );
|
|
|
|
tr_free( str );
|
|
|
|
|
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-02-04 19:54:47 +00:00
|
|
|
loadCumulativeStats( &stats->old );
|
|
|
|
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;
|
|
|
|
tr_getCumulativeSessionStats( handle, &cumulative );
|
|
|
|
saveCumulativeStats( &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 )
|
|
|
|
{
|
|
|
|
static struct tr_stats_handle nullObject;
|
|
|
|
|
|
|
|
return handle && handle->sessionStats
|
|
|
|
? handle->sessionStats
|
|
|
|
: &nullObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
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
|
|
|
|
tr_getSessionStats( const tr_handle * handle,
|
|
|
|
tr_session_stats * setme )
|
|
|
|
{
|
2008-02-04 19:54:47 +00:00
|
|
|
const struct tr_stats_handle * stats = getStats( handle );
|
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
|
|
|
|
tr_getCumulativeSessionStats( const tr_handle * handle,
|
|
|
|
tr_session_stats * setme )
|
|
|
|
{
|
2008-02-04 19:54:47 +00:00
|
|
|
tr_session_stats current;
|
|
|
|
tr_getSessionStats( handle, ¤t );
|
|
|
|
addStats( setme, &getStats(handle)->old, ¤t );
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
|
|
|
|
{
|
2008-02-04 19:54:47 +00:00
|
|
|
getStats(handle)->single.uploadedBytes += bytes;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
|
|
|
|
{
|
2008-02-04 19:54:47 +00:00
|
|
|
getStats(handle)->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-02-04 19:54:47 +00:00
|
|
|
getStats(handle)->single.filesAdded++;
|
2007-11-21 20:03:53 +00:00
|
|
|
}
|