]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hstat.c
Initial Import
[irc/quakenet/newserv.git] / helpmod2 / hstat.c
1 #include <time.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <assert.h>
5
6 #include "hchannel.h"
7 #include "hstat.h"
8 #include "helpmod.h"
9 #include "hgen.h"
10
11 int hstat_is_prime_time(void)
12 {
13 time_t timer = time(NULL);
14 struct tm *tstruct = localtime(&timer);
15
16 if (tstruct->tm_hour > 12 && tstruct->tm_hour < 23)
17 return !0;
18 return 0;
19 }
20
21 void hstat_scheduler(void)
22 {
23 time_t timer = time(NULL);
24 struct tm *tstruct = localtime(&timer);
25 int is_sunday = (tstruct->tm_wday == 0); /* is it sunday ? */
26 int i;
27 hstat_cycle++;
28
29 { /* accounts */
30 haccount *ptr = haccounts;
31 if (is_sunday)
32 for (;ptr;ptr = ptr->next)
33 if (ptr->level > H_PEON)
34 {
35 hstat_account *ptr2 = ptr->stats;
36 for (;ptr2;ptr2 = ptr2->next)
37 {
38 HSTAT_ACCOUNT_ZERO(ptr2->longterm[(hstat_week() - 1) % 10]);
39 for (i=0;i<7;i++)
40 {
41 HSTAT_ACCOUNT_SUM(ptr2->longterm[(hstat_week() - 1) % 10], ptr2->longterm[(hstat_week() - 1) % 10], ptr2->week[i]);
42 }
43 /*ptr2->longterm[(hstat_week() - 1) % 10] = ptr2->longterm[hstat_week()];*/
44 /*for (i=0;i<7;i++)
45 {
46 HSTAT_ACCOUNT_ZERO(ptr2->week[i]);
47 }*/
48 }
49 }
50 }
51 { /* hchannels */
52 hchannel *ptr = hchannels;
53 for (;ptr;ptr = ptr->next)
54 {
55 if ((ptr->flags & H_REPORT) && hchannel_is_valid(ptr))
56 {
57 hstat_channel_entry *entry = &ptr->stats->week[hstat_day()];
58 helpmod_message_channel(ptr->report_to, "Daily summary for channel %s: Time spent %s, joins %d and queue usage %d", hchannel_get_name(ptr),helpmod_strtime(entry->time_spent), entry->joins, entry->queue_use);
59 }
60 if (is_sunday)
61 {
62 hstat_channel *ptr2 = ptr->stats;
63 HSTAT_CHANNEL_ZERO(ptr2->longterm[(hstat_week() - 1) % 10]);
64 for (i=0;i<7;i++)
65 {
66 HSTAT_CHANNEL_SUM(ptr2->longterm[(hstat_week() - 1) % 10], ptr2->longterm[(hstat_week() - 1) % 10], ptr2->week[i]);
67 }
68 /*ptr2->longterm[(hstat_week() - 1) % 10] = ptr2->longterm[hstat_week()];*/
69 /*for (i=0;i<7;i++)
70 {
71 HSTAT_CHANNEL_ZERO(ptr2->week[i]);
72 }*/
73 }
74 }
75 }
76 }
77
78 int hstat_get_schedule_time(void)
79 {
80 time_t timer = time(NULL);
81 struct tm *tstruct = localtime(&timer);
82
83 return timer + (HDEF_d - (HDEF_h * tstruct->tm_hour + HDEF_m * tstruct->tm_min + tstruct->tm_sec));
84 }
85
86 hstat_channel *get_hstat_channel(void)
87 {
88 hstat_channel *tmp;
89
90 tmp = malloc(sizeof(hstat_channel));
91
92 memset(tmp, 0x00, sizeof(hstat_channel));
93
94 return tmp;
95 }
96
97 void hstat_del_channel(void* ptr)
98 {
99 hchannel *hchan = (hchannel*)ptr;
100 haccount *hacc = haccounts;
101 hstat_account **hs_acc;
102 for (;hacc;hacc = hacc->next)
103 for (hs_acc = &hacc->stats;*hs_acc;hs_acc = &(*hs_acc)->next)
104 if ((*hs_acc)->hchan == hchan)
105 {
106 hstat_account *tmp = (*hs_acc)->next;
107 free(*hs_acc);
108 *hs_acc = tmp;
109 break;
110 }
111 }
112
113 hstat_account *get_hstat_account(void)
114 {
115 hstat_account *tmp;
116
117 tmp = malloc(sizeof(hstat_account));
118
119 memset(tmp, 0x00, sizeof(hstat_account));
120
121 tmp->next = NULL; /* just for style */
122
123 return tmp;
124 }
125
126 void hstat_calculate_general(void* param1, void* param2, const char *message)
127 {
128 hchannel *hchan = (hchannel*)param1;
129 huser *husr = (huser*)param2;
130 hstat_account **acc_stat;
131 hstat_account_entry *acc_entry;
132 hstat_channel_entry *chan_entry;
133 huser_channel *huserchan = huser_on_channel(husr, hchan);
134
135 int wordc = 0, time_spent;
136
137 if (huserchan == NULL || husr->account == NULL)
138 return;
139
140 for (acc_stat = &husr->account->stats;*acc_stat;acc_stat = &(*acc_stat)->next)
141 if ((*acc_stat)->hchan == hchan)
142 break;
143
144 if (*acc_stat == NULL)
145 /* this user has no stats for the channel -> create them */
146 {
147 *acc_stat = get_hstat_account();
148 (*acc_stat)->hchan = hchan;
149 }
150
151 acc_entry = &(*acc_stat)->week[hstat_day()];
152 chan_entry = &hchan->stats->week[hstat_day()];
153
154 time_spent = time(NULL) - huserchan->last_activity;
155
156 if (time_spent < HSTAT_ACTIVITY_TRESHOLD)
157 {
158 acc_entry->time_spent+=time_spent;
159 chan_entry->time_spent+=time_spent;
160
161 if (hstat_is_prime_time())
162 {
163 acc_entry->prime_time_spent+=time_spent;
164 chan_entry->prime_time_spent+=time_spent;
165 }
166 }
167
168 acc_entry->lines++;
169 chan_entry->lines++;
170
171 wordc = hword_count(message);
172
173 acc_entry->words+=wordc;
174 chan_entry->words+=wordc;
175 }
176
177 void hstat_add_join(void *param)
178 {
179 hchannel *hchan = (hchannel*)param;
180 hchan->stats->week[hstat_day()].joins++;
181 }
182
183 void hstat_add_queue(void *param, int amount)
184 {
185 hchannel *hchan = (hchannel*)param;
186 hchan->stats->week[hstat_day()].queue_use+=amount;
187 }
188
189
190 const char *hstat_channel_print(hstat_channel_entry *entry, int type)
191 {
192 static char buffer[256];
193 buffer[0] = '\0';
194 switch (type)
195 {
196 case HSTAT_CHANNEL_LONG:
197 sprintf(buffer, "%-15s %-15s %-10d %-10d %-10d %-10d", helpmod_strtime(entry->time_spent), helpmod_strtime(entry->prime_time_spent), entry->joins, entry->queue_use, entry->lines, entry->words);
198 break;
199 case HSTAT_CHANNEL_SHORT:
200 sprintf(buffer, "%-15s %-15s", helpmod_strtime(entry->time_spent), helpmod_strtime(entry->prime_time_spent));
201 break;
202 }
203 return buffer;
204 }
205
206 const char *hstat_account_print(hstat_account_entry *entry, int type)
207 {
208 static char buffer[256];
209 buffer[0] = '\0';
210 switch (type)
211 {
212 case HSTAT_ACCOUNT_LONG:
213 sprintf(buffer, "%-15s %-15s %-10d %-10d", helpmod_strtime(entry->time_spent), helpmod_strtime(entry->prime_time_spent), entry->lines, entry->words);
214 break;
215 case HSTAT_ACCOUNT_SHORT:
216 sprintf(buffer, "%-15s %-15s", helpmod_strtime(entry->time_spent), helpmod_strtime(entry->prime_time_spent));
217 break;
218 }
219 return buffer;
220 }
221
222 const char *hstat_header(hstat_type type)
223 {
224 switch (type)
225 {
226 case HSTAT_ACCOUNT_SHORT:
227 return "TimeSpent PrimeTimeSpent";
228 case HSTAT_ACCOUNT_LONG:
229 return "TimeSpent PrimeTimeSpent Lines Words";
230 case HSTAT_CHANNEL_SHORT:
231 return "TimeSpent PrimeTimeSpent";
232 case HSTAT_CHANNEL_LONG:
233 return "TimeSpent PrimeTimeSpent Joins QueueUse Lines Words";
234 default:
235 return "error: please contact strutsi";
236 }
237 }
238
239 int hstat_week(void)
240 {
241 return (hstat_cycle / 7) % 10;
242 }
243
244 int hstat_day(void)
245 {
246 time_t timer = time(NULL);
247 struct tm *tstruct = localtime(&timer);
248
249 return tstruct->tm_wday;
250 }
251
252 int hstat_account_count(hstat_account *hs_acc)
253 {
254 int count = 0;
255 for (;hs_acc;hs_acc = hs_acc->next)
256 count++;
257 return count;
258 }
259
260 hstat_account_entry_sum hstat_account_last_week(hstat_account *hs_acc)
261 {
262 hstat_account_entry_sum tmp = {0,0,0,0};
263 int i;
264 for (i=0;i<7;i++)
265 {
266 HSTAT_ACCOUNT_SUM(tmp, tmp, hs_acc->week[i]);
267 }
268 return tmp;
269 }
270
271 hstat_account_entry_sum hstat_account_last_month(hstat_account *hs_acc)
272 {
273 hstat_account_entry_sum tmp = {0,0,0,0};
274 int i;
275
276 //HSTAT_ACCOUNT_SUM(tmp, tmp, hstat_account_last_week(hs_acc));
277 for (i=0;i<4;i++)
278 {
279 HSTAT_ACCOUNT_SUM(tmp, tmp, hs_acc->longterm[(hstat_week() + i) % 10]);
280 }
281 return tmp;
282 }
283
284 hstat_channel_entry hstat_channel_last_week(hstat_channel *hs_chan)
285 {
286 hstat_channel_entry tmp = {0,0,0,0,0,0};
287 int i;
288 for (i=0;i<7;i++)
289 {
290 HSTAT_CHANNEL_SUM(tmp, tmp, hs_chan->week[i]);
291 }
292 return tmp;
293 }
294
295 hstat_channel_entry hstat_channel_last_month(hstat_channel *hs_chan)
296 {
297 hstat_channel_entry tmp = {0,0,0,0,0,0};
298 int i;
299
300 //HSTAT_CHANNEL_SUM(tmp, tmp, hstat_channel_last_week(hs_chan));
301 for (i=0;i<4;i++)
302 {
303 HSTAT_CHANNEL_SUM(tmp, tmp, hs_chan->longterm[(hstat_week() + i) % 10]);
304 }
305 return tmp;
306 }
307
308 static int hstat_account_compare(hstat_account_entry_sum *e1, hstat_account_entry_sum *e2)
309 {
310 return e2->prime_time_spent - e1->prime_time_spent;
311 }
312
313 hstat_accounts_array create_hstat_account_array(void* tptr, hlevel lvl)
314 {
315 hchannel *hchan = (hchannel*)tptr;
316 hstat_accounts_array arr = {NULL, 0};
317 hstat_account *ptr;
318 hstat_account_entry_sum tmp1, tmp2;
319 haccount *hacc = haccounts;
320 int initial_arrlen = 0;
321
322 if (lvl == H_OPER)
323 initial_arrlen = haccount_count(H_OPER) + haccount_count(H_ADMIN);
324 else if (lvl == H_STAFF)
325 initial_arrlen = haccount_count(H_STAFF) + haccount_count(H_TRIAL);
326 else
327 initial_arrlen = haccount_count(lvl);
328
329 if (!initial_arrlen)
330 return arr;
331
332 arr.array = malloc(sizeof(hstat_account_entry_sum) * initial_arrlen);
333 for (;hacc;hacc = hacc->next)
334 if ((lvl == H_OPER && (hacc->level == H_OPER || hacc->level == H_ADMIN)) ||
335 (lvl == H_STAFF && (hacc->level == H_TRIAL || hacc->level == H_STAFF)))
336 //if ((lvl == H_OPER)?(hacc->level >= H_OPER && hacc->level < H_SERVICE):(hacc->level > H_PEON || hacc->level < H_OPER))
337 for (ptr = hacc->stats;ptr;ptr = ptr->next)
338 if (ptr->hchan == hchan)
339 {
340 assert(arr.arrlen < initial_arrlen);
341 tmp1 = hstat_account_last_month(ptr);
342 tmp2 = hstat_account_last_week(ptr);
343 HSTAT_ACCOUNT_SUM(arr.array[arr.arrlen], tmp1, tmp2);
344 arr.array[arr.arrlen].owner = (void*)hacc;
345 arr.arrlen++;
346 }
347
348 qsort(arr.array, arr.arrlen, sizeof(hstat_account_entry_sum), (int(*)(const void*, const void*))hstat_account_compare);
349 return arr;
350 }