--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2011/04/19 15:48:51 1309 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2011/04/19 16:19:45 1310 @@ -4,12 +4,14 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FilenameFilter; 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.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -20,7 +22,6 @@ import java.util.zip.ZipOutputStream; import javax.servlet.ServletException; -import javax.servlet.annotation.WebListener; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -57,7 +58,118 @@ boolean isGz(String fileStr) { return fileStr.substring(fileStr.length() - 3).equals(".gz"); } + + protected File[] getFiles(int count) { + File accessLogDir = new File("/home/app/domain1/logs/access/"); + + File logFiles[] = accessLogDir.listFiles( new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.startsWith("server_access_log"); + } + }); + + if (logFiles == null) { + File[] empty = {}; + return empty; + } + + int from = logFiles.length - count; + int to = logFiles.length-1; + + return Arrays.copyOfRange(logFiles, from, to); + } + + protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ + PositionContainer positions = new PositionContainer(); + + try { + + int count; + + if (multiple == false) { + count = 1; + } else { + count = 4; + } + + File files[] = getFiles(count); + + SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); + + Date now = new Date(); + for (File f : files ) { + log.info("Parsing file: " + f.getName() ); + + if ( !f.exists() ) { + continue; + } + + + InputStream input = new FileInputStream(f); + + BufferedReader in = new BufferedReader( new InputStreamReader(input) ); + + + 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].replaceAll("\"", ""); + + pos.time = df.parse( toks[2].replace("\"", "") ); + + String argpart = toks[5].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 if ( timediff < (7*24*60*60*1000)) { + positions.green.add(pos); //GREEN + } else { + positions.blue.add(pos); //BLUE + } + + } + in.close(); + input.close(); + + } + } catch (ParseException pe) { + log.log(Level.SEVERE, "parseException", pe); + throw new IOException(pe); + } catch (IOException e) { + log.log(Level.SEVERE, "getKml()", e); + throw e; + } + + return positions; + } + + + /* old apache code protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ PositionContainer positions = new PositionContainer(); @@ -148,7 +260,7 @@ } return positions; - } + }*/ protected void formatPositions(StringBuilder sb, String color, List list) { sb.append( "\n");