/[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 802 by torben, Mon Jun 7 11:46:25 2010 UTC revision 893 by torben, Thu Jun 24 16:29:44 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    
# Line 39  public class RequestPlotter extends Http Line 42  public class RequestPlotter extends Http
42                  public String lat;                  public String lat;
43                  public String lng;                  public String lng;
44          }          }
45            
46            class PositionContainer {
47                    List<RequestPosition> green = new ArrayList<RequestPosition>();
48                    List<RequestPosition> yellow = new ArrayList<RequestPosition>();
49                    List<RequestPosition> red = new ArrayList<RequestPosition>();
50            }
51    
52    
53            boolean isGz(String fileStr) {
54                    return fileStr.substring(fileStr.length() - 3).equals(".gz");
55            }
56    
57          protected List<RequestPosition> getRequestsFromFile() throws IOException{          protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
58                  List<RequestPosition> positions = new ArrayList<RequestPosition>();                  PositionContainer positions = new PositionContainer();
59    
60                  try {                  try {
61                          FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");                          String files_single[] = {"/var/log/apache2/app_access.log"};
62                          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"};
63                            
64                            String files[];
65                            
66                            if (multiple == false) {
67                                    files = files_single;
68                            } else {
69                                    files = files_multi;
70                            }
71    
72                          SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");                          SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
73                    
74                          String line;                          Date now = new Date();
75                          while ( (line=in.readLine()) != null) {                          for (String fileStr : files ) {
76                                  if (line.indexOf("LocateStation") == -1 ){                                  File f = new File(fileStr);
77                                    if ( !f.exists() ) {
78                                          continue;                                          continue;
79                                  }                                  }
80                                    
81                                  if (line.indexOf("latitude") == -1 ) {                                  FileInputStream fis = new FileInputStream(fileStr);
82                                          continue;                                  
83                                    InputStream input;
84                                    if ( isGz(fileStr)) {
85                                            input = new GZIPInputStream(fis);
86                                    } else {
87                                            input = fis;
88                                  }                                  }
89                                  RequestPosition pos = new RequestPosition();                                  
90                                    BufferedReader in = new BufferedReader( new InputStreamReader(input) );
                                 String toks[] = line.split(" ");  
                                 pos.ip = toks[0];  
   
                                 pos.time = df.parse( toks[3].replace("[", "") );  
91    
92                                  String argpart = toks[6].split("\\?")[1];          
93                                    String line;
94                                  String args[] = argpart.split("&");                                  while ( (line=in.readLine()) != null) {
95                                            if (line.indexOf("LocateStation") == -1 ){
96                                  pos.lat = args[0].split("=")[1];                                                  continue;
97                                  pos.lng = args[1].split("=")[1];                                          }
98            
99                                  positions.add(pos);                                          if (line.indexOf("latitude=") == -1 ) {
100                                                    continue;
101                                            }
102                                            
103                                            if (line.indexOf("longitude=") == -1) {
104                                                    continue;
105                                            }
106                                            
107                                            RequestPosition pos = new RequestPosition();
108            
109                                            String toks[] = line.split(" ");
110                                            pos.ip = toks[0];
111            
112                                            pos.time = df.parse( toks[3].replace("[", "") );
113            
114                                            String argpart = toks[6].split("\\?")[1];
115            
116                                            String args[] = argpart.split("&");
117            
118                                            pos.lat = args[0].split("=")[1];
119                                            pos.lng = args[1].split("=")[1];
120                                            
121                                            
122                                            long timediff = now.getTime() - pos.time.getTime();                                    
123                                            if ( timediff < (3*60*60*1000) ) {
124                                                    positions.red.add(pos); //RED
125                                            } else if ( timediff < (24*60*60*1000)) {
126                                                    positions.yellow.add(pos); //YELLOW
127                                            } else {
128                                                    positions.green.add(pos); //GREEN
129                                            }
130            
131                                    }
132                                    in.close();
133                                    input.close();
134                                    fis.close();
135                          }                          }
136                  } catch (ParseException pe) {                  } catch (ParseException pe) {
137                          log.log(Level.SEVERE, "parseException", pe);                          log.log(Level.SEVERE, "parseException", pe);
# Line 86  public class RequestPlotter extends Http Line 143  public class RequestPlotter extends Http
143    
144                  return positions;                  return positions;
145          }          }
146            
147            protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) {
148                    sb.append( "<Folder>\n");
149                    sb.append( " <name>" ).append(color).append("</name>\n");
150                    sb.append( " <open>0</open>\n" );
151                    sb.append( " <description>Count=").append(list.size()).append("</description>\n");
152                    
153                    for(RequestPosition current : list) {                  
154                            sb.append( " <Placemark>\n" );
155                            sb.append( "  <styleUrl>#" + color + "</styleUrl>\n" );
156                            sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );
157                            sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
158                            sb.append( " </Placemark>\n" );                
159                    }              
160                    
161                    sb.append("</Folder>\n");
162            }
163    
164          protected String formatXml(List<RequestPosition> list) {          protected String formatXml(PositionContainer positions) {
165                  StringBuilder sb = new StringBuilder();                  StringBuilder sb = new StringBuilder();
166    
167                  sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );                  sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
# Line 126  public class RequestPlotter extends Http Line 200  public class RequestPlotter extends Http
200          */                */      
201                                    
202    
203                    formatPositions(sb, "green", positions.green);
204                    formatPositions(sb, "yellow", positions.yellow);
205                    formatPositions(sb, "red", positions.red);
206                    
207    
208                    sb.append( "</Document>\n" );
   
   
                 Date now = new Date();  
                 for(RequestPosition current : list) {  
                           
                         String style;  
                         long timediff = now.getTime() - current.time.getTime();  
                           
                         if ( timediff < (3*60*60*1000) ) {  
                                 style = "#red";  
                         } else if ( timediff < (24*60*60*1000)) {  
                                 style = "#yellow";  
                         } else {  
                                 style = "#green";  
                         }  
                           
                         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(  "</Document>\n" );  
209                  sb.append( "</kml>\n" );                  sb.append( "</kml>\n" );
210    
211                  return sb.toString();                  return sb.toString();
212          }          }
213            
214            protected String getRequestsFromFile(boolean multiple) throws IOException {            
215                    String kmlData = null;
216                    String key;
217                    
218                    if (multiple == false)  {
219                            key = "kmldata";
220                    } else {
221                            key = "kmldata-multi";
222                    }
223                                            
224                    kmlData = cache.get(key);
225                            
226                    if (kmlData == null) {
227                            kmlData = formatXml( getRequestsFromFileWorker(multiple) );
228                            cache.put(key, kmlData);
229                            kmlData += "<!-- from source -->";                      
230                    } else {
231                            kmlData += "<!-- cached -->";
232                    }
233                                            
234                    return kmlData;
235            }
236            
237            boolean enabled(String param) {
238                    if (param == null || param.equals("")) {
239                            return false;
240                    }
241                    
242                    int p = 0;
243                    try {
244                            p = Integer.parseInt(param);
245                    } catch (Exception e) {}
246                    
247                    return (p != 0);
248            }
249    
250          @Override          @Override
251          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
252                                    
253                  String data = cache.get("data");                  boolean multiple = enabled( req.getParameter("multi") );
254                                    
255                  if (data == null) {                  String kmlData = getRequestsFromFile(multiple);
                         data = formatXml( getRequestsFromFile() );  
                         cache.put("data", data);  
                         data += "<!-- from source -->";                  
                 } else {  
                         data += "<!-- cached -->";  
                 }  
   
256                                    
257                    if ( enabled(req.getParameter("zip")) ) {
                 if (req.getParameter("zip") != null) {  
258    
259                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
260    
261                          ZipOutputStream zip = new ZipOutputStream(baos);                          ZipOutputStream zip = new ZipOutputStream(baos);
262                          zip.putNextEntry( new ZipEntry("trains.kml") );                          zip.putNextEntry( new ZipEntry("trains.kml") );
263                          zip.write( data.getBytes() );                          zip.write( kmlData.getBytes() );
264                          zip.closeEntry();                          zip.closeEntry();
265                          zip.close();                          zip.close();
266    
# Line 190  public class RequestPlotter extends Http Line 272  public class RequestPlotter extends Http
272    
273                  } else {                  } else {
274                          resp.setContentType(KML);                          resp.setContentType(KML);
275                          resp.getWriter().print( data );                          resp.getWriter().print( kmlData );
276                  }                  }
277          }          }
278  }  }

Legend:
Removed from v.802  
changed lines
  Added in v.893

  ViewVC Help
Powered by ViewVC 1.1.20