001/*
002 *  Copyright 2023 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.queriesdirectory.statistics;
017
018import java.util.List;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.plugins.repository.AmetysObjectResolver;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.plugin.component.PluginAware;
027import org.ametys.runtime.plugins.admin.statistics.Statistics;
028import org.ametys.runtime.plugins.admin.statistics.StatisticsNode;
029import org.ametys.runtime.plugins.admin.statistics.StatisticsProvider;
030import org.ametys.runtime.plugins.admin.statistics.StatisticsValue;
031
032/**
033 * Stats about queries
034 */
035public class QueriesStatisticsProvider implements StatisticsProvider, Serviceable, PluginAware
036{
037    
038    /** The Ametys object resolver */
039    protected AmetysObjectResolver _resolver;
040
041    private String _id;
042    
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    public void setPluginInfo(String pluginName, String featureName, String id)
049    {
050        _id = id;
051    }
052    
053    public Statistics getStatistics()
054    {
055        long queriesCount = _getQueriesCount();
056        long foldersCount = _getFoldersCount();
057        return new StatisticsNode(
058                _id,
059                new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_STATISTICS_QUERIESDIRECTORY_LABEL"),
060                "ametysicon-code-html-form",
061                queriesCount + foldersCount,
062                List.of(
063                    new StatisticsValue(
064                        "queries",
065                        new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_STATISTICS_QUERIESDIRECTORY_QUERIES_LABEL"),
066                        "ametysicon-system-sgbd-search",
067                        queriesCount
068                    ),
069                    new StatisticsValue(
070                        "folders",
071                        new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_STATISTICS_QUERIESDIRECTORY_FOLDERS_LABEL"),
072                        "ametysicon-folder249",
073                        foldersCount
074                    )
075                ),
076                true
077            );
078    }
079
080    private long _getQueriesCount()
081    {
082        String xpathQuery = "//element(*, ametys:query)";
083        return _resolver.query(xpathQuery).getSize();
084    }
085    
086    private long _getFoldersCount()
087    {
088        String xpathQuery = "//element(*, ametys:queries)";
089        return _resolver.query(xpathQuery).getSize();
090    }
091}