--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/24 13:01:35 891 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/24 16:25:54 892 @@ -42,14 +42,20 @@ 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"}; @@ -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() ) { @@ -110,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(); @@ -127,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" ); @@ -167,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, "yello", positions.yellow); + formatPositions(sb, "red", positions.red); + + sb.append( "\n" ); sb.append( "\n" );