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 * Front (without apache) cache stats objects 030 */ 031public class FrontOnlyResourceStatistics implements ResourceStatistics 032{ 033 private final String _site; 034 private final String _path; 035 private final String _hashPath; 036 private final boolean _cacheable; 037 private final boolean _cacheHit1; 038 private final boolean _cacheHit2; 039 private final int _newHits; 040 041 /** 042 * Creates a statistics 043 * @param site The site 044 * @param hashPath a consistent hash of the path. 045 * @param path The path 046 * @param cacheable The cacheable status 047 * @param cacheHit1 The cache hit 1 status 048 * @param cacheHit2 The cache hit 2 status 049 * @param newHits The number of hits 050 */ 051 public FrontOnlyResourceStatistics(String site, String hashPath, String path, boolean cacheable, boolean cacheHit1, boolean cacheHit2, int newHits) 052 { 053 _site = site; 054 _path = path; 055 _hashPath = hashPath; 056 _cacheable = cacheable; 057 _cacheHit1 = cacheHit1; 058 _cacheHit2 = cacheHit2; 059 _newHits = newHits; 060 } 061 062 public boolean statExists(SqlSession sqlSession) 063 { 064 String stmtId = "CacheMonitoringStatistics.findFrontOnlyResourceStatistics"; 065 066 Map<String, Object> params = new HashMap<>(); 067 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS); 068 069 params.put("Front_Site", _site); 070 params.put("Front_Path_Hash", _hashPath); 071 072 int count = sqlSession.selectOne(stmtId, params); 073 return count > 0; 074 } 075 076 public void createStat(SqlSession sqlSession) 077 { 078 String stmtId = "CacheMonitoringStatistics.createFrontOnlyResourceStatistics"; 079 080 Map<String, Object> params = new HashMap<>(); 081 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS); 082 083 params.put("Front_Site", _site); 084 params.put("Front_Path_Hash", _hashPath); 085 params.put("Front_Path", _path); 086 params.put("Front_Cacheable", _cacheable); 087 params.put("Front_Hits", _newHits); 088 params.put("Front_Cache_Hits_1", _getCacheHits1()); 089 params.put("Front_Cache_Hits_2", _getCacheHits2()); 090 091 Timestamp now = new Timestamp(System.currentTimeMillis()); 092 params.put("Created_At", now); 093 params.put("Updated_At", now); 094 095 sqlSession.insert(stmtId, params); 096 } 097 098 public void updateStat(SqlSession sqlSession) 099 { 100 String stmtId = "CacheMonitoringStatistics.updateFrontOnlyResourceStatistics"; 101 102 Map<String, Object> params = new HashMap<>(); 103 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS); 104 105 params.put("Front_Cacheable", _cacheable); 106 params.put("New_Front_Hits", _newHits); 107 params.put("New_Front_Cache_Hits_1", _getCacheHits1()); 108 params.put("New_Front_Cache_Hits_2", _getCacheHits2()); 109 params.put("Updated_At", new Timestamp(System.currentTimeMillis())); 110 111 params.put("Front_Site", _site); 112 params.put("Front_Path_Hash", _hashPath); 113 114 sqlSession.update(stmtId, params); 115 } 116 117 @Override 118 public int getHits() 119 { 120 return _newHits; 121 } 122 123 private int _getCacheHits1() 124 { 125 return _cacheHit1 ? _newHits : 0; 126 } 127 128 private int _getCacheHits2() 129 { 130 return _cacheHit2 ? _newHits : 0; 131 } 132}