📄 serverhitbin.java
字号:
int type;
boolean limitLength;
long startTime;
long endTime;
long numberHits;
long totalRunningTime;
long minTime;
long maxTime;
public ServerHitBin(String id, int type, boolean limitLength, GenericDelegator delegator) {
super();
if (delegator == null) {
throw new IllegalArgumentException("The delgator passed to countHitSinceStart cannot be null");
}
this.id = id;
this.type = type;
this.limitLength = limitLength;
this.delegator = delegator;
this.delegatorName = delegator.getDelegatorName();
reset(getEvenStartingTime());
}
public GenericDelegator getDelegator() {
if (this.delegator == null) {
this.delegator = GenericDelegator.getGenericDelegator(this.delegatorName);
}
// if still null, then we have a problem
if (this.delegator == null) {
throw new IllegalArgumentException("Could not perform stats operation: could not find delegator with name: " + this.delegatorName);
}
return this.delegator;
}
long getEvenStartingTime() {
// binLengths should be a divisable evenly into 1 hour
long curTime = System.currentTimeMillis();
long binLength = getNewBinLength();
// find the first previous millis that are even on the hour
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(curTime));
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
while (cal.getTime().getTime() < (curTime - binLength)) {
cal.add(Calendar.MILLISECOND, (int) binLength);
}
return cal.getTime().getTime();
}
static long getNewBinLength() {
long binLength = (long) UtilProperties.getPropertyNumber("serverstats", "stats.bin.length.millis");
// if no or 0 binLength specified, set to 30 minutes
if (binLength <= 0) binLength = 1800000;
// if binLength is more than an hour, set it to one hour
if (binLength > 3600000) binLength = 3600000;
return binLength;
}
void reset(long startTime) {
this.startTime = startTime;
if (limitLength) {
long binLength = getNewBinLength();
// subtract 1 millisecond to keep bin starting times even
this.endTime = startTime + binLength - 1;
} else {
this.endTime = 0;
}
this.numberHits = 0;
this.totalRunningTime = 0;
this.minTime = Long.MAX_VALUE;
this.maxTime = 0;
}
ServerHitBin(ServerHitBin oldBin) {
super();
this.id = oldBin.id;
this.type = oldBin.type;
this.limitLength = oldBin.limitLength;
this.delegator = oldBin.delegator;
this.delegatorName = oldBin.delegatorName;
this.startTime = oldBin.startTime;
this.endTime = oldBin.endTime;
this.numberHits = oldBin.numberHits;
this.totalRunningTime = oldBin.totalRunningTime;
this.minTime = oldBin.minTime;
this.maxTime = oldBin.maxTime;
}
public String getId() {
return this.id;
}
public int getType() {
return this.type;
}
public String getTypeString() {
return typeNames[this.type];
}
/** returns the startTime of the bin */
public long getStartTime() {
return this.startTime;
}
/** Returns the end time if the length of the bin is limited, otherwise returns the current system time */
public long getEndTime() {
return limitLength ? this.endTime : System.currentTimeMillis();
}
/** returns the startTime of the bin */
public String getStartTimeString() {
// using Timestamp toString because I like the way it formats it
return new java.sql.Timestamp(this.getStartTime()).toString();
}
/** Returns the end time if the length of the bin is limited, otherwise returns the current system time */
public String getEndTimeString() {
return new java.sql.Timestamp(this.getEndTime()).toString();
}
/** returns endTime - startTime */
public long getBinLength() {
return this.getEndTime() - this.getStartTime();
}
/** returns (endTime - startTime)/60000 */
public double getBinLengthMinutes() {
return ((double) this.getBinLength()) / 60000.0;
}
public long getNumberHits() {
return this.numberHits;
}
public long getTotalRunningTime() {
return this.totalRunningTime;
}
public long getMinTime() {
return this.minTime;
}
public double getMinTimeSeconds() {
return ((double) this.minTime) / 1000.0;
}
public long getMaxTime() {
return this.maxTime;
}
public double getMaxTimeSeconds() {
return ((double) this.maxTime) / 1000.0;
}
public double getAvgTime() {
return ((double) this.totalRunningTime) / ((double) this.numberHits);
}
public double getAvgTimeSeconds() {
return this.getAvgTime() / 1000.0;
}
/** return the hits per minute using the entire length of the bin as returned by getBinLengthMinutes() */
public double getHitsPerMinute() {
return ((double) this.numberHits) / ((double) this.getBinLengthMinutes());
}
synchronized void addHit(long startTime, long runningTime) {
advanceBin(startTime + runningTime);
this.numberHits++;
this.totalRunningTime += runningTime;
if (runningTime < this.minTime)
this.minTime = runningTime;
if (runningTime > this.maxTime)
this.maxTime = runningTime;
}
synchronized void advanceBin(long toTime) {
// first check to see if this bin has expired, if so save and recycle it
while (limitLength && toTime > this.endTime) {
LinkedList binList = null;
switch (type) {
case REQUEST:
binList = (LinkedList) requestHistory.get(id);
break;
case EVENT:
binList = (LinkedList) eventHistory.get(id);
break;
case VIEW:
binList = (LinkedList) viewHistory.get(id);
break;
case ENTITY:
binList = (LinkedList) entityHistory.get(id);
break;
case SERVICE:
binList = (LinkedList) serviceHistory.get(id);
break;
}
// the first in the list will be this object, remove and copy it,
// put the copy at the first of the list, then put this object back on
binList.removeFirst();
if (this.numberHits > 0) {
binList.addFirst(new ServerHitBin(this));
// persist each bin when time ends if option turned on
if (UtilProperties.propertyValueEqualsIgnoreCase("serverstats", "stats.persist." + ServerHitBin.typeIds[type] + ".bin", "true")) {
GenericValue serverHitBin = delegator.makeValue("ServerHitBin", null);
Long nextId = getDelegator().getNextSeqId("ServerHitBin");
if (nextId == null) {
Debug.logError("Not persisting ServerHitBin, could not get next seq id", module);
} else {
serverHitBin.set("serverHitBinId", nextId.toString());
serverHitBin.set("contentId", this.id);
serverHitBin.set("hitTypeId", ServerHitBin.typeIds[this.type]);
serverHitBin.set("binStartDateTime", new java.sql.Timestamp(this.startTime));
serverHitBin.set("binEndDateTime", new java.sql.Timestamp(this.endTime));
serverHitBin.set("numberHits", new Long(this.numberHits));
serverHitBin.set("totalTimeMillis", new Long(this.totalRunningTime));
serverHitBin.set("minTimeMillis", new Long(this.minTime));
serverHitBin.set("maxTimeMillis", new Long(this.maxTime));
// get localhost ip address and hostname to store
try {
InetAddress address = InetAddress.getLocalHost();
if (address != null) {
serverHitBin.set("serverIpAddress", address.getHostAddress());
serverHitBin.set("serverHostName", address.getHostName());
} else {
Debug.logError("Unable to get localhost internet address, was null", module);
}
} catch (java.net.UnknownHostException e) {
Debug.logError("Unable to get localhost internet address: " + e.toString(), module);
}
try {
serverHitBin.create();
} catch (GenericEntityException e) {
Debug.logError(e, "Could not save ServerHitBin:", module);
}
}
}
}
this.reset(this.endTime + 1);
binList.addFirst(this);
}
}
void saveHit(HttpServletRequest request, long startTime, long runningTime, GenericValue userLogin) {
// persist record of hit in ServerHit entity if option turned on
if (UtilProperties.propertyValueEqualsIgnoreCase("serverstats", "stats.persist." + ServerHitBin.typeIds[type] + ".hit", "true")) {
// if the hit type is ENTITY and the name contains "ServerHit" don't
// persist; avoids the infinite loop and a bunch of annoying data
if (this.type == ENTITY && this.id.indexOf("ServerHit") > 0) {
return;
}
// check for type data before running.
GenericValue serverHitType = null;
try {
serverHitType = delegator.findByPrimaryKeyCache("ServerHitType", UtilMisc.toMap("hitTypeId", ServerHitBin.typeIds[this.type]));
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (serverHitType == null) {
// datamodel data not loaded; not storing hit.
Debug.logWarning("The datamodel data has not been loaded; cannot find hitTypeId '" + ServerHitBin.typeIds[this.type] + " not storing ServerHit.", module);
return;
}
String visitId = VisitHandler.getVisitId(request.getSession());
if (visitId == null || visitId.length() == 0) {
// no visit info stored, so don't store the ServerHit
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);
return;
}
GenericValue serverHit = delegator.makeValue("ServerHit", null);
serverHit.set("visitId", visitId);
serverHit.set("hitStartDateTime", new java.sql.Timestamp(startTime));
serverHit.set("hitTypeId", ServerHitBin.typeIds[this.type]);
if (userLogin != null) {
serverHit.set("userLoginId", userLogin.get("userLoginId"));
serverHit.set("partyId", userLogin.get("partyId"));
}
serverHit.set("contentId", this.id);
serverHit.set("runningTimeMillis", new Long(runningTime));
String fullRequestUrl = UtilHttp.getFullRequestUrl(request).toString();
serverHit.set("requestUrl", fullRequestUrl.length() > 250 ? fullRequestUrl.substring(0, 250) : fullRequestUrl);
String referrerUrl = request.getHeader("Referer") != null ? request.getHeader("Referer") : "";
serverHit.set("referrerUrl", referrerUrl.length() > 250 ? referrerUrl.substring(0, 250) : referrerUrl);
// get localhost ip address and hostname to store
try {
InetAddress address = InetAddress.getLocalHost();
if (address != null) {
serverHit.set("serverIpAddress", address.getHostAddress());
serverHit.set("serverHostName", address.getHostName());
} else {
Debug.logError("Unable to get localhost internet address, was null", module);
}
} catch (java.net.UnknownHostException e) {
Debug.logError("Unable to get localhost internet address: " + e.toString(), module);
}
try {
serverHit.create();
} catch (GenericEntityException e) {
Debug.logError(e, "Could not save ServerHit:", module);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -