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 * Page element cache stats objects 031 */ 032public class PageElementResourceStatistics implements ResourceStatistics 033{ 034 private final String _pageElementId; 035 private final String _pageId; 036 private final String _renderingContext; 037 private final String _workspaceJCR; 038 private final boolean _cacheable; 039 private final boolean _cacheHit; 040 private final int _newHits; 041 042 /** 043 * Creates a statistics 044 * @param pageElementId The page element id 045 * @param pageId The page id 046 * @param renderingContext The rendering context 047 * @param jcrWorkspace The jcr workspace 048 * @param cacheable The cacheable status 049 * @param cacheHit The cache hit status 050 * @param increment The number of increment 051 */ 052 public PageElementResourceStatistics(String pageElementId, String pageId, String renderingContext, String jcrWorkspace, boolean cacheable, boolean cacheHit, int increment) 053 { 054 _pageElementId = pageElementId; 055 _pageId = pageId; 056 _renderingContext = renderingContext; 057 _workspaceJCR = jcrWorkspace; 058 _cacheable = cacheable; 059 _cacheHit = cacheHit; 060 _newHits = increment; 061 } 062 063 @Override 064 public boolean statExists(SqlSession sqlSession) 065 { 066 String stmtId = "CacheMonitoringStatistics.findPageElementResourceStatistics"; 067 068 Map<String, Object> params = new HashMap<>(); 069 params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ELEMENTS_STATISTICS); 070 071 params.put("Page_Element_Id", _pageElementId); 072 params.put("Page_Id", _pageId); 073 params.put("Rendering_Context", _renderingContext); 074 params.put("Workspace_JCR", _workspaceJCR); 075 076 int count = sqlSession.selectOne(stmtId, params); 077 return count > 0; 078 } 079 080 public void createStat(SqlSession sqlSession) 081 { 082 String stmtId = "CacheMonitoringStatistics.createPageElementResourceStatistics"; 083 084 Map<String, Object> params = new HashMap<>(); 085 params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ELEMENTS_STATISTICS); 086 087 params.put("Page_Element_Id", _pageElementId); 088 params.put("Page_Id", _pageId); 089 params.put("Rendering_Context", _renderingContext); 090 params.put("Workspace_JCR", _workspaceJCR); 091 params.put("Cacheable", _cacheable); 092 params.put("Hits", _newHits); 093 params.put("Cache_Hits", _getCacheHits()); 094 095 Timestamp now = new Timestamp(System.currentTimeMillis()); 096 params.put("Created_At", now); 097 params.put("Updated_At", now); 098 099 sqlSession.insert(stmtId, params); 100 } 101 102 public void updateStat(SqlSession sqlSession) 103 { 104 String stmtId = "CacheMonitoringStatistics.updatePageElementResourceStatistics"; 105 106 Map<String, Object> params = new HashMap<>(); 107 params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ELEMENTS_STATISTICS); 108 109 params.put("Cacheable", _cacheable); 110 params.put("New_Hits", _newHits); 111 params.put("New_Cache_Hits", _getCacheHits()); 112 params.put("Updated_At", new Timestamp(System.currentTimeMillis())); 113 114 params.put("Page_Element_Id", _pageElementId); 115 params.put("Page_Id", _pageId); 116 params.put("Rendering_Context", _renderingContext); 117 params.put("Workspace_JCR", _workspaceJCR); 118 119 sqlSession.update(stmtId, params); 120 } 121 122 @Override 123 public int getHits() 124 { 125 return _newHits; 126 } 127 128 private int _getCacheHits() 129 { 130 return _cacheHit ? _newHits : 0; 131 } 132}