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 PageResourceStatistics 034 */ 035public final class PageResourceStatisticsFactory implements ResourceStatisticsFactory 036{ 037 private static PageResourceStatisticsFactory _instance = new PageResourceStatisticsFactory(); 038 039 private PageResourceStatisticsFactory() 040 { 041 // private constructor 042 } 043 044 /** 045 * Get the unique instance 046 * @return A non null instance 047 */ 048 public static PageResourceStatisticsFactory 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_PAGE_ACCESS); 065 066 // First query 067 String stmtId = "CacheMonitoringStatistics.getResourceAccessPageResourceStatisticsPart1"; 068 List<Map<String, Object>> rawResourceList = sqlSession.selectList(stmtId, params); 069 070 // Second query 071 stmtId = "CacheMonitoringStatistics.getResourceAccessPageResourceStatisticsPart2"; 072 Map<String, String> pathMap = new HashMap<>(); 073 Map<String, String> hashPathMap = 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 id = result.get("Page_Id"); 082 String path = result.get("Page_Path"); 083 String hash = result.get("Page_Path_Hash"); 084 085 if (!pathMap.containsKey(id)) 086 { 087 pathMap.put(id, path); 088 hashPathMap.put(id, hash); 089 } 090 } 091 ); 092 093 List<ResourceStatistics> resourceStatisticsList = new ArrayList<>(); 094 095 // Creating ResourceStatistics objects 096 for (Map<String, Object> rawResourceData : rawResourceList) 097 { 098 String id = (String) rawResourceData.get("Page_Id"); 099 100 resourceStatisticsList.add(new PageResourceStatistics( 101 id, 102 hashPathMap.get(id), 103 pathMap.get(id), 104 (String) rawResourceData.get("Rendering_Context"), 105 (String) rawResourceData.get("Workspace_JCR"), 106 (Boolean) rawResourceData.get("Cacheable"), 107 (Integer) rawResourceData.get("increment"))); 108 } 109 110 return resourceStatisticsList; 111 } 112 113 @Override 114 public int markResourcesAsProcessed(SqlSession sqlSession) 115 { 116 String stmtId = "CacheMonitoringStatistics.markResourcesAsProcessed"; 117 118 Map<String, Object> params = new HashMap<>(); 119 params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ACCESS); 120 121 return sqlSession.update(stmtId, params); 122 } 123 124 @Override 125 public int purgeRawData(SqlSession sqlSession) 126 { 127 String stmtId = "CacheMonitoringStatistics.purgeRawData"; 128 129 Map<String, Object> params = new HashMap<>(); 130 params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ACCESS); 131 132 // one-day window 133 Timestamp purgeThreshold = new Timestamp(System.currentTimeMillis() - 24 * 60 * 60 * 1000); 134 params.put("threshold", purgeThreshold); 135 136 return sqlSession.delete(stmtId, params); 137 } 138}