--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/09 14:17:17 809 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/24 18:33:34 897 @@ -42,18 +42,24 @@ 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 getRequestsFromFileWorker(boolean multiple) throws IOException{ - List positions = new ArrayList(); + protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ + PositionContainer positions = new PositionContainer(); try { 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_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[]; @@ -64,7 +70,8 @@ } SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); - + + Date now = new Date(); for (String fileStr : files ) { File f = new File(fileStr); if ( !f.exists() ) { @@ -89,9 +96,14 @@ continue; } - if (line.indexOf("latitude") == -1 ) { + if (line.indexOf("latitude=") == -1 ) { continue; } + + if (line.indexOf("longitude=") == -1) { + continue; + } + RequestPosition pos = new RequestPosition(); String toks[] = line.split(" "); @@ -105,8 +117,17 @@ 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 + } - positions.add(pos); } in.close(); input.close(); @@ -122,9 +143,26 @@ 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( " #").append(color).append("\n" ); + sb.append( " IP=").append(current.ip).append(" Time=").append(current.time).append("\n" ); + sb.append( " ").append(current.lng).append(",").append(current.lat).append(",0\n" ); + sb.append( " \n" ); + } + + sb.append("\n"); + } - protected String formatXml(List list) { - StringBuilder sb = new StringBuilder(); + protected String formatXml(PositionContainer positions) { + StringBuilder sb = new StringBuilder(1024*1024); sb.append( "\n" ); sb.append( "\n" ); @@ -162,42 +200,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) { - - 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, "yellow", positions.yellow); + formatPositions(sb, "red", positions.red); + + sb.append( "\n" ); sb.append( "\n" ); @@ -226,15 +233,28 @@ 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); + } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - boolean multiple = req.getParameter("multi") != null; + boolean multiple = enabled( req.getParameter("multi") ); String kmlData = getRequestsFromFile(multiple); - if (req.getParameter("zip") != null) { + if ( enabled(req.getParameter("zip")) ) { ByteArrayOutputStream baos = new ByteArrayOutputStream();