source: trunk/libtransmission/stats.c @ 3988

Last change on this file since 3988 was 3988, checked in by charles, 15 years ago

plug in the stats.filesAdded field

File size: 5.0 KB
Line 
1/*
2 * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
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.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: peer-msgs.c 3906 2007-11-20 17:29:56Z charles $
11 */
12
13#include <string.h> /* memset */
14
15#include "transmission.h"
16#include "bencode.h"
17#include "platform.h" /* tr_getPrefsDirectory */
18#include "utils.h" /* tr_buildPath */
19
20/***
21****
22***/
23
24struct tr_stats_handle
25{
26    tr_session_stats single;
27    tr_session_stats cumulative;
28    time_t startTime;
29};
30
31static void
32parseCumulativeStats( tr_session_stats  * setme,
33                      const uint8_t     * content,
34                      size_t              len )
35{
36    benc_val_t top;
37
38    if( !tr_bencLoad( content, len, &top, NULL ) )
39    {
40        const benc_val_t * val;
41
42        if(( val = tr_bencDictFindType( &top, "uploaded-bytes", TYPE_INT )))
43            setme->uploadedBytes = (uint64_t) tr_bencGetInt( val );
44
45        if(( val = tr_bencDictFindType( &top, "downloaded-bytes", TYPE_INT )))
46            setme->downloadedBytes = (uint64_t) tr_bencGetInt( val );
47
48        if(( val = tr_bencDictFindType( &top, "files-added", TYPE_INT )))
49            setme->filesAdded = (uint64_t) tr_bencGetInt( val );
50
51        if(( val = tr_bencDictFindType( &top, "session-count", TYPE_INT )))
52            setme->sessionCount = (uint64_t) tr_bencGetInt( val );
53
54        if(( val = tr_bencDictFindType( &top, "seconds-active", TYPE_INT )))
55            setme->secondsActive = (uint64_t) tr_bencGetInt( val );
56
57        tr_bencFree( &top );
58    }
59}
60
61static char*
62getFilename( char * buf, size_t buflen )
63{
64    tr_buildPath( buf, buflen, tr_getPrefsDirectory(), "stats.benc", NULL );
65    return buf;
66}
67
68static void
69loadCumulativeStats( tr_session_stats * setme )
70{
71    size_t len;
72    uint8_t * content;
73    char filename[MAX_PATH_LENGTH];
74
75    getFilename( filename, sizeof(filename) );
76    content = tr_loadFile( filename, &len );
77    if( content != NULL )
78        parseCumulativeStats( setme, content, len );
79
80    tr_free( content );
81}
82
83static void
84saveCumulativeStats( const tr_session_stats * stats )
85{
86    FILE * fp;
87    char * str;
88    char filename[MAX_PATH_LENGTH];
89    int len;
90    benc_val_t top;
91
92    tr_bencInit( &top, TYPE_DICT );
93    tr_bencDictReserve( &top, 5 );
94    tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), stats->uploadedBytes );
95    tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), stats->downloadedBytes );
96    tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), stats->filesAdded );
97    tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), stats->sessionCount );
98    tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), stats->secondsActive );
99
100    str = tr_bencSave( &top, &len );
101    getFilename( filename, sizeof(filename) );
102    fp = fopen( filename, "wb+" );
103    fwrite( str, 1, len, fp );
104    fclose( fp );
105    tr_free( str );
106
107    tr_bencFree( &top );
108}
109
110/***
111****
112***/
113
114void
115tr_statsInit( tr_handle * handle )
116{
117    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
118    loadCumulativeStats( &stats->cumulative );
119    stats->cumulative.sessionCount++;
120    stats->startTime = time(NULL);
121    handle->sessionStats = stats;
122}
123
124void
125tr_statsClose( tr_handle * handle )
126{
127    tr_session_stats tmp;
128    tr_getCumulativeSessionStats( handle, &tmp );
129    saveCumulativeStats( &tmp );
130
131    tr_free( handle->sessionStats );
132    handle->sessionStats = NULL;
133}
134
135void
136tr_getSessionStats( const tr_handle   * handle,
137                    tr_session_stats  * setme )
138{
139    const struct tr_stats_handle * stats = handle->sessionStats;
140    *setme = stats->single;
141    setme->ratio = setme->downloadedBytes ? (double)setme->uploadedBytes / (double)setme->downloadedBytes : TR_RATIO_NA;
142    setme->secondsActive += ( time(NULL) - stats->startTime );
143}
144
145void
146tr_getCumulativeSessionStats( const tr_handle   * handle,
147                              tr_session_stats  * setme )
148{
149    const struct tr_stats_handle * stats = handle->sessionStats;
150    *setme = stats->cumulative;
151    setme->ratio = setme->downloadedBytes ? (double)setme->uploadedBytes / (double)setme->downloadedBytes : TR_RATIO_NA;
152    setme->secondsActive += ( time(NULL) - stats->startTime );
153}
154
155/**
156***
157**/
158
159void
160tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
161{
162    struct tr_stats_handle * stats = handle->sessionStats;
163    stats->single.uploadedBytes += bytes;
164    stats->cumulative.uploadedBytes += bytes;
165}
166
167void
168tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
169{
170    struct tr_stats_handle * stats = handle->sessionStats;
171    stats->single.downloadedBytes += bytes;
172    stats->cumulative.downloadedBytes += bytes;
173}
174
175void
176tr_statsFileCreated( tr_handle * handle )
177{
178    struct tr_stats_handle * stats = handle->sessionStats;
179    ++stats->cumulative.filesAdded;
180    ++stats->single.filesAdded;
181}
Note: See TracBrowser for help on using the repository browser.