1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.integration.slicer;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.gwe.api.EventListener;
26 import org.gwe.api.event.Event;
27 import org.gwe.persistence.model.ModelSummary;
28
29
30
31
32
33 public class GWECLMProxyAppEventListener extends EventListener {
34
35 private static Log log = LogFactory.getLog(GWECLMProxyAppEventListener.class);
36
37 private static final int NUM_PROGRESS_EVENTS = 1;
38 private static final int PROGRESS_BAR_LENGTH = 50;
39 private static final float PROGRESS_BAR_PERCENTAGE_UNIT = (float) (100.0 / PROGRESS_BAR_LENGTH);
40 private static final String FILTER_NAME = "GWECLMProxyApp";
41
42 private int oId = -1;
43 private float stepPercentageIncrement;
44 private Map<Integer, Integer> progress = new HashMap<Integer, Integer>();
45 private boolean liveReporting = false;
46 private long stepStartTime;
47 private boolean started = false;
48
49 public GWECLMProxyAppEventListener() {
50 stepStartTime = System.currentTimeMillis();
51 setNumJobs(1);
52 }
53
54 public void reportProgressFor(int orderId) {
55 if (liveReporting) throw new RuntimeException("Cannot change order to monitor!!!");
56 oId = orderId;
57 liveReporting = true;
58 outputStartProgressMsg(0);
59 }
60
61 public void eventPerformed(Event ev) {
62 super.eventPerformed(ev);
63
64 List<ModelSummary> modelIds = ev.getModelIdentifiers();
65 switch (ev.getEventType()) {
66 case ORDER_EXPANDED:
67 setNumJobs(ev.getModelIdentifiers().size() - 1);
68 break;
69
70 case JOB_COMPLETED:
71 logStep(modelIds, 2);
72 break;
73
74 case ORDER_COMPLETED:
75 if (logStep(modelIds, 0))
76 exit();
77 break;
78 }
79 }
80
81 private void setNumJobs(int numJobs) {
82 stepPercentageIncrement = (float) (100.0 / (numJobs * NUM_PROGRESS_EVENTS));
83 }
84
85 private boolean logStep(List<ModelSummary> modelIds, int index) {
86 ModelSummary orderSummary = modelIds.get(index);
87 int orderId = Integer.parseInt(orderSummary.getKey().toString());
88 int pSteps = getProgressSteps(orderId);
89 progress.put(orderId, progress.get(orderId) + 1);
90 if (oId != orderId) return false;
91 outputEndProgressMsg(pSteps);
92 stepStartTime = System.currentTimeMillis();
93 pSteps++;
94 outputStartProgressMsg(pSteps);
95 return true;
96 }
97
98 private void outputEndProgressMsg(int pSteps) {
99 outputProgress(" <filter-end>" + createFilterName(pSteps) + "<filter-time>" + (System.currentTimeMillis() - stepStartTime) + "</filter-time></filter-end>");
100 }
101
102 private void outputStartProgressMsg(int pSteps) {
103 int pPercent = Math.min((int)(pSteps * stepPercentageIncrement), 100);
104 if (pPercent >= 100)
105 exit();
106 outputProgress("<filter-start>" + createFilterName(pSteps) + createFilterComment(pPercent) + "</filter-start>");
107 }
108
109 private int getProgressSteps(int orderId) {
110 Integer prog = progress.get(orderId);
111 if (prog == null) {
112 prog = 0;
113 progress.put(orderId, prog);
114 }
115 return prog;
116 }
117
118 private String createFilterName(int pSteps) {
119 return "<filter-name>" + FILTER_NAME + pSteps + "</filter-name>";
120 }
121
122 private String createFilterComment(int pPercent) {
123 String result = "<filter-comment>";
124 int barUnitsCompleted = (int)(pPercent / PROGRESS_BAR_PERCENTAGE_UNIT);
125 int barUnitsIncompleted = PROGRESS_BAR_LENGTH - barUnitsCompleted;
126 for (int count = 0; count < barUnitsCompleted; count++) result += ">";
127 result += " Grid Execution Progress ";
128 for (int count = 0; count < barUnitsIncompleted; count++) result += ">";
129 return result + "</filter-comment>";
130 }
131
132 private void outputProgress(String progress) {
133 System.out.print(progress);
134 log.info("Order Progress: " + progress);
135 }
136
137 private void exit() {
138 outputProgress("<filter-start>FINISH<filter-comment> Grid Execution Completed!</filter-comment></filter-start><filter-end>FINISH<filter-time>0</filter-time></filter-end>");
139 System.exit(0);
140 }
141 }