| 1 | /* class to encapsulate set of functions to manage a circular array; |
|---|
| 2 | * recent hit information is stored in the looping array; each hit |
|---|
| 3 | * has information written into the next slot of structs. If |
|---|
| 4 | * we reach the end, start over. Then the top-URL/IP is summed up from |
|---|
| 5 | * this information. The bigger the table, the more hits you'll have |
|---|
| 6 | * to summarise from. |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include "apachetop.h" |
|---|
| 10 | |
|---|
| 11 | extern map *hm, *um, *rm; |
|---|
| 12 | |
|---|
| 13 | int Hits_Circle::create(unsigned int passed_size) |
|---|
| 14 | { |
|---|
| 15 | size = passed_size; |
|---|
| 16 | pos = 0; |
|---|
| 17 | walkpos = 0; |
|---|
| 18 | |
|---|
| 19 | tab = (circle_struct *) |
|---|
| 20 | calloc(size, sizeof( circle_struct)); |
|---|
| 21 | if (!tab) |
|---|
| 22 | { |
|---|
| 23 | abort(); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | reqcount = bytecount = 0; |
|---|
| 27 | memset(rc_summary, (char) NULL, sizeof(rc_summary)); |
|---|
| 28 | |
|---|
| 29 | return 0; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | int Hits_Circle::insert(struct logbits lb) |
|---|
| 33 | { |
|---|
| 34 | circle_struct *posptr; |
|---|
| 35 | short rc_tmp_old, rc_tmp_new; |
|---|
| 36 | |
|---|
| 37 | /* insert the given data into the current position, |
|---|
| 38 | * and update pos to point at next position */ |
|---|
| 39 | posptr = &tab[pos]; |
|---|
| 40 | |
|---|
| 41 | if (posptr->time == 0) |
|---|
| 42 | /* if this is a new insert, increment our count */ |
|---|
| 43 | ++reqcount; |
|---|
| 44 | else |
|---|
| 45 | { |
|---|
| 46 | /* if this is re-using an old slot, remove refcount for the |
|---|
| 47 | * previous data before we vape it */ |
|---|
| 48 | hm->sub_ref(posptr->host_pos); |
|---|
| 49 | um->sub_ref(posptr->url_pos); |
|---|
| 50 | rm->sub_ref(posptr->ref_pos); |
|---|
| 51 | |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /* maintain some stats */ |
|---|
| 55 | /* bytecount; remove the previous one and add the new one */ |
|---|
| 56 | bytecount -= posptr->bytes; |
|---|
| 57 | bytecount += lb.bytes; |
|---|
| 58 | /* retcodes, remember how many we have of each */ |
|---|
| 59 | rc_tmp_old = (int)posptr->retcode/100; |
|---|
| 60 | rc_tmp_new = (int)lb.retcode/100; |
|---|
| 61 | if (rc_tmp_old != rc_tmp_new) |
|---|
| 62 | { |
|---|
| 63 | --rc_summary[rc_tmp_old]; |
|---|
| 64 | ++rc_summary[rc_tmp_new]; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /* store the data */ |
|---|
| 68 | memcpy(posptr, &lb, sizeof(lb)); |
|---|
| 69 | |
|---|
| 70 | ++pos; |
|---|
| 71 | |
|---|
| 72 | /* see if we're running out of space. We'd like to keep however many |
|---|
| 73 | * hits of data the user has asked for; if we're not managing |
|---|
| 74 | * that, increase */ |
|---|
| 75 | if (pos == size) |
|---|
| 76 | { |
|---|
| 77 | /* loop round */ |
|---|
| 78 | pos = 0; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return 0; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | //int Hits_Circle::walk(unsigned int *url_pos, unsigned int *ip_pos, |
|---|
| 85 | // int *bytes, time_t *time, unsigned int *ipl, unsigned int *retcode) |
|---|
| 86 | int Hits_Circle::walk(struct logbits **lb) |
|---|
| 87 | { |
|---|
| 88 | /* return each value in the circle one by one, starting at 0 and |
|---|
| 89 | * working up; return 0 when there are more to go, or -1 when we're |
|---|
| 90 | * done */ |
|---|
| 91 | |
|---|
| 92 | *lb = NULL; |
|---|
| 93 | |
|---|
| 94 | if (walkpos == size || tab[walkpos].time == 0) |
|---|
| 95 | { |
|---|
| 96 | walkpos = 0; |
|---|
| 97 | return -1; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | *lb = &tab[walkpos]; |
|---|
| 101 | |
|---|
| 102 | ++walkpos; |
|---|
| 103 | return 0; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | time_t Hits_Circle::oldest(void) |
|---|
| 107 | { |
|---|
| 108 | int tmp; |
|---|
| 109 | |
|---|
| 110 | /* return the first entry we have. normally this will be pos+1, but |
|---|
| 111 | * cater for circumstances where it is isn't; ie we're initially |
|---|
| 112 | * filling up the array (use 0), or we're at position size (use 0) */ |
|---|
| 113 | if (pos == size) |
|---|
| 114 | tmp = 0; /* earliest will be 0 */ |
|---|
| 115 | else |
|---|
| 116 | tmp = pos + 1; /* earliest is next element */ |
|---|
| 117 | |
|---|
| 118 | if (tab[tmp].time > 0) |
|---|
| 119 | return tab[tmp].time; |
|---|
| 120 | |
|---|
| 121 | return tab[0].time; |
|---|
| 122 | } |
|---|
| 123 | |
|---|