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.survey.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.repository.AmetysObject;
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.plugins.survey.data.SurveyAnswerDao;
030import org.ametys.plugins.survey.repository.Survey;
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.runtime.plugin.component.PluginAware;
033import org.ametys.runtime.plugins.admin.statistics.Statistics;
034import org.ametys.runtime.plugins.admin.statistics.StatisticsNode;
035import org.ametys.runtime.plugins.admin.statistics.StatisticsProvider;
036import org.ametys.runtime.plugins.admin.statistics.StatisticsValue;
037
038/**
039 * Send statistics about survey
040 */
041public class SurveyStatisticsProvider implements StatisticsProvider, Serviceable, PluginAware
042{
043    private String _id;
044    private AmetysObjectResolver _ametysObjectResolver;
045    private SurveyAnswerDao _surveyAnswerDAO;
046
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _ametysObjectResolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
050        _surveyAnswerDAO = (SurveyAnswerDao) manager.lookup(SurveyAnswerDao.ROLE);
051    }
052    
053    public void setPluginInfo(String pluginName, String featureName, String id)
054    {
055        _id = id;
056    }
057    
058    public Statistics getStatistics()
059    {
060        List<String> surveyIds = _getSurveyIds();
061        List<Map<String, Object>> allSessionCount = _surveyAnswerDAO.getAllSessionCount();
062        
063        List<Long> counts = allSessionCount.stream()
064                                            .filter(m -> surveyIds.contains(m.get("surveyId")))
065                                            .map(m -> m.get("count(*)"))
066                                            .map(Long.class::cast)
067                                            .sorted()
068                                            .toList();
069        
070        long count = counts.stream()
071                            .mapToLong(l -> l)
072                            .sum();
073
074        
075        return new StatisticsNode(
076            _id,
077            new I18nizableText("plugin.survey", "PLUGINS_SURVEY_USAGESTATISTICS_LABEL"),
078            "ametysicon-desktop-clipboard-list",
079            null,
080            List.of(
081                new StatisticsNode(
082                    "count",
083                    new I18nizableText("plugin.survey", "PLUGINS_SURVEY_USAGESTATISTICS_COUNT_LABEL"),
084                    "ametysicon-maths-number-zero-one",
085                    surveyIds.size(),
086                    List.of(
087                        new StatisticsValue(
088                            "count",
089                            new I18nizableText("plugin.survey", "PLUGINS_SURVEY_USAGESTATISTICS_COUNT_WITH_A_VALUE_LABEL"),
090                            "ametysicon-maths-number-zero-one",
091                            counts.size()
092                        )
093                    ),
094                    true
095                ),
096                new StatisticsNode(
097                    "answers",
098                    new I18nizableText("plugin.survey", "PLUGINS_SURVEY_USAGESTATISTICS_ANSWERS_LABEL"),
099                    "ametysicon-art-pencil",
100                    count,
101                    List.of(
102                        new StatisticsValue(
103                            "max",
104                            new I18nizableText("plugin.survey", "PLUGINS_SURVEY_USAGESTATISTICS_ANSWERS_MAX_LABEL"),
105                            "ametysicon-sort51",
106                            counts.size() > 0 ? counts.get(counts.size() - 1) : 0
107                        ),
108                        new StatisticsValue(
109                            "median",
110                            new I18nizableText("plugin.survey", "PLUGINS_SURVEY_USAGESTATISTICS_ANSWERS_MEDIAN_LABEL"),
111                            "ametysicon-maths-window-symbol-x",
112                            counts.size() > 0 ? counts.get(counts.size() / 2) : 0
113                        )                        
114                    ),
115                    false
116                )
117            ),
118            true
119        );
120    }
121    
122    private List<String> _getSurveyIds()
123    {
124        List<String> surveyIds = new ArrayList<>();
125        try (AmetysObjectIterable<AmetysObject> nodes = _ametysObjectResolver.query("//element(*, ametys:survey)"))
126        {
127            for (AmetysObject ametysObject : nodes)
128            {
129                Survey survey = (Survey) ametysObject;
130                surveyIds.add(survey.getId());
131            }
132        }
133        return surveyIds;
134    }
135}