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.forms.statistics;
017
018import java.util.List;
019import java.util.stream.IntStream;
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.forms.repository.Form;
026import org.ametys.plugins.repository.AmetysObjectResolver;
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 * Stats for the "new" content forms
036 */
037public class FormsStatisticsProvider implements StatisticsProvider, Serviceable, PluginAware
038{
039    
040    /** The Ametys object resolver */
041    protected AmetysObjectResolver _resolver;
042
043    private String _id;
044    
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    public void setPluginInfo(String pluginName, String featureName, String id)
051    {
052        _id = id;
053    }
054    
055    public Statistics getStatistics()
056    {
057        List<Integer> formsSubmissions = _getFormsSubmissions();
058        int submissions = formsSubmissions.stream().flatMapToInt(IntStream::of).sum();
059        
060        return new StatisticsNode(
061                _id,
062                new I18nizableText("plugin.forms", "PLUGINS_FORMS_STATISTICS_FORMS_LABEL"),
063                "ametysicon-code-html-form",
064                formsSubmissions.size(),
065                List.of(
066                    new StatisticsNode(
067                        "submissions",
068                        new I18nizableText("plugin.forms", "PLUGINS_FORMS_STATISTICS_FORMS_SUBMISSIONS_LABEL"),
069                        "ametysicon-desktop-school-tool",
070                        submissions,
071                        List.of(
072                            new StatisticsValue(
073                                "max",
074                                new I18nizableText("plugin.forms", "PLUGINS_FORMS_STATISTICS_FORMS_SUBMISSIONS_MAX_LABEL"),
075                                "ametysicon-sort51",
076                                formsSubmissions.size() > 0 ? formsSubmissions.get(formsSubmissions.size() - 1) : 0
077                            ),
078                            new StatisticsValue(
079                                "median",
080                                new I18nizableText("plugin.forms", "PLUGINS_FORMS_STATISTICS_FORMS_SUBMISSIONS_MEDIAN_LABEL"),
081                                "ametysicon-maths-window-symbol-x",
082                                formsSubmissions.size() > 0 ? formsSubmissions.get(formsSubmissions.size() / 2) : 0
083                            )
084                        ),
085                        false
086                    )
087                ),
088                true
089            );
090    }
091
092    
093    private List<Integer> _getFormsSubmissions()
094    {
095        String xpathQuery = "//element(*, ametys:form)";
096        return _resolver.query(xpathQuery)
097            .stream()
098            .filter(Form.class::isInstance)
099            .map(Form.class::cast)
100            .map(Form::getEntries)
101            .map(List::size)
102            .sorted()
103            .toList();
104    }
105}