001    package hirondelle.web4jtools.logview.downtime;
002    
003    import hirondelle.web4j.action.ActionImpl;
004    import hirondelle.web4j.action.ResponsePage;
005    import hirondelle.web4j.model.AppException;
006    import hirondelle.web4j.model.ModelCtorException;
007    import hirondelle.web4j.request.RequestParser;
008    import hirondelle.web4j.util.Util;
009    import hirondelle.web4jtools.logview.parsedview.LoggerRecord;
010    import hirondelle.web4jtools.logview.simpleview.LogFileDAO;
011    
012    import java.io.File;
013    import java.util.ArrayList;
014    import java.util.Date;
015    import java.util.List;
016    import java.util.logging.Logger;
017    
018    /**
019    * Display list of application down times, and their durations.
020    * 
021    * <P>It is recommended that the view suppress display of items 
022    * having small down times (say, under 2-3 minutes).
023    */
024    public final class DownTimeAction extends ActionImpl {
025    
026      public DownTimeAction(RequestParser aRequestParser) {
027        super(FORWARD, aRequestParser);
028      }
029      
030      @Override public ResponsePage execute() throws AppException {
031        
032        LogFileDAO dao = new LogFileDAO(getRequestParser().getRequest());
033        
034        List<File> allAppLogFiles = dao.listAllAppLogFiles();
035        List<LoggerRecord> firstRecords = dao.listFirstRecordsFor(allAppLogFiles);
036        List<DownTime> downtimes = calculateDownTimes(allAppLogFiles, firstRecords);
037        
038        addToRequest("downTimes" , downtimes);
039        return getResponsePage();
040      }
041      
042      // PRIVATE //
043      private static final ResponsePage FORWARD = new ResponsePage("Down Time From Logs", "view.jsp", DownTimeAction.class);
044      private static final Logger fLogger = Util.getLogger(DownTimeAction.class);
045      
046      private List<DownTime> calculateDownTimes(List<File> aAllAppLogFiles, List<LoggerRecord> aFirstRecords) {
047        fLogger.fine("Num files : " + aAllAppLogFiles.size());
048        fLogger.fine("Num First Records: " + aFirstRecords.size());
049        List<DownTime> result = new ArrayList<DownTime>();
050        for (int idx = 1; idx <= aFirstRecords.size() - 1 ; ++idx) {
051          Date from = new Date(aAllAppLogFiles.get(idx-1).lastModified());
052          Date to = aFirstRecords.get(idx).getDate();
053          DownTime downTime = null;
054          try {
055            downTime = new DownTime(from, to);
056          }
057          catch(ModelCtorException ex){
058            throw new RuntimeException(ex);
059          }
060          result.add(downTime);
061        }
062        return result;
063      }
064    }