source: trunk/libtransmission/stats.c @ 5643

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

benc cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 4.6 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 5643 2008-04-18 16:23:59Z 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 char*
32getFilename( const tr_handle * handle, char * buf, size_t buflen )
33{
34    tr_buildPath( buf, buflen, tr_getConfigDir(handle), "stats.benc", NULL );
35    return buf;
36}
37
38static void
39loadCumulativeStats( const tr_handle * handle, tr_session_stats * setme )
40{
41    char filename[MAX_PATH_LENGTH];
42    tr_benc top;
43
44    getFilename( handle, filename, sizeof(filename) );
45    if( !tr_bencLoadFile( filename, &top ) )
46    {
47        int64_t i;
48
49        if( tr_bencDictFindInt( &top, "uploaded-bytes", &i ) )
50            setme->uploadedBytes = (uint64_t) i;
51
52        if( tr_bencDictFindInt( &top, "downloaded-bytes", &i ) )
53            setme->downloadedBytes = (uint64_t) i;
54
55        if( tr_bencDictFindInt( &top, "files-added", &i ) )
56            setme->filesAdded = (uint64_t) i;
57
58        if( tr_bencDictFindInt( &top, "session-count", &i ) )
59            setme->sessionCount = (uint64_t) i;
60
61        if( tr_bencDictFindInt( &top, "seconds-active", &i ) )
62            setme->secondsActive = (uint64_t) i;
63
64        tr_bencFree( &top );
65    }
66}
67
68static void
69saveCumulativeStats( const tr_handle * handle, const tr_session_stats * s )
70{
71    char filename[MAX_PATH_LENGTH];
72    tr_benc top;
73
74    tr_bencInitDict( &top, 5 );
75    tr_bencDictAddInt( &top, "uploaded-bytes",   s->uploadedBytes );
76    tr_bencDictAddInt( &top, "downloaded-bytes", s->downloadedBytes );
77    tr_bencDictAddInt( &top, "files-added",      s->filesAdded );
78    tr_bencDictAddInt( &top, "session-count",    s->sessionCount );
79    tr_bencDictAddInt( &top, "seconds-active",   s->secondsActive );
80
81    getFilename( handle, filename, sizeof(filename) );
82    tr_bencSaveFile( filename, &top );
83
84    tr_bencFree( &top );
85}
86
87/***
88****
89***/
90
91void
92tr_statsInit( tr_handle * handle )
93{
94    struct tr_stats_handle * stats = tr_new0( struct tr_stats_handle, 1 );
95    loadCumulativeStats( handle, &stats->old );
96    stats->single.sessionCount = 1;
97    stats->startTime = time( NULL );
98    handle->sessionStats = stats;
99}
100
101void
102tr_statsClose( tr_handle * handle )
103{
104    tr_session_stats cumulative;
105    tr_getCumulativeSessionStats( handle, &cumulative );
106    saveCumulativeStats( handle, &cumulative );
107
108    tr_free( handle->sessionStats );
109    handle->sessionStats = NULL;
110}
111
112static struct tr_stats_handle *
113getStats( const tr_handle * handle )
114{
115    static struct tr_stats_handle nullObject;
116
117    return handle && handle->sessionStats
118        ? handle->sessionStats
119        : &nullObject;
120}
121
122/***
123****
124***/
125
126static void
127updateRatio( tr_session_stats * setme )
128{
129    setme->ratio = tr_getRatio( setme->uploadedBytes,
130                                setme->downloadedBytes );
131}
132
133static void
134addStats( tr_session_stats       * setme,
135          const tr_session_stats * a,
136          const tr_session_stats * b )
137{
138    setme->uploadedBytes   = a->uploadedBytes   + b->uploadedBytes;
139    setme->downloadedBytes = a->downloadedBytes + b->downloadedBytes;
140    setme->filesAdded      = a->filesAdded      + b->filesAdded;
141    setme->sessionCount    = a->sessionCount    + b->sessionCount;
142    setme->secondsActive   = a->secondsActive   + b->secondsActive;
143    updateRatio( setme );
144}
145
146void
147tr_getSessionStats( const tr_handle   * handle,
148                    tr_session_stats  * setme )
149{
150    const struct tr_stats_handle * stats = getStats( handle );
151    *setme = stats->single;
152    setme->secondsActive = time( NULL ) - stats->startTime;
153    updateRatio( setme );
154}
155
156void
157tr_getCumulativeSessionStats( const tr_handle   * handle,
158                              tr_session_stats  * setme )
159{
160    tr_session_stats current;
161    tr_getSessionStats( handle, &current );
162    addStats( setme, &getStats(handle)->old, &current );
163}
164
165/**
166***
167**/
168
169void
170tr_statsAddUploaded( tr_handle * handle, uint32_t bytes )
171{
172    getStats(handle)->single.uploadedBytes += bytes;
173}
174
175void
176tr_statsAddDownloaded( tr_handle * handle, uint32_t bytes )
177{
178    getStats(handle)->single.downloadedBytes += bytes;
179}
180
181void
182tr_statsFileCreated( tr_handle * handle )
183{
184    getStats(handle)->single.filesAdded++;
185}
Note: See TracBrowser for help on using the repository browser.