summaryrefslogtreecommitdiff
path: root/htroot/AccessTracker_p.java
blob: f9d7120402b79b040b515c36c8557d93ae562f51 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// AccessTracker_p.java
// (C) 2006 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 14.01.2007 on http://www.yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
// $LastChangedRevision: 1986 $
// $LastChangedBy: orbiter $
//
// LICENSE
// 
// 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map.Entry;

import de.anomic.http.httpHeader;
import de.anomic.net.natLib;
import de.anomic.plasma.plasmaSearchQuery;
import de.anomic.plasma.plasmaSwitchboard;
import de.anomic.server.serverDate;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;
import de.anomic.yacy.yacySeed;

public class AccessTracker_p {
    
	private static final TreeMap<Long, String> treemapclone(TreeMap<Long, String> m) {
		TreeMap<Long, String> accessClone = new TreeMap<Long, String>();
		try {
			accessClone.putAll(m);
		} catch (ConcurrentModificationException e) {}
		return accessClone;
	}
	
    public static serverObjects respond(httpHeader header, serverObjects post, serverSwitch<?> env) {
        plasmaSwitchboard sb = (plasmaSwitchboard) env;
     
        // return variable that accumulates replacements
        serverObjects prop = new serverObjects();
        prop.setLocalized(!((String)header.get("PATH")).endsWith(".xml"));
        int page = 0;
        if (post != null) page = post.getInt("page", 0);
        prop.put("page", page);
     
        int maxCount = 1000;
        boolean dark = true;
        if (page == 0) {
            Iterator<String> i = sb.accessHosts();
            String host;
            TreeMap<Long, String> access;
            int entCount = 0;
            try {
            while ((entCount < maxCount) && (i.hasNext())) {
                host = (String) i.next();
                access = sb.accessTrack(host);
                prop.putHTML("page_list_" + entCount + "_host", host);
                prop.putNum("page_list_" + entCount + "_countSecond", access.tailMap(new Long(System.currentTimeMillis() - 1000)).size());
                prop.putNum("page_list_" + entCount + "_countMinute", access.tailMap(new Long(System.currentTimeMillis() - 1000 * 60)).size());
                prop.putNum("page_list_" + entCount + "_count10Minutes", access.tailMap(new Long(System.currentTimeMillis() - 1000 * 60 * 10)).size());
                prop.putNum("page_list_" + entCount + "_countHour", access.tailMap(new Long(System.currentTimeMillis() - 1000 * 60 * 60)).size());
                entCount++;
            }
            } catch (ConcurrentModificationException e) {} // we dont want to synchronize this
            prop.put("page_list", entCount);
            prop.put("page_num", entCount);
        }
        if (page == 1) {
            String host = (post == null) ? "" : post.get("host", "");
            int entCount = 0;
            TreeMap<Long, String> access;
            Map.Entry<Long, String> entry;
            if (host.length() > 0) {
				access = sb.accessTrack(host);
				if (access != null) {
					try {
						Iterator<Map.Entry<Long, String>> ii = treemapclone(access).entrySet().iterator();
						while (ii.hasNext()) {
							entry = ii.next();
							prop.putHTML("page_list_" + entCount + "_host", host);
							prop.put("page_list_" + entCount + "_date", serverDate.formatShortSecond(new Date(((Long) entry.getKey()).longValue())));
							prop.putHTML("page_list_" + entCount + "_path", (String) entry.getValue());
							entCount++;
						}
					} catch (ConcurrentModificationException e) {} // we dont want to synchronize this
				}
			} else {
                try {
                	Iterator<String> i = sb.accessHosts();
                    while ((entCount < maxCount) && (i.hasNext())) {
						host = (String) i.next();
						access = sb.accessTrack(host);
						Iterator<Map.Entry<Long, String>> ii = treemapclone(access).entrySet().iterator();
						while (ii.hasNext()) {
							entry = ii.next();
							prop.putHTML("page_list_" + entCount + "_host", host);
							prop.put("page_list_" + entCount + "_date", serverDate.formatShortSecond(new Date(((Long) entry.getKey()).longValue())));
							prop.putHTML("page_list_" + entCount + "_path", (String) entry.getValue());
							entCount++;
						}
					}
				} catch (ConcurrentModificationException e) {} // we dont want to synchronize this
			}
            prop.put("page_list", entCount);
            prop.put("page_num", entCount);
        }
        if ((page == 2) || (page == 4)) {
            ArrayList<plasmaSearchQuery> array = (page == 2) ? sb.localSearches : sb.remoteSearches;
            plasmaSearchQuery searchProfile;
            int m = Math.min(maxCount, array.size());
            long qcountSum = 0;
            long rcountSum = 0;
            long utimeSum = 0;
            long stimeSum = 0;
            long rtimeSum = 0;
            
            for (int entCount = 0; entCount < m; entCount++) {
                searchProfile = array.get(array.size() - entCount - 1);
            
                // put values in template
                prop.put("page_list_" + entCount + "_dark", ((dark) ? 1 : 0) );
                dark =! dark;
                prop.putHTML("page_list_" + entCount + "_host", searchProfile.host);
                prop.put("page_list_" + entCount + "_date", serverDate.formatShortSecond(new Date(searchProfile.handle.longValue())));
                prop.put("page_list_" + entCount + "_timestamp", searchProfile.handle.longValue());
                if (page == 2) {
                    // local search
                    prop.putNum("page_list_" + entCount + "_offset", searchProfile.offset);
                    prop.putHTML("page_list_" + entCount + "_querystring", searchProfile.queryString);
                } else {
                    // remote search
                    prop.putHTML("page_list_" + entCount + "_peername", (searchProfile.remotepeer == null) ? "<unknown>" : searchProfile.remotepeer.getName());
                    prop.put("page_list_" + entCount + "_queryhashes", plasmaSearchQuery.anonymizedQueryHashes(searchProfile.queryHashes));
                }
                prop.putNum("page_list_" + entCount + "_querycount", searchProfile.linesPerPage);
                prop.putNum("page_list_" + entCount + "_resultcount", searchProfile.resultcount);
                prop.putNum("page_list_" + entCount + "_urltime", searchProfile.urlretrievaltime);
                prop.putNum("page_list_" + entCount + "_snippettime", searchProfile.snippetcomputationtime);
                prop.putNum("page_list_" + entCount + "_resulttime", searchProfile.searchtime);
                qcountSum += searchProfile.linesPerPage;
                rcountSum += searchProfile.resultcount;
                utimeSum += searchProfile.urlretrievaltime;
                stimeSum += searchProfile.snippetcomputationtime;
                rtimeSum += searchProfile.searchtime;
            }
            prop.put("page_list", m);
            prop.put("page_num", m);
            
            // Put -1 instead of NaN as result for empty search list
            if (m == 0) m = -1;
            prop.putNum("page_querycount_avg", (double) qcountSum / m);
            prop.putNum("page_resultcount_avg", (double) rcountSum / m);
            prop.putNum("page_urltime_avg", (double) utimeSum / m);
            prop.putNum("page_snippettime_avg", (double) stimeSum / m);
            prop.putNum("page_resulttime_avg", (double) rtimeSum / m);
            prop.putNum("page_total", (page == 2) ? sb.localSearches.size() : sb.remoteSearches.size());
        }
        if ((page == 3) || (page == 5)) {
            Iterator<Entry<String, TreeSet<Long>>> i = (page == 3) ? sb.localSearchTracker.entrySet().iterator() : sb.remoteSearchTracker.entrySet().iterator();
            String host;
            TreeSet<Long> handles;
            int entCount = 0;
            int qphSum = 0;
            Map.Entry<String, TreeSet<Long>> entry;
            try {
            while ((entCount < maxCount) && (i.hasNext())) {
                entry = i.next();
                host = entry.getKey();
                handles = entry.getValue();
                
                int dateCount = 0;
                Iterator<Long> ii = handles.iterator();
                while (ii.hasNext()) {
                	Long timestamp = ii.next();
                	prop.put("page_list_" + entCount + "_dates_" + dateCount + "_date", serverDate.formatShortSecond(new Date(timestamp.longValue())));
                	prop.put("page_list_" + entCount + "_dates_" + dateCount + "_timestamp", timestamp.toString());
                	dateCount++;
                }
                prop.put("page_list_" + entCount + "_dates", dateCount);
                int qph = handles.tailSet(new Long(System.currentTimeMillis() - 1000 * 60 * 60)).size();
                qphSum += qph;
                prop.put("page_list_" + entCount + "_qph", qph);
                
                prop.put("page_list_" + entCount + "_dark", ((dark) ? 1 : 0) ); dark =! dark;
                prop.putHTML("page_list_" + entCount + "_host", host);
                if (page == 5) {
                    yacySeed remotepeer = sb.wordIndex.seedDB.lookupByIP(natLib.getInetAddress(host), true, true, true);
                    prop.putHTML("page_list_" + entCount + "_peername", (remotepeer == null) ? "UNKNOWN" : remotepeer.getName());
                }
                prop.putNum("page_list_" + entCount + "_count", handles.size());

                // next
                entCount++;
            }
            } catch (ConcurrentModificationException e) {} // we dont want to synchronize this
            prop.put("page_list", entCount);
            prop.putNum("page_num", entCount);
            prop.putNum("page_total", (page == 3) ? sb.localSearches.size() : sb.remoteSearches.size());
            prop.putNum("page_qph_sum", qphSum);
        }
        // return rewrite properties
        return prop;
    }
 
}