001/*
002 *  Copyright 2010 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 */
016package org.ametys.plugins.repository.provider;
017
018import java.awt.Color;
019import java.io.IOException;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.rrd4j.ConsolFun;
027import org.rrd4j.DsType;
028import org.rrd4j.core.RrdDef;
029import org.rrd4j.core.Sample;
030import org.rrd4j.graph.RrdGraphDef;
031
032import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.sample.AbstractSampleManager;
033
034/**
035 * Monitoring active JCR sessions.
036 */
037public class SessionSampleManager extends AbstractSampleManager implements Serviceable
038{
039    private JackrabbitRepository _repository;
040    
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _repository = (JackrabbitRepository) manager.lookup(AbstractRepository.ROLE);
044    } 
045    
046    @Override
047    protected void _configureDatasources(RrdDef rrdDef)
048    {
049        _registerDatasources(rrdDef, "sessions", DsType.GAUGE, 0, Double.NaN);
050    }
051
052    @Override
053    protected String _getGraphTitle()
054    {
055        return "JCR Sessions";
056    }
057
058    @Override
059    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
060    {
061        Map<String, Object> result = new HashMap<>();
062        
063        int sessions = _repository.getSessionCount();
064        sample.setValue("sessions", sessions);
065        result.put("sessions", sessions);
066        return result;
067    }
068
069    @Override
070    protected void _populateGraphDefinition(RrdGraphDef graphDef, String rrdFilePath)
071    {
072        graphDef.datasource("sessions", rrdFilePath, "sessions", ConsolFun.AVERAGE);
073        graphDef.area("sessions", new Color(148, 30, 109), "Active JCR sessions count");
074        
075        graphDef.gprint("sessions", ConsolFun.LAST, "Cur count: %.0f");
076        graphDef.gprint("sessions", ConsolFun.MAX, "Max count: %.0f");
077
078        // Do not scale units
079        graphDef.setUnitsExponent(0);
080        graphDef.setVerticalLabel("sessions count");
081    }
082}