aboutsummaryrefslogtreecommitdiff
path: root/lib/rate.c
blob: bf7ab8d8a07ffaada6372f3e5dfd95c915bed3fb (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* This file is part of Mailfromd.
   Copyright (C) 2005-2012, 2015-2016 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 3, 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, see <http://www.gnu.org/licenses/>. */

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

#include <mailutils/mailutils.h>
#include "libmf.h"
#include "mfdb.h"
#include "filenames.h"

int predict_next_option;//FIXME
double predict_rate;//FIXME

static void rate_print_item(struct mu_dbm_datum const *key,
			    struct mu_dbm_datum const *val);
static int rate_expire_item(struct mu_dbm_datum const *content);

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

struct db_format *rate_format = &rate_format_struct;

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

mf_status
get_rate(char *email, long *ret, unsigned long interval, size_t mincount,
	 size_t threshold)
{
	mu_dbm_file_t db;
	struct mu_dbm_datum key;
	struct mu_dbm_datum contents;
	struct rate_result *rp, rate;
	long result;
	time_t t;
	int res;
	int obsolete;
	
	mu_debug(rate_format->debug_handle, MU_DEBUG_TRACE5,
		 ("getting rate info for %s", email));
	db = mf_dbm_open(rate_format->dbname, MU_STREAM_RDWR, 0600);
	if (!db)
		return mf_failure;

	memset(&key, 0, sizeof key);
	memset(&contents, 0, sizeof contents);
	key.mu_dptr = email;
	key.mu_dsize = strlen (email) + 1;
	t = time(NULL);
	if ((res = mu_dbm_fetch(db, &key, &contents)) == 0) {
		rp = (struct rate_result *) contents.mu_dptr;
		mu_debug(rate_format->debug_handle, MU_DEBUG_TRACE5,
			 ("found time: %lu, interval: %lu, count: %lu, "
			  "rate: %g",
			  rp->timestamp, rp->interval,
			  (unsigned long) 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, rate_format->dbname,
				 mu_dbm_strerror(db));	
		/* Initialize the structure */
		rate.timestamp = t;
		rate.interval = 0;
		rate.count = 0;
		rp = &rate;
	}

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

	if (rp->interval == 0 || rp->count < mincount)
		result = 0;
	else
		result = ((double) rp->count / rp->interval) * interval;
	mu_debug(rate_format->debug_handle, MU_DEBUG_TRACE5, 
		 ("rate for %s is %ld/%lu", email, result, interval));

	/* Update the db */
	if (threshold && result > threshold)
		mu_debug(rate_format->debug_handle, MU_DEBUG_TRACE5,
			 ("not updating %s rates", email));
	else {
		struct mu_dbm_datum dat;

		if (obsolete
		    || (rate_format->expire_interval
			&& rp->interval > rate_format->expire_interval)) {
			mu_debug(rate_format->debug_handle, MU_DEBUG_TRACE5,
				 ("expiring %s rates", email));
			rp->interval = 0;
			rp->count = 1;
		} else 
			mu_debug(rate_format->debug_handle, MU_DEBUG_TRACE5,
			      ("updating %s rates", email));

		dat.mu_dptr = (void*)rp;
		dat.mu_dsize = sizeof(*rp);
		res = mu_dbm_store(db, &key, &dat, 1);
		if (res)
			mu_error (_("cannot insert datum `%s' into `%s': %s"),
				  email, rate_format->dbname,
				  res == MU_ERR_FAILURE ?
				   mu_dbm_strerror(db) :
				   mu_strerror(res));
	}
	
	mu_dbm_datum_free(&contents);

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

static void
rate_print_item(struct mu_dbm_datum const *key,
		struct mu_dbm_datum const *val)
{
	const struct rate_result *res = (const struct rate_result *)
		                            val->mu_dptr;
	double rate, exp_rate;
	int size = key->mu_dsize - 1; /* Size includes the trailing nul */

	printf("%*.*s ", size, size, key->mu_dptr);
	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(struct mu_dbm_datum const *content)
{
	struct rate_result const const *res =
		(struct rate_result const const *) content->mu_dptr;
	return rate_format->expire_interval &&
  	   time(NULL) - res->timestamp + res->interval >
		 rate_format->expire_interval;
}

Return to:

Send suggestions and report system problems to the System administrator.