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.explorer.statistics;
017
018import java.util.HashMap;
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.plugins.explorer.ExplorerNode;
026import org.ametys.plugins.explorer.resources.Resource;
027import org.ametys.plugins.explorer.resources.ResourceCollection;
028import org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.AmetysObjectIterable;
031import org.ametys.plugins.repository.TraversableAmetysObject;
032import org.ametys.runtime.i18n.I18nizableText;
033import org.ametys.runtime.plugin.component.PluginAware;
034import org.ametys.runtime.plugins.admin.statistics.Statistics;
035import org.ametys.runtime.plugins.admin.statistics.StatisticsNode;
036import org.ametys.runtime.plugins.admin.statistics.StatisticsProvider;
037import org.ametys.runtime.plugins.admin.statistics.StatisticsValue;
038
039/**
040 * Explorer statistics
041 */
042public class ExplorerStatisticsProvider implements StatisticsProvider, Serviceable, PluginAware
043{
044    private static String __FOLDER_COUNT = "FOLDERCOUNT";
045    private static String __RESOURCE_COUNT = "RESOURCECOUNT";
046    private static String __TOTAL_SIZE = "TOTALSIZE";
047    
048    private String _id;
049    private ExplorerResourcesDAO _explorerResourcesDAO;
050
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _explorerResourcesDAO = (ExplorerResourcesDAO) manager.lookup(ExplorerResourcesDAO.ROLE);
054    }
055    
056    public void setPluginInfo(String pluginName, String featureName, String id)
057    {
058        _id = id;
059    }
060    
061    public Statistics getStatistics()
062    {
063        HashMap<String, Long> values = new HashMap<>();
064        values.put(__FOLDER_COUNT, 0L);
065        values.put(__RESOURCE_COUNT, 0L);
066        values.put(__TOTAL_SIZE, 0L);
067        
068        for (ExplorerNode explorerNode : _explorerResourcesDAO.getResourcesRootNodes())
069        {
070            _processResources((ResourceCollection) explorerNode, values);
071        }
072        
073        return new StatisticsNode(
074            _id,
075            new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_STATISTICS_LABEL"),
076            "ametysicon-folder249",
077            values.get(__TOTAL_SIZE),
078            List.of(
079                new StatisticsValue(
080                    "folders",
081                    new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_STATISTICS_FOLDERS_LABEL"),
082                    "ametysicon-folder250",
083                    values.get(__FOLDER_COUNT)
084                ),
085                new StatisticsValue(
086                    "files",
087                    new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_STATISTICS_FILES_LABEL"),
088                    "ametysicon-document77",
089                    values.get(__RESOURCE_COUNT)
090                )                
091            ),
092            true
093        );
094    }
095    
096    private void _processResources(TraversableAmetysObject resourceContainer, HashMap<String, Long> values)
097    {
098        AmetysObjectIterable<? extends AmetysObject> objects = resourceContainer.getChildren();
099        
100        for (AmetysObject object : objects)
101        {
102            // Traverse the child nodes if depth < 0 (generate all) or depth > 0 (we're not in the last level).
103            if (object instanceof ResourceCollection)
104            {
105                values.put(__FOLDER_COUNT, values.get(__FOLDER_COUNT) + 1);
106                
107                _processResources((ResourceCollection) object, values);
108            }
109            else if (object instanceof Resource)
110            {
111                Resource resource = (Resource) object;
112                
113                values.put(__RESOURCE_COUNT, values.get(__RESOURCE_COUNT) + 1);
114                values.put(__TOTAL_SIZE, values.get(__TOTAL_SIZE) + resource.getLength());
115            }
116        }
117    }
118}