--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 11:29:55 801 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/09 14:17:17 809 @@ -2,16 +2,20 @@ import java.io.BufferedReader; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; 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.GZIPInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -20,6 +24,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; @@ -27,6 +33,8 @@ 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; @@ -36,40 +44,73 @@ } + boolean isGz(String fileStr) { + return fileStr.substring(fileStr.length() - 3).equals(".gz"); + } - protected List getRequestsFromFile() throws IOException{ + protected List getRequestsFromFileWorker(boolean multiple) throws IOException{ List positions = new ArrayList(); try { - FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); - BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); + String files_single[] = {"/var/log/apache2/app_access.log"}; + String files_multi[] = {"/var/log/apache2/app_access.log.2.gz", "/var/log/apache2/app_access.log.1", "/var/log/apache2/app_access.log"}; + + String files[]; + + if (multiple == false) { + files = files_single; + } else { + files = files_multi; + } SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); - - String line; - while ( (line=in.readLine()) != null) { - if (line.indexOf("LocateStation") == -1 ){ + + for (String fileStr : files ) { + File f = new File(fileStr); + if ( !f.exists() ) { continue; } - - if (line.indexOf("latitude") == -1 ) { - continue; + + FileInputStream fis = new FileInputStream(fileStr); + + InputStream input; + if ( isGz(fileStr)) { + input = new GZIPInputStream(fis); + } else { + input = fis; } - RequestPosition pos = new RequestPosition(); - - String toks[] = line.split(" "); - pos.ip = toks[0]; - - pos.time = df.parse( toks[3].replace("[", "") ); - - String argpart = toks[6].split("\\?")[1]; + + BufferedReader in = new BufferedReader( new InputStreamReader(input) ); - String args[] = argpart.split("&"); - - pos.lat = args[0].split("=")[1]; - pos.lng = args[1].split("=")[1]; - - positions.add(pos); + + String line; + while ( (line=in.readLine()) != null) { + if (line.indexOf("LocateStation") == -1 ){ + continue; + } + + if (line.indexOf("latitude") == -1 ) { + continue; + } + RequestPosition pos = new RequestPosition(); + + String toks[] = line.split(" "); + pos.ip = toks[0]; + + 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); + } + in.close(); + input.close(); + fis.close(); } } catch (ParseException pe) { log.log(Level.SEVERE, "parseException", pe); @@ -121,10 +162,11 @@ */ + final String STYLE_GREEN = "green"; + final String STYLE_YELLOW = "yellow"; + final String STYLE_RED = "red"; - - - + String oldstyle = null; Date now = new Date(); for(RequestPosition current : list) { @@ -132,38 +174,73 @@ long timediff = now.getTime() - current.time.getTime(); if ( timediff < (3*60*60*1000) ) { - style = "#red"; + style = STYLE_RED; } else if ( timediff < (24*60*60*1000)) { - style = "#yellow"; + style = STYLE_YELLOW; } else { - style = "#green"; + style = STYLE_GREEN; + } + + if ( !style.equals(oldstyle) ) { + if (oldstyle != null) + sb.append( "\n"); + sb.append( "\n"); + sb.append( " " ).append(style).append("\n"); + sb.append( " 0\n" ); + + oldstyle = style; } sb.append( " \n" ); - sb.append( " " + style + "\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" ); } - - sb.append( "\n" ); + sb.append( "\n" ); + sb.append( "\n" ); sb.append( "\n" ); return sb.toString(); } + + protected String getRequestsFromFile(boolean multiple) throws IOException { + String kmlData = null; + String key; + + if (multiple == false) { + key = "kmldata"; + } else { + key = "kmldata-multi"; + } + + kmlData = cache.get(key); + + if (kmlData == null) { + kmlData = formatXml( getRequestsFromFileWorker(multiple) ); + cache.put(key, kmlData); + kmlData += ""; + } else { + kmlData += ""; + } + + return kmlData; + } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - String data = formatXml( getRequestsFromFile() ); - + + boolean multiple = req.getParameter("multi") != null; + + String kmlData = getRequestsFromFile(multiple); + if (req.getParameter("zip") != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(baos); zip.putNextEntry( new ZipEntry("trains.kml") ); - zip.write( data.getBytes() ); + zip.write( kmlData.getBytes() ); zip.closeEntry(); zip.close(); @@ -175,7 +252,7 @@ } else { resp.setContentType(KML); - resp.getWriter().print( data ); + resp.getWriter().print( kmlData ); } } }