/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1310 - (show annotations) (download)
Tue Apr 19 16:19:45 2011 UTC (13 years, 1 month ago) by torben
File size: 11857 byte(s)
Make requestplotter work with GFv3 access log files
1 package dk.thoerup.traininfoservice;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FilenameFilter;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.text.ParseException;
12 import java.text.SimpleDateFormat;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Date;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20 import java.util.zip.GZIPInputStream;
21 import java.util.zip.ZipEntry;
22 import java.util.zip.ZipOutputStream;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.annotation.WebServlet;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import dk.thoerup.traininfoservice.banedk.TimeoutMap;
31
32 @WebServlet(urlPatterns={"/RequestPlotter"})
33 public class RequestPlotter extends HttpServlet {
34 private static final long serialVersionUID = 1L;
35
36 static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
37
38 static final String KML = "application/vnd.google-earth.kml";
39 static final String KMZ = "application/vnd.google-earth.kmz";
40
41 Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000);
42
43 class RequestPosition {
44 public String ip;
45 public Date time;
46 public String lat;
47 public String lng;
48 }
49
50 class PositionContainer {
51 List<RequestPosition> blue = new ArrayList<RequestPosition>();
52 List<RequestPosition> green = new ArrayList<RequestPosition>();
53 List<RequestPosition> yellow = new ArrayList<RequestPosition>();
54 List<RequestPosition> red = new ArrayList<RequestPosition>();
55 }
56
57
58 boolean isGz(String fileStr) {
59 return fileStr.substring(fileStr.length() - 3).equals(".gz");
60 }
61
62 protected File[] getFiles(int count) {
63 File accessLogDir = new File("/home/app/domain1/logs/access/");
64
65 File logFiles[] = accessLogDir.listFiles( new FilenameFilter() {
66 @Override
67 public boolean accept(File dir, String name) {
68 return name.startsWith("server_access_log");
69 }
70 });
71
72 if (logFiles == null) {
73 File[] empty = {};
74 return empty;
75 }
76
77 int from = logFiles.length - count;
78 int to = logFiles.length-1;
79
80 return Arrays.copyOfRange(logFiles, from, to);
81 }
82
83 protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
84 PositionContainer positions = new PositionContainer();
85
86 try {
87
88 int count;
89
90 if (multiple == false) {
91 count = 1;
92 } else {
93 count = 4;
94 }
95
96 File files[] = getFiles(count);
97
98 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
99
100 Date now = new Date();
101 for (File f : files ) {
102 log.info("Parsing file: " + f.getName() );
103
104 if ( !f.exists() ) {
105 continue;
106 }
107
108
109 InputStream input = new FileInputStream(f);
110
111
112 BufferedReader in = new BufferedReader( new InputStreamReader(input) );
113
114
115 String line;
116 while ( (line=in.readLine()) != null) {
117 if (line.indexOf("LocateStation") == -1 ){
118 continue;
119 }
120
121 if (line.indexOf("latitude=") == -1 ) {
122 continue;
123 }
124
125 if (line.indexOf("longitude=") == -1) {
126 continue;
127 }
128
129 RequestPosition pos = new RequestPosition();
130
131 String toks[] = line.split(" ");
132 pos.ip = toks[0].replaceAll("\"", "");
133
134 pos.time = df.parse( toks[2].replace("\"", "") );
135
136 String argpart = toks[5].split("\\?")[1];
137
138 String args[] = argpart.split("&");
139
140 pos.lat = args[0].split("=")[1];
141 pos.lng = args[1].split("=")[1];
142
143
144 long timediff = now.getTime() - pos.time.getTime();
145 if ( timediff < (3*60*60*1000) ) {
146 positions.red.add(pos); //RED
147 } else if ( timediff < (24*60*60*1000)) {
148 positions.yellow.add(pos); //YELLOW
149 } else if ( timediff < (7*24*60*60*1000)) {
150 positions.green.add(pos); //GREEN
151 } else {
152 positions.blue.add(pos); //BLUE
153 }
154
155 }
156 in.close();
157 input.close();
158
159 }
160 } catch (ParseException pe) {
161 log.log(Level.SEVERE, "parseException", pe);
162 throw new IOException(pe);
163 } catch (IOException e) {
164 log.log(Level.SEVERE, "getKml()", e);
165 throw e;
166 }
167
168 return positions;
169 }
170
171
172 /* old apache code
173 protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
174 PositionContainer positions = new PositionContainer();
175
176 try {
177 String files_single[] = {"/var/log/apache2/app_access.log"};
178 String files_multi[] = {"/var/log/apache2/app_access.log.3.gz", "/var/log/apache2/app_access.log.2.gz", "/var/log/apache2/app_access.log.1", "/var/log/apache2/app_access.log"};
179
180 String files[];
181
182 if (multiple == false) {
183 files = files_single;
184 } else {
185 files = files_multi;
186 }
187
188 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
189
190 Date now = new Date();
191 for (String fileStr : files ) {
192 File f = new File(fileStr);
193 if ( !f.exists() ) {
194 continue;
195 }
196
197 FileInputStream fis = new FileInputStream(fileStr);
198
199 InputStream input;
200 if ( isGz(fileStr)) {
201 input = new GZIPInputStream(fis);
202 } else {
203 input = fis;
204 }
205
206 BufferedReader in = new BufferedReader( new InputStreamReader(input) );
207
208
209 String line;
210 while ( (line=in.readLine()) != null) {
211 if (line.indexOf("LocateStation") == -1 ){
212 continue;
213 }
214
215 if (line.indexOf("latitude=") == -1 ) {
216 continue;
217 }
218
219 if (line.indexOf("longitude=") == -1) {
220 continue;
221 }
222
223 RequestPosition pos = new RequestPosition();
224
225 String toks[] = line.split(" ");
226 pos.ip = toks[0];
227
228 pos.time = df.parse( toks[3].replace("[", "") );
229
230 String argpart = toks[6].split("\\?")[1];
231
232 String args[] = argpart.split("&");
233
234 pos.lat = args[0].split("=")[1];
235 pos.lng = args[1].split("=")[1];
236
237
238 long timediff = now.getTime() - pos.time.getTime();
239 if ( timediff < (3*60*60*1000) ) {
240 positions.red.add(pos); //RED
241 } else if ( timediff < (24*60*60*1000)) {
242 positions.yellow.add(pos); //YELLOW
243 } else if ( timediff < (7*24*60*60*1000)) {
244 positions.green.add(pos); //GREEN
245 } else {
246 positions.blue.add(pos); //BLUE
247 }
248
249 }
250 in.close();
251 input.close();
252 fis.close();
253 }
254 } catch (ParseException pe) {
255 log.log(Level.SEVERE, "parseException", pe);
256 throw new IOException(pe);
257 } catch (IOException e) {
258 log.log(Level.SEVERE, "getKml()", e);
259 throw e;
260 }
261
262 return positions;
263 }*/
264
265 protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) {
266 sb.append( "<Folder>\n");
267 sb.append( " <name>" ).append(color).append("</name>\n");
268 sb.append( " <open>0</open>\n" );
269
270 int count=0;
271 for(RequestPosition current : list) {
272 String id = color + count++;
273 sb.append( " <Placemark id=\"" + id + "\">\n" );
274 sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" );
275 sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" );
276 sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" );
277 sb.append( " </Placemark>\n" );
278 }
279
280 sb.append("</Folder>\n");
281 }
282
283 protected String formatXml(PositionContainer positions) {
284 StringBuilder sb = new StringBuilder(1024*1024);
285
286 sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
287 sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
288 sb.append( "<Document>\n" );
289 sb.append( " <description><![CDATA[");
290 sb.append( " Red:").append(positions.red.size()).append(" (whithin 3 hours)<br/>\n");
291 sb.append( " Yellow:").append(positions.yellow.size()).append(" (within 24 hours)<br/>\n");
292 sb.append( " Green:").append(positions.green.size()).append(" (within one week)<br/>\n");
293 sb.append( " Blue:").append(positions.blue.size()).append(" (older)<br/>\n");
294 sb.append( " ]]></description>");
295
296
297 sb.append( " <Style id=\"red\">\n" );
298 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
299 sb.append( " </Style>\n");
300
301 sb.append( " <Style id=\"yellow\">\n" );
302 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
303 sb.append( " </Style>\n" );
304
305 sb.append( " <Style id=\"green\">\n" );
306 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
307 sb.append( " </Style>\n\n" );
308
309 sb.append( " <Style id=\"blue\">\n" );
310 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" );
311 sb.append( " </Style>\n\n" );
312 /*
313 String overlay =
314 " <ScreenOverlay>" +
315 " <name>Absolute Positioning: Top left</name>" +
316 " <description>Absolute Positioning: Top left</description>" +
317 " <visibility>1</visibility>" +
318 " <Icon>" +
319 " <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" +
320 " </Icon>" +
321 " <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
322 " <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
323 " <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
324 " <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
325 " </ScreenOverlay>" ;
326
327 sb.append(overlay);
328 */
329
330
331 formatPositions(sb, "blue", positions.blue);
332 formatPositions(sb, "green", positions.green);
333 formatPositions(sb, "yellow", positions.yellow);
334 formatPositions(sb, "red", positions.red);
335
336
337 sb.append( "</Document>\n" );
338 sb.append( "</kml>\n" );
339
340 return sb.toString();
341 }
342
343 protected String getRequestsFromFile(boolean multiple) throws IOException {
344 String kmlData = null;
345 String key;
346
347 if (multiple == false) {
348 key = "kmldata";
349 } else {
350 key = "kmldata-multi";
351 }
352
353 kmlData = cache.get(key);
354
355 if (kmlData == null) {
356 kmlData = formatXml( getRequestsFromFileWorker(multiple) );
357 cache.put(key, kmlData);
358 kmlData += "<!-- from source -->";
359 } else {
360 kmlData += "<!-- cached -->";
361 }
362
363 return kmlData;
364 }
365
366 boolean enabled(String param) {
367 if (param == null || param.equals("")) {
368 return false;
369 }
370
371 int p = 0;
372 try {
373 p = Integer.parseInt(param);
374 } catch (Exception e) {}
375
376 return (p != 0);
377 }
378
379 @Override
380 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
381
382 boolean multiple = enabled( req.getParameter("multi") );
383
384 String kmlData = getRequestsFromFile(multiple);
385
386 if ( enabled(req.getParameter("zip")) ) {
387
388 ByteArrayOutputStream baos = new ByteArrayOutputStream();
389
390 ZipOutputStream zip = new ZipOutputStream(baos);
391 zip.putNextEntry( new ZipEntry("trains.kml") );
392 zip.write( kmlData.getBytes() );
393 zip.closeEntry();
394 zip.close();
395
396 byte bytes[] = baos.toByteArray();
397
398 resp.setContentType(KMZ);
399 resp.setContentLength( bytes.length );
400 resp.getOutputStream().write(bytes);
401
402 } else {
403 resp.setContentType(KML);
404 resp.getWriter().print( kmlData );
405 }
406 }
407 }

  ViewVC Help
Powered by ViewVC 1.1.20