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.webanalytics.matomo;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.commons.lang.StringUtils;
030
031import org.ametys.core.cocoon.JSonReader;
032import org.ametys.web.WebHelper;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.repository.site.SiteManager;
035
036/**
037 * Get the page data from matomo
038 */
039public class GetPageDataAction extends ServiceableAction
040{
041    /** The Matomo data helper */
042    protected MatomoDataHelper _matomoHelper;
043    
044    /** The site manager */
045    protected SiteManager _siteManager;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        _matomoHelper = (MatomoDataHelper) smanager.lookup(MatomoDataHelper.ROLE);
052        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
053    }
054    
055    @Override
056    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
057    {
058        Map<String, Object> result = new HashMap<>();
059        
060        Request request = ObjectModelHelper.getRequest(objectModel);
061        String pageUrl = request.getParameter("pageUrl");
062        String siteName = WebHelper.getSiteName(request);
063        if (StringUtils.isNotBlank(siteName))
064        {
065            Site site = _siteManager.getSite(siteName);
066            result.put("nbLastDays", _matomoHelper.getMatomoSiteNbLastDays(site));
067            
068            try
069            {
070                Integer nbVisitsFromLastDays = _matomoHelper.getNbVisitsFromLastDays(site, pageUrl);
071                Integer nbTotalVisits = _matomoHelper.getNbTotalVisits(site, pageUrl);
072                
073                result.put("nbVisits", nbTotalVisits);
074                result.put("nbVisitsFromLastDays", nbVisitsFromLastDays);
075            }
076            catch (MatomoException e) 
077            {
078                getLogger().error("An error occurred getting matomo data from site '" + site.getTitle() + "' and page URL '" + pageUrl + "'", e);
079            }
080        }
081        
082        request.setAttribute(JSonReader.OBJECT_TO_READ, result);
083        return EMPTY_MAP;
084    }
085}