View Javadoc

1   /*
2    * #%L
3    * Bitrepository Integrity Service
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  
23  package org.bitrepository.service.workflow;
24  
25  import java.util.Date;
26  import java.util.LinkedList;
27  import java.util.List;
28  import org.bitrepository.common.utils.TimeUtils;
29  
30  /**
31   * Provides details about the statistics of an workflow. The statistics will not be fully populated until the
32   * workflow has finished.
33   */
34  public class WorkflowStatistic {
35      private final String name;
36      private Date start;
37      private Date finish;
38      private final List<WorkflowStatistic> subStatistics = new LinkedList<WorkflowStatistic>();
39  
40      private static final String LINEFEED = "\n";
41  
42      /**
43       * Creates a fresh <code>WorkflowStatistic</code> instance.
44       * @param name The name of this instance to be used as title for the statistics.
45       */
46      public WorkflowStatistic(String name) {
47          this.name = name;
48      }
49  
50      /**
51       * Marks the start time of the statistics.
52       */
53      public void start() {
54          start = new Date();
55      }
56  
57      /**
58       * Marks the start time of a sub step of the statistics statistics.
59       * @param name The name of the substep to be used as identifier in the statistics.
60       */
61      public void startSubStatistic(String name) {
62          subStatistics.add(new WorkflowStatistic(name));
63          getCurrentSubStatistic().start();
64      }
65  
66      /**
67       * Marks the end time of a sub step of the statistics statistics.
68       */
69      public void finishSubStatistic() {
70          getCurrentSubStatistic().finish();
71      }
72  
73      /**
74       * @return A string representation of statistics, including all substatistics.
75       */
76      public String getFullStatistics() {
77          if (start == null) {
78              return "Haven't finished a run yet";
79          }
80          StringBuilder sb = new StringBuilder();
81          sb.append(getName() + " duration: " + TimeUtils.millisecondsToHuman(getDuration()));
82          for (WorkflowStatistic stepStat: subStatistics) {
83              sb.append(LINEFEED +stepStat.getFullStatistics());
84          }
85          return sb.toString();
86      }
87  
88      /**
89       * Will generate a string of the form 'step duration'/'workflow duration'. If the workflow isn't running
90       * a "Not running" string will be returned.
91       * @return
92       */
93      public String getPartStatistics() {
94          if (start == null) {
95              return "Not started yet";
96          }
97          else if (finish != null ) {
98              return "Idle";
99          } else {
100             WorkflowStatistic currentSubStatistic = getCurrentSubStatistic();
101             return currentSubStatistic.getName() + LINEFEED +
102                     "Running for " +
103                     TimeUtils.millisecondsToHuman(currentSubStatistic.getDuration()) + "/" +
104                     TimeUtils.millisecondsToHuman(getDuration()) + ")";
105         }
106     }
107 
108     /**
109      * Will return the statistics for the current step if the workflow statistics have an active step.
110      * @return
111      */
112     public WorkflowStatistic getCurrentSubStatistic() {
113         if (subStatistics.isEmpty() || finish != null) {
114             return null;
115         } else return subStatistics.get(subStatistics.size()-1);
116     }
117 
118     public Date getStart() {
119         return start;
120     }
121 
122     public Date getFinish() {
123         return finish;
124     }
125     public void finish() {
126         this.finish = new Date();
127     }
128 
129     public String getName() {
130         return name;
131     }
132 
133     /**
134      * @return The duration of the workflow if it has been started, else 0.
135      */
136     private long getDuration() {
137         if (start == null) {
138             return 0;
139         }
140         if (getFinish() == null) {
141             return System.currentTimeMillis() - start.getTime();
142         } else {
143             return finish.getTime() - start.getTime();
144         }
145     }
146 
147     @Override
148     public String toString() {
149         return "WorkflowStatistic{" +
150                 "name='" + name + '\'' +
151                 ", start=" + start +
152                 ", finish=" + finish +
153                 ", subStatistics=" + subStatistics +
154                 '}';
155     }
156 }