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