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.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.ibatis.session.SqlSession;
025
026import org.ametys.web.cache.monitoring.Constants;
027import org.ametys.web.cache.monitoring.process.statistics.ResourceStatistics;
028import org.ametys.web.cache.monitoring.process.statistics.ResourceStatisticsFactory;
029
030/**
031 * The factory for PageElementResourceStatistics
032 */
033public final class PageElementResourceStatisticsFactory implements ResourceStatisticsFactory
034{
035    private static PageElementResourceStatisticsFactory _instance = new PageElementResourceStatisticsFactory();
036    
037    private PageElementResourceStatisticsFactory()
038    {
039        // private constructor
040    }
041    
042    /**
043     * Get the unique instance
044     * @return A non null instance
045     */
046    public static PageElementResourceStatisticsFactory getInstance()
047    {
048        return _instance;
049    }
050    
051    @Override
052    public List<ResourceStatistics> getResourceAccessToProcess(SqlSession session)
053    {
054        String stmtId = "CacheMonitoringStatistics.getResourceAccessPageElementResourceStatistics";
055        
056        Map<String, Object> params = new HashMap<>();
057        params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ELEMENTS_ACCESS);
058        
059        return session.selectList(stmtId, params);
060    }
061    
062    @Override
063    public int markResourcesAsProcessed(SqlSession sqlSession)
064    {
065        String stmtId = "CacheMonitoringStatistics.markResourcesAsProcessed";
066        
067        Map<String, Object> params = new HashMap<>();
068        params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ELEMENTS_ACCESS);
069        
070        return sqlSession.update(stmtId, params);
071    }
072    
073    @Override
074    public int purgeRawData(SqlSession sqlSession)
075    {
076        String stmtId = "CacheMonitoringStatistics.purgeRawData";
077        
078        Map<String, Object> params = new HashMap<>();
079        params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ELEMENTS_ACCESS);
080        
081        // one-day window
082        Timestamp purgeThreshold = new Timestamp(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
083        params.put("threshold", purgeThreshold);
084        
085        return sqlSession.delete(stmtId, params);
086    }
087}