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 from apache cache stats objects 030 */ 031public class FrontFromHTTPServerResourceStatistics implements ResourceStatistics 032{ 033 private final String _httpserverSite; 034 private final String _httpserverPath; 035 private final String _httpserverPathHash; 036 private final boolean _httpserverCacheHit; 037 038 private final String _frontSite; 039 private final String _frontPath; 040 private final String _frontPathHash; 041 private final boolean _frontCacheable; 042 private final boolean _frontCacheHit1; 043 private final boolean _frontCacheHit2; 044 045 private final int _newHits; 046 047 /** 048 * Creates a statistic 049 * @param httpserverSite The httpserver site 050 * @param httpserverPathHash a consistent hash of the httpserver path. 051 * @param httpserverPath The httpserver path 052 * @param httpserverCacheHit The httpserver cache hit 053 * @param frontSite The front site 054 * @param frontPathHash a consistent hash of the front path. 055 * @param frontPath The front path 056 * @param frontCacheable The front cacheable status 057 * @param frontCacheHit1 The front cache hit 1 status 058 * @param frontCacheHit2 The front cache hit 2 status 059 * @param newHits The hits 060 */ 061 public FrontFromHTTPServerResourceStatistics(String httpserverSite, String httpserverPathHash, String httpserverPath, boolean httpserverCacheHit, String frontSite, String frontPathHash, String frontPath, boolean frontCacheable, boolean frontCacheHit1, boolean frontCacheHit2, int newHits) 062 { 063 _httpserverSite = httpserverSite; 064 _httpserverPath = httpserverPath; 065 _httpserverPathHash = httpserverPathHash; 066 _httpserverCacheHit = httpserverCacheHit; 067 068 _frontSite = frontSite; 069 _frontPath = frontPath; 070 _frontPathHash = frontPathHash; 071 _frontCacheable = frontCacheable; 072 _frontCacheHit1 = frontCacheHit1; 073 _frontCacheHit2 = frontCacheHit2; 074 075 _newHits = newHits; 076 } 077 078 public boolean statExists(SqlSession sqlSession) 079 { 080 String stmtId = "CacheMonitoringStatistics.findFrontFromHTTPServerResourceStatistics"; 081 082 Map<String, Object> params = new HashMap<>(); 083 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS); 084 085 params.put("Server_Site", _httpserverSite); 086 params.put("Server_Path_Hash", _httpserverPathHash); 087 088 int count = sqlSession.selectOne(stmtId, params); 089 return count > 0; 090 } 091 092 public void createStat(SqlSession sqlSession) 093 { 094 String stmtId = "CacheMonitoringStatistics.createFrontFromHTTPServerResourceStatistics"; 095 096 Map<String, Object> params = new HashMap<>(); 097 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS); 098 099 params.put("Server_Site", _httpserverSite); 100 params.put("Server_Path_Hash", _httpserverPathHash); 101 params.put("Server_Path", _httpserverPath); 102 params.put("Server_Hits", _newHits); 103 params.put("Server_Cache_Hits", _getServerCacheHits()); 104 105 params.put("Front_Site", _frontSite); 106 params.put("Front_Path_Hash", _frontPathHash); 107 params.put("Front_Path", _frontPath); 108 params.put("Front_Cacheable", _frontCacheable); 109 params.put("Front_Hits", _newHits); 110 params.put("Front_Cache_Hits_1", _getFrontCacheHits1()); 111 params.put("Front_Cache_Hits_2", _getFrontCacheHits2()); 112 113 Timestamp now = new Timestamp(System.currentTimeMillis()); 114 params.put("Created_At", now); 115 params.put("Updated_At", now); 116 117 sqlSession.insert(stmtId, params); 118 } 119 120 public void updateStat(SqlSession sqlSession) 121 { 122 String stmtId = "CacheMonitoringStatistics.updateFrontFromHTTPServerResourceStatistics"; 123 124 Map<String, Object> params = new HashMap<>(); 125 params.put("tableName", Constants.SQL_TABLE_NAME_HTTPSERVER_AND_FRONT_STATISTICS); 126 127 params.put("New_Server_Hits", _newHits); 128 params.put("New_Server_Cache_Hits", _getServerCacheHits()); 129 params.put("Front_Site", _frontSite); 130 params.put("Front_Path_Hash", _frontPathHash); 131 params.put("Front_Path", _frontPath); 132 params.put("Front_Cacheable", _frontCacheable); 133 params.put("New_Front_Hits", _newHits); 134 params.put("New_Front_Cache_Hits_1", _getFrontCacheHits1()); 135 params.put("New_Front_Cache_Hits_2", _getFrontCacheHits2()); 136 params.put("Updated_At", new Timestamp(System.currentTimeMillis())); 137 138 params.put("Server_Site", _httpserverSite); 139 params.put("Server_Path_Hash", _httpserverPathHash); 140 141 sqlSession.update(stmtId, params); 142 } 143 144 @Override 145 public int getHits() 146 { 147 return _newHits; 148 } 149 150 private int _getServerCacheHits() 151 { 152 return _httpserverCacheHit ? _newHits : 0; 153 } 154 155 private int _getFrontCacheHits1() 156 { 157 return _frontCacheHit1 ? _newHits : 0; 158 } 159 160 private int _getFrontCacheHits2() 161 { 162 return _frontCacheHit2 ? _newHits : 0; 163 } 164}