source: trunk/libtransmission/stats.c @ 5611

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

'resume' cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 5.0 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 5611 2008-04-14 11:52:50Z 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 * s )
85{
86    char filename[MAX_PATH_LENGTH];
87    tr_benc top;
88
89    tr_bencInitDict( &top, 5 );
90    tr_bencInitInt( tr_bencDictAdd( &top, "uploaded-bytes" ), s->uploadedBytes );
91    tr_bencInitInt( tr_bencDictAdd( &top, "downloaded-bytes" ), s->downloadedBytes );
92    tr_bencInitInt( tr_bencDictAdd( &top, "files-added" ), s->filesAdded );
93    tr_bencInitInt( tr_bencDictAdd( &top, "session-count" ), s->sessionCount );
94    tr_bencInitInt( tr_bencDictAdd( &top, "seconds-active" ), s->secondsActive );
95
96    getFilename( handle, filename, sizeof(filename) );
97    tr_bencSaveFile( filename, &top );
98
99    tr_bencFree( &top );
100}
101
102/***
103****
104***/
105
106void
107tr_statsInit( tr_handle * handle )
108{
109    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
110    loadCumulativeStats( handle, &stats->old );
111    stats->single.sessionCount = 1;
112    stats->startTime = time( NULL );
113    handle->sessionStats = stats;
114}
115
116void
117tr_statsClose( tr_handle * handle )
118{
119    tr_session_stats cumulative;
120    tr_getCumulativeSessionStats( handle, &cumulative );
121    saveCumulativeStats( handle, &cumulative );
122
123    tr_free( handle->sessionStats );
124    handle->sessionStats = NULL;
125}
126
127static struct tr_stats_handle *
128getStats( const tr_handle * handle )
129{
130    static struct tr_stats_handle nullObject;
131
132    return handle && handle->sessionStats
133        ? handle->sessionStats
134        : &nullObject;
135}
136
137/***
138****
139***/
140
141static void
142updateRatio( tr_session_stats * setme )
143{
144    setme->ratio = tr_getRatio( setme->uploadedBytes,
145                                setme->downloadedBytes );
146}
147
148static void
149addStats( tr_session_stats       * setme,
150          const tr_session_stats * a,
151          const tr_session_stats * b )
152{
153    setme->uploadedBytes   = a->uploadedBytes   + b->uploadedBytes;
154    setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
155    setme->filesAdded      = a->filesAdded      + b->filesAdded;
156    setme->sessionCount    = a->sessionCount    + b->sessionCount;
157    setme->secondsActive   = a->secondsActive   + b->secondsActive;
158    updateRatio( setme );
159}
160
161void
162tr_getSessionStats( const tr_handle   * handle,
163                    tr_session_stats  * setme )
164{
165    const struct tr_stats_handle * stats = getStats( handle );
166    *setme = stats->single;
167    setme->secondsActive = time( NULL ) - stats->startTime;
168    updateRatio( setme );
169}
170
171void
172tr_getCumulativeSessionStats( const tr_handle   * handle,
173                              tr_session_stats  * setme )
174{
175    tr_session_stats current;
176    tr_getSessionStats( handle, &current );
177    addStats( setme, &getStats(handle)->old, &current );
178}
179
180/**
181***
182**/
183
184void
185tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
186{
187    getStats(handle)->single.uploadedBytes += bytes;
188}
189
190void
191tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
192{
193    getStats(handle)->single.downloadedBytes += bytes;
194}
195
196void
197tr_statsFileCreated( tr_handle * handle )
198{
199    getStats(handle)->single.filesAdded++;
200}
Note: See TracBrowser for help on using the repository browser.