1 16 package scriptella.execution; 17 18 import scriptella.configuration.Location; 19 import scriptella.spi.Connection; 20 21 import java.util.Date ; 22 import java.util.Stack ; 23 24 25 44 public class ExecutionStatisticsBuilder { 45 private ExecutionStatistics executionStatistics; 46 47 protected Stack <ExecutionStatistics.ElementInfo> executionStack = new Stack <ExecutionStatistics.ElementInfo>(); 49 50 55 public void elementStarted(final Location loc, Connection connection) { 56 ExecutionStatistics.ElementInfo ei = getInfo(loc); 57 executionStack.push(ei); 58 ei.statementsOnStart = connection.getExecutedStatementsCount(); 59 ei.connection = connection; 60 ei.started=System.nanoTime(); 61 } 62 63 66 public void elementExecuted() { 67 setElementState(true); 68 } 69 70 73 public void etlStarted() { 74 executionStatistics = new ExecutionStatistics(); 75 executionStatistics.setStarted(new Date ()); 76 } 77 78 81 public void etlComplete() { 82 if (executionStatistics==null) { 83 throw new IllegalStateException ("etlStarted not called"); 84 } 85 86 executionStatistics.setFinished(new Date ()); 88 } 89 90 91 94 private void setElementState(boolean ok) { 95 ExecutionStatistics.ElementInfo ended = executionStack.pop(); 96 long ti = System.nanoTime() - ended.started; 97 ended.workingTime += ti; if (ended.workingTime < 0) { 99 ended.workingTime = 0; 100 } 101 final Connection con = ended.connection; 102 ended.connection=null; long conStatements = con.getExecutedStatementsCount(); 104 long elStatements = conStatements - ended.statementsOnStart; 105 if (ok) { 106 ended.okCount++; 107 } else { 108 ended.failedCount++; 109 } 110 111 if (elStatements > 0) { 112 ended.statements += elStatements; 113 executionStatistics.statements+=elStatements; 114 } 115 116 117 for (int i=executionStack.size()-1;i>=0;i--) { 120 final ExecutionStatistics.ElementInfo parent = executionStack.get(i); 121 parent.workingTime -= ti; 122 if (parent.connection==con) { parent.statementsOnStart+=elStatements; 124 } 125 } 126 127 } 128 129 public void elementFailed() { 130 setElementState(false); 131 } 132 133 private ExecutionStatistics.ElementInfo getInfo(final Location loc) { 134 if (executionStatistics==null) { 135 throw new IllegalStateException ("etlStarted must be invoked prior to calling this method"); 136 } 137 ExecutionStatistics.ElementInfo ei = executionStatistics.elements.get(loc.getXPath()); 138 139 if (ei == null) { 140 ei = new ExecutionStatistics.ElementInfo(); 141 ei.id = loc.getXPath(); 142 executionStatistics.elements.put(loc.getXPath(), ei); 143 } 144 145 return ei; 146 } 147 148 public ExecutionStatistics getStatistics() { 149 return executionStatistics; 150 } 151 } 152 | Popular Tags |