1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
32
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
44
45
46 public WorkflowStatistic(String name) {
47 this.name = name;
48 }
49
50
51
52
53 public void start() {
54 start = new Date();
55 }
56
57
58
59
60
61 public void startSubStatistic(String name) {
62 subStatistics.add(new WorkflowStatistic(name));
63 getCurrentSubStatistic().start();
64 }
65
66
67
68
69 public void finishSubStatistic() {
70 getCurrentSubStatistic().finish();
71 }
72
73
74
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
90
91
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
110
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
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 }