001/*
002 *  Copyright 2024 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.rocket.chat;
017
018import java.util.List;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.plugins.repository.AmetysObjectIterable;
025import org.ametys.runtime.config.Config;
026import org.ametys.runtime.i18n.I18nizableText;
027import org.ametys.runtime.plugin.component.PluginAware;
028import org.ametys.runtime.plugins.admin.statistics.Statistics;
029import org.ametys.runtime.plugins.admin.statistics.StatisticsNode;
030import org.ametys.runtime.plugins.admin.statistics.StatisticsProvider;
031import org.ametys.runtime.plugins.admin.statistics.StatisticsValue;
032import org.ametys.web.repository.site.Site;
033import org.ametys.web.repository.site.SiteManager;
034
035/**
036 * Send Rocket.Chat statistics
037 */
038public class RocketChatStatisticsProvider implements StatisticsProvider, Serviceable, PluginAware
039{
040    private String _id;
041    private SiteManager _siteManager;
042
043    public void setPluginInfo(String pluginName, String featureName, String id)
044    {
045        _id = id;
046    }
047    
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
051    }
052    
053    public Statistics getStatistics()
054    {
055        return new StatisticsNode(
056            _id,
057            new I18nizableText("plugin.rocket.chat", "PLUGINS_ROCKETCHAT_STATISTICS_LABEL"),
058            "ametysicon-abecedary4",
059            null,
060            List.of(
061                _configStats()
062            ),
063            true
064        );
065    }
066
067    private StatisticsNode _configStats()
068    {
069        boolean configChat = Config.getInstance().getValue("rocket.chat.active", false, false);
070        
071        int chatSites = 0;
072        
073        if (configChat)
074        {
075            try (AmetysObjectIterable<Site> sites = _siteManager.getSites())
076            {
077                for (Site site : sites)
078                {
079                    chatSites += site.getValue("rocket_chat_active", true, true) ? 1 : 0;
080                }
081            }
082        }
083        
084        return new StatisticsNode(
085            "config",
086            new I18nizableText("plugin.rocket.chat", "PLUGINS_ROCKETCHAT_STATISTICS_CONFIG_LABEL"),
087            "ametysicon-gear39",
088            configChat ? 1 : 0,
089            List.of(
090                new StatisticsNode(
091                    "chat",
092                    new I18nizableText("plugin.rocket.chat", "PLUGINS_ROCKETCHAT_STATISTICS_CONFIG_CHAT_LABEL"),
093                    "ametysicon-object-megaphone",
094                    configChat,
095                    List.of(
096                        new StatisticsValue(
097                            "sites",
098                            new I18nizableText("plugin.rocket.chat", "PLUGINS_ROCKETCHAT_STATISTICS_CONFIG_CHATSITES_LABEL"),
099                            "ametysicon-world-earth-black",
100                            chatSites
101                        )
102                    ),
103                    true
104                )
105            ),
106            false
107        );
108    }
109}