1 24 package org.ofbiz.webapp.stats; 25 26 import java.net.InetAddress ; 27 import java.util.Calendar ; 28 import java.util.Date ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.Map ; 33 import javax.servlet.http.HttpServletRequest ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.UtilHttp; 37 import org.ofbiz.base.util.UtilMisc; 38 import org.ofbiz.base.util.UtilProperties; 39 import org.ofbiz.entity.GenericDelegator; 40 import org.ofbiz.entity.GenericEntityException; 41 import org.ofbiz.entity.GenericValue; 42 43 52 public class ServerHitBin { 53 public static final String module = ServerHitBin.class.getName(); 55 56 public static final int REQUEST = 1; 57 public static final int EVENT = 2; 58 public static final int VIEW = 3; 59 public static final int ENTITY = 4; 60 public static final int SERVICE = 5; 61 public static final String [] typeNames = {"", "Request", "Event", "View", "Entity", "Service"}; 62 public static final String [] typeIds = {"", "REQUEST", "EVENT", "VIEW", "ENTITY", "SERVICE"}; 63 64 public static void countRequest(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, 65 GenericDelegator delegator) { 66 countHit(id, REQUEST, request, startTime, runningTime, userLogin, delegator); 67 } 68 69 public static void countEvent(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, 70 GenericDelegator delegator) { 71 countHit(id, EVENT, request, startTime, runningTime, userLogin, delegator); 72 } 73 74 public static void countView(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, 75 GenericDelegator delegator) { 76 countHit(id, VIEW, request, startTime, runningTime, userLogin, delegator); 77 } 78 79 public static void countEntity(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, 80 GenericDelegator delegator) { 81 countHit(id, ENTITY, request, startTime, runningTime, userLogin, delegator); 82 } 83 84 public static void countService(String id, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, 85 GenericDelegator delegator) { 86 countHit(id, SERVICE, request, startTime, runningTime, userLogin, delegator); 87 } 88 89 public static void countHit(String id, int type, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, 90 GenericDelegator delegator) { 91 if (!"true".equals(UtilProperties.getPropertyValue("serverstats", "stats.enable." + typeIds[type]))) return; 93 countHit(id, type, request, startTime, runningTime, userLogin, delegator, true); 94 } 95 96 public static void advanceAllBins(long toTime) { 97 advanceAllBins(toTime, requestHistory); 98 advanceAllBins(toTime, eventHistory); 99 advanceAllBins(toTime, viewHistory); 100 advanceAllBins(toTime, entityHistory); 101 advanceAllBins(toTime, serviceHistory); 102 } 103 104 static void advanceAllBins(long toTime, Map binMap) { 105 Iterator entries = binMap.entrySet().iterator(); 106 107 while (entries.hasNext()) { 108 Map.Entry entry = (Map.Entry ) entries.next(); 109 110 if (entry.getValue() != null) { 111 ServerHitBin bin = (ServerHitBin) entry.getValue(); 112 113 bin.advanceBin(toTime); 114 } 115 } 116 } 117 118 protected static void countHit(String id, int type, HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin, 119 GenericDelegator delegator, boolean isOriginal) { 120 if (delegator == null) { 121 throw new IllegalArgumentException ("The delgator passed to countHit cannot be null"); 122 } 123 124 ServerHitBin bin = null; 125 LinkedList binList = null; 126 127 switch (type) { 128 case REQUEST: 129 binList = (LinkedList ) requestHistory.get(id); 130 break; 131 132 case EVENT: 133 binList = (LinkedList ) eventHistory.get(id); 134 break; 135 136 case VIEW: 137 binList = (LinkedList ) viewHistory.get(id); 138 break; 139 140 case ENTITY: 141 binList = (LinkedList ) entityHistory.get(id); 142 break; 143 144 case SERVICE: 145 binList = (LinkedList ) serviceHistory.get(id); 146 break; 147 } 148 149 if (binList == null) { 150 synchronized (ServerHitBin.class) { 151 switch (type) { 152 case REQUEST: 153 binList = (LinkedList ) requestHistory.get(id); 154 break; 155 156 case EVENT: 157 binList = (LinkedList ) eventHistory.get(id); 158 break; 159 160 case VIEW: 161 binList = (LinkedList ) viewHistory.get(id); 162 break; 163 164 case ENTITY: 165 binList = (LinkedList ) entityHistory.get(id); 166 break; 167 168 case SERVICE: 169 binList = (LinkedList ) serviceHistory.get(id); 170 break; 171 } 172 if (binList == null) { 173 binList = new LinkedList (); 174 switch (type) { 175 case REQUEST: 176 requestHistory.put(id, binList); 177 break; 178 179 case EVENT: 180 eventHistory.put(id, binList); 181 break; 182 183 case VIEW: 184 viewHistory.put(id, binList); 185 break; 186 187 case ENTITY: 188 entityHistory.put(id, binList); 189 break; 190 191 case SERVICE: 192 serviceHistory.put(id, binList); 193 break; 194 } 195 } 196 } 197 } 198 199 if (binList.size() > 0) { 200 bin = (ServerHitBin) binList.getFirst(); 201 } 202 if (bin == null) { 203 synchronized (ServerHitBin.class) { 204 if (binList.size() > 0) { 205 bin = (ServerHitBin) binList.getFirst(); 206 } 207 if (bin == null) { 208 bin = new ServerHitBin(id, type, true, delegator); 209 binList.addFirst(bin); 210 } 211 } 212 } 213 214 bin.addHit(startTime, runningTime); 215 if (isOriginal && !"GLOBAL".equals(id)) { 216 bin.saveHit(request, startTime, runningTime, userLogin); 217 } 218 219 if (!"GLOBAL".equals(id)) 221 countHitSinceStart(id, type, startTime, runningTime, isOriginal, delegator); 222 223 if (id.indexOf('.') > 0) { 225 countHit(id.substring(0, id.lastIndexOf('.')), type, request, startTime, runningTime, userLogin, delegator, false); 226 } 227 228 if (isOriginal && !"GLOBAL".equals(id)) 229 countHit("GLOBAL", type, request, startTime, runningTime, userLogin, delegator, true); 230 } 231 232 static void countHitSinceStart(String id, int type, long startTime, long runningTime, boolean isOriginal, 233 GenericDelegator delegator) { 234 if (delegator == null) { 235 throw new IllegalArgumentException ("The delgator passed to countHitSinceStart cannot be null"); 236 } 237 238 ServerHitBin bin = null; 239 240 switch (type) { 242 case REQUEST: 243 bin = (ServerHitBin) requestSinceStarted.get(id); 244 break; 245 246 case EVENT: 247 bin = (ServerHitBin) eventSinceStarted.get(id); 248 break; 249 250 case VIEW: 251 bin = (ServerHitBin) viewSinceStarted.get(id); 252 break; 253 254 case ENTITY: 255 bin = (ServerHitBin) entitySinceStarted.get(id); 256 break; 257 258 case SERVICE: 259 bin = (ServerHitBin) serviceSinceStarted.get(id); 260 break; 261 } 262 263 if (bin == null) { 264 synchronized (ServerHitBin.class) { 265 switch (type) { 266 case REQUEST: 267 bin = (ServerHitBin) requestSinceStarted.get(id); 268 break; 269 270 case EVENT: 271 bin = (ServerHitBin) eventSinceStarted.get(id); 272 break; 273 274 case VIEW: 275 bin = (ServerHitBin) viewSinceStarted.get(id); 276 break; 277 278 case ENTITY: 279 bin = (ServerHitBin) entitySinceStarted.get(id); 280 break; 281 282 case SERVICE: 283 bin = (ServerHitBin) serviceSinceStarted.get(id); 284 break; 285 } 286 287 if (bin == null) { 288 bin = new ServerHitBin(id, type, false, delegator); 289 switch (type) { 290 case REQUEST: 291 requestSinceStarted.put(id, bin); 292 break; 293 294 case EVENT: 295 eventSinceStarted.put(id, bin); 296 break; 297 298 case VIEW: 299 viewSinceStarted.put(id, bin); 300 break; 301 302 case ENTITY: 303 entitySinceStarted.put(id, bin); 304 break; 305 306 case SERVICE: 307 serviceSinceStarted.put(id, bin); 308 break; 309 } 310 } 311 } 312 } 313 314 bin.addHit(startTime, runningTime); 315 316 if (isOriginal) 317 countHitSinceStart("GLOBAL", type, startTime, runningTime, false, delegator); 318 } 319 320 public static Map requestHistory = new HashMap (); 322 public static Map eventHistory = new HashMap (); 323 public static Map viewHistory = new HashMap (); 324 public static Map entityHistory = new HashMap (); 325 public static Map serviceHistory = new HashMap (); 326 327 public static Map requestSinceStarted = new HashMap (); 329 public static Map eventSinceStarted = new HashMap (); 330 public static Map viewSinceStarted = new HashMap (); 331 public static Map entitySinceStarted = new HashMap (); 332 public static Map serviceSinceStarted = new HashMap (); 333 334 GenericDelegator delegator; 335 String delegatorName; 336 String id; 337 int type; 338 boolean limitLength; 339 long startTime; 340 long endTime; 341 long numberHits; 342 long totalRunningTime; 343 long minTime; 344 long maxTime; 345 346 public ServerHitBin(String id, int type, boolean limitLength, GenericDelegator delegator) { 347 super(); 348 if (delegator == null) { 349 throw new IllegalArgumentException ("The delgator passed to countHitSinceStart cannot be null"); 350 } 351 352 this.id = id; 353 this.type = type; 354 this.limitLength = limitLength; 355 this.delegator = delegator; 356 this.delegatorName = delegator.getDelegatorName(); 357 reset(getEvenStartingTime()); 358 } 359 360 public GenericDelegator getDelegator() { 361 if (this.delegator == null) { 362 this.delegator = GenericDelegator.getGenericDelegator(this.delegatorName); 363 } 364 if (this.delegator == null) { 366 throw new IllegalArgumentException ("Could not perform stats operation: could not find delegator with name: " + this.delegatorName); 367 } 368 return this.delegator; 369 } 370 371 long getEvenStartingTime() { 372 long curTime = System.currentTimeMillis(); 374 long binLength = getNewBinLength(); 375 376 Calendar cal = Calendar.getInstance(); 378 379 cal.setTime(new Date (curTime)); 380 cal.set(Calendar.MINUTE, 0); 381 cal.set(Calendar.SECOND, 0); 382 cal.set(Calendar.MILLISECOND, 0); 383 384 while (cal.getTime().getTime() < (curTime - binLength)) { 385 cal.add(Calendar.MILLISECOND, (int) binLength); 386 } 387 388 return cal.getTime().getTime(); 389 } 390 391 static long getNewBinLength() { 392 long binLength = (long) UtilProperties.getPropertyNumber("serverstats", "stats.bin.length.millis"); 393 394 if (binLength <= 0) binLength = 1800000; 396 if (binLength > 3600000) binLength = 3600000; 398 return binLength; 399 } 400 401 void reset(long startTime) { 402 this.startTime = startTime; 403 if (limitLength) { 404 long binLength = getNewBinLength(); 405 406 this.endTime = startTime + binLength - 1; 408 } else { 409 this.endTime = 0; 410 } 411 this.numberHits = 0; 412 this.totalRunningTime = 0; 413 this.minTime = Long.MAX_VALUE; 414 this.maxTime = 0; 415 } 416 417 ServerHitBin(ServerHitBin oldBin) { 418 super(); 419 420 this.id = oldBin.id; 421 this.type = oldBin.type; 422 this.limitLength = oldBin.limitLength; 423 this.delegator = oldBin.delegator; 424 this.delegatorName = oldBin.delegatorName; 425 this.startTime = oldBin.startTime; 426 this.endTime = oldBin.endTime; 427 this.numberHits = oldBin.numberHits; 428 this.totalRunningTime = oldBin.totalRunningTime; 429 this.minTime = oldBin.minTime; 430 this.maxTime = oldBin.maxTime; 431 } 432 433 public String getId() { 434 return this.id; 435 } 436 437 public int getType() { 438 return this.type; 439 } 440 441 public String getTypeString() { 442 return typeNames[this.type]; 443 } 444 445 446 public long getStartTime() { 447 return this.startTime; 448 } 449 450 451 public long getEndTime() { 452 return limitLength ? this.endTime : System.currentTimeMillis(); 453 } 454 455 456 public String getStartTimeString() { 457 return new java.sql.Timestamp (this.getStartTime()).toString(); 459 } 460 461 462 public String getEndTimeString() { 463 return new java.sql.Timestamp (this.getEndTime()).toString(); 464 } 465 466 467 public long getBinLength() { 468 return this.getEndTime() - this.getStartTime(); 469 } 470 471 472 public double getBinLengthMinutes() { 473 return ((double) this.getBinLength()) / 60000.0; 474 } 475 476 public long getNumberHits() { 477 return this.numberHits; 478 } 479 480 public long getTotalRunningTime() { 481 return this.totalRunningTime; 482 } 483 484 public long getMinTime() { 485 return this.minTime; 486 } 487 488 public double getMinTimeSeconds() { 489 return ((double) this.minTime) / 1000.0; 490 } 491 492 public long getMaxTime() { 493 return this.maxTime; 494 } 495 496 public double getMaxTimeSeconds() { 497 return ((double) this.maxTime) / 1000.0; 498 } 499 500 public double getAvgTime() { 501 return ((double) this.totalRunningTime) / ((double) this.numberHits); 502 } 503 504 public double getAvgTimeSeconds() { 505 return this.getAvgTime() / 1000.0; 506 } 507 508 509 public double getHitsPerMinute() { 510 return ((double) this.numberHits) / ((double) this.getBinLengthMinutes()); 511 } 512 513 synchronized void addHit(long startTime, long runningTime) { 514 advanceBin(startTime + runningTime); 515 516 this.numberHits++; 517 this.totalRunningTime += runningTime; 518 if (runningTime < this.minTime) 519 this.minTime = runningTime; 520 if (runningTime > this.maxTime) 521 this.maxTime = runningTime; 522 } 523 524 synchronized void advanceBin(long toTime) { 525 while (limitLength && toTime > this.endTime) { 527 LinkedList binList = null; 528 529 switch (type) { 530 case REQUEST: 531 binList = (LinkedList ) requestHistory.get(id); 532 break; 533 534 case EVENT: 535 binList = (LinkedList ) eventHistory.get(id); 536 break; 537 538 case VIEW: 539 binList = (LinkedList ) viewHistory.get(id); 540 break; 541 542 case ENTITY: 543 binList = (LinkedList ) entityHistory.get(id); 544 break; 545 546 case SERVICE: 547 binList = (LinkedList ) serviceHistory.get(id); 548 break; 549 } 550 551 binList.removeFirst(); 554 if (this.numberHits > 0) { 555 binList.addFirst(new ServerHitBin(this)); 556 557 if (UtilProperties.propertyValueEqualsIgnoreCase("serverstats", "stats.persist." + ServerHitBin.typeIds[type] + ".bin", "true")) { 559 GenericValue serverHitBin = delegator.makeValue("ServerHitBin", null); 560 serverHitBin.set("serverHitBinId", getDelegator().getNextSeqId("ServerHitBin")); 561 serverHitBin.set("contentId", this.id); 562 serverHitBin.set("hitTypeId", ServerHitBin.typeIds[this.type]); 563 serverHitBin.set("binStartDateTime", new java.sql.Timestamp (this.startTime)); 564 serverHitBin.set("binEndDateTime", new java.sql.Timestamp (this.endTime)); 565 serverHitBin.set("numberHits", new Long (this.numberHits)); 566 serverHitBin.set("totalTimeMillis", new Long (this.totalRunningTime)); 567 serverHitBin.set("minTimeMillis", new Long (this.minTime)); 568 serverHitBin.set("maxTimeMillis", new Long (this.maxTime)); 569 try { 571 InetAddress address = InetAddress.getLocalHost(); 572 573 if (address != null) { 574 serverHitBin.set("serverIpAddress", address.getHostAddress()); 575 serverHitBin.set("serverHostName", address.getHostName()); 576 } else { 577 Debug.logError("Unable to get localhost internet address, was null", module); 578 } 579 } catch (java.net.UnknownHostException e) { 580 Debug.logError("Unable to get localhost internet address: " + e.toString(), module); 581 } 582 try { 583 serverHitBin.create(); 584 } catch (GenericEntityException e) { 585 Debug.logError(e, "Could not save ServerHitBin:", module); 586 } 587 } 588 } 589 this.reset(this.endTime + 1); 590 binList.addFirst(this); 591 } 592 } 593 594 void saveHit(HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin) { 595 if (UtilProperties.propertyValueEqualsIgnoreCase("serverstats", "stats.persist." + ServerHitBin.typeIds[type] + ".hit", "true")) { 597 if (this.type == ENTITY && this.id.indexOf("ServerHit") > 0) { 600 return; 601 } 602 603 GenericValue serverHitType = null; 605 606 try { 607 serverHitType = delegator.findByPrimaryKeyCache("ServerHitType", UtilMisc.toMap("hitTypeId", ServerHitBin.typeIds[this.type])); 608 } catch (GenericEntityException e) { 609 Debug.logError(e, module); 610 } 611 if (serverHitType == null) { 612 Debug.logWarning("The datamodel data has not been loaded; cannot find hitTypeId '" + ServerHitBin.typeIds[this.type] + " not storing ServerHit.", module); 614 return; 615 } 616 617 String visitId = VisitHandler.getVisitId(request.getSession()); 618 619 if (visitId == null || visitId.length() == 0) { 620 Debug.logWarning("Could not find a visitId, so not storing ServerHit. This is probably a configuration error. If you turn of persistance of visits you should also turn off persistence of hits.", module); 622 return; 623 } 624 625 GenericValue serverHit = delegator.makeValue("ServerHit", null); 626 627 serverHit.set("visitId", visitId); 628 serverHit.set("hitStartDateTime", new java.sql.Timestamp (startTime)); 629 serverHit.set("hitTypeId", ServerHitBin.typeIds[this.type]); 630 if (userLogin != null) { 631 serverHit.set("userLoginId", userLogin.get("userLoginId")); 632 serverHit.set("partyId", userLogin.get("partyId")); 633 } 634 serverHit.set("contentId", this.id); 635 serverHit.set("runningTimeMillis", new Long (runningTime)); 636 637 String fullRequestUrl = UtilHttp.getFullRequestUrl(request).toString(); 638 639 serverHit.set("requestUrl", fullRequestUrl.length() > 250 ? fullRequestUrl.substring(0, 250) : fullRequestUrl); 640 String referrerUrl = request.getHeader("Referer") != null ? request.getHeader("Referer") : ""; 641 642 serverHit.set("referrerUrl", referrerUrl.length() > 250 ? referrerUrl.substring(0, 250) : referrerUrl); 643 644 try { 646 InetAddress address = InetAddress.getLocalHost(); 647 648 if (address != null) { 649 serverHit.set("serverIpAddress", address.getHostAddress()); 650 serverHit.set("serverHostName", address.getHostName()); 651 } else { 652 Debug.logError("Unable to get localhost internet address, was null", module); 653 } 654 } catch (java.net.UnknownHostException e) { 655 Debug.logError("Unable to get localhost internet address: " + e.toString(), module); 656 } 657 658 try { 659 serverHit.create(); 660 } catch (GenericEntityException e) { 661 Debug.logError(e, "Could not save ServerHit:", module); 662 } 663 } 664 } 665 } 666 | Popular Tags |