/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 806 by torben, Mon Jun 7 18:35:40 2010 UTC revision 1000 by torben, Sat Jul 17 06:29:04 2010 UTC
# Line 2  package dk.thoerup.traininfoservice; Line 2  package dk.thoerup.traininfoservice;
2    
3  import java.io.BufferedReader;  import java.io.BufferedReader;
4  import java.io.ByteArrayOutputStream;  import java.io.ByteArrayOutputStream;
5    import java.io.File;
6  import java.io.FileInputStream;  import java.io.FileInputStream;
7  import java.io.IOException;  import java.io.IOException;
8    import java.io.InputStream;
9  import java.io.InputStreamReader;  import java.io.InputStreamReader;
10  import java.text.ParseException;  import java.text.ParseException;
11  import java.text.SimpleDateFormat;  import java.text.SimpleDateFormat;
# Line 13  import java.util.List; Line 15  import java.util.List;
15  import java.util.Map;  import java.util.Map;
16  import java.util.logging.Level;  import java.util.logging.Level;
17  import java.util.logging.Logger;  import java.util.logging.Logger;
18    import java.util.zip.GZIPInputStream;
19  import java.util.zip.ZipEntry;  import java.util.zip.ZipEntry;
20  import java.util.zip.ZipOutputStream;  import java.util.zip.ZipOutputStream;
21    
22  import javax.servlet.ServletException;  import javax.servlet.ServletException;
23    import javax.servlet.annotation.WebListener;
24    import javax.servlet.annotation.WebServlet;
25  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;  import javax.servlet.http.HttpServletResponse;
28    
29  import dk.thoerup.traininfoservice.banedk.TimeoutMap;  import dk.thoerup.traininfoservice.banedk.TimeoutMap;
30    
31    @WebServlet(urlPatterns={"/RequestPlotter"})
32  public class RequestPlotter extends HttpServlet {  public class RequestPlotter extends HttpServlet {
33          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
34    
# Line 31  public class RequestPlotter extends Http Line 37  public class RequestPlotter extends Http
37          static final String KML = "application/vnd.google-earth.kml";          static final String KML = "application/vnd.google-earth.kml";
38          static final String KMZ = "application/vnd.google-earth.kmz";          static final String KMZ = "application/vnd.google-earth.kmz";
39                    
40          Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000);          Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000);
41    
42          class RequestPosition {          class RequestPosition {
43                  public String ip;                  public String ip;
# Line 39  public class RequestPlotter extends Http Line 45  public class RequestPlotter extends Http
45                  public String lat;                  public String lat;
46                  public String lng;                  public String lng;
47          }          }
48            
49            class PositionContainer {
50                    List<RequestPosition> blue = new ArrayList<RequestPosition>();
51                    List<RequestPosition> green = new ArrayList<RequestPosition>();
52                    List<RequestPosition> yellow = new ArrayList<RequestPosition>();
53                    List<RequestPosition> red = new ArrayList<RequestPosition>();
54            }
55    
56    
57            boolean isGz(String fileStr) {
58                    return fileStr.substring(fileStr.length() - 3).equals(".gz");
59            }
60    
61          protected List<RequestPosition> getRequestsFromFile() throws IOException{          protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
62                  List<RequestPosition> positions = new ArrayList<RequestPosition>();                  PositionContainer positions = new PositionContainer();
63    
64                  try {                  try {
65                          FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");                          String files_single[] = {"/var/log/apache2/app_access.log"};
66                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );                          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"};
67                            
68                            String files[];
69                            
70                            if (multiple == false) {
71                                    files = files_single;
72                            } else {
73                                    files = files_multi;
74                            }
75    
76                          SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");                          SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
77                    
78                          String line;                          Date now = new Date();
79                          while ( (line=in.readLine()) != null) {                          for (String fileStr : files ) {
80                                  if (line.indexOf("LocateStation") == -1 ){                                  File f = new File(fileStr);
81                                    if ( !f.exists() ) {
82                                          continue;                                          continue;
83                                  }                                  }
84                                    
85                                  if (line.indexOf("latitude") == -1 ) {                                  FileInputStream fis = new FileInputStream(fileStr);
86                                          continue;                                  
87                                    InputStream input;
88                                    if ( isGz(fileStr)) {
89                                            input = new GZIPInputStream(fis);
90                                    } else {
91                                            input = fis;
92                                  }                                  }
93                                  RequestPosition pos = new RequestPosition();                                  
94                                    BufferedReader in = new BufferedReader( new InputStreamReader(input) );
                                 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];  
95    
96                                  positions.add(pos);          
97                                    String line;
98                                    while ( (line=in.readLine()) != null) {
99                                            if (line.indexOf("LocateStation") == -1 ){
100                                                    continue;
101                                            }
102            
103                                            if (line.indexOf("latitude=") == -1 ) {
104                                                    continue;
105                                            }
106                                            
107                                            if (line.indexOf("longitude=") == -1) {
108                                                    continue;
109                                            }
110                                            
111                                            RequestPosition pos = new RequestPosition();
112            
113                                            String toks[] = line.split(" ");
114                                            pos.ip = toks[0];
115            
116                                            pos.time = df.parse( toks[3].replace("[", "") );
117            
118                                            String argpart = toks[6].split("\\?")[1];
119            
120                                            String args[] = argpart.split("&");
121            
122                                            pos.lat = args[0].split("=")[1];
123                                            pos.lng = args[1].split("=")[1];
124                                            
125                                            
126                                            long timediff = now.getTime() - pos.time.getTime();                                    
127                                            if ( timediff < (3*60*60*1000) ) {
128                                                    positions.red.add(pos); //RED
129                                            } else if ( timediff < (24*60*60*1000)) {
130                                                    positions.yellow.add(pos); //YELLOW
131                                            } else if ( timediff < (7*24*60*60*1000)) {
132                                                    positions.green.add(pos); //GREEN
133                                            } else {
134                                                    positions.blue.add(pos); //BLUE
135                                            }
136            
137                                    }
138                                    in.close();
139                                    input.close();
140                                    fis.close();
141                          }                          }
142                  } catch (ParseException pe) {                  } catch (ParseException pe) {
143                          log.log(Level.SEVERE, "parseException", pe);                          log.log(Level.SEVERE, "parseException", pe);
# Line 86  public class RequestPlotter extends Http Line 149  public class RequestPlotter extends Http
149    
150                  return positions;                  return positions;
151          }          }
152            
153            protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) {
154                    sb.append( "<Folder>\n");
155                    sb.append( " <name>" ).append(color).append("</name>\n");
156                    sb.append( " <open>0</open>\n" );
157                    
158                    int count=0;
159                    for(RequestPosition current : list) {
160                            String id = color + count++;
161                            sb.append( " <Placemark id=\"" + id + "\">\n" );
162                            sb.append( "  <styleUrl>#").append(color).append("</styleUrl>\n" );
163                            sb.append( "  <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" );
164                            sb.append( "  <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" );
165                            sb.append( " </Placemark>\n" );                
166                    }              
167                    
168                    sb.append("</Folder>\n");
169            }
170    
171          protected String formatXml(List<RequestPosition> list) {          protected String formatXml(PositionContainer positions) {
172                  StringBuilder sb = new StringBuilder();                  StringBuilder sb = new StringBuilder(1024*1024);
173    
174                  sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );                  sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
175                  sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );                  sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
176                  sb.append( "<Document>\n" );                              sb.append( "<Document>\n" );            
177                    sb.append( " <description><![CDATA[");
178                    sb.append( "  Red:").append(positions.red.size()).append(" (whithin 3 hours)<br/>\n");
179                    sb.append( "  Yellow:").append(positions.yellow.size()).append(" (within 24 hours)<br/>\n");
180                    sb.append( "  Green:").append(positions.green.size()).append(" (within one week)<br/>\n");
181                    sb.append( "  Blue:").append(positions.blue.size()).append(" (older)<br/>\n");          
182                    sb.append( " ]]></description>");
183                                    
184                                    
185                  sb.append( " <Style id=\"red\">\n" );                  sb.append( " <Style id=\"red\">\n" );
# Line 107  public class RequestPlotter extends Http Line 194  public class RequestPlotter extends Http
194                  sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );                  sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
195                  sb.append( " </Style>\n\n" );                  sb.append( " </Style>\n\n" );
196                                    
197                    sb.append( " <Style id=\"blue\">\n" );
198                    sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" );
199                    sb.append( " </Style>\n\n" );          
200  /*  /*
201                  String overlay =                  String overlay =
202                " <ScreenOverlay>" +                " <ScreenOverlay>" +
# Line 126  public class RequestPlotter extends Http Line 216  public class RequestPlotter extends Http
216          */                */      
217                                    
218    
219                  final String STYLE_GREEN = "green";                  formatPositions(sb, "blue", positions.blue);
220                  final String STYLE_YELLOW = "yellow";                  formatPositions(sb, "green", positions.green);
221                  final String STYLE_RED = "red";                  formatPositions(sb, "yellow", positions.yellow);
222                    formatPositions(sb, "red", positions.red);
223                    
224    
                 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( "</Folder>\n");  
                                 sb.append( "<Folder>\n");  
                                 sb.append( " <name>" ).append(style).append("</name>\n");  
                                 sb.append( " <open>0</open>\n" );  
                                   
                                 oldstyle = style;  
                         }  
                           
                         sb.append( " <Placemark>\n" );  
                         sb.append( "  <styleUrl>#" + style + "</styleUrl>\n" );  
                         sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );  
                         sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );  
                         sb.append( " </Placemark>\n" );                  
                 }  
                 sb.append( "</Folder>\n" );  
225                  sb.append( "</Document>\n" );                  sb.append( "</Document>\n" );
226                  sb.append( "</kml>\n" );                  sb.append( "</kml>\n" );
227    
228                  return sb.toString();                  return sb.toString();
229          }          }
230            
231          @Override          protected String getRequestsFromFile(boolean multiple) throws IOException {            
232          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {                  String kmlData = null;
233                  final String KEY = "kmldata";                  String key;
                   
                 String kmlData = cache.get(KEY);  
234                                    
235                    if (multiple == false)  {
236                            key = "kmldata";
237                    } else {
238                            key = "kmldata-multi";
239                    }
240                                            
241                    kmlData = cache.get(key);
242                            
243                  if (kmlData == null) {                  if (kmlData == null) {
244                          kmlData = formatXml( getRequestsFromFile() );                          kmlData = formatXml( getRequestsFromFileWorker(multiple) );
245                          cache.put(KEY, kmlData);                          cache.put(key, kmlData);
246                          kmlData += "<!-- from source -->";                                                kmlData += "<!-- from source -->";                      
247                  } else {                  } else {
248                          kmlData += "<!-- cached -->";                          kmlData += "<!-- cached -->";
249                  }                  }
250                                            
251                    return kmlData;
252            }
253            
254            boolean enabled(String param) {
255                    if (param == null || param.equals("")) {
256                            return false;
257                    }
258                    
259                    int p = 0;
260                    try {
261                            p = Integer.parseInt(param);
262                    } catch (Exception e) {}
263                                    
264                    return (p != 0);
265            }
266    
267                  if (req.getParameter("zip") != null) {          @Override
268            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
269                    
270                    boolean multiple = enabled( req.getParameter("multi") );
271                    
272                    String kmlData = getRequestsFromFile(multiple);
273                    
274                    if ( enabled(req.getParameter("zip")) ) {
275    
276                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
277    

Legend:
Removed from v.806  
changed lines
  Added in v.1000

  ViewVC Help
Powered by ViewVC 1.1.20