StatisticheGiornaliere.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 org.openspcoop2.core.statistiche.Statistica;
  22. import org.openspcoop2.core.statistiche.StatisticaContenuti;
  23. import org.openspcoop2.core.statistiche.StatisticaGiornaliera;
  24. import org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;
  25. import org.openspcoop2.core.statistiche.dao.IDBStatisticaGiornalieraService;
  26. import org.openspcoop2.core.statistiche.dao.IStatisticaGiornalieraService;
  27. import org.openspcoop2.monitor.sdk.constants.StatisticType;
  28. import org.openspcoop2.monitor.sdk.exceptions.StatisticException;
  29. import org.openspcoop2.monitor.sdk.plugins.IStatisticProcessing;
  30. import org.openspcoop2.monitor.sdk.statistic.IStatistic;

  31. import java.util.Calendar;
  32. import java.util.Date;

  33. import org.slf4j.Logger;
  34. import org.openspcoop2.generic_project.exception.NotImplementedException;
  35. import org.openspcoop2.generic_project.exception.ServiceException;

  36. /**
  37.  * StatisticheGiornaliere
  38.  *
  39.  * @author Poli Andrea (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class StatisticheGiornaliere extends AbstractStatistiche {

  44.     public static StatisticheGiornaliere getInstanceForUtils(){
  45.         return new StatisticheGiornaliere();
  46.     }
  47.     StatisticheGiornaliere(){
  48.             super();
  49.     }
  50.     public StatisticheGiornaliere(
  51.             Logger logger, boolean logQuery,boolean useUnionForLatency,  
  52.             boolean generazioneStatisticheCustom, boolean analisiTransazioniCustom,
  53.             StatisticsForceIndexConfig forceIndexConfig,
  54.             org.openspcoop2.core.statistiche.dao.IServiceManager statisticheSM,
  55.             org.openspcoop2.core.transazioni.dao.IServiceManager transazioniSM,
  56.             org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager pluginsStatisticheSM,
  57.             org.openspcoop2.core.plugins.dao.IServiceManager pluginsBaseSM,
  58.             org.openspcoop2.core.commons.search.dao.IServiceManager utilsSM,
  59.             org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager pluginsTransazioniSM) {
  60.         super(logger,logQuery,useUnionForLatency,
  61.                 generazioneStatisticheCustom,analisiTransazioniCustom,
  62.                 forceIndexConfig,
  63.                 statisticheSM,transazioniSM,
  64.                 pluginsStatisticheSM,pluginsBaseSM,utilsSM,pluginsTransazioniSM);
  65.        
  66.         try {
  67.            
  68.             if(statisticheSM==null){
  69.                 throw new ServiceException("ServiceManager ["+org.openspcoop2.core.statistiche.dao.IServiceManager.class.getName()+"] non inizializzato");
  70.             }
  71.            
  72.             this.model = StatisticaGiornaliera.model().STATISTICA_BASE;
  73.             this.statisticaServiceDAO = this.statisticheSM.getStatisticaGiornalieraService();
  74.             this.statisticaServiceSearchDAO = this.statisticheSM.getStatisticaGiornalieraServiceSearch();
  75.        
  76.         } catch (ServiceException e) {
  77.             this.logger.error(e.getMessage(), e);
  78.         } catch (NotImplementedException e) {
  79.             this.logger.error(e.getMessage(), e);
  80.         }
  81.     }
  82.    
  83.     private Calendar truncDate(Date date){
  84.         Calendar cTmp = Calendar.getInstance();
  85.         cTmp.setTime(date);
  86.         cTmp.set(Calendar.HOUR_OF_DAY, 0);
  87.         cTmp.set(Calendar.MINUTE, 0);
  88.         cTmp.set(Calendar.SECOND, 0);
  89.         cTmp.set(Calendar.MILLISECOND, 0);
  90.         return cTmp;
  91.     }
  92.    
  93.     @Override
  94.     public Date truncDate(Date date,boolean print){
  95.         Calendar cTmp = truncDate(date);
  96.         if(print){
  97.             printCalendar(cTmp);
  98.         }
  99.         return cTmp.getTime();
  100.     }
  101.    
  102.     @Override
  103.     public String getIntervalloStatistica(Date date,boolean print){
  104.        
  105.         //System.out.println("INTERVALLO GIORNALIERO PER ["+date.toString()+"]");
  106.        
  107.         Calendar cTmp = truncDate(date);
  108.         StringBuilder bf = new StringBuilder();
  109.         bf.append(cTmp.get(Calendar.DAY_OF_MONTH));
  110.         bf.append("/");
  111.         bf.append((cTmp.get(Calendar.MONTH)+1));
  112.         bf.append("/");
  113.         bf.append(cTmp.get(Calendar.YEAR));
  114.        
  115.         //System.out.println("INTERVALLO GIORNALIERO PER ["+date.toString()+"]: "+bf.toString());
  116.        
  117.         return bf.toString();
  118.     }
  119.    
  120.     @Override
  121.     public Date incrementDate(Date date,boolean print){
  122.         return operation(date, print, +1);
  123.     }
  124.    
  125.     @Override
  126.     public Date decrementDate(Date date,boolean print){
  127.         return operation(date, print, -1);
  128.     }
  129.    
  130.     private Date operation(Date date,boolean print, int value){
  131.         Calendar cTmp = Calendar.getInstance();
  132.         cTmp.setTime(date);
  133.         if(print){
  134.             super.logger.debug(">>> incrementDate >>>>");
  135.             super.logger.debug("Before: ");
  136.             printCalendar(cTmp);
  137.         }
  138.         cTmp.add(Calendar.DAY_OF_YEAR, value);
  139.         if(print){
  140.             super.logger.debug("After: ");
  141.             printCalendar(cTmp);
  142.             super.logger.debug(">>> incrementDate end >>>>");
  143.         }
  144.         return cTmp.getTime();
  145.     }
  146.    
  147.     @Override
  148.     public TipoIntervalloStatistico getTipoStatistiche(){
  149.         return TipoIntervalloStatistico.STATISTICHE_GIORNALIERE; //"StatisticheGiornaliere";
  150.     }
  151.  
  152.     @Override
  153.     public void callStatisticPluginMethod(IStatisticProcessing statProcessing,
  154.             StatisticBean stat) throws StatisticException {
  155.         IStatistic statContext = new CustomStatisticsSdkGenerator(stat, StatisticType.GIORNALIERA, this);
  156.         statProcessing.createDailyStatisticData(statContext);
  157.     }


  158.     @Override
  159.     public String getStatisticPluginMethodName() throws StatisticException {
  160.         return "createDailyStatisticData";
  161.     }

  162.     @Override
  163.     protected Long insertStatistica(Statistica statistica) throws StatisticException {
  164.        
  165.         try{
  166.        
  167.             StatisticaGiornaliera stat = new StatisticaGiornaliera();
  168.             stat.setStatisticaBase(statistica);
  169.             ((IStatisticaGiornalieraService)this.statisticaServiceDAO).create(stat);
  170.             return stat.getId();
  171.            
  172.         }catch(Exception e){
  173.             throw new StatisticException(e.getMessage(),e);
  174.         }

  175.     }
  176.    
  177.     @Override
  178.     protected void updateStatistica(long idStatistica, StatisticaContenuti ... statisticaContenuti) throws StatisticException {
  179.        
  180.         try{
  181.        
  182.             StatisticaGiornaliera stat = ((IDBStatisticaGiornalieraService)this.statisticaServiceDAO).get(idStatistica);
  183.             for (int i = 0; i < statisticaContenuti.length; i++) {
  184.                 statisticaContenuti[i].setData(stat.getStatisticaBase().getData());
  185.                 stat.addStatisticaGiornalieraContenuti(statisticaContenuti[i]);
  186.             }
  187.             ((IStatisticaGiornalieraService)this.statisticaServiceDAO).update(stat);
  188.            
  189.         }catch(Exception e){
  190.             throw new StatisticException(e.getMessage(),e);
  191.         }

  192.     }
  193. }