Index: main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/ResourceAccessUtils.java =================================================================== --- main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/ResourceAccessUtils.java (revision 0) +++ main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/ResourceAccessUtils.java (revision 0) @@ -0,0 +1,52 @@ +package org.ametys.plugins.site.cache.monitoring.process.access; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import org.apache.avalon.framework.logger.Logger; +import org.apache.commons.codec.binary.Hex; + +import org.ametys.runtime.util.LoggerFactory; + +/** + * Utility methods used when dealing with ResourceAccess object. + */ +public final class ResourceAccessUtils +{ + private static final Logger __LOGGER = LoggerFactory.getLoggerFor(ResourceAccessUtils.class); + + private ResourceAccessUtils() + { + // empty private constructor + } + + /** + * Computes the SHA-256 hash for a string. + * @param str The string to be hashed. + * @return The hashed string. + */ + public static String toHash(String str) + { + MessageDigest md; + byte[] hash; + + try + { + md = MessageDigest.getInstance("SHA-256"); + hash = md.digest(str.getBytes("UTF-8")); + } + catch (NoSuchAlgorithmException e) + { + __LOGGER.error("Cannot get the message digest instance for SHA-256", e); + return null; + } + catch (UnsupportedEncodingException e) + { + __LOGGER.error("UnsupportedEncodingException UTF-8", e); + return null; + } + + return Hex.encodeHexString(hash); + } +} Index: main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/impl/HTTPServerResourceAccess.java =================================================================== --- main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/impl/HTTPServerResourceAccess.java (revision 21258) +++ main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/impl/HTTPServerResourceAccess.java (working copy) @@ -33,6 +33,7 @@ import org.ametys.plugins.site.cache.monitoring.Constants; import org.ametys.plugins.site.cache.monitoring.process.access.ResourceAccess; +import org.ametys.plugins.site.cache.monitoring.process.access.ResourceAccessUtils; import org.ametys.runtime.util.LoggerFactory; /** @@ -43,14 +44,14 @@ { /** logger */ protected static final Logger _LOGGER = LoggerFactory.getLoggerFor(HTTPServerResourceAccess.class); - + private static final Pattern __PATTERN; static { final String sPattern = "^([A-Za-z0-9@-]+) (\\S+) (\\S+) \\S+ .* \\[([^\\]]+)\\] \"([^\"]+)\" (\\d{3})/(\\d{3}) [\\d-]\\d* (-|1) \"([^\"]+)\" \"([^\"]+)\"$"; __PATTERN = Pattern.compile(sPattern); } - + private enum Field { UNIQUE_ID, @@ -67,17 +68,18 @@ REFERER, USER_AGENT } - + private final String _uniqueID; private final String _site; private final Date _date; private final String _httpMethod; private final String _httpPath; + private final String _httpPathHash; private final String _httpQueryString; private final String _originalStatusCode; private final String _returnedStatusCode; private final boolean _cacheHit; - + /** * Ctor * @param params @@ -88,14 +90,20 @@ _site = (String) params.get(Field.SITE); _date = (Date) params.get(Field.DATE); _httpMethod = (String) params.get(Field.HTTP_METHOD); + + // Calculate a hash for the path. _httpPath = (String) params.get(Field.HTTP_PATH); + String hash = ResourceAccessUtils.toHash(_httpPath); + _httpPathHash = hash; + String qs = (String) params.get(Field.HTTP_QUERY_STRING); _httpQueryString = StringUtils.defaultIfEmpty(qs, "-"); + _originalStatusCode = (String) params.get(Field.ORI_STATUS_CODE); _returnedStatusCode = (String) params.get(Field.RET_STATUS_CODE); _cacheHit = (Boolean) params.get(Field.CACHE_HIT); } - + /** * Create a new record instance * @param entry the server access log entry @@ -105,16 +113,16 @@ public static HTTPServerResourceAccess createRecord(String entry, DateFormat df) { Matcher m = __PATTERN.matcher(entry); - + if (m.matches()) { boolean success = true; - + Map params = new HashMap(); params.put(Field.UNIQUE_ID, m.group(1)); params.put(Field.SITE, m.group(2)); params.put(Field.REMOTE_HOST_NAME, m.group(3)); - + try { params.put(Field.DATE, df.parse(m.group(4))); @@ -122,7 +130,7 @@ catch (NumberFormatException e) { success = false; - + // Could happen if there is a synchronization issue with the // DateFormat instance used to parse the date. String msg = "NumberFormatException when trying to parse the resource from the httpserver access logs.\nInput catched string to be parsed '%s'"; @@ -133,35 +141,35 @@ _LOGGER.error("Error while parsing the a date from the httpserver access logs"); success = false; } - + String[] httpReq = StringUtils.split(m.group(5), ' '); params.put(Field.HTTP_METHOD, httpReq[0]); params.put(Field.HTTP_PATH, StringUtils.substringBefore(httpReq[1], "?")); params.put(Field.HTTP_QUERY_STRING, StringUtils.substringAfter(httpReq[1], "?")); params.put(Field.HTTP_PROTOCOL, httpReq[2]); - + params.put(Field.ORI_STATUS_CODE, m.group(6)); params.put(Field.RET_STATUS_CODE, m.group(7)); params.put(Field.CACHE_HIT, !"1".equals(m.group(8))); params.put(Field.REFERER, m.group(9)); params.put(Field.USER_AGENT, m.group(10)); - + if (success) { return new HTTPServerResourceAccess(params); } } - + _LOGGER.error("Access log entry does not match the pattern."); return null; } - + @Override public PreparedStatement getInsertStatement(Connection connection) throws SQLException { - return connection.prepareStatement("INSERT INTO " + Constants.SQL_TABLE_NAME_HTTPSERVER_ACCESS + " (Unique_Id, Site, Request_Date, Method, Path, Query_String, Ori_Status_Code, Ret_Status_Code, Cache_Hit, Created_At) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + return connection.prepareStatement("INSERT INTO " + Constants.SQL_TABLE_NAME_HTTPSERVER_ACCESS + " (Unique_Id, Site, Request_Date, Method, Path_Hash, Path, Query_String, Ori_Status_Code, Ret_Status_Code, Cache_Hit, Created_At) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); } - + @Override public void configureInsertStatement(PreparedStatement stmt) throws SQLException { @@ -169,14 +177,15 @@ stmt.setString(2, _site); stmt.setTimestamp(3, new Timestamp(_date.getTime())); stmt.setString(4, _httpMethod); - stmt.setString(5, _httpPath); - stmt.setString(6, _httpQueryString); - stmt.setString(7, _originalStatusCode); - stmt.setString(8, _returnedStatusCode); - stmt.setInt(9, BooleanUtils.toInteger(_cacheHit)); - stmt.setTimestamp(10, new Timestamp(System.currentTimeMillis())); + stmt.setString(5, _httpPathHash); + stmt.setString(6, _httpPath); + stmt.setString(7, _httpQueryString); + stmt.setString(8, _originalStatusCode); + stmt.setString(9, _returnedStatusCode); + stmt.setInt(10, BooleanUtils.toInteger(_cacheHit)); + stmt.setTimestamp(11, new Timestamp(System.currentTimeMillis())); } - + /** * Indicates if this record should be persisted in the database. * If it returns false, it means that this record must be filtered out and must not be inserted into the database. @@ -187,7 +196,7 @@ { return date.before(_date); } - + @Override public String toString() { Index: main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/impl/FrontResourceAccess.java =================================================================== --- main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/impl/FrontResourceAccess.java (revision 21258) +++ main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/access/impl/FrontResourceAccess.java (working copy) @@ -25,6 +25,7 @@ import org.ametys.plugins.site.cache.monitoring.Constants; import org.ametys.plugins.site.cache.monitoring.process.access.ResourceAccess; +import org.ametys.plugins.site.cache.monitoring.process.access.ResourceAccessUtils; /** * Front resource access. Represent an access to a resource from the Front-office. @@ -35,10 +36,11 @@ private final String _internalUuid; private final String _site; private final String _path; + private final String _pathHash; private boolean _cacheable; private boolean _cacheHit1; private boolean _cacheHit2; - + /** * Ctor * @param uniqueID @@ -51,28 +53,32 @@ _uniqueID = StringUtils.defaultIfEmpty(uniqueID, "-"); _internalUuid = StringUtils.defaultIfEmpty(internalUuid, "-"); _site = StringUtils.defaultIfEmpty(site, "-"); - _path = StringUtils.substringBefore(path, "?"); + + // Calculate a hash for the path. + _path = StringUtils.defaultIfEmpty(StringUtils.substringBefore(path, "?"), "-"); + _pathHash = ResourceAccessUtils.toHash(_path); } - + @Override public PreparedStatement getInsertStatement(Connection connection) throws SQLException { - return connection.prepareStatement("INSERT INTO " + Constants.SQL_TABLE_NAME_FRONT_ACCESS + " (Unique_Id, Internal_Uuid, Site, Ametys_Path, Cacheable, Cache_Hit_1, Cache_Hit_2, Created_At) values (?, ?, ?, ?, ?, ?, ?, ?)"); + return connection.prepareStatement("INSERT INTO " + Constants.SQL_TABLE_NAME_FRONT_ACCESS + " (Unique_Id, Internal_Uuid, Site, Ametys_Path_Hash, Ametys_Path, Cacheable, Cache_Hit_1, Cache_Hit_2, Created_At) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"); } - + @Override public void configureInsertStatement(PreparedStatement stmt) throws SQLException { stmt.setString(1, _uniqueID); stmt.setString(2, _internalUuid); stmt.setString(3, _site); - stmt.setString(4, _path); - stmt.setInt(5, BooleanUtils.toInteger(_cacheable)); - stmt.setInt(6, BooleanUtils.toInteger(_cacheHit1)); - stmt.setInt(7, BooleanUtils.toInteger(_cacheHit2)); - stmt.setTimestamp(8, new Timestamp(System.currentTimeMillis())); + stmt.setString(4, _pathHash); + stmt.setString(5, _path); + stmt.setInt(6, BooleanUtils.toInteger(_cacheable)); + stmt.setInt(7, BooleanUtils.toInteger(_cacheHit1)); + stmt.setInt(8, BooleanUtils.toInteger(_cacheHit2)); + stmt.setTimestamp(9, new Timestamp(System.currentTimeMillis())); } - + /** * Set the resource as cacheable or not. * @param cacheable @@ -81,7 +87,7 @@ { _cacheable = cacheable; } - + /** * Set the first cache hit to true/false * @param hit @@ -90,7 +96,7 @@ { _cacheHit1 = hit; } - + /** * Set the second cache hit to true/false * @param hit @@ -99,6 +105,6 @@ { _cacheHit2 = hit; } - + } Index: main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/FrontCacheMonitoringScheduler.java =================================================================== --- main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/FrontCacheMonitoringScheduler.java (revision 21258) +++ main/plugin-site/src/org/ametys/plugins/site/cache/monitoring/process/FrontCacheMonitoringScheduler.java (working copy) @@ -92,7 +92,8 @@ calendar.set(Calendar.MILLISECOND, 0); // TODO uncomment after testing - _timer.scheduleAtFixedRate(this, calendar.getTime(), 60 * 60 * 1000); +// _timer.scheduleAtFixedRate(this, calendar.getTime(), 60 * 60 * 1000); + _timer.scheduleAtFixedRate(this, 15 * 1000, 60 * 1000); if (_logger.isInfoEnabled()) {