source: branches/1.3x/libtransmission/stats.c @ 6489

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

(1.3x) lots of C correctness tweaks suggested by sparse/cgcc

  • Property svn:keywords set to Date Rev Author Id
File size: 5.7 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 6489 2008-08-11 19:05:00Z charles $
11 */
12
13#include <assert.h>
14
15#include "transmission.h"
16#include "bencode.h"
17#include "platform.h" /* tr_sessionGetConfigDir() */
18#include "stats.h"
19#include "utils.h" /* tr_buildPath */
20
21/***
22****
23***/
24
25struct tr_stats_handle
26{
27    tr_session_stats single;
28    tr_session_stats old;
29    time_t startTime;
30};
31
32static char*
33getOldFilename( const tr_handle * handle, char * buf, size_t buflen )
34{
35    tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
36                               "stats.benc",
37                               NULL );
38    return buf;
39}
40
41static char*
42getFilename( const tr_handle * handle, char * buf, size_t buflen )
43{
44    tr_buildPath( buf, buflen, tr_sessionGetConfigDir(handle),
45                               "stats.json",
46                               NULL );
47    return buf;
48}
49
50static void
51loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
52{
53    int loaded = FALSE;
54    char filename[MAX_PATH_LENGTH];
55    tr_benc top;
56
57    getFilename( handle, filename, sizeof(filename) );
58    loaded = !tr_bencLoadJSONFile( filename, &top );
59    if( !loaded ) {
60        getOldFilename( handle, filename, sizeof(filename) );
61        loaded = !tr_bencLoadFile( filename, &top );
62    }
63    if( loaded )
64    {
65        int64_t i;
66
67        if( tr_bencDictFindInt( &top, "downloaded-bytes", &i ) )
68            setme->downloadedBytes = (uint64_t) i;
69        if( tr_bencDictFindInt( &top, "files-added", &i ) )
70            setme->filesAdded = (uint64_t) i;
71        if( tr_bencDictFindInt( &top, "seconds-active", &i ) )
72            setme->secondsActive = (uint64_t) i;
73        if( tr_bencDictFindInt( &top, "session-count", &i ) )
74            setme->sessionCount = (uint64_t) i;
75        if( tr_bencDictFindInt( &top, "uploaded-bytes", &i ) )
76            setme->uploadedBytes = (uint64_t) i;
77
78        tr_bencFree( &top );
79    }
80}
81
82static void
83saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
84{
85    char filename[MAX_PATH_LENGTH];
86    tr_benc top;
87
88    tr_bencInitDict( &top, 5 );
89    tr_bencDictAddInt( &top, "downloaded-bytes", s->downloadedBytes );
90    tr_bencDictAddInt( &top, "files-added",      s->filesAdded );
91    tr_bencDictAddInt( &top, "seconds-active",   s->secondsActive );
92    tr_bencDictAddInt( &top, "session-count",    s->sessionCount );
93    tr_bencDictAddInt( &top, "uploaded-bytes",   s->uploadedBytes );
94
95    getFilename( handle, filename, sizeof(filename) );
96    tr_deepLog( __FILE__, __LINE__, NULL, "Saving stats to \"%s\"", filename );
97    tr_bencSaveJSONFile( 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_sessionGetCumulativeStats( 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    return handle->sessionStats;
131}
132
133/***
134****
135***/
136
137static void
138updateRatio( tr_session_stats * setme )
139{
140    setme->ratio = tr_getRatio( setme->uploadedBytes,
141                                setme->downloadedBytes );
142}
143
144static void
145addStats( tr_session_stats       * setme,
146          const tr_session_stats * a,
147          const tr_session_stats * b )
148{
149    setme->uploadedBytes   = a->uploadedBytes   + b->uploadedBytes;
150    setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
151    setme->filesAdded      = a->filesAdded      + b->filesAdded;
152    setme->sessionCount    = a->sessionCount    + b->sessionCount;
153    setme->secondsActive   = a->secondsActive   + b->secondsActive;
154    updateRatio( setme );
155}
156
157void
158tr_sessionGetStats( const tr_handle   * handle,
159                    tr_session_stats  * setme )
160{
161    const struct tr_stats_handle * stats = getStats( handle );
162    assert( stats );
163    *setme = stats->single;
164    setme->secondsActive = time( NULL ) - stats->startTime;
165    updateRatio( setme );
166}
167
168void
169tr_sessionGetCumulativeStats( const tr_handle   * handle,
170                              tr_session_stats  * setme )
171{
172    const struct tr_stats_handle * stats = getStats( handle );
173    tr_session_stats current;
174    assert( stats );
175    tr_sessionGetStats( handle, &current );
176    addStats( setme, &stats->old, &current );
177}
178
179void
180tr_sessionClearStats( tr_handle * handle )
181{
182    tr_session_stats zero;
183    zero.uploadedBytes = 0;
184    zero.downloadedBytes = 0;
185    zero.ratio = TR_RATIO_NA;
186    zero.filesAdded = 0;
187    zero.sessionCount = 0;
188    zero.secondsActive = 0;
189    handle->sessionStats->single = handle->sessionStats->old = zero;
190
191    handle->sessionStats->startTime = time( NULL );
192}
193
194/**
195***
196**/
197
198void
199tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
200{
201    struct tr_stats_handle * s;
202    if(( s = getStats( handle )))
203        s->single.uploadedBytes += bytes;
204}
205
206void
207tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
208{
209    struct tr_stats_handle * s;
210    if(( s = getStats( handle )))
211        s->single.downloadedBytes += bytes;
212}
213
214void
215tr_statsFileCreated( tr_handle * handle )
216{
217    struct tr_stats_handle * s;
218    if(( s = getStats( handle )))
219        s->single.filesAdded++;
220}
Note: See TracBrowser for help on using the repository browser.