001/* 002 * Copyright 2013 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.web.cache.monitoring.process.statistics.impl; 018 019import java.sql.Timestamp; 020import java.util.ArrayList; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.ibatis.session.ResultHandler; 026import org.apache.ibatis.session.SqlSession; 027 028import org.ametys.web.cache.monitoring.Constants; 029import org.ametys.web.cache.monitoring.process.statistics.ResourceStatistics; 030import org.ametys.web.cache.monitoring.process.statistics.ResourceStatisticsFactory; 031 032/** 033 * The factory for HTTPServerOnlyResourceStatistics 034 */ 035public final class HTTPServerOnlyResourceStatisticsFactory implements ResourceStatisticsFactory 036{ 037 private static HTTPServerOnlyResourceStatisticsFactory _instance = new HTTPServerOnlyResourceStatisticsFactory(); 038 039 private HTTPServerOnlyResourceStatisticsFactory() 040 { 041 // private constructor 042 } 043 044 /** 045 * Get the unique instance 046 * @return A non null instance 047 */ 048 public static HTTPServerOnlyResourceStatisticsFactory getInstance() 049 { 050 return _instance; 051 } 052 053 @Override 054 public List<ResourceStatistics> getResourceAccessToProcess(SqlSession sqlSession) 055 { 056 // Two queries are necessary in order to retrieve the needed information 057 // while being compatible with every supported DBMS. 058 // This raw list holds the data retrieved by the first query. The real 059 // ResourceStatistics objects will be instantiated during the processing 060 // of the 2nd query. 061 062 // Common params 063 Map<String, Object> params = new HashMap<>(); 064 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_ACCESS); 065 066 // First query. 067 String stmtId = "CacheMonitoringStatistics.getResourceAccessHTTPServerOnlyResourceStatisticsPart1"; 068 List<Map<String, Object>> rawResourceList = sqlSession.selectList(stmtId, params); 069 070 071 // Second query. 072 stmtId = "CacheMonitoringStatistics.getResourceAccessHTTPServerOnlyResourceStatisticsPart2"; 073 Map<String, String> pathMap = new HashMap<>(); 074 075 sqlSession.select(stmtId, params, 076 // Anonymous result handler 077 (ResultHandler<Map<String, String>>) context -> 078 { 079 final Map<String, String> result = context.getResultObject(); 080 081 String hash = result.get("Path_Hash"); 082 String path = result.get("Path"); 083 084 if (!pathMap.containsKey(hash)) 085 { 086 pathMap.put(hash, path); 087 } 088 } 089 ); 090 091 List<ResourceStatistics> resourceStatisticsList = new ArrayList<>(); 092 093 for (Map<String, Object> rawResourceData : rawResourceList) 094 { 095 String hash = (String) rawResourceData.get("Path_Hash"); 096 097 resourceStatisticsList.add(new HTTPServerOnlyResourceStatistics( 098 (String) rawResourceData.get("Site"), 099 hash, 100 pathMap.get(hash), 101 (Boolean) rawResourceData.get("Cache_Hit"), 102 (Integer) rawResourceData.get("increment"))); 103 } 104 105 return resourceStatisticsList; 106 } 107 108 @Override 109 public int markResourcesAsProcessed(SqlSession sqlSession) 110 { 111 String stmtId = "CacheMonitoringStatistics.markResourcesAsProcessedHTTPServerOnlyResourceStatistics"; 112 113 Map<String, Object> params = new HashMap<>(); 114 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_ACCESS); 115 116 return sqlSession.update(stmtId, params); 117 } 118 119 @Override 120 public int purgeRawData(SqlSession sqlSession) 121 { 122 String stmtId = "CacheMonitoringStatistics.purgeRawData"; 123 124 Map<String, Object> params = new HashMap<>(); 125 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_ACCESS); 126 127 // one-day window 128 Timestamp purgeThreshold = new Timestamp(System.currentTimeMillis() - 24 * 60 * 60 * 1000); 129 params.put("threshold", purgeThreshold); 130 131 return sqlSession.delete(stmtId, params); 132 } 133}