| 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 | |
package net.objectlab.qalab.parser; |
| 40 | |
|
| 41 | |
import net.objectlab.qalab.util.QALabTags; |
| 42 | |
|
| 43 | |
import java.util.Calendar; |
| 44 | |
import java.util.Date; |
| 45 | |
import java.util.Iterator; |
| 46 | |
import java.util.Map; |
| 47 | |
import java.util.Set; |
| 48 | |
import java.util.Stack; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public class FileStats { |
| 58 | |
private static final int ONE_DAY = 24; |
| 59 | |
|
| 60 | |
|
| 61 | |
private Date startDateCutoff; |
| 62 | |
|
| 63 | |
|
| 64 | 252 | private Date endDateCutoff = null; |
| 65 | |
|
| 66 | |
|
| 67 | 252 | private StringBuffer upBuf = new StringBuffer(); |
| 68 | |
|
| 69 | |
|
| 70 | 252 | private StringBuffer downBuf = new StringBuffer(); |
| 71 | |
|
| 72 | |
|
| 73 | |
private String filename; |
| 74 | |
|
| 75 | |
|
| 76 | |
private String pathToFile; |
| 77 | |
|
| 78 | |
|
| 79 | |
private Map typesSurveyed; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | 252 | private int offsetForLastRun = ONE_DAY; |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | 252 | public FileStats(final Map types, final String fileSurveyed, final String pathToFile, final Date cutoff) { |
| 95 | 252 | this.typesSurveyed = types; |
| 96 | 252 | this.filename = fileSurveyed; |
| 97 | 252 | this.pathToFile = pathToFile; |
| 98 | 252 | this.startDateCutoff = new Date(cutoff.getTime()); |
| 99 | 252 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public final String getFileName() { |
| 105 | 114 | return filename; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public final String getUpXmlResult() { |
| 112 | 252 | return upBuf.toString(); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public final String getDownXmlResult() { |
| 119 | 252 | return downBuf.toString(); |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
public final void constructXML() { |
| 126 | 252 | Set keys = (Set) typesSurveyed.keySet(); |
| 127 | 252 | Iterator i = keys.iterator(); |
| 128 | |
|
| 129 | 543 | while (i.hasNext()) { |
| 130 | 291 | Stack s = (Stack) typesSurveyed.get((String) i.next()); |
| 131 | |
|
| 132 | 291 | processFilesResultBeans(s); |
| 133 | 291 | } |
| 134 | |
|
| 135 | 252 | if (upBuf.length() > 0) { |
| 136 | 60 | upBuf.insert(0, " <file name=\"" + getFileName() + "\" path=\"" + getPathToFile() + "\">" + QALabTags.LINE_END); |
| 137 | 60 | upBuf.append(" </file>").append(QALabTags.LINE_END); |
| 138 | |
} |
| 139 | |
|
| 140 | 252 | if (downBuf.length() > 0) { |
| 141 | 54 | downBuf.insert(0, " <file name=\"" + getFileName() + "\" path=\"" + getPathToFile() + "\">" + QALabTags.LINE_END); |
| 142 | 54 | downBuf.append(" </file>").append(QALabTags.LINE_END); |
| 143 | |
} |
| 144 | 252 | } |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
private void processFilesResultBeans(final Stack stack) { |
| 154 | 291 | if (stack.empty()) { |
| 155 | 147 | return; |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | 144 | SingleStat currentResult = (SingleStat) stack.pop(); |
| 160 | |
|
| 161 | 144 | Iterator it = stack.iterator(); |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | 144 | Calendar currentday = Calendar.getInstance(); |
| 166 | 144 | currentday.setTime(getEndDateCutoff()); |
| 167 | 144 | currentday.add(Calendar.HOUR, -offsetForLastRun); |
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | 144 | if (!currentResult.getDate().before(startDateCutoff) |
| 182 | |
&& currentResult.getDate().before(currentday.getTime())) { |
| 183 | 30 | appendStats(downBuf, currentResult, null); |
| 184 | |
} |
| 185 | |
|
| 186 | 144 | checkPreviousResults(stack, currentResult, it); |
| 187 | 144 | } |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
private void checkPreviousResults(final Stack stack, SingleStat currentResult, Iterator it) { |
| 195 | |
SingleStat previousResult; |
| 196 | 144 | Calendar oneDayBeforeEndCutoff = Calendar.getInstance(); |
| 197 | 144 | oneDayBeforeEndCutoff.setTime(endDateCutoff); |
| 198 | 144 | oneDayBeforeEndCutoff.add(Calendar.HOUR, -offsetForLastRun); |
| 199 | |
|
| 200 | |
|
| 201 | 624 | while (it.hasNext()) { |
| 202 | 480 | previousResult = (SingleStat) stack.pop(); |
| 203 | |
|
| 204 | 480 | int previousCount = previousResult.getStatValue(); |
| 205 | 480 | final int currentCount = currentResult.getStatValue(); |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | 480 | final int diff = currentCount - previousCount; |
| 212 | |
|
| 213 | 480 | if (diff > 0) { |
| 214 | 36 | handleUp(currentResult, previousResult); |
| 215 | 36 | } else if (diff < 0) { |
| 216 | 48 | handleDown(currentResult, previousResult); |
| 217 | 48 | } else if (diff == 0 && fallInWindow(currentResult) |
| 218 | |
&& !currentResult.getDate().before(oneDayBeforeEndCutoff.getTime()) |
| 219 | |
&& !previousResult.getDate().after(startDateCutoff)) { |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | 0 | appendStats(upBuf, null, currentResult); |
| 224 | |
} |
| 225 | |
|
| 226 | 480 | currentResult = previousResult; |
| 227 | 480 | } |
| 228 | |
|
| 229 | |
|
| 230 | 144 | if (fallInWindow(currentResult)) { |
| 231 | 66 | appendStats(upBuf, null, currentResult); |
| 232 | |
} |
| 233 | 144 | } |
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
private void handleDown(SingleStat currentResult, SingleStat previousResult) { |
| 240 | 48 | if (fallInWindow(currentResult)) { |
| 241 | |
|
| 242 | 33 | appendStats(downBuf, previousResult, currentResult); |
| 243 | |
} |
| 244 | 48 | } |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
private void handleUp(SingleStat currentResult, SingleStat previousResult) { |
| 251 | 36 | if (fallInWindow(currentResult)) { |
| 252 | |
|
| 253 | 30 | appendStats(upBuf, previousResult, currentResult); |
| 254 | |
} |
| 255 | |
|
| 256 | 36 | } |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
private boolean fallInWindow(SingleStat stat) { |
| 265 | 624 | return !stat.getDate().before(startDateCutoff) && !stat.getDate().after(endDateCutoff); |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
private void appendStats(final StringBuffer buffer, final SingleStat previousResult, final SingleStat currentResult) { |
| 279 | 159 | int previousCount = 0; |
| 280 | |
|
| 281 | 159 | if (previousResult != null) { |
| 282 | 93 | previousCount = previousResult.getStatValue(); |
| 283 | |
} |
| 284 | |
|
| 285 | 159 | int currentCount = 0; |
| 286 | |
|
| 287 | 159 | if (currentResult != null) { |
| 288 | 129 | currentCount = currentResult.getStatValue(); |
| 289 | |
} |
| 290 | |
|
| 291 | 159 | int diff = currentCount - previousCount; |
| 292 | |
|
| 293 | 159 | if (diff != 0) { |
| 294 | 159 | buffer.append(" <diff previousrun=\""); |
| 295 | |
|
| 296 | 159 | if (previousResult != null) { |
| 297 | 93 | writeDate(buffer, previousResult); |
| 298 | 93 | } else { |
| 299 | 66 | buffer.append("-"); |
| 300 | |
} |
| 301 | |
|
| 302 | 159 | buffer.append("\" currentrun=\""); |
| 303 | |
|
| 304 | 159 | if (currentResult != null) { |
| 305 | 129 | writeDate(buffer, currentResult); |
| 306 | 129 | buffer.append("\" type=\"").append(currentResult.getType()); |
| 307 | 129 | } else { |
| 308 | 30 | buffer.append("-"); |
| 309 | 30 | buffer.append("\" type=\"").append(previousResult.getType()); |
| 310 | |
} |
| 311 | |
|
| 312 | 159 | buffer.append("\" previouserrors=\""); |
| 313 | |
|
| 314 | 159 | buffer.append(previousCount); |
| 315 | 159 | buffer.append("\" currenterrors=\""); |
| 316 | 159 | buffer.append(currentCount); |
| 317 | 159 | buffer.append("\" diff=\""); |
| 318 | 159 | buffer.append(diff); |
| 319 | 159 | buffer.append("\"/>").append(QALabTags.LINE_END); |
| 320 | |
} |
| 321 | 159 | } |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
private void writeDate(final StringBuffer buffer, final SingleStat currentResult) { |
| 328 | 222 | Date date = currentResult.getDate(); |
| 329 | 222 | Calendar cal = Calendar.getInstance(); |
| 330 | 222 | cal.setTime(date); |
| 331 | |
|
| 332 | 222 | if (0 == cal.get(Calendar.SECOND) && 0 == cal.get(Calendar.MINUTE) && 0 == cal.get(Calendar.HOUR)) { |
| 333 | 0 | buffer.append(QALabTags.DEFAULT_DATE_FORMAT.format(currentResult.getDate())); |
| 334 | 0 | } else { |
| 335 | 222 | buffer.append(QALabTags.DEFAULT_DATETIME_FORMAT.format(currentResult.getDate())); |
| 336 | |
} |
| 337 | 222 | } |
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
public final void setEndDateCutoff(final Date endDate) { |
| 344 | 252 | this.endDateCutoff = new Date(endDate.getTime()); |
| 345 | 252 | } |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
public final Date getEndDateCutoff() { |
| 351 | 144 | return new Date(endDateCutoff.getTime()); |
| 352 | |
} |
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
public final String getPathToFile() { |
| 358 | 114 | return pathToFile; |
| 359 | |
} |
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
public final void setPathToFile(String pathToFile) { |
| 366 | 0 | this.pathToFile = pathToFile; |
| 367 | 0 | } |
| 368 | |
|
| 369 | |
public int getOffsetForLastRun() { |
| 370 | 0 | return offsetForLastRun; |
| 371 | |
} |
| 372 | |
|
| 373 | |
public void setOffsetForLastRun(int offsetForLastRun) { |
| 374 | 252 | this.offsetForLastRun = offsetForLastRun; |
| 375 | 252 | } |
| 376 | |
} |
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|