1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 09:13:06 +00:00

work on the stats a little bit.

This commit is contained in:
Charles Kerr 2007-11-26 18:55:36 +00:00
parent c79f772acd
commit 3aae5534bd
4 changed files with 100 additions and 53 deletions

View file

@ -194,8 +194,7 @@ struct tr_handle
uint8_t isClosed; uint8_t isClosed;
tr_session_stats sessionStats; struct tr_stats_handle * sessionStats;
tr_session_stats cumulativeStats;
}; };
void tr_globalLock ( struct tr_handle * ); void tr_globalLock ( struct tr_handle * );

View file

@ -21,6 +21,15 @@
**** ****
***/ ***/
struct tr_stats_handle
{
tr_session_stats single;
tr_session_stats cumulative;
uint32_t bytes_up;
uint32_t bytes_down;
time_t startTime;
};
static void static void
parseCumulativeStats( tr_session_stats * setme, parseCumulativeStats( tr_session_stats * setme,
const uint8_t * content, const uint8_t * content,
@ -32,17 +41,11 @@ parseCumulativeStats( tr_session_stats * setme,
{ {
const benc_val_t * val; const benc_val_t * val;
if(( val = tr_bencDictFindType( &top, "uploaded-gigabytes", TYPE_INT ))) if(( val = tr_bencDictFindType( &top, "uploaded-mib", TYPE_INT )))
setme->uploadedGigs = (uint64_t) tr_bencGetInt( val ); setme->uploadedMiB = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "uploaded-bytes", TYPE_INT ))) if(( val = tr_bencDictFindType( &top, "downloaded-mib", TYPE_INT )))
setme->uploadedBytes = (uint64_t) tr_bencGetInt( val ); setme->downloadedMiB = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "downloaded-gigabytes", TYPE_INT )))
setme->downloadedGigs = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "downloaded-bytes", TYPE_INT )))
setme->downloadedBytes = (uint64_t) tr_bencGetInt( val );
if(( val = tr_bencDictFindType( &top, "files-added", TYPE_INT ))) if(( val = tr_bencDictFindType( &top, "files-added", TYPE_INT )))
setme->filesAdded = (uint64_t) tr_bencGetInt( val ); setme->filesAdded = (uint64_t) tr_bencGetInt( val );
@ -57,21 +60,55 @@ parseCumulativeStats( tr_session_stats * setme,
} }
} }
static char*
getFilename( char * buf, size_t buflen )
{
tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "stats.benc", NULL );
return buf;
}
static void static void
loadCumulativeStats( tr_session_stats * setme ) loadCumulativeStats( tr_session_stats * setme )
{ {
size_t len; size_t len;
uint8_t * content; uint8_t * content;
char path[MAX_PATH_LENGTH]; char filename[MAX_PATH_LENGTH];
tr_buildPath( path, sizeof(path), tr_getPrefsDirectory(), "stats.benc", NULL ); getFilename( filename, sizeof(filename) );
content = tr_loadFile( path, &len ); content = tr_loadFile( filename, &len );
if( content != NULL ) if( content != NULL )
parseCumulativeStats( setme, content, len ); parseCumulativeStats( setme, content, len );
tr_free( content ); tr_free( content );
} }
static void
saveCumulativeStats( const tr_session_stats * stats )
{
FILE * fp;
char * str;
char filename[MAX_PATH_LENGTH];
int len;
benc_val_t top, *val;
tr_bencInit( &top, TYPE_DICT );
tr_bencDictReserve( &top, 5 );
tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-mib" ), stats->uploadedMiB );
tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-mib" ), stats->downloadedMiB );
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 );
tr_BencFree( &top );
}
/*** /***
**** ****
***/ ***/
@ -79,69 +116,82 @@ loadCumulativeStats( tr_session_stats * setme )
void void
tr_statsInit( tr_handle * handle ) tr_statsInit( tr_handle * handle )
{ {
memset( &handle->sessionStats, 0, sizeof( tr_session_stats ) ); struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
memset( &handle->cumulativeStats, 0, sizeof( tr_session_stats ) ); loadCumulativeStats( &stats->cumulative );
stats->cumulative.sessionCount++;
loadCumulativeStats( &handle->cumulativeStats ); stats->startTime = time(NULL);
handle->sessionStats = stats;
} }
void void
tr_statsClose( const tr_handle * handle UNUSED ) tr_statsClose( tr_handle * handle )
{ {
fprintf( stderr, "FIXME" ); tr_session_stats tmp;
} tr_getCumulativeSessionStats( handle, &tmp );
saveCumulativeStats( &tmp );
static void tr_free( handle->sessionStats );
updateRatio( tr_session_stats * stats UNUSED ) handle->sessionStats = NULL;
{
fprintf( stderr, "FIXME" );
} }
void void
tr_getSessionStats( const tr_handle * handle, tr_getSessionStats( const tr_handle * handle,
tr_session_stats * setme ) tr_session_stats * setme )
{ {
*setme = handle->sessionStats; const struct tr_stats_handle * stats = handle->sessionStats;
updateRatio( setme ); *setme = stats->single;
setme->ratio = (double)setme->uploadedMiB / (double)setme->downloadedMiB;
setme->secondsActive += (time(NULL) - stats->startTime );
} }
void void
tr_getCumulativeSessionStats( const tr_handle * handle, tr_getCumulativeSessionStats( const tr_handle * handle,
tr_session_stats * setme ) tr_session_stats * setme )
{ {
*setme = handle->cumulativeStats; const struct tr_stats_handle * stats = handle->sessionStats;
updateRatio( setme ); *setme = stats->cumulative;
setme->ratio = (double)setme->uploadedMiB / (double)setme->downloadedMiB;
setme->secondsActive += (time(NULL) - stats->startTime );
} }
/** /**
*** ***
**/ **/
static void #define MiB 1048576
add( uint64_t * gigs, uint64_t * bytes, uint32_t addme )
{
uint64_t i;
const uint64_t GIGABYTE = 1073741824;
i = *bytes;
i += addme;
*gigs += i / GIGABYTE;
*bytes = i % GIGABYTE;
}
void void
tr_statsAddUploaded( tr_handle * handle, uint32_t bytes ) tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
{ {
add( &handle->sessionStats.uploadedGigs, struct tr_stats_handle * stats = handle->sessionStats;
&handle->sessionStats.uploadedBytes, bytes ); stats->bytes_up += bytes;
add( &handle->cumulativeStats.uploadedGigs, if( stats->bytes_up >= MiB )
&handle->cumulativeStats.uploadedBytes, bytes ); {
const uint32_t megs = stats->bytes_up / MiB;
stats->bytes_up %= MiB;
stats->single.uploadedMiB += megs;
stats->cumulative.uploadedMiB += megs;
}
} }
void void
tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes ) tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
{ {
add( &handle->sessionStats.downloadedGigs, struct tr_stats_handle * stats = handle->sessionStats;
&handle->sessionStats.downloadedBytes, bytes ); stats->bytes_down += bytes;
add( &handle->cumulativeStats.downloadedGigs, if( stats->bytes_down >= MiB )
&handle->cumulativeStats.downloadedBytes, bytes ); {
const uint32_t megs = stats->bytes_down / MiB;
stats->bytes_down %= MiB;
stats->single.downloadedMiB += megs;
stats->cumulative.downloadedMiB += megs;
}
}
void
tr_torrentAdded( tr_handle * handle, const tr_torrent * torrent )
{
struct tr_stats_handle * stats = handle->sessionStats;
stats->cumulative.filesAdded += torrent->info.fileCount;
stats->single.filesAdded += torrent->info.fileCount;
} }

View file

@ -15,7 +15,7 @@
void tr_statsInit( tr_handle * handle ); void tr_statsInit( tr_handle * handle );
void tr_statsClose( const tr_handle * handle ); void tr_statsClose( tr_handle * handle );
void tr_statsAddUploaded( tr_handle * handle, uint32_t bytes ); void tr_statsAddUploaded( tr_handle * handle, uint32_t bytes );

View file

@ -118,10 +118,8 @@ void tr_close( tr_handle * );
typedef struct tr_session_stats typedef struct tr_session_stats
{ {
uint64_t downloadedGigs; /* total down / GiB */ uint64_t uploadedMiB; /* total up */
uint64_t downloadedBytes; /* total down % GiB */ uint64_t downloadedMiB; /* total down */
uint64_t uploadedGigs; /* total up / GiB */
uint64_t uploadedBytes; /* total up % GiB */
double ratio; /* total up / total down */ double ratio; /* total up / total down */
uint64_t filesAdded; /* number of files added */ uint64_t filesAdded; /* number of files added */
uint64_t sessionCount; /* program started N times */ uint64_t sessionCount; /* program started N times */