cscvs to tla changeset 69
[onak.git] / stats.c
1 /*
2  * stats.c - various routines to do stats on the key graph
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2000-2002 Project Purple
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "getcgi.h"
13 #include "hash.h"
14 #include "keydb.h"
15 #include "ll.h"
16 #include "stats.h"
17
18 /**
19  *      initcolour - Clear the key graph ready for use.
20  *      @parent: Do we want to clear the parent pointers too?
21  *
22  *      Clears the parent and colour information on all elements in the key
23  *      graph.
24  */
25 void initcolour(bool parent)
26 {
27         unsigned long loop;
28         struct ll *curkey;
29
30         /*
31          * Init the colour/parent values. We get each entry list from the hash
32          * table and walk along it, zeroing the values.
33          */
34         for (loop = 0; loop < HASHSIZE; loop++) {
35                 curkey = gethashtableentry(loop);
36                 while (curkey != NULL) {
37                         ((struct stats_key *)curkey->object)->colour = 0;
38                         if (parent) {
39                                 ((struct stats_key *)curkey->object)->parent =
40                                         0;
41                         }
42                         curkey = curkey->next;
43                 }
44         }
45 }
46
47 /**
48  *      findpath - Given 2 keys finds a path between them.
49  *      @have: The key we have.
50  *      @want: The key we want to get to.
51  *
52  *      This does a breadth first search on the key tree, starting with the
53  *      key we have. It returns as soon as a path is found or when we run out
54  *      of keys; whichever comes sooner.
55  */
56 unsigned long findpath(struct stats_key *have, struct stats_key *want)
57 {
58         struct ll *keys = NULL;
59         struct ll *oldkeys = NULL;
60         struct ll *sigs = NULL;
61         struct ll *nextkeys = NULL;
62         long curdegree = 0;
63         long count = 0;
64         
65         curdegree = 1;
66         keys = lladd(NULL, want);
67         oldkeys = keys;
68
69         while (keys != NULL && have->colour == 0) {
70                 sigs = cached_getkeysigs(((struct stats_key *)
71                                         keys->object)->keyid);
72                 while (sigs != NULL && have->colour == 0) {
73                         /*
74                          * Check if we've seen this key before and if not mark
75                          * it and add its sigs to the list we want to look at.
76                          */
77                         if (!((struct stats_key *)sigs->object)->disabled &&
78                             ((struct stats_key *)sigs->object)->colour == 0) {
79                                 count++;
80                                 ((struct stats_key *)sigs->object)->colour =
81                                         curdegree;
82                                 ((struct stats_key *)sigs->object)->parent =
83                                         ((struct stats_key *)
84                                          keys->object)->keyid;
85                                 nextkeys = lladd(nextkeys, sigs->object);
86                         }
87                         sigs = sigs->next;
88                 }
89                 keys = keys->next;
90                 if (keys == NULL) {
91                         keys = nextkeys;
92                         llfree(oldkeys, NULL);
93                         oldkeys = keys;
94                         nextkeys = NULL;
95                         curdegree++;
96                 }
97         }
98         if (oldkeys != NULL) {
99                 llfree(oldkeys, NULL);
100                 oldkeys = NULL;
101         }
102         if (nextkeys != NULL) {
103                 llfree(nextkeys, NULL);
104                 nextkeys = NULL;
105         }
106
107         return count;
108 }
109
110 /**
111  *      dofindpath - Given 2 keys displays a path between them.
112  *      @have: The key we have.
113  *      @want: The key we want to get to.
114  *      @html: Should we output in html.
115  *      @count: How many paths we should look for.
116  *
117  *      This does a breadth first search on the key tree, starting with the
118  *      key we have. It returns as soon as a path is found or when we run out
119  *      of keys; whichever comes sooner.
120  */
121 void dofindpath(uint64_t have, uint64_t want, bool html, int count)
122 {
123         struct stats_key *keyinfoa, *keyinfob, *curkey;
124         uint64_t fullhave, fullwant;
125         int rec;
126         int pathnum;
127         char *uid;
128
129         fullhave = getfullkeyid(have);
130         fullwant = getfullkeyid(want);
131
132         /*
133          * Make sure the keys we have and want are in the cache.
134          */
135         cached_getkeysigs(fullhave);
136         cached_getkeysigs(fullwant);
137
138         if ((keyinfoa = findinhash(fullhave)) == NULL) {
139                 printf("Couldn't find key 0x%llX.\n", have);
140                 return;
141         }
142         if ((keyinfob = findinhash(fullwant)) == NULL) {
143                 printf("Couldn't find key 0x%llX.\n", want);
144                 return;
145         }
146
147         pathnum = 0;
148         
149         while (pathnum < count) {
150                 /*
151                  * Fill the tree info up.
152                  */
153                 initcolour(true);
154                 rec = findpath(keyinfoa, keyinfob);
155                 keyinfob->parent = 0;
156
157                 printf("%s%d nodes examined. %ld elements in the hash%s\n",
158                         html ? "<HR>" : "",
159                         rec,
160                         hashelements(),
161                         html ? "<BR>" : "");
162                 if (keyinfoa->colour == 0) {
163                         printf("Can't find a link from 0x%08llX to 0x%08llX"
164                                 "%s\n",
165                                 have,
166                                 want,
167                                 html ? "<BR>" : "");
168                 } else {
169                         printf("%d steps from 0x%08llX to 0x%08llX%s\n",
170                                 keyinfoa->colour, have & 0xFFFFFFFF,
171                                 want & 0xFFFFFFFF,
172                                 html ? "<BR>" : "");
173                         curkey = keyinfoa;
174                         while (curkey != NULL && curkey->keyid != 0) {
175                                 uid = keyid2uid(curkey->keyid);
176                                 if (html && uid == NULL) {
177                                         printf("<a href=\"lookup?op=get&search="
178                                                 "0x%08llX\">0x%08llX</a> (["
179                                                 "User id not found])%s<BR>\n",
180                                                 curkey->keyid & 0xFFFFFFFF,
181                                                 curkey->keyid & 0xFFFFFFFF,
182                                                 (curkey->keyid == fullwant) ?
183                                                         "" : " signs");
184                                 } else if (html && uid != NULL) {
185                                         printf("<a href=\"lookup?op=get&search="
186                                                 "0x%08llX\">0x%08llX</a>"
187                                                 " (<a href=\"lookup?op=vindex&"
188                                                 "search=0x%08llX\">%s</a>)%s"
189                                                 "<BR>\n",
190                                                 curkey->keyid & 0xFFFFFFFF,
191                                                 curkey->keyid & 0xFFFFFFFF,
192                                                 curkey->keyid & 0xFFFFFFFF,
193                                                 txt2html(uid),
194                                                 (curkey->keyid == fullwant) ?
195                                                 "" : " signs");
196                                 } else {
197                                         printf("0x%08llX (%s)%s\n",
198                                                 curkey->keyid & 0xFFFFFFFF,
199                                                 (uid == NULL) ?
200                                                         "[User id not found]" :
201                                                         uid,
202                                                 (curkey->keyid == fullwant) ?
203                                                 "" : " signs");
204                                 }
205                                 if (uid != NULL) {
206                                         free(uid);
207                                         uid = NULL;
208                                 }
209                                 if (curkey != keyinfoa && curkey != keyinfob) {
210                                         curkey->disabled = true;
211                                 }
212                                 curkey = findinhash(curkey->parent);
213                         }
214                         if (html) {
215                                 puts("<P>List of key ids in path:</P>");
216                         } else {
217                                 puts("List of key ids in path:");
218                         }
219                         curkey = keyinfoa;
220                         while (curkey != NULL && curkey->keyid != 0) {
221                                 printf("0x%08llX ", curkey->keyid & 0xFFFFFFFF);
222                                 curkey = findinhash(curkey->parent);
223                         }
224                         putchar('\n');
225                 }
226                 pathnum++;
227         }
228 }
229
230
231
232 struct stats_key *furthestkey(struct stats_key *have)
233 {
234         unsigned long count = 0;
235         unsigned long curdegree = 0;
236         struct ll *curll, *nextll, *tmp;
237         struct ll *sigs = NULL;
238         struct stats_key *max;
239
240         if (have == NULL) {
241                 return NULL;
242         }
243
244         ++curdegree;
245
246         nextll = NULL;
247         max = have;
248         curll = lladd(NULL, have);
249
250         while (curll != NULL) {
251                 sigs = cached_getkeysigs(((struct stats_key *)
252                                 curll->object)->keyid);
253                 while (sigs != NULL) {
254                         if (((struct stats_key *) sigs->object)->colour == 0) {
255                                 /*
256                                  * We've never seen it. Count it, mark it and
257                                  * explore its subtree.
258                                  */
259                                 count++;
260                                 max = (struct stats_key *)sigs->object;
261                                 ((struct stats_key *)sigs->object)->colour = 
262                                         curdegree;
263                                 ((struct stats_key *)sigs->object)->parent = 
264                                         ((struct stats_key *)
265                                          curll->object)->keyid;
266                                 
267                                 nextll=lladd(nextll, sigs->object);
268                         }
269                         sigs=sigs->next;
270                 }
271                 tmp = curll->next;
272                 free(curll);
273                 curll = tmp;
274                 if (curll == NULL) {
275                         curll = nextll;
276                         nextll = NULL;
277                         ++curdegree;
278                 };
279         }
280
281         return max;
282 }