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.bpm.statistics;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.plugins.bpm.BPMWorkflowManager;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.plugin.component.PluginAware;
029import org.ametys.runtime.plugins.admin.statistics.Statistics;
030import org.ametys.runtime.plugins.admin.statistics.StatisticsNode;
031import org.ametys.runtime.plugins.admin.statistics.StatisticsProvider;
032import org.ametys.runtime.plugins.admin.statistics.StatisticsValue;
033
034/**
035 * Basic statistics for BPM 
036 */
037public class BPMStatisticsProvider implements StatisticsProvider, Serviceable, PluginAware
038{
039    private String _id;
040    private BPMWorkflowManager _bpmWorkflowManager;
041
042    public void setPluginInfo(String pluginName, String featureName, String id)
043    {
044        _id = id;
045    }
046    
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _bpmWorkflowManager = (BPMWorkflowManager) manager.lookup(BPMWorkflowManager.ROLE);
050    }
051    
052    public Statistics getStatistics()
053    {
054        long processes = 0;
055        long maxProcesses = 0;
056        List<Long> allProcesses = new ArrayList<>();
057        long count = 0;
058        
059        for (Object workflows : _bpmWorkflowManager.getWorkflows().values())
060        {
061            count++;
062            
063            if (workflows instanceof List l)
064            {
065                for (Object workflow : l)
066                {
067                    if (workflow instanceof Map m)
068                    {
069                        long process = ((Number) m.get("process")).longValue();
070                        processes += process;
071                        maxProcesses = Math.max(maxProcesses, process);
072                        allProcesses.add(process);
073                    }
074                }
075            }
076        }
077        
078        allProcesses.sort(null);
079        
080        return new StatisticsNode(
081                _id,
082                new I18nizableText("plugin.bpm", "PLUGINS_BPM_STATISTICS_LABEL"),
083                "ametysicon-workflow",
084                null,
085                List.of(
086                    new StatisticsValue(
087                        "count",
088                        new I18nizableText("plugin.bpm", "PLUGINS_BPM_STATISTICS_COUNT_LABEL"),
089                        "ametysicon-maths-abacus",
090                        count
091                    ),
092                    new StatisticsNode(
093                        "processes",
094                        new I18nizableText("plugin.bpm", "PLUGINS_BPM_STATISTICS_PROCESSES_LABEL"),
095                        "ametysicon-table28",
096                        processes,
097                        List.of(
098                            new StatisticsValue(
099                                "max",
100                                new I18nizableText("plugin.bpm", "PLUGINS_BPM_STATISTICS_PROCESSES_MAX_LABEL"),
101                                "ametysicon-sort51",
102                                maxProcesses
103                            ),
104                            new StatisticsValue(
105                                "median",
106                                new I18nizableText("plugin.bpm", "PLUGINS_BPM_STATISTICS_PROCESSES_MEDIAN_LABEL"),
107                                "ametysicon-maths-window-symbol-x",
108                                allProcesses.size() > 0 ? allProcesses.get(allProcesses.size() / 2) : 0
109                            )
110                        ),
111                        false
112                    )
113                ),
114                true
115            );
116    }
117}