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.Map;
022
023import org.apache.ibatis.session.SqlSession;
024
025import org.ametys.web.cache.monitoring.Constants;
026import org.ametys.web.cache.monitoring.process.statistics.ResourceStatistics;
027
028
029/**
030 * Apache cache stats objects
031 */
032public class HTTPServerOnlyResourceStatistics implements ResourceStatistics
033{
034    private final String _site;
035    private final String _path;
036    private final String _hashPath;
037    private final boolean _cacheHit;
038    private final int _newHits;
039    
040    /**
041     * Creates a statistics
042     * @param site The site
043     * @param hashPath a consistent hash of the path.
044     * @param path The path
045     * @param cacheHit The cache hit status
046     * @param newHits The number of hits
047     */
048    public HTTPServerOnlyResourceStatistics(String site, String hashPath, String path, boolean cacheHit, int newHits)
049    {
050        _site = site;
051        _path = path;
052        _hashPath = hashPath;
053        _cacheHit = cacheHit;
054        _newHits = newHits;
055    }
056    
057    public boolean statExists(SqlSession sqlSession)
058    {
059        String stmtId = "CacheMonitoringStatistics.findHTTPServerOnlyResourceStatistics";
060        
061        Map<String, Object> params = new HashMap<>();
062        params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS);
063        
064        params.put("Server_Site", _site);
065        params.put("Server_Path_Hash", _hashPath);
066        
067        int count = sqlSession.selectOne(stmtId, params);
068        return count > 0;
069    }
070    
071    public void createStat(SqlSession sqlSession)
072    {
073        String stmtId = "CacheMonitoringStatistics.createHTTPServerOnlyResourceStatistics";
074        
075        Map<String, Object> params = new HashMap<>();
076        params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS);
077        
078        params.put("Server_Site", _site);
079        params.put("Server_Path_Hash", _hashPath);
080        params.put("Server_Path", _path);
081        params.put("Server_Hits", _newHits);
082        params.put("Server_Cache_Hits", _getCacheHits());
083        
084        Timestamp now = new Timestamp(System.currentTimeMillis());
085        params.put("Created_At", now);
086        params.put("Updated_At", now);
087        
088        sqlSession.insert(stmtId, params);
089    }
090    
091    public void updateStat(SqlSession sqlSession)
092    {
093        String stmtId = "CacheMonitoringStatistics.updateHTTPServerOnlyResourceStatistics";
094        
095        Map<String, Object> params = new HashMap<>();
096        params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS);
097        
098        params.put("New_Server_Hits", _newHits);
099        params.put("New_Server_Cache_Hits", _getCacheHits());
100        params.put("Updated_At", new Timestamp(System.currentTimeMillis()));
101      
102        params.put("Server_Site", _site);
103        params.put("Server_Path_Hash", _hashPath);
104        
105        sqlSession.update(stmtId, params);
106    }
107    
108    @Override
109    public int getHits()
110    {
111        return _newHits;
112    }
113    
114    private int _getCacheHits()
115    {
116        return _cacheHit ? _newHits : 0;
117    }
118}