/[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 941 by torben, Tue Jun 29 16:11:43 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("[", "") );  
   
                                 String argpart = toks[6].split("\\?")[1];  
   
                                 String args[] = argpart.split("&");  
   
                                 pos.lat = args[0].split("=")[1];  
                                 pos.lng = args[1].split("=")[1];  
91    
92                                  positions.add(pos);          
93                                    String line;
94                                    while ( (line=in.readLine()) != null) {
95                                            if (line.indexOf("LocateStation") == -1 ){
96                                                    continue;
97                                            }
98            
99                                            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                    
152                    for(RequestPosition current : list) {                  
153                            sb.append( " <Placemark>\n" );
154                            sb.append( "  <styleUrl>#").append(color).append("</styleUrl>\n" );
155                            sb.append( "  <description>IP=").append(current.ip).append("  Time=").append(current.time).append("</description>\n" );
156                            sb.append( "  <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" );
157                            sb.append( " </Placemark>\n" );                
158                    }              
159                    
160                    sb.append("</Folder>\n");
161            }
162    
163          protected String formatXml(List<RequestPosition> list) {          protected String formatXml(PositionContainer positions) {
164                  StringBuilder sb = new StringBuilder();                  StringBuilder sb = new StringBuilder(1024*1024);
165    
166                  sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );                  sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
167                  sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );                  sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
168                  sb.append( "<Document>\n" );                              sb.append( "<Document>\n" );            
169                    sb.append( " <description><![CDATA[");
170                    sb.append( "  Green:").append(positions.green.size()).append("<br/>\n");
171                    sb.append( "  Yellow:").append(positions.yellow.size()).append("<br/>\n");
172                    sb.append( "  Red:").append(positions.red.size()).append("\n");
173                    sb.append( " ]]></description>");
174                                    
175                                    
176                  sb.append( " <Style id=\"red\">\n" );                  sb.append( " <Style id=\"red\">\n" );
# Line 126  public class RequestPlotter extends Http Line 204  public class RequestPlotter extends Http
204          */                */      
205                                    
206    
207                  final String STYLE_GREEN = "green";                  formatPositions(sb, "green", positions.green);
208                  final String STYLE_YELLOW = "yellow";                  formatPositions(sb, "yellow", positions.yellow);
209                  final String STYLE_RED = "red";                  formatPositions(sb, "red", positions.red);
210                    
211                  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" );  
212                  sb.append( "</Document>\n" );                  sb.append( "</Document>\n" );
213                  sb.append( "</kml>\n" );                  sb.append( "</kml>\n" );
214    
215                  return sb.toString();                  return sb.toString();
216          }          }
217            
218          @Override          protected String getRequestsFromFile(boolean multiple) throws IOException {            
219          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {                  String kmlData = null;
220                  final String KEY = "kmldata";                  String key;
                   
                 String kmlData = cache.get(KEY);  
221                                    
222                    if (multiple == false)  {
223                            key = "kmldata";
224                    } else {
225                            key = "kmldata-multi";
226                    }
227                                            
228                    kmlData = cache.get(key);
229                            
230                  if (kmlData == null) {                  if (kmlData == null) {
231                          kmlData = formatXml( getRequestsFromFile() );                          kmlData = formatXml( getRequestsFromFileWorker(multiple) );
232                          cache.put(KEY, kmlData);                          cache.put(key, kmlData);
233                          kmlData += "<!-- from source -->";                                                kmlData += "<!-- from source -->";                      
234                  } else {                  } else {
235                          kmlData += "<!-- cached -->";                          kmlData += "<!-- cached -->";
236                  }                  }
237                                            
238                    return kmlData;
239            }
240            
241            boolean enabled(String param) {
242                    if (param == null || param.equals("")) {
243                            return false;
244                    }
245                    
246                    int p = 0;
247                    try {
248                            p = Integer.parseInt(param);
249                    } catch (Exception e) {}
250                                    
251                    return (p != 0);
252            }
253    
254                  if (req.getParameter("zip") != null) {          @Override
255            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
256                    
257                    boolean multiple = enabled( req.getParameter("multi") );
258                    
259                    String kmlData = getRequestsFromFile(multiple);
260                    
261                    if ( enabled(req.getParameter("zip")) ) {
262    
263                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
264    

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

  ViewVC Help
Powered by ViewVC 1.1.20