/[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 803 by torben, Mon Jun 7 12:01:42 2010 UTC revision 818 by torben, Thu Jun 10 08:09:55 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 41  public class RequestPlotter extends Http Line 44  public class RequestPlotter extends Http
44          }          }
45    
46    
47            boolean isGz(String fileStr) {
48                    return fileStr.substring(fileStr.length() - 3).equals(".gz");
49            }
50    
51          protected List<RequestPosition> getRequestsFromFile() throws IOException{          protected List<RequestPosition> getRequestsFromFileWorker(boolean multiple) throws IOException{
52                  List<RequestPosition> positions = new ArrayList<RequestPosition>();                  List<RequestPosition> positions = new ArrayList<RequestPosition>();
53    
54                  try {                  try {
55                          FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");                          String files_single[] = {"/var/log/apache2/app_access.log"};
56                          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"};
57                            
58                            String files[];
59                            
60                            if (multiple == false) {
61                                    files = files_single;
62                            } else {
63                                    files = files_multi;
64                            }
65    
66                          SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");                          SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
67                            
68                          String line;                          for (String fileStr : files ) {
69                          while ( (line=in.readLine()) != null) {                                  File f = new File(fileStr);
70                                  if (line.indexOf("LocateStation") == -1 ){                                  if ( !f.exists() ) {
71                                          continue;                                          continue;
72                                  }                                  }
73                                    
74                                  if (line.indexOf("latitude") == -1 ) {                                  FileInputStream fis = new FileInputStream(fileStr);
75                                          continue;                                  
76                                    InputStream input;
77                                    if ( isGz(fileStr)) {
78                                            input = new GZIPInputStream(fis);
79                                    } else {
80                                            input = fis;
81                                  }                                  }
82                                  RequestPosition pos = new RequestPosition();                                  
83                                    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];  
84    
85                                  String args[] = argpart.split("&");          
86                                    String line;
87                                  pos.lat = args[0].split("=")[1];                                  while ( (line=in.readLine()) != null) {
88                                  pos.lng = args[1].split("=")[1];                                          if (line.indexOf("LocateStation") == -1 ){
89                                                    continue;
90                                  positions.add(pos);                                          }
91            
92                                            if (line.indexOf("latitude=") == -1 ) {
93                                                    continue;
94                                            }
95                                            
96                                            if (line.indexOf("longitude=") == -1) {
97                                                    continue;
98                                            }
99                                            
100                                            RequestPosition pos = new RequestPosition();
101            
102                                            String toks[] = line.split(" ");
103                                            pos.ip = toks[0];
104            
105                                            pos.time = df.parse( toks[3].replace("[", "") );
106            
107                                            String argpart = toks[6].split("\\?")[1];
108            
109                                            String args[] = argpart.split("&");
110            
111                                            pos.lat = args[0].split("=")[1];
112                                            pos.lng = args[1].split("=")[1];
113            
114                                            positions.add(pos);
115                                    }
116                                    in.close();
117                                    input.close();
118                                    fis.close();
119                          }                          }
120                  } catch (ParseException pe) {                  } catch (ParseException pe) {
121                          log.log(Level.SEVERE, "parseException", pe);                          log.log(Level.SEVERE, "parseException", pe);
# Line 126  public class RequestPlotter extends Http Line 167  public class RequestPlotter extends Http
167          */                */      
168                                    
169    
170                    final String STYLE_GREEN = "green";
171                    final String STYLE_YELLOW = "yellow";
172                    final String STYLE_RED = "red";
173    
174                    String oldstyle = null;
   
   
175                  Date now = new Date();                  Date now = new Date();
176                  for(RequestPosition current : list) {                  for(RequestPosition current : list) {
177                                                    
# Line 137  public class RequestPlotter extends Http Line 179  public class RequestPlotter extends Http
179                          long timediff = now.getTime() - current.time.getTime();                          long timediff = now.getTime() - current.time.getTime();
180                                                    
181                          if ( timediff < (3*60*60*1000) ) {                          if ( timediff < (3*60*60*1000) ) {
182                                  style = "#red";                                  style = STYLE_RED;
183                          } else if ( timediff < (24*60*60*1000)) {                          } else if ( timediff < (24*60*60*1000)) {
184                                  style = "#yellow";                                  style = STYLE_YELLOW;
185                          } else {                          } else {
186                                  style = "#green";                                  style = STYLE_GREEN;
187                            }
188                            
189                            if (  !style.equals(oldstyle) ) {
190                                    if (oldstyle != null)                          
191                                            sb.append( "</Folder>\n");
192                                    sb.append( "<Folder>\n");
193                                    sb.append( " <name>" ).append(style).append("</name>\n");
194                                    sb.append( " <open>0</open>\n" );
195                                    
196                                    oldstyle = style;
197                          }                          }
198                                                    
199                          sb.append( " <Placemark>\n" );                          sb.append( " <Placemark>\n" );
200                          sb.append( "  <styleUrl>" + style + "</styleUrl>\n" );                          sb.append( "  <styleUrl>#" + style + "</styleUrl>\n" );
201                          sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );                          sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );
202                          sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );                          sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
203                          sb.append( " </Placemark>\n" );                                          sb.append( " </Placemark>\n" );                
204                  }                  }
205                    sb.append( "</Folder>\n" );
206                  sb.append(  "</Document>\n" );                  sb.append( "</Document>\n" );
207                  sb.append( "</kml>\n" );                  sb.append( "</kml>\n" );
208    
209                  return sb.toString();                  return sb.toString();
210          }          }
211            
212          @Override          protected String getRequestsFromFile(boolean multiple) throws IOException {            
213          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {                  String kmlData = null;
214                  final String KEY = "kmldata";                  String key;
                   
                 String kmlData = cache.get(KEY);  
215                                    
216                    if (multiple == false)  {
217                            key = "kmldata";
218                    } else {
219                            key = "kmldata-multi";
220                    }
221                                            
222                    kmlData = cache.get(key);
223                            
224                  if (kmlData == null) {                  if (kmlData == null) {
225                          kmlData = formatXml( getRequestsFromFile() );                          kmlData = formatXml( getRequestsFromFileWorker(multiple) );
226                          cache.put(KEY, kmlData);                          cache.put(key, kmlData);
227                          kmlData += "<!-- from source -->";                                                kmlData += "<!-- from source -->";                      
228                  } else {                  } else {
229                          kmlData += "<!-- cached -->";                          kmlData += "<!-- cached -->";
230                  }                  }
231                                            
232                    return kmlData;
233            }
234            
235            boolean enabled(String param) {
236                    if (param == null || param.equals("")) {
237                            return false;
238                    }
239                    
240                    int p = 0;
241                    try {
242                            p = Integer.parseInt(param);
243                    } catch (Exception e) {}
244                                    
245                    return (p != 0);
246            }
247    
248                  if (req.getParameter("zip") != null) {          @Override
249            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
250                    
251                    boolean multiple = enabled( req.getParameter("multi") );
252                    
253                    String kmlData = getRequestsFromFile(multiple);
254                    
255                    if ( enabled(req.getParameter("zip")) ) {
256    
257                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
258    

Legend:
Removed from v.803  
changed lines
  Added in v.818

  ViewVC Help
Powered by ViewVC 1.1.20