001/*
002 *  Copyright 2026 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.externaldata.data;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.runtime.plugin.component.AbstractLogEnabled;
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;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * Provide basic statistics on the plugin usage
035 */
036public class ExternalDataStatisticsProvider extends AbstractLogEnabled implements StatisticsProvider, Serviceable
037{
038    private QueryDao _queryDAO;
039    private SiteManager _siteManager;
040
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _queryDAO = (QueryDao) manager.lookup(QueryDao.ROLE);
044        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
045    }
046    
047    public Statistics getStatistics()
048    {
049        return new StatisticsNode("external-data", new I18nizableText("plugin.external-data", "PLUGINS_EXTERNAL_DATA_STATISTICS"), "ametysicon-data112", null,
050            List.of(
051                _getQueryCount()
052            ),
053            true);
054    }
055
056    private StatisticsValue _getQueryCount()
057    {
058        List<Integer> countPerSite = new ArrayList<>();
059        int totalCount = 0;
060        int maxCount = 0;
061        for (String sitename : _siteManager.getSiteNames())
062        {
063            try
064            {
065                int count = _queryDAO.getQueries(sitename).size();
066                countPerSite.add(count);
067                totalCount += count;
068                maxCount = Math.max(count, maxCount);
069            }
070            catch (DataInclusionException e)
071            {
072                getLogger().warn("An error occured while computing the external data query count for site {}", sitename, e);
073            }
074        }
075        
076        countPerSite.sort(null);
077        
078        return new StatisticsNode("queries", new I18nizableText("plugin.external-data", "PLUGINS_EXTERNAL_DATA_STATISTICS_QUERIES"), "ametysicon-system-sgbd-search", totalCount,
079                List.of(
080                    new StatisticsValue(
081                        "max",
082                        new I18nizableText("plugin.external-data", "PLUGINS_EXTERNAL_DATA_STATISTICS_MAX_LABEL"),
083                        "ametysicon-sort51",
084                        maxCount
085                    ),
086                    new StatisticsValue(
087                        "median",
088                        new I18nizableText("plugin.external-data", "PLUGINS_EXTERNAL_DATA_STATISTICS_MEDIAN_LABEL"),
089                        "ametysicon-maths-window-symbol-x",
090                        countPerSite.size() > 0 ? countPerSite.get(countPerSite.size() / 2) : 0
091                    )
092                ),
093                false
094            );
095    }
096
097}