--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 08:55:34 799 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 12:01:42 803 @@ -5,8 +5,12 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.ZipEntry; @@ -17,6 +21,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import dk.thoerup.traininfoservice.banedk.TimeoutMap; + public class RequestPlotter extends HttpServlet { private static final long serialVersionUID = 1L; @@ -24,10 +30,12 @@ static final String KML = "application/vnd.google-earth.kml"; static final String KMZ = "application/vnd.google-earth.kmz"; + + Map cache = new TimeoutMap(2*60*1000); class RequestPosition { public String ip; - public String time; + public Date time; public String lat; public String lng; } @@ -41,6 +49,8 @@ FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); + SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); + String line; while ( (line=in.readLine()) != null) { if (line.indexOf("LocateStation") == -1 ){ @@ -54,16 +64,21 @@ String toks[] = line.split(" "); pos.ip = toks[0]; - pos.time = toks[3].replace("[", ""); + + pos.time = df.parse( toks[3].replace("[", "") ); + String argpart = toks[6].split("\\?")[1]; String args[] = argpart.split("&"); pos.lat = args[0].split("=")[1]; pos.lng = args[1].split("=")[1]; - + positions.add(pos); } + } catch (ParseException pe) { + log.log(Level.SEVERE, "parseException", pe); + throw new IOException(pe); } catch (IOException e) { log.log(Level.SEVERE, "getKml()", e); throw e; @@ -78,9 +93,59 @@ sb.append( "\n" ); sb.append( "\n" ); sb.append( "\n" ); + + + sb.append( " \n"); + + sb.append( " \n" ); + + sb.append( " \n\n" ); + +/* + String overlay = + " " + + " Absolute Positioning: Top left" + + " Absolute Positioning: Top left" + + " 1" + + " " + + " http://code.google.com/apis/kml/documentation/top_left.jpg" + + " " + + " " + + " " + + " " + + " " + + " " ; + + sb.append(overlay); + */ + + + + + + Date now = new Date(); for(RequestPosition current : list) { + + String style; + long timediff = now.getTime() - current.time.getTime(); + + if ( timediff < (3*60*60*1000) ) { + style = "#red"; + } else if ( timediff < (24*60*60*1000)) { + style = "#yellow"; + } else { + style = "#green"; + } + sb.append( " \n" ); + sb.append( " " + style + "\n" ); sb.append( " IP=" + current.ip + " Time=" + current.time + "\n" ); sb.append( " " + current.lng + "," + current.lat + ",0\n" ); sb.append( " \n" ); @@ -94,8 +159,19 @@ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + final String KEY = "kmldata"; + + String kmlData = cache.get(KEY); + + if (kmlData == null) { + kmlData = formatXml( getRequestsFromFile() ); + cache.put(KEY, kmlData); + kmlData += ""; + } else { + kmlData += ""; + } - String data = formatXml( getRequestsFromFile() ); + if (req.getParameter("zip") != null) { @@ -103,7 +179,7 @@ ZipOutputStream zip = new ZipOutputStream(baos); zip.putNextEntry( new ZipEntry("trains.kml") ); - zip.write( data.getBytes() ); + zip.write( kmlData.getBytes() ); zip.closeEntry(); zip.close(); @@ -115,7 +191,7 @@ } else { resp.setContentType(KML); - resp.getWriter().print( data ); + resp.getWriter().print( kmlData ); } } }