StatisticheOrarie.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.StatisticaOraria;
  24. import org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;
  25. import org.openspcoop2.core.statistiche.dao.IDBStatisticaOrariaService;
  26. import org.openspcoop2.core.statistiche.dao.IStatisticaOrariaService;
  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.  * StatisticheOrarie
  38.  *
  39.  * @author Poli Andrea (apoli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public class StatisticheOrarie extends AbstractStatistiche {

  44.     public static StatisticheOrarie getInstanceForUtils(){
  45.         return new StatisticheOrarie();
  46.     }
  47.     StatisticheOrarie(){
  48.             super();
  49.     }
  50.     public StatisticheOrarie(
  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 = StatisticaOraria.model().STATISTICA_BASE;
  73.             this.statisticaServiceDAO = this.statisticheSM.getStatisticaOrariaService();
  74.             this.statisticaServiceSearchDAO = this.statisticheSM.getStatisticaOrariaServiceSearch();
  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.     }
  84.    
  85.     private Calendar truncDate(Date date){
  86.         Calendar cTmp = Calendar.getInstance();
  87.         cTmp.setTime(date);
  88.         cTmp.set(Calendar.MINUTE, 0);
  89.         cTmp.set(Calendar.SECOND, 0);
  90.         cTmp.set(Calendar.MILLISECOND, 0);
  91.         return cTmp;
  92.     }
  93.    
  94.     @Override
  95.     public Date truncDate(Date date,boolean print){
  96.         Calendar cTmp = truncDate(date);
  97.         if(print){
  98.             printCalendar(cTmp);
  99.         }
  100.         return cTmp.getTime();
  101.     }
  102.    
  103.     @Override
  104.     public String getIntervalloStatistica(Date date,boolean print){
  105.        
  106.         //System.out.println("INTERVALLO ORARIOE PER ["+date.toString()+"]");
  107.        
  108.         Calendar cTmp = truncDate(date);
  109.         StringBuilder bf = new StringBuilder();
  110.         bf.append(cTmp.get(Calendar.DAY_OF_MONTH));
  111.         bf.append("/");
  112.         bf.append((cTmp.get(Calendar.MONTH)+1));
  113.         bf.append("/");
  114.         bf.append(cTmp.get(Calendar.YEAR));
  115.         bf.append(" ");
  116.         bf.append(cTmp.get(Calendar.HOUR_OF_DAY));
  117.         bf.append(":00.000-");
  118.         bf.append(cTmp.get(Calendar.HOUR_OF_DAY));
  119.         bf.append(":59.999-");
  120.        
  121.         //System.out.println("INTERVALLO ORARIOE PER ["+date.toString()+"]: "+bf.toString());
  122.        
  123.         return bf.toString();
  124.     }
  125.    
  126.     @Override
  127.     public Date incrementDate(Date date,boolean print){
  128.         return operation(date, print, +1);
  129.     }
  130.    
  131.     @Override
  132.     public Date decrementDate(Date date,boolean print){
  133.         return operation(date, print, -1);
  134.     }
  135.    
  136.     private Date operation(Date date,boolean print, int value){
  137.    
  138.         Calendar cTmp = Calendar.getInstance();
  139.         cTmp.setTime(date);
  140.         if(print){
  141.             super.logger.debug(">>> incrementDate >>>>");
  142.             super.logger.debug("Before: ");
  143.             printCalendar(cTmp);
  144.         }
  145.         cTmp.add(Calendar.HOUR_OF_DAY, value);
  146.         if(print){
  147.             super.logger.debug("After: ");
  148.             printCalendar(cTmp);
  149.             super.logger.debug(">>> incrementDate end >>>>");
  150.         }
  151.         return cTmp.getTime();
  152.     }
  153.    
  154.     @Override
  155.     public TipoIntervalloStatistico getTipoStatistiche(){
  156.         return TipoIntervalloStatistico.STATISTICHE_ORARIE;
  157.     }
  158.  

  159.     @Override
  160.     public void callStatisticPluginMethod(IStatisticProcessing statProcessing,
  161.             StatisticBean stat) throws StatisticException {
  162.         IStatistic statContext = new CustomStatisticsSdkGenerator(stat, StatisticType.ORARIA , this);
  163.         statProcessing.createHourlyStatisticData(statContext);
  164.     }

  165.     @Override
  166.     public String getStatisticPluginMethodName() throws StatisticException {
  167.         return "createHourlyStatisticData";
  168.     }
  169.    
  170.    
  171.     @Override
  172.     protected Long insertStatistica(Statistica statistica) throws StatisticException {
  173.        
  174.         try{
  175.        
  176.             StatisticaOraria stat = new StatisticaOraria();
  177.             stat.setStatisticaBase(statistica);
  178.             ((IStatisticaOrariaService)this.statisticaServiceDAO).create(stat);
  179.             return stat.getId();
  180.            
  181.         }catch(Exception e){
  182.             throw new StatisticException(e.getMessage(),e);
  183.         }

  184.     }
  185.    
  186.     @Override
  187.     protected void updateStatistica(long idStatistica, StatisticaContenuti ... statisticaContenuti) throws StatisticException {
  188.        
  189.         try{
  190.        
  191.             StatisticaOraria stat = ((IDBStatisticaOrariaService)this.statisticaServiceDAO).get(idStatistica);
  192.             for (int i = 0; i < statisticaContenuti.length; i++) {
  193.                 statisticaContenuti[i].setData(stat.getStatisticaBase().getData());
  194.                 stat.addStatisticaOrariaContenuti(statisticaContenuti[i]);
  195.             }
  196.             ((IStatisticaOrariaService)this.statisticaServiceDAO).update(stat);
  197.            
  198.         }catch(Exception e){
  199.             throw new StatisticException(e.getMessage(),e);
  200.         }

  201.     }
  202. }