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.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.ibatis.session.ResultHandler; 025import org.apache.ibatis.session.SqlSession; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029import org.ametys.web.cache.monitoring.Constants; 030import org.ametys.web.cache.monitoring.process.statistics.ResourceStatistics; 031import org.ametys.web.cache.monitoring.process.statistics.ResourceStatisticsFactory; 032 033/** 034 * The factory for FrontFromHTTPServerResourceStatistics 035 */ 036public final class FrontFromHTTPServerResourceStatisticsFactory implements ResourceStatisticsFactory 037{ 038 private static FrontFromHTTPServerResourceStatisticsFactory _instance = new FrontFromHTTPServerResourceStatisticsFactory(); 039 040 private static Logger __LOGGER = LoggerFactory.getLogger(FrontFromHTTPServerResourceStatisticsFactory.class); 041 042 private FrontFromHTTPServerResourceStatisticsFactory() 043 { 044 // private constructor 045 } 046 047 /** 048 * Get the unique instance 049 * @return A non null instance 050 */ 051 public static FrontFromHTTPServerResourceStatisticsFactory getInstance() 052 { 053 return _instance; 054 } 055 056 @Override 057 public List<ResourceStatistics> getResourceAccessToProcess(SqlSession sqlSession) 058 { 059 // Two queries are necessary in order to retrieve the needed information 060 // while being compatible with every supported DBMS. 061 // This raw list holds the data retrieved by the first query. The real 062 // ResourceStatistics objects will be instantiated during the processing 063 // of the 2nd query. 064 065 // Common params 066 Map<String, Object> params = new HashMap<>(); 067 params.put("tableName", Constants.SQL_TABLE_NAME_FRONT_ACCESS); 068 params.put("joinTableName", Constants.SQL_TABLE_NAME_HTTPSERVER_ACCESS); 069 070 // First query 071 String stmtId = "CacheMonitoringStatistics.getResourceAccessFrontFromHTTPServerResourceStatisticsPart1"; 072 List<Map<String, Object>> rawResourceList = sqlSession.selectList(stmtId, params); 073 074 // Second query 075 stmtId = "CacheMonitoringStatistics.getResourceAccessFrontFromHTTPServerResourceStatisticsPart2"; 076 Map<String, String> sPathMap = new HashMap<>(); 077 Map<String, String> fAmetysPathMap = new HashMap<>(); 078 079 sqlSession.select(stmtId, params, 080 // Anonymous result handler 081 (ResultHandler<Map<String, String>>) context -> 082 { 083 final Map<String, String> result = context.getResultObject(); 084 085 String hash = result.get("S_Path_Hash"); 086 String path = result.get("S_Path"); 087 088 if (!sPathMap.containsKey(hash)) 089 { 090 sPathMap.put(hash, path); 091 } 092 093 hash = result.get("F_Ametys_Path_Hash"); 094 path = result.get("F_Ametys_Path"); 095 096 if (!fAmetysPathMap.containsKey(hash)) 097 { 098 fAmetysPathMap.put(hash, path); 099 } 100 } 101 ); 102 103 // Creating ResourceStatistics objects 104 List<ResourceStatistics> resourceStatisticsList = new ArrayList<>(); 105 106 for (Map<String, Object> rawResourceData : rawResourceList) 107 { 108 String sHash = (String) rawResourceData.get("S_Path_Hash"); 109 String fAmetysHash = (String) rawResourceData.get("F_Ametys_Path_Hash"); 110 111 resourceStatisticsList.add(new FrontFromHTTPServerResourceStatistics( 112 (String) rawResourceData.get("S_Site"), 113 sHash, 114 sPathMap.get(sHash), 115 (Boolean) rawResourceData.get("S_Cache_Hit"), 116 (String) rawResourceData.get("F_Site"), 117 fAmetysHash, 118 fAmetysPathMap.get(fAmetysHash), 119 (Boolean) rawResourceData.get("F_Cacheable"), 120 (Boolean) rawResourceData.get("F_Cache_Hit_1"), 121 (Boolean) rawResourceData.get("F_Cache_Hit_2"), 122 (Integer) rawResourceData.get("increment")) 123 ); 124 } 125 126 return resourceStatisticsList; 127 } 128 129 @Override 130 public int markResourcesAsProcessed(SqlSession sqlSession) 131 { 132 // Common params 133 Map<String, Object> params = new HashMap<>(); 134 params.put("tableNameFront", Constants.SQL_TABLE_NAME_FRONT_ACCESS); 135 params.put("tableNameHttpServer", Constants.SQL_TABLE_NAME_HTTPSERVER_ACCESS); 136 137 // First query 138 String stmtId = "CacheMonitoringStatistics.markResourcesAsProcessedFrontFromHTTPServerResourceStatisticsPart1"; 139 int processed = sqlSession.update(stmtId, params); 140 141 // Second query 142 stmtId = "CacheMonitoringStatistics.markResourcesAsProcessedFrontFromHTTPServerResourceStatisticsPart2"; 143 int processed2 = sqlSession.update(stmtId, params); 144 145 if (__LOGGER.isDebugEnabled()) 146 { 147 __LOGGER.debug(String.format("Comparing processed and apacheProcessed for type 'FRONT_FROM_APACHE': %s-%s . These values should be equals.", processed, processed2)); 148 } 149 150 return processed; 151 } 152 153 @Override 154 public int purgeRawData(SqlSession sqlSession) 155 { 156 // Nothing to do here (as it is already done is FrontOnly & HTTPServerOnly factories) 157 return 0; 158 } 159}