/[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 809 by torben, Wed Jun 9 14:17:17 2010 UTC revision 896 by torben, Thu Jun 24 18:31:19 2010 UTC
# Line 42  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) {          boolean isGz(String fileStr) {
54                  return fileStr.substring(fileStr.length() - 3).equals(".gz");                  return fileStr.substring(fileStr.length() - 3).equals(".gz");
55          }          }
56    
57          protected List<RequestPosition> getRequestsFromFileWorker(boolean multiple) 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                          String files_single[] = {"/var/log/apache2/app_access.log"};                          String files_single[] = {"/var/log/apache2/app_access.log"};
62                          String files_multi[] = {"/var/log/apache2/app_access.log.2.gz", "/var/log/apache2/app_access.log.1", "/var/log/apache2/app_access.log"};                          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[];                          String files[];
65                                                    
# Line 64  public class RequestPlotter extends Http Line 70  public class RequestPlotter extends Http
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                            Date now = new Date();
75                          for (String fileStr : files ) {                          for (String fileStr : files ) {
76                                  File f = new File(fileStr);                                  File f = new File(fileStr);
77                                  if ( !f.exists() ) {                                  if ( !f.exists() ) {
# Line 89  public class RequestPlotter extends Http Line 96  public class RequestPlotter extends Http
96                                                  continue;                                                  continue;
97                                          }                                          }
98                    
99                                          if (line.indexOf("latitude") == -1 ) {                                          if (line.indexOf("latitude=") == -1 ) {
100                                                  continue;                                                  continue;
101                                          }                                          }
102                                            
103                                            if (line.indexOf("longitude=") == -1) {
104                                                    continue;
105                                            }
106                                            
107                                          RequestPosition pos = new RequestPosition();                                          RequestPosition pos = new RequestPosition();
108                    
109                                          String toks[] = line.split(" ");                                          String toks[] = line.split(" ");
# Line 105  public class RequestPlotter extends Http Line 117  public class RequestPlotter extends Http
117                    
118                                          pos.lat = args[0].split("=")[1];                                          pos.lat = args[0].split("=")[1];
119                                          pos.lng = args[1].split("=")[1];                                          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                    
                                         positions.add(pos);  
131                                  }                                  }
132                                  in.close();                                  in.close();
133                                  input.close();                                  input.close();
# Line 122  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>#").append(color).append("</styleUrl>\n" );
156                            sb.append( "  <description>IP=").append(current.ip).append("  Time=").append(current.time).append("</description>\n" );
157                            sb.append( "  <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",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 162  public class RequestPlotter extends Http Line 200  public class RequestPlotter extends Http
200          */                */      
201                                    
202    
203                  final String STYLE_GREEN = "green";                  formatPositions(sb, "green", positions.green);
204                  final String STYLE_YELLOW = "yellow";                  formatPositions(sb, "yellow", positions.yellow);
205                  final String STYLE_RED = "red";                  formatPositions(sb, "red", positions.red);
206                    
207                  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" );  
208                  sb.append( "</Document>\n" );                  sb.append( "</Document>\n" );
209                  sb.append( "</kml>\n" );                  sb.append( "</kml>\n" );
210    
# Line 226  public class RequestPlotter extends Http Line 233  public class RequestPlotter extends Http
233                                                                                    
234                  return kmlData;                  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                  boolean multiple = req.getParameter("multi") != null;                  boolean multiple = enabled( req.getParameter("multi") );
254                                    
255                  String kmlData = getRequestsFromFile(multiple);                  String kmlData = getRequestsFromFile(multiple);
256                                    
257                  if (req.getParameter("zip") != null) {                  if ( enabled(req.getParameter("zip")) ) {
258    
259                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
260    

Legend:
Removed from v.809  
changed lines
  Added in v.896

  ViewVC Help
Powered by ViewVC 1.1.20