CQWorkflowStatisticsService.java
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.statistics.StatisticsService
* com.day.cq.workflow.WorkflowException
* com.day.cq.workflow.WorkflowService
* com.day.cq.workflow.WorkflowSession
* com.day.cq.workflow.event.WorkflowEvent
* com.day.cq.workflow.exec.HistoryItem
* com.day.cq.workflow.exec.WorkItem
* com.day.cq.workflow.exec.Workflow
* com.day.cq.workflow.model.WorkflowModel
* com.day.cq.workflow.statistics.WorkflowStatisticService
* com.day.crx.statistics.Entry
* com.day.crx.statistics.Report
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.component.ComponentContext
* org.osgi.service.event.Event
* org.osgi.service.event.EventHandler
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.workflow.impl.statistics;
import com.day.cq.statistics.StatisticsService;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.event.WorkflowEvent;
import com.day.cq.workflow.exec.HistoryItem;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.impl.ServiceLoginUtil;
import com.day.cq.workflow.impl.statistics.WorkflowInstanceReport;
import com.day.cq.workflow.impl.statistics.WorkflowInstanceView;
import com.day.cq.workflow.impl.statistics.WorkflowReport;
import com.day.cq.workflow.impl.statistics.WorkflowView;
import com.day.cq.workflow.model.WorkflowModel;
import com.day.cq.workflow.statistics.WorkflowStatisticService;
import com.day.crx.statistics.Entry;
import com.day.crx.statistics.Report;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, immediate=1)
@Service(value={EventHandler.class, WorkflowStatisticService.class})
@Properties(value={@Property(name="event.topics", value={"com/day/cq/workflow/event"}, propertyPrivate=1), @Property(name="event.filter", value={"(!(event.application=*))"}, propertyPrivate=1)})
public class CQWorkflowStatisticsService
implements WorkflowStatisticService,
EventHandler {
private static final Logger log = LoggerFactory.getLogger(CQWorkflowStatisticsService.class);
private static final String WORKFLOW_STATS_PREFIX = "/var/statistics/workflows";
private static final String WORKFLOW_STATISTICS_SERVICE_STOP = "WORKFLOW_STATISTICS_SERVICE_STOP";
@Reference(policy=ReferencePolicy.STATIC)
private WorkflowService workflowService = null;
@Reference
private StatisticsService statisticsService = null;
@Reference
private SlingRepository repository = null;
@Property(description="Indicates if the workflow stats collection should be enabled", label="enable workflow stats collection", boolValue={1})
private static final String PROP_ENABLED = "stats.enabled";
private boolean statsCollectionEnabled = true;
private final BlockingQueue<WorkflowEvent> queue = new LinkedBlockingQueue<WorkflowEvent>();
private EventProcessor processor = null;
@Activate
protected void activate(ComponentContext context) {
this.statsCollectionEnabled = PropertiesUtil.toBoolean(context.getProperties().get("stats.enabled"), (boolean)true);
if (this.statsCollectionEnabled) {
this.processor = new EventProcessor();
Thread thread = new Thread((Runnable)this.processor, this.getClass().getSimpleName() + "-Processor");
thread.setDaemon(true);
thread.start();
}
log.debug("Stats activate. Enabled is: {} processor is: {}", (Object)this.statsCollectionEnabled, (Object)this.processor);
}
@Deactivate
protected void deactivate(ComponentContext context) {
log.debug("Stats deactivate. Enabled is: {} processor is: {}", (Object)this.statsCollectionEnabled, (Object)this.processor);
if (this.processor != null) {
this.processor.stop();
Hashtable<String, Boolean> props = new Hashtable<String, Boolean>();
props.put("WORKFLOW_STATISTICS_SERVICE_STOP", true);
this.queue.offer(new WorkflowEvent(props));
}
}
public Map getReport() {
throw new UnsupportedOperationException("This method is no longer supported. Please use getReport(Session)");
}
public Map getReport(Session session) {
HashMap<String, Object> workflowStats = new HashMap<String, Object>();
try {
Iterator itr = this.statisticsService.runReport(session, (Report)new WorkflowReport("/var/statistics/workflows"));
Object[] stats = (Object[])itr.next();
workflowStats.put("Workflow Model Id", stats[0]);
workflowStats.put("Total Count", stats[1]);
workflowStats.put("Total Time", stats[2]);
workflowStats.put("Average Throughput Time", stats[3]);
workflowStats.put("Minimal Throughput Time", stats[4]);
workflowStats.put("Maximal Throughput Time", stats[5]);
workflowStats.put("Number of Running Instances", stats[6]);
workflowStats.put("Number of Aborted Instances", stats[7]);
workflowStats.put("Number of Completed Instances", stats[8]);
workflowStats.put("Number of Suspended Instances", stats[9]);
}
catch (RepositoryException re) {
log.warn("Unable to fetch workflow report: " + (Object)re);
}
return workflowStats;
}
public Map getInstanceReport(WorkflowModel model) {
throw new UnsupportedOperationException("This method is no longer supported. Please use getInstanceReport(Session, WorkflowModel");
}
public Map getInstanceReport(Session session, WorkflowModel model) {
Map workflowStats = new HashMap();
try {
Iterator itr = this.statisticsService.runReport(session, (Report)new WorkflowInstanceReport("/var/statistics/workflows", model));
workflowStats = (Map)itr.next();
}
catch (RepositoryException re) {
log.warn("Unable to fetch workflow instance report for " + model.getId() + ": " + (Object)re);
}
return workflowStats;
}
public void handleEvent(Event event) {
if (this.statsCollectionEnabled && "com/day/cq/workflow/event".equals(event.getTopic())) {
try {
this.queue.put((WorkflowEvent)event);
if (log.isDebugEnabled()) {
log.info("handleEvent: put event [{}] in queue, size now [{}]", event.getProperty("EventType"), (Object)this.queue.size());
}
}
catch (InterruptedException e) {
log.debug("Queue put interrupted");
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void processEvent(WorkflowEvent event) {
block16 : {
WorkflowSession wfSession = this.getWorkflowSession();
try {
Object eventType = event.getProperty("EventType");
String instanceId = (String)event.getProperty("WorkflowInstanceId");
Workflow instance = null;
try {
instance = instanceId != null ? wfSession.getWorkflow(instanceId) : null;
}
catch (WorkflowException we) {
log.warn("Unable to load workflow instance {}", (Object)instanceId, (Object)we);
}
if (instance == null) break block16;
try {
WorkflowView view;
if (eventType.equals("WorkflowStarted") || eventType.equals("WorkflowResumed") || eventType.equals("WorkflowSuspended")) {
view = new WorkflowView("/var/statistics/workflows", instance, eventType);
this.statisticsService.addEntry((Entry)view);
} else if (eventType.equals("WorkflowAborted") || eventType.equals("WorkflowCompleted")) {
view = new WorkflowView("/var/statistics/workflows", instance, eventType);
this.statisticsService.addEntry((Entry)view);
try {
List historyItems = wfSession.getHistory(instance);
WorkflowInstanceView iView = new WorkflowInstanceView("/var/statistics/workflows", instance.getWorkflowModel(), historyItems, null);
this.statisticsService.addEntry((Entry)iView);
}
catch (Throwable t) {
log.warn("Cannot create view", (Object)t.getMessage());
}
}
if (eventType.equals("NodeTransition")) {
WorkItem currentItem = (WorkItem)event.getProperty("Workitem");
try {
WorkflowInstanceView view2 = new WorkflowInstanceView("/var/statistics/workflows", instance.getWorkflowModel(), wfSession.getHistory(instance), currentItem);
this.statisticsService.addEntry((Entry)view2);
}
catch (Throwable t) {
log.warn("Cannot create view", (Object)t.getMessage());
}
}
}
catch (RepositoryException re) {
log.warn("Unable to update statistics: " + re.getMessage());
}
}
finally {
if (wfSession != null) {
wfSession.logout();
}
}
}
}
private WorkflowSession getWorkflowSession() {
Session session = ServiceLoginUtil.createWorkflowServiceSession(this.repository);
return this.workflowService.getWorkflowSession(session);
}
protected void bindWorkflowService(WorkflowService workflowService) {
this.workflowService = workflowService;
}
protected void unbindWorkflowService(WorkflowService workflowService) {
if (this.workflowService == workflowService) {
this.workflowService = null;
}
}
protected void bindStatisticsService(StatisticsService statisticsService) {
this.statisticsService = statisticsService;
}
protected void unbindStatisticsService(StatisticsService statisticsService) {
if (this.statisticsService == statisticsService) {
this.statisticsService = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
private class EventProcessor
implements Runnable {
private volatile boolean isRunning;
private EventProcessor() {
this.isRunning = true;
}
public void stop() {
this.isRunning = false;
}
@Override
public void run() {
log.debug("Workflow Statistics Processor started");
while (this.isRunning) {
try {
WorkflowEvent event = (WorkflowEvent)CQWorkflowStatisticsService.this.queue.take();
log.debug("Workflow Statistics processor. Processing {} type: {} ", (Object)event.getTopic(), event.getProperty("EventType"));
CQWorkflowStatisticsService.this.processEvent(event);
}
catch (InterruptedException e) {
log.debug("Workflow Statistics processor interrupted");
}
}
log.debug("Workflow Statistics Processor done");
}
}
}