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