aboutsummaryrefslogtreecommitdiff
path: root/src/rate.c
blob: 624a16e6cb8199ab15a08afd599a45e67ec39640 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* This file is part of mailfromd.
   Copyright (C) 2005, 2006, 2007 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#define MF_SOURCE_NAME  MF_SOURCE_RATE

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <mailutils/mailutils.h>
#include "mailfromd.h"

struct rate_result {
	time_t timestamp;
	time_t interval;
	size_t count;
};

mf_status
get_rate(char *email, double *ret)
{
	DBM_FILE db;
	DBM_DATUM key;
	DBM_DATUM contents;
	int local_contents = 0;
	struct rate_result *rp, rate;
	double result;
	time_t t;
	int res;
	
	debug1(50, "getting rate info for %s", email);
	if (mu_dbm_open(rate_format->dbname, &db, MU_STREAM_RDWR, 0600, NULL)) {
		mu_error("mu_dbm_open(%s) failed: %s", rate_format->dbname,
			 mu_dbm_strerror());
		return mf_failure;
	}

	memset (&key, 0, sizeof key);
	memset (&contents, 0, sizeof contents);
	MU_DATUM_PTR (key) = email;
	MU_DATUM_SIZE (key) = strlen (email) + 1;
	t = time(NULL);
	if ((res = mu_dbm_fetch(&db, key, &contents)) == 0) {
		rp = (struct rate_result *) MU_DATUM_PTR(contents);
		debug4(50,
		       "found time: %lu, interval: %lu, count: %lu, "
		       "rate: %g",
		       rp->timestamp, rp->interval, rp->count,
		       rp->interval == 0 ? -1 :
		       (double)rp->count/rp->interval);
	} else {
		if (res != MU_ERR_NOENT)
			mu_error("Cannot fetch `%s' from `%s': %s",
				 email, db.name, mu_dbm_strerror());	
		/* Initialize the structure */
		rate.timestamp = t;
		rate.interval = 0;
		rate.count = 0;
		rp = &rate;
		local_contents = 1;
	}

	/* Update the structure */
	rp->interval = t - rp->timestamp + rp->interval;
	rp->timestamp = t;
	rp->count++;

	if (rp->interval == 0)
		result = 0;
	else
		result = (double) rp->count / rp->interval;
	debug2(50, "rate for %s is %g", email, result);

	/* Update the db */
	if (rate_format->expire_interval
	    && rp->interval > rate_format->expire_interval) {
		debug1(50, "expiring %s rates", email);
		rp->interval = 0;
		rp->count = 1;
	} else 
		debug1(50, "updating %s rates", email);
	
	MU_DATUM_PTR(contents) = (void*)rp;
	MU_DATUM_SIZE(contents) = sizeof(*rp);
	if (mu_dbm_insert(&db, key, contents, 1))
		mu_error ("Cannot insert datum `%s' into `%s': %s",
	                  email, db.name, mu_dbm_strerror());

	if (!local_contents)
		mu_dbm_datum_free(&contents);

	mu_dbm_close(&db);
	*ret = result;
	return mf_success;
}

static void
rate_print_item(const char *email, size_t size, const void *content)
{
	const struct rate_result *res = content;
	double rate, exp_rate;

	size--; /* compensate for trailing zero */
	printf("%*.*s ", size, size, email);
	if (res->interval) {
		rate = (double)res->count / res->interval;
		exp_rate = (double)(res->count + 1) /
			   (time(NULL) - res->timestamp + res->interval);
	} else 
		rate = exp_rate = -1;

	format_time_str(stdout, res->timestamp);
	
	printf(" %6lu %6lu ",
	       res->interval,
	       (unsigned long) res->count);
	if (rate > 0)
		printf("%8.3g ", rate);
	else
		printf("%8.8s ", "N/A");
	if (exp_rate > 0)
		printf("%8.3g", exp_rate);
	else
		printf("%8.8s", "N/A");
	
	if (predict_next_option) {
		time_t next_time;
		time_t now = time(NULL);
		
		if (res->interval == 0) {
			next_time = 0;
		} else {
			double interval = (double) (res->count + 1) /
				                     predict_rate
				                     - res->interval;
			next_time = res->timestamp + interval;
		}
		if (next_time < now)
			printf("; free to send\n");
		else {
			printf("; in %lu sec. on ",
			       (next_time - now));
			format_time_str(stdout, next_time);
			putchar('\n');
		}
	} else
		printf("\n");
}

static int
rate_expire_item(const void *content)
{
	const struct rate_result *res = content;
	return rate_format->expire_interval &&
  	   time(NULL) - res->timestamp + res->interval >
		 rate_format->expire_interval;
}

static struct db_format rate_format_struct = {
	"rate",
	DEFAULT_RATE_DATABASE,
	1,
	DEFAULT_EXPIRE_INTERVAL,
	rate_print_item,
	rate_expire_item
};

struct db_format *rate_format = &rate_format_struct;

Return to:

Send suggestions and report system problems to the System administrator.