--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 18:35:40 806 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/24 16:25:54 892 @@ -2,8 +2,10 @@ 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; @@ -13,6 +15,7 @@ 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; @@ -39,42 +42,96 @@ public String lat; public String lng; } + + class PositionContainer { + List green = new ArrayList(); + List yellow = new ArrayList(); + List red = new ArrayList(); + } + boolean isGz(String fileStr) { + return fileStr.substring(fileStr.length() - 3).equals(".gz"); + } - protected List getRequestsFromFile() throws IOException{ - List positions = new ArrayList(); + protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ + PositionContainer positions = new PositionContainer(); 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.3.gz", "/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 ){ + + Date now = new Date(); + 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]; - - String args[] = argpart.split("&"); - - pos.lat = args[0].split("=")[1]; - pos.lng = args[1].split("=")[1]; + + BufferedReader in = new BufferedReader( new InputStreamReader(input) ); - positions.add(pos); + + String line; + while ( (line=in.readLine()) != null) { + if (line.indexOf("LocateStation") == -1 ){ + continue; + } + + if (line.indexOf("latitude=") == -1 ) { + continue; + } + + if (line.indexOf("longitude=") == -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]; + + + long timediff = now.getTime() - pos.time.getTime(); + if ( timediff < (3*60*60*1000) ) { + positions.red.add(pos); //RED + } else if ( timediff < (24*60*60*1000)) { + positions.yellow.add(pos); //YELLOW + } else { + positions.green.add(pos); //GREEN + } + + } + in.close(); + input.close(); + fis.close(); } } catch (ParseException pe) { log.log(Level.SEVERE, "parseException", pe); @@ -86,8 +143,25 @@ return positions; } + + protected void formatPositions(StringBuilder sb, String color, List list) { + sb.append( "\n"); + sb.append( " " ).append(color).append("\n"); + sb.append( " 0\n" ); + sb.append( " Count=").append(list.size()).append("\n"); + + for(RequestPosition current : list) { + sb.append( " \n" ); + sb.append( " #" + color + "\n" ); + sb.append( " IP=" + current.ip + " Time=" + current.time + "\n" ); + sb.append( " " + current.lng + "," + current.lat + ",0\n" ); + sb.append( " \n" ); + } + + sb.append("\n"); + } - protected String formatXml(List list) { + protected String formatXml(PositionContainer positions) { StringBuilder sb = new StringBuilder(); sb.append( "\n" ); @@ -126,65 +200,61 @@ */ - 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) { - - String style; - long timediff = now.getTime() - current.time.getTime(); - - if ( timediff < (3*60*60*1000) ) { - style = STYLE_RED; - } else if ( timediff < (24*60*60*1000)) { - style = STYLE_YELLOW; - } else { - 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( " IP=" + current.ip + " Time=" + current.time + "\n" ); - sb.append( " " + current.lng + "," + current.lat + ",0\n" ); - sb.append( " \n" ); - } - sb.append( "\n" ); + formatPositions(sb, "green", positions.green); + formatPositions(sb, "yello", positions.yellow); + formatPositions(sb, "red", positions.red); + + sb.append( "\n" ); sb.append( "\n" ); return sb.toString(); } - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - final String KEY = "kmldata"; - - String kmlData = cache.get(KEY); + + 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( getRequestsFromFile() ); - cache.put(KEY, kmlData); + kmlData = formatXml( getRequestsFromFileWorker(multiple) ); + cache.put(key, kmlData); kmlData += ""; } else { kmlData += ""; } - + + return kmlData; + } + + boolean enabled(String param) { + if (param == null || param.equals("")) { + return false; + } + + int p = 0; + try { + p = Integer.parseInt(param); + } catch (Exception e) {} + return (p != 0); + } - if (req.getParameter("zip") != null) { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + boolean multiple = enabled( req.getParameter("multi") ); + + String kmlData = getRequestsFromFile(multiple); + + if ( enabled(req.getParameter("zip")) ) { ByteArrayOutputStream baos = new ByteArrayOutputStream();