Changeset 3976


Ignore:
Timestamp:
Nov 26, 2007, 6:55:36 PM (15 years ago)
Author:
charles
Message:

work on the stats a little bit.

Location:
trunk/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/internal.h

    r3970 r3976  
    195195    uint8_t                    isClosed;
    196196
    197     tr_session_stats           sessionStats;
    198     tr_session_stats           cumulativeStats;
     197    struct tr_stats_handle   * sessionStats;
    199198};
    200199
  • trunk/libtransmission/stats.c

    r3921 r3976  
    2222***/
    2323
     24struct tr_stats_handle
     25{
     26    tr_session_stats single;
     27    tr_session_stats cumulative;
     28    uint32_t bytes_up;
     29    uint32_t bytes_down;
     30    time_t startTime;
     31};
     32
    2433static void
    2534parseCumulativeStats( tr_session_stats  * setme,
     
    3342        const benc_val_t * val;
    3443
    35         if(( val = tr_bencDictFindType( &top, "uploaded-gigabytes", TYPE_INT )))
    36             setme->uploadedGigs = (uint64_t) tr_bencGetInt( val );
     44        if(( val = tr_bencDictFindType( &top, "uploaded-mib", TYPE_INT )))
     45            setme->uploadedMiB = (uint64_t) tr_bencGetInt( val );
    3746
    38         if(( val = tr_bencDictFindType( &top, "uploaded-bytes", TYPE_INT )))
    39             setme->uploadedBytes = (uint64_t) tr_bencGetInt( val );
    40 
    41         if(( val = tr_bencDictFindType( &top, "downloaded-gigabytes", TYPE_INT )))
    42             setme->downloadedGigs = (uint64_t) tr_bencGetInt( val );
    43 
    44         if(( val = tr_bencDictFindType( &top, "downloaded-bytes", TYPE_INT )))
    45             setme->downloadedBytes = (uint64_t) tr_bencGetInt( val );
     47        if(( val = tr_bencDictFindType( &top, "downloaded-mib", TYPE_INT )))
     48            setme->downloadedMiB = (uint64_t) tr_bencGetInt( val );
    4649
    4750        if(( val = tr_bencDictFindType( &top, "files-added", TYPE_INT )))
     
    5861}
    5962
     63static char*
     64getFilename( char * buf, size_t buflen )
     65{
     66    tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "stats.benc", NULL );
     67    return buf;
     68}
     69
    6070static void
    6171loadCumulativeStats( tr_session_stats * setme )
     
    6373    size_t len;
    6474    uint8_t * content;
    65     char path[MAX_PATH_LENGTH];
     75    char filename[MAX_PATH_LENGTH];
    6676
    67     tr_buildPath( path, sizeof(path), tr_getPrefsDirectory(), "stats.benc", NULL );
    68     content = tr_loadFile( path, &len );
     77    getFilename( filename, sizeof(filename) );
     78    content = tr_loadFile( filename, &len );
    6979    if( content != NULL )
    7080        parseCumulativeStats( setme, content, len );
    7181
    7282    tr_free( content );
     83}
     84
     85static void
     86saveCumulativeStats( const tr_session_stats * stats )
     87{
     88    FILE * fp;
     89    char * str;
     90    char filename[MAX_PATH_LENGTH];
     91    int len;
     92    benc_val_t top, *val;
     93
     94    tr_bencInit( &top, TYPE_DICT );
     95    tr_bencDictReserve( &top, 5 );
     96    tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-mib" ), stats->uploadedMiB );
     97    tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-mib" ), stats->downloadedMiB );
     98    tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), stats->filesAdded );
     99    tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), stats->sessionCount );
     100    tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), stats->secondsActive );
     101
     102    str = tr_bencSave( &top, &len );
     103    getFilename( filename, sizeof(filename) );
     104    fp = fopen( filename, "wb+" );
     105    fwrite( str, 1, len, fp );
     106    fclose( fp );
     107    tr_free( str );
     108
     109    tr_BencFree( &top );
    73110}
    74111
     
    80117tr_statsInit( tr_handle * handle )
    81118{
    82     memset( &handle->sessionStats, 0, sizeof( tr_session_stats ) );
    83     memset( &handle->cumulativeStats, 0, sizeof( tr_session_stats ) );
    84 
    85     loadCumulativeStats( &handle->cumulativeStats );
     119    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
     120    loadCumulativeStats( &stats->cumulative );
     121    stats->cumulative.sessionCount++;
     122    stats->startTime = time(NULL);
     123    handle->sessionStats = stats;
    86124}
    87125
    88126void
    89 tr_statsClose( const tr_handle * handle UNUSED )
     127tr_statsClose( tr_handle * handle )
    90128{
    91     fprintf( stderr, "FIXME" );
    92 }
     129    tr_session_stats tmp;
     130    tr_getCumulativeSessionStats( handle, &tmp );
     131    saveCumulativeStats( &tmp );
    93132
    94 static void
    95 updateRatio( tr_session_stats * stats UNUSED )
    96 {
    97     fprintf( stderr, "FIXME" );
     133    tr_free( handle->sessionStats );
     134    handle->sessionStats = NULL;
    98135}
    99136
     
    102139                    tr_session_stats  * setme )
    103140{
    104     *setme = handle->sessionStats;
    105     updateRatio( setme );
     141    const struct tr_stats_handle * stats = handle->sessionStats;
     142    *setme = stats->single;
     143    setme->ratio = (double)setme->uploadedMiB / (double)setme->downloadedMiB;
     144    setme->secondsActive += (time(NULL) - stats->startTime );
    106145}
    107146
     
    110149                              tr_session_stats  * setme )
    111150{
    112     *setme = handle->cumulativeStats;
    113     updateRatio( setme );
     151    const struct tr_stats_handle * stats = handle->sessionStats;
     152    *setme = stats->cumulative;
     153    setme->ratio = (double)setme->uploadedMiB / (double)setme->downloadedMiB;
     154    setme->secondsActive += (time(NULL) - stats->startTime );
    114155}
    115156
     
    118159**/
    119160
    120 static void
    121 add( uint64_t * gigs, uint64_t * bytes, uint32_t addme )
    122 {
    123     uint64_t i;
    124     const uint64_t GIGABYTE = 1073741824;
    125     i = *bytes;
    126     i += addme;
    127     *gigs += i / GIGABYTE;
    128     *bytes = i % GIGABYTE;
    129 }
     161#define MiB 1048576
    130162
    131163void
    132164tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
    133165{
    134     add( &handle->sessionStats.uploadedGigs,
    135          &handle->sessionStats.uploadedBytes, bytes );
    136     add( &handle->cumulativeStats.uploadedGigs,
    137          &handle->cumulativeStats.uploadedBytes, bytes );
     166    struct tr_stats_handle * stats = handle->sessionStats;
     167    stats->bytes_up += bytes;
     168    if( stats->bytes_up >= MiB )
     169    {
     170        const uint32_t megs = stats->bytes_up / MiB;
     171        stats->bytes_up %= MiB;
     172        stats->single.uploadedMiB += megs;
     173        stats->cumulative.uploadedMiB += megs;
     174    }
    138175}
    139176
     
    141178tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
    142179{
    143     add( &handle->sessionStats.downloadedGigs,
    144          &handle->sessionStats.downloadedBytes, bytes );
    145     add( &handle->cumulativeStats.downloadedGigs,
    146          &handle->cumulativeStats.downloadedBytes, bytes );
     180    struct tr_stats_handle * stats = handle->sessionStats;
     181    stats->bytes_down += bytes;
     182    if( stats->bytes_down >= MiB )
     183    {
     184        const uint32_t megs = stats->bytes_down / MiB;
     185        stats->bytes_down %= MiB;
     186        stats->single.downloadedMiB += megs;
     187        stats->cumulative.downloadedMiB += megs;
     188    }
    147189}
     190
     191void
     192tr_torrentAdded( tr_handle * handle, const tr_torrent * torrent )
     193{
     194    struct tr_stats_handle * stats = handle->sessionStats;
     195    stats->cumulative.filesAdded += torrent->info.fileCount;
     196    stats->single.filesAdded += torrent->info.fileCount;
     197}
  • trunk/libtransmission/stats.h

    r3921 r3976  
    1616void tr_statsInit( tr_handle * handle );
    1717
    18 void tr_statsClose( const tr_handle * handle );
     18void tr_statsClose( tr_handle * handle );
    1919
    2020void tr_statsAddUploaded( tr_handle * handle, uint32_t bytes );
  • trunk/libtransmission/transmission.h

    r3926 r3976  
    119119typedef struct tr_session_stats
    120120{
    121     uint64_t downloadedGigs;  /* total down / GiB */
    122     uint64_t downloadedBytes; /* total down % GiB */
    123     uint64_t uploadedGigs;    /* total up / GiB */
    124     uint64_t uploadedBytes;   /* total up % GiB */
     121    uint64_t uploadedMiB;     /* total up */
     122    uint64_t downloadedMiB;   /* total down */
    125123    double ratio;             /* total up / total down */
    126124    uint64_t filesAdded;      /* number of files added */
Note: See TracChangeset for help on using the changeset viewer.