StatisticByResource.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.monitor.engine.statistic;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import org.openspcoop2.core.commons.dao.DAOFactory;
  24. import org.openspcoop2.core.id.IDServizio;
  25. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  26. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  27. import org.openspcoop2.monitor.engine.config.BasicServiceLibrary;
  28. import org.openspcoop2.monitor.engine.config.BasicServiceLibraryReader;
  29. import org.openspcoop2.monitor.engine.config.TransactionServiceLibrary;
  30. import org.openspcoop2.monitor.engine.config.TransactionServiceLibraryReader;
  31. import org.openspcoop2.monitor.engine.config.transazioni.ConfigurazioneTransazioneRisorsaContenuto;
  32. import org.openspcoop2.monitor.engine.exceptions.RegolaDumpNotExistsException;
  33. import org.openspcoop2.monitor.sdk.condition.Context;
  34. import org.openspcoop2.monitor.sdk.condition.FilterFactory;
  35. import org.openspcoop2.monitor.sdk.condition.IStatisticFilter;
  36. import org.openspcoop2.monitor.sdk.condition.StatisticsContext;
  37. import org.openspcoop2.monitor.sdk.constants.ParameterType;
  38. import org.openspcoop2.monitor.sdk.exceptions.ParameterException;
  39. import org.openspcoop2.monitor.sdk.exceptions.SearchException;
  40. import org.openspcoop2.monitor.sdk.exceptions.StatisticException;
  41. import org.openspcoop2.monitor.sdk.parameters.Parameter;
  42. import org.openspcoop2.monitor.sdk.parameters.ParameterFactory;
  43. import org.openspcoop2.monitor.sdk.plugins.StatisticProcessing;
  44. import org.openspcoop2.monitor.sdk.statistic.IStatistic;
  45. import org.slf4j.Logger;

  46. /**
  47.  * StatisticByResource
  48.  *
  49.  * @author Poli Andrea (apoli@link.it)
  50.  * @author $Author$
  51.  * @version $Rev$, $Date$
  52.  */
  53. public class StatisticByResource extends StatisticProcessing {

  54.     // plugins ConfigurazioneStatistica
  55.     public static final String ID = "__StatisticByResource__";
  56.     public static final String LABEL = "Risorse";
  57.     public static final String DESCRIZIONE = "Informazioni Statistiche sulle Risorse associate alle Transazioni";
  58.    
  59.    
  60.     public static final String PARAM_RESOURCE = "Risorsa";
  61.    

  62.     // popolamento

  63.     @Override
  64.     public void createHourlyStatisticData(IStatistic context) throws StatisticException {
  65.         this.createStatisticData(context);
  66.     }
  67.     @Override
  68.     public void createWeeklyStatisticData(IStatistic context) throws StatisticException {
  69.         this.createStatisticData(context);
  70.     }
  71.     @Override
  72.     public void createMonthlyStatisticData(IStatistic context) throws StatisticException {
  73.         this.createStatisticData(context);
  74.     }
  75.     @Override
  76.     public void createDailyStatisticData(IStatistic context) throws StatisticException {
  77.         this.createStatisticData(context);
  78.     }
  79.     private void createStatisticData(IStatistic context) throws StatisticException {

  80.         try{

  81.             IDServizio idServizio = IDServizioFactory.getInstance().
  82.                     getIDServizioFromValues(context.getTipoServizio(), context.getServizio(),
  83.                             context.getTipoSoggettoDestinatario(), context.getSoggettoDestinatario(),
  84.                             context.getVersioneServizio());
  85.             idServizio.setAzione(context.getAzione());

  86.             boolean debug = false;
  87.             if(context instanceof CustomStatisticsSdkGenerator){
  88.                 CustomStatisticsSdkGenerator custom = (CustomStatisticsSdkGenerator) context;
  89.                 debug = custom.getAbstractStatisticheCore().isDebug();
  90.             }
  91.             List<String> listResources = this.readResources(idServizio, context.getLogger(), debug);
  92.            
  93.             /* ****  Creo filtri che mi serviranno per effettuare la campionatura in base ad ogni risorsa ***** */
  94.             for (String resourceID : listResources) {
  95.                 context.createStatistics(resourceID,resourceID); // associo come id statistica lo stesso nome dell'id risorsa
  96.             }
  97.            
  98.         } catch(Exception e) {
  99.             throw new StatisticException(e.getMessage(),e);
  100.         }
  101.        
  102.     }
  103.    



  104.    
  105.     // grafica (ricerca)
  106.    
  107.     @Override
  108.     public List<Parameter<?>> getParameters(Context context) throws SearchException, ParameterException {
  109.        
  110.         List<String> listResources = null;
  111.         try{
  112.             listResources = this.readResources(this.toIDServizio(context), context.getLogger(), false);
  113.         } catch(Exception e) {
  114.             throw new SearchException(e);
  115.         }
  116.        
  117.         List<Parameter<?>> parameters = new ArrayList<Parameter<?>>();
  118.    
  119.         Parameter<?> pRisorsa = ParameterFactory.createParameter(ParameterType.SELECT_LIST, StatisticByResource.PARAM_RESOURCE);
  120.         pRisorsa.getRendering().setLabel("Risorsa");
  121.         pRisorsa.getRendering().setSuggestion("Indicare la risorsa per cui si vuole visualizzare le informazioni statistiche");
  122.         pRisorsa.getRendering().setValues(listResources);
  123.         pRisorsa.getRendering().setRequired(true);
  124.         parameters.add(pRisorsa);
  125.        
  126.         return parameters;
  127.            
  128.     }
  129.    
  130.    

  131.     // estrazione

  132.     @Override
  133.     public IStatisticFilter createSearchFilter(StatisticsContext context)
  134.             throws StatisticException {

  135.         try{
  136.             // Recupero valore inserito dall'utente
  137.             Parameter<?> resource = context.getParameter(StatisticByResource.PARAM_RESOURCE);
  138.             String resourceValue = (String) resource.getValue();
  139.             if(resourceValue==null){
  140.                 throw new StatisticException("Parameter undefined");
  141.             }

  142.             IStatisticFilter filter = FilterFactory.newFilterStatisticRepository(context);
  143.             filter.setIdStatistic(resourceValue);

  144.             return filter;

  145.         }catch(Exception e){
  146.             throw new StatisticException(e.getMessage(),e);
  147.         }
  148.     }




  149.     // Utils
  150.    
  151.     private IDServizio toIDServizio(Context context) throws DriverRegistroServiziException{
  152.         IDServizio idServizio = IDServizioFactory.getInstance().
  153.                 getIDServizioFromValues(context.getTipoServizio(), context.getServizio(),
  154.                         context.getTipoSoggettoDestinatario(), context.getSoggettoDestinatario(),
  155.                         context.getVersioneServizio());
  156.         idServizio.setAzione(context.getAzione());
  157.         return idServizio;
  158.     }
  159.    
  160.     private List<String> readResources(IDServizio idServizio,Logger log, boolean debug) throws Exception{
  161.         TransactionServiceLibrary transactionServiceLibrary = this.getObjectReadFromDB(idServizio, log, debug);
  162.         List<ConfigurazioneTransazioneRisorsaContenuto> list = transactionServiceLibrary.mergeServiceActionTransactionLibrary_resources();
  163.         List<String> resources = new ArrayList<>();
  164.         if(list!=null){
  165.             for (ConfigurazioneTransazioneRisorsaContenuto configurazioneTransazioneRisorsaContenuto : list) {
  166.                 if(configurazioneTransazioneRisorsaContenuto.isStatEnabled()){
  167.                     resources.add(configurazioneTransazioneRisorsaContenuto.getNome());
  168.                 }
  169.             }
  170.         }
  171.         return resources;
  172.     }
  173.    
  174.     private TransactionServiceLibrary getObjectReadFromDB(IDServizio idServizio,Logger log, boolean debug) throws Exception,RegolaDumpNotExistsException{
  175.        
  176.         DAOFactory daoFactory = DAOFactory.getInstance(log);
  177.        
  178.         org.openspcoop2.core.plugins.dao.IServiceManager jdbcServiceManagerPluginsBase =
  179.                 (org.openspcoop2.core.plugins.dao.IServiceManager) daoFactory.getServiceManager(org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance());
  180.         org.openspcoop2.core.commons.search.dao.IServiceManager jdbcServiceManagerUtils =
  181.                 (org.openspcoop2.core.commons.search.dao.IServiceManager) daoFactory.getServiceManager(org.openspcoop2.core.commons.search.utils.ProjectInfo.getInstance());
  182.         org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager serviceManagerPluginsTransazioni =
  183.                 (org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager) daoFactory.getServiceManager(org.openspcoop2.monitor.engine.config.transazioni.utils.ProjectInfo.getInstance());
  184.        
  185.         BasicServiceLibraryReader basicServiceLibraryReader = new BasicServiceLibraryReader(jdbcServiceManagerPluginsBase, jdbcServiceManagerUtils, debug);
  186.                
  187.         BasicServiceLibrary basicServiceLibrary = basicServiceLibraryReader.read(idServizio, log);
  188.         if(basicServiceLibrary==null){
  189.             throw new RegolaDumpNotExistsException("Regola di dump non esiste per servizio ["+idServizio+"]");
  190.         }
  191.        
  192.         TransactionServiceLibraryReader transactionServiceLibraryReader =
  193.                 new TransactionServiceLibraryReader(serviceManagerPluginsTransazioni, debug);
  194.         TransactionServiceLibrary transactionServiceLibrary = transactionServiceLibraryReader.readConfigurazioneTransazione(basicServiceLibrary, log);
  195.         if(transactionServiceLibrary==null){
  196.             throw new RegolaDumpNotExistsException("Regola di dump (Transaction-Info) non esiste per servizio ["+idServizio+"]");
  197.         }  
  198.        
  199.         return transactionServiceLibrary;
  200.        
  201.     }


  202. }