source: trunk/libtransmission/stats.c @ 5608

Last change on this file since 5608 was 5608, checked in by charles, 14 years ago

#394: resume files should use original name instead of hash name

  • Property svn:keywords set to Date Rev Author Id
File size: 5.2 KB
Line 
1/*
2 * This file Copyright (C) 2007-2008 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: stats.c 5608 2008-04-13 22:31:07Z charles $
11 */
12
13#include <string.h> /* memset */
14
15#include "transmission.h"
16#include "bencode.h"
17#include "platform.h" /* tr_getConfigDir() */
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 old;
28    time_t startTime;
29};
30
31static void
32parseCumulativeStats( tr_session_stats  * setme,
33                      const uint8_t     * content,
34                      size_t              len )
35{
36    tr_benc top;
37
38    if( !tr_bencLoad( content, len, &top, NULL ) )
39    {
40        int64_t i;
41
42        if( tr_bencDictFindInt( &top, "uploaded-bytes", &i ) )
43            setme->uploadedBytes = (uint64_t) i;
44
45        if( tr_bencDictFindInt( &top, "downloaded-bytes", &i ) )
46            setme->downloadedBytes = (uint64_t) i;
47
48        if( tr_bencDictFindInt( &top, "files-added", &i ) )
49            setme->filesAdded = (uint64_t) i;
50
51        if( tr_bencDictFindInt( &top, "session-count", &i ) )
52            setme->sessionCount = (uint64_t) i;
53
54        if( tr_bencDictFindInt( &top, "seconds-active", &i ) )
55            setme->secondsActive = (uint64_t) i;
56
57        tr_bencFree( &top );
58    }
59}
60
61static char*
62getFilename( const tr_handle * handle, char * buf, size_t buflen )
63{
64    tr_buildPath( buf, buflen, tr_getConfigDir(handle), "stats.benc", NULL );
65    return buf;
66}
67
68static void
69loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
70{
71    size_t len;
72    uint8_t * content;
73    char filename[MAX_PATH_LENGTH];
74
75    getFilename( handle, 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_handle * handle, const tr_session_stats * stats )
85{
86    FILE * fp;
87    char * str;
88    char filename[MAX_PATH_LENGTH];
89    int len;
90    tr_benc top;
91
92    tr_bencInitDict( &top, 5 );
93    tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), stats->uploadedBytes );
94    tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), stats->downloadedBytes );
95    tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), stats->filesAdded );
96    tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), stats->sessionCount );
97    tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), stats->secondsActive );
98
99    str = tr_bencSave( &top, &len );
100    getFilename( handle, filename, sizeof(filename) );
101    fp = fopen( filename, "wb+" );
102    fwrite( str, 1, len, fp );
103    fclose( fp );
104    tr_free( str );
105
106    tr_bencFree( &top );
107}
108
109/***
110****
111***/
112
113void
114tr_statsInit( tr_handle * handle )
115{
116    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
117    loadCumulativeStats( handle, &stats->old );
118    stats->single.sessionCount = 1;
119    stats->startTime = time( NULL );
120    handle->sessionStats = stats;
121}
122
123void
124tr_statsClose( tr_handle * handle )
125{
126    tr_session_stats cumulative;
127    tr_getCumulativeSessionStats( handle, &cumulative );
128    saveCumulativeStats( handle, &cumulative );
129
130    tr_free( handle->sessionStats );
131    handle->sessionStats = NULL;
132}
133
134static struct tr_stats_handle *
135getStats( const tr_handle * handle )
136{
137    static struct tr_stats_handle nullObject;
138
139    return handle && handle->sessionStats
140        ? handle->sessionStats
141        : &nullObject;
142}
143
144/***
145****
146***/
147
148static void
149updateRatio( tr_session_stats * setme )
150{
151    setme->ratio = tr_getRatio( setme->uploadedBytes,
152                                setme->downloadedBytes );
153}
154
155static void
156addStats( tr_session_stats       * setme,
157          const tr_session_stats * a,
158          const tr_session_stats * b )
159{
160    setme->uploadedBytes   = a->uploadedBytes   + b->uploadedBytes;
161    setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
162    setme->filesAdded      = a->filesAdded      + b->filesAdded;
163    setme->sessionCount    = a->sessionCount    + b->sessionCount;
164    setme->secondsActive   = a->secondsActive   + b->secondsActive;
165    updateRatio( setme );
166}
167
168void
169tr_getSessionStats( const tr_handle   * handle,
170                    tr_session_stats  * setme )
171{
172    const struct tr_stats_handle * stats = getStats( handle );
173    *setme = stats->single;
174    setme->secondsActive = time( NULL ) - stats->startTime;
175    updateRatio( setme );
176}
177
178void
179tr_getCumulativeSessionStats( const tr_handle   * handle,
180                              tr_session_stats  * setme )
181{
182    tr_session_stats current;
183    tr_getSessionStats( handle, &current );
184    addStats( setme, &getStats(handle)->old, &current );
185}
186
187/**
188***
189**/
190
191void
192tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
193{
194    getStats(handle)->single.uploadedBytes += bytes;
195}
196
197void
198tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
199{
200    getStats(handle)->single.downloadedBytes += bytes;
201}
202
203void
204tr_statsFileCreated( tr_handle * handle )
205{
206    getStats(handle)->single.filesAdded++;
207}
Note: See TracBrowser for help on using the repository browser.