TimerStatisticheLib.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.pdd.timers;


  21. import java.sql.Connection;

  22. import org.openspcoop2.core.commons.dao.DAOFactory;
  23. import org.openspcoop2.core.commons.dao.DAOFactoryProperties;
  24. import org.openspcoop2.core.config.driver.IDriverConfigurazioneGet;
  25. import org.openspcoop2.core.config.driver.db.DriverConfigurazioneDB;
  26. import org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;
  27. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  28. import org.openspcoop2.monitor.engine.statistic.StatisticsConfig;
  29. import org.openspcoop2.monitor.engine.statistic.StatisticsLibrary;
  30. import org.openspcoop2.pdd.config.ConfigurazionePdDReader;
  31. import org.openspcoop2.pdd.config.DBStatisticheManager;
  32. import org.openspcoop2.pdd.config.DBTransazioniManager;
  33. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  34. import org.openspcoop2.pdd.config.Resource;
  35. import org.openspcoop2.pdd.core.CostantiPdD;
  36. import org.openspcoop2.pdd.core.GestoreMessaggi;
  37. import org.openspcoop2.pdd.logger.MsgDiagnostico;
  38. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  39. import org.openspcoop2.pdd.services.OpenSPCoop2Startup;
  40. import org.openspcoop2.utils.TipiDatabase;
  41. import org.openspcoop2.utils.Utilities;
  42. import org.openspcoop2.utils.date.DateManager;
  43. import org.openspcoop2.utils.id.serial.InfoStatistics;
  44. import org.openspcoop2.utils.semaphore.Semaphore;
  45. import org.openspcoop2.utils.semaphore.SemaphoreConfiguration;
  46. import org.openspcoop2.utils.semaphore.SemaphoreMapping;
  47. import org.slf4j.Logger;


  48. /**    
  49.  * TimerStatisticheLib
  50.  *
  51.  * @author Poli Andrea (poli@link.it)
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  */
  55. public class TimerStatisticheLib {

  56.     private static TimerState STATE_STATISTICHE_ORARIE = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  57.     public static TimerState getSTATE_STATISTICHE_ORARIE() {
  58.         return STATE_STATISTICHE_ORARIE;
  59.     }
  60.     public static void setSTATE_STATISTICHE_ORARIE(TimerState sTATE_STATISTICHE_ORARIE) {
  61.         STATE_STATISTICHE_ORARIE = sTATE_STATISTICHE_ORARIE;
  62.     }

  63.     private static TimerState STATE_STATISTICHE_GIORNALIERE = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  64.     public static TimerState getSTATE_STATISTICHE_GIORNALIERE() {
  65.         return STATE_STATISTICHE_GIORNALIERE;
  66.     }
  67.     public static void setSTATE_STATISTICHE_GIORNALIERE(TimerState sTATE_STATISTICHE_GIORNALIERE) {
  68.         STATE_STATISTICHE_GIORNALIERE = sTATE_STATISTICHE_GIORNALIERE;
  69.     }

  70.     private static TimerState STATE_STATISTICHE_SETTIMANALI = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  71.     public static TimerState getSTATE_STATISTICHE_SETTIMANALI() {
  72.         return STATE_STATISTICHE_SETTIMANALI;
  73.     }
  74.     public static void setSTATE_STATISTICHE_SETTIMANALI(TimerState sTATE_STATISTICHE_SETTIMANALI) {
  75.         STATE_STATISTICHE_SETTIMANALI = sTATE_STATISTICHE_SETTIMANALI;
  76.     }

  77.     private static TimerState STATE_STATISTICHE_MENSILI = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  78.     public static TimerState getSTATE_STATISTICHE_MENSILI() {
  79.         return STATE_STATISTICHE_MENSILI;
  80.     }
  81.     public static void setSTATE_STATISTICHE_MENSILI(TimerState sTATE_STATISTICHE_MENSILI) {
  82.         STATE_STATISTICHE_MENSILI = sTATE_STATISTICHE_MENSILI;
  83.     }

  84.     /** Logger utilizzato per debug. */
  85.     private Logger logCore = null;
  86.     private Logger logSql = null;
  87.     private Logger logTimer = null;
  88.     private MsgDiagnostico msgDiag = null;

  89.     /** OpenSPCoop2Properties */
  90.     private OpenSPCoop2Properties op2Properties = null;
  91.    
  92.     /** Indicazione se devono essere generate le statistiche custom */
  93.     private boolean generazioneStatisticheCustom = false;
  94.     private boolean analisiTransazioniCustom = false;
  95.    
  96.     /** Indicazione se deve essere effettuato il log delle query */
  97.     private boolean debug = false;  
  98.    
  99.     /** Indicazione se deve essere usata la union per calcolare i tempi di latenza */
  100.     private boolean useUnionForLatency = false;
  101.    
  102.     /** Intervallo di attesa prima di passare al prossimo intervallo */
  103.     private long waitMsBeforeNextInterval = -1;
  104.    
  105.     /** Attesa che tutte le transazioni siano state consegnate con successo prima di passare al prossimo intervallo */
  106.     private boolean waitStatiInConsegna = false;
  107.    
  108.     /** Tipologie di statistiche */
  109.     private TipoIntervalloStatistico tipoStatistica;
  110.     private boolean statisticheOrarie = false;
  111.     private boolean statisticheGiornaliere = false;
  112.     private boolean statisticheSettimanali = false;
  113.     private boolean statisticheMensili = false;
  114.    
  115.     /** Tipologie di statistiche: gestione ultimo intervallo */
  116.     private boolean statisticheOrarie_gestioneUltimoIntervallo = false;
  117.     private boolean statisticheGiornaliere_gestioneUltimoIntervallo = false;
  118.     private boolean statisticheSettimanali_gestioneUltimoIntervallo = false;
  119.     private boolean statisticheMensili_gestioneUltimoIntervallo = false;
  120.    
  121.     /** Database */
  122. //  private DataSource ds = null;
  123. //  private String datasource = null;
  124.     private String tipoDatabase = null; //tipoDatabase
  125.     private DAOFactory daoFactory = null;
  126.     private Logger daoFactoryLogger = null;
  127.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesTransazioni = null;
  128.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesStatistiche = null;
  129.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesPluginsStatistiche = null;
  130.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesPluginsBase = null;
  131.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesUtils = null;
  132.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesPluginsTransazioni = null;

  133.     /** StatisticsConfig */
  134.     private StatisticsConfig statisticsConfig;
  135.    
  136.     /** Timer */
  137.     private TimerLock timerLock = null;

  138.     /** Semaforo */
  139.     private Semaphore semaphore = null;
  140.     private InfoStatistics semaphore_statistics;

  141.    
  142.    
  143.     /** Costruttore */
  144.     public TimerStatisticheLib(TipoIntervalloStatistico tipoStatistica, MsgDiagnostico msgDiag,Logger logTimer,OpenSPCoop2Properties p) throws Exception{
  145.    
  146.         this.msgDiag = msgDiag;
  147.         this.op2Properties = p;
  148.        
  149.         this.debug = this.op2Properties.isStatisticheGenerazioneDebug();
  150.        
  151.         this.logCore = OpenSPCoop2Logger.getLoggerOpenSPCoopStatistiche(tipoStatistica, this.debug);
  152.         this.logSql = OpenSPCoop2Logger.getLoggerOpenSPCoopStatisticheSql(tipoStatistica, this.debug);
  153.         this.logTimer = logTimer;
  154.        
  155.         this.useUnionForLatency = this.op2Properties.isStatisticheGenerazioneUseUnionForLatency();
  156.         this.generazioneStatisticheCustom = this.op2Properties.isStatisticheGenerazioneCustomEnabled();
  157.         this.analisiTransazioniCustom = this.op2Properties.isStatisticheGenerazioneCustomSdkEnabled();
  158.        
  159.         this.tipoStatistica = tipoStatistica;
  160.        
  161.         switch (this.tipoStatistica) {
  162.         case STATISTICHE_ORARIE:
  163.             this.statisticheOrarie = this.op2Properties.isStatisticheGenerazioneBaseOrariaEnabled();
  164.             this.statisticheOrarie_gestioneUltimoIntervallo = this.op2Properties.isStatisticheGenerazioneBaseOrariaEnabledUltimaOra();
  165.             break;
  166.         case STATISTICHE_GIORNALIERE:
  167.             this.statisticheGiornaliere = this.op2Properties.isStatisticheGenerazioneBaseGiornalieraEnabled();
  168.             this.statisticheGiornaliere_gestioneUltimoIntervallo = this.op2Properties.isStatisticheGenerazioneBaseGiornalieraEnabledUltimoGiorno();
  169.             break;
  170.         case STATISTICHE_SETTIMANALI:
  171.             this.statisticheSettimanali = this.op2Properties.isStatisticheGenerazioneBaseSettimanaleEnabled();
  172.             this.statisticheSettimanali_gestioneUltimoIntervallo = this.op2Properties.isStatisticheGenerazioneBaseSettimanaleEnabledUltimaSettimana();
  173.             break;
  174.         case STATISTICHE_MENSILI:
  175.             this.statisticheMensili = this.op2Properties.isStatisticheGenerazioneBaseMensileEnabled();
  176.             this.statisticheMensili_gestioneUltimoIntervallo = this.op2Properties.isStatisticheGenerazioneBaseMensileEnabledUltimoMese();
  177.             break;
  178.         default:
  179.             break;
  180.         }
  181.        
  182.         this.waitStatiInConsegna = this.op2Properties.isStatisticheGenerazioneAttendiCompletamentoTransazioniInFasiIntermedie();
  183.         this.waitMsBeforeNextInterval = this.op2Properties.getStatisticheGenerazioneTradeOffMs();
  184.                
  185.         try{
  186.            
  187.             // se avviato il thread le statistiche devono essere nella base dati delle transazioni (altrimenti si usa il batch)
  188.             this.tipoDatabase = this.op2Properties.getDatabaseType();
  189.             if(this.tipoDatabase==null){
  190.                 throw new Exception("Tipo Database non definito");
  191.             }
  192.            
  193.             String tipoDatabaseTransazioni = DBTransazioniManager.getInstance().getTipoDatabase();
  194.             if(tipoDatabaseTransazioni==null){
  195.                 throw new Exception("Tipo Database Transazioni non definito");
  196.             }

  197.             // Inizializzazione datasource
  198. //          GestoreJNDI jndi = new GestoreJNDI();
  199. //          this.ds = (DataSource) jndi.lookup(this.datasource);
  200.        
  201.             // DAOFactory
  202.             DAOFactoryProperties daoFactoryProperties = null;
  203.             this.daoFactoryLogger = this.logSql;
  204.             this.daoFactory = DAOFactory.getInstance(this.daoFactoryLogger);
  205.             daoFactoryProperties = DAOFactoryProperties.getInstance(this.daoFactoryLogger);
  206.            
  207.             this.daoFactoryServiceManagerPropertiesTransazioni =
  208.                     daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance());
  209.             this.daoFactoryServiceManagerPropertiesTransazioni.setShowSql(this.debug);  
  210.             this.daoFactoryServiceManagerPropertiesTransazioni.setDatabaseType(tipoDatabaseTransazioni);
  211.            
  212.             this.daoFactoryServiceManagerPropertiesStatistiche =
  213.                     daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.statistiche.utils.ProjectInfo.getInstance());
  214.             this.daoFactoryServiceManagerPropertiesStatistiche.setShowSql(this.debug);  
  215.             this.daoFactoryServiceManagerPropertiesStatistiche.setDatabaseType(tipoDatabaseTransazioni);
  216.            
  217.             if(this.generazioneStatisticheCustom){
  218.            
  219.                 this.daoFactoryServiceManagerPropertiesPluginsStatistiche =
  220.                         daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.monitor.engine.config.statistiche.utils.ProjectInfo.getInstance());
  221.                 this.daoFactoryServiceManagerPropertiesPluginsStatistiche.setShowSql(this.debug);  
  222.                 this.daoFactoryServiceManagerPropertiesPluginsStatistiche.setDatabaseType(this.tipoDatabase);
  223.                
  224.                 this.daoFactoryServiceManagerPropertiesPluginsBase =
  225.                         daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance());
  226.                 this.daoFactoryServiceManagerPropertiesPluginsBase.setShowSql(this.debug);  
  227.                 this.daoFactoryServiceManagerPropertiesPluginsBase.setDatabaseType(this.tipoDatabase);
  228.                
  229.                 this.daoFactoryServiceManagerPropertiesUtils =
  230.                         daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.commons.search.utils.ProjectInfo.getInstance());
  231.                 this.daoFactoryServiceManagerPropertiesUtils.setShowSql(this.debug);    
  232.                 this.daoFactoryServiceManagerPropertiesUtils.setDatabaseType(this.tipoDatabase);
  233.                
  234.                 if(this.analisiTransazioniCustom){
  235.                
  236.                     this.daoFactoryServiceManagerPropertiesPluginsTransazioni =
  237.                             daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.monitor.engine.config.transazioni.utils.ProjectInfo.getInstance());
  238.                     this.daoFactoryServiceManagerPropertiesPluginsTransazioni.setShowSql(this.debug);  
  239.                     this.daoFactoryServiceManagerPropertiesPluginsTransazioni.setDatabaseType(this.tipoDatabase);
  240.                    
  241.                 }
  242.                
  243.             }
  244.                
  245.         }catch(Exception e){
  246.             throw new Exception("Errore durante l'inizializzazione del datasource: "+e.getMessage(),e);
  247.         }
  248.        
  249.        
  250.         try{
  251.             this.statisticsConfig = new StatisticsConfig(false);
  252.            
  253.             this.statisticsConfig.setLogCore(this.logCore);
  254.             this.statisticsConfig.setLogSql(this.logSql);
  255.             this.statisticsConfig.setGenerazioneStatisticheCustom(this.generazioneStatisticheCustom);
  256.             this.statisticsConfig.setAnalisiTransazioniCustom(this.analisiTransazioniCustom);
  257.             this.statisticsConfig.setDebug(this.debug);
  258.             this.statisticsConfig.setUseUnionForLatency(this.useUnionForLatency);
  259.             this.statisticsConfig.setStatisticheOrarie(this.statisticheOrarie);
  260.             this.statisticsConfig.setStatisticheGiornaliere(this.statisticheGiornaliere);
  261.             this.statisticsConfig.setStatisticheSettimanali(this.statisticheSettimanali);
  262.             this.statisticsConfig.setStatisticheMensili(this.statisticheMensili);
  263.             this.statisticsConfig.setStatisticheOrarieGestioneUltimoIntervallo(this.statisticheOrarie_gestioneUltimoIntervallo);
  264.             this.statisticsConfig.setStatisticheGiornaliereGestioneUltimoIntervallo(this.statisticheGiornaliere_gestioneUltimoIntervallo);
  265.             this.statisticsConfig.setStatisticheSettimanaliGestioneUltimoIntervallo(this.statisticheSettimanali_gestioneUltimoIntervallo);
  266.             this.statisticsConfig.setStatisticheMensiliGestioneUltimoIntervallo(this.statisticheMensili_gestioneUltimoIntervallo);
  267.             this.statisticsConfig.setWaitMsBeforeNextInterval(this.waitMsBeforeNextInterval);
  268.             this.statisticsConfig.setWaitStatiInConsegna(this.waitStatiInConsegna);
  269.         }catch(Exception e){
  270.             throw new Exception("Errore durante la generazione delle statistiche (InitConfigurazione): "+e.getMessage(),e);
  271.         }
  272.        
  273.         switch (this.tipoStatistica) {
  274.         case STATISTICHE_ORARIE:
  275.             this.timerLock = new TimerLock(TipoLock.GENERAZIONE_STATISTICHE_ORARIE);
  276.             break;
  277.         case STATISTICHE_GIORNALIERE:
  278.             this.timerLock = new TimerLock(TipoLock.GENERAZIONE_STATISTICHE_GIORNALIERE);
  279.             break;
  280.         case STATISTICHE_SETTIMANALI:
  281.             this.timerLock = new TimerLock(TipoLock.GENERAZIONE_STATISTICHE_SETTIMANALI);
  282.             break;
  283.         case STATISTICHE_MENSILI:
  284.             this.timerLock = new TimerLock(TipoLock.GENERAZIONE_STATISTICHE_MENSILI);
  285.             break;
  286.         default:
  287.             break;
  288.         }
  289.        
  290.         if(this.op2Properties.isTimerLockByDatabase()) {
  291.             this.semaphore_statistics = new InfoStatistics();

  292.             SemaphoreConfiguration config = GestoreMessaggi.newSemaphoreConfiguration(this.op2Properties.getStatisticheGenerazioneTimerLockMaxLife(),
  293.                     this.op2Properties.getStatisticheGenerazioneTimerLockIdleTime());

  294.             TipiDatabase databaseType = TipiDatabase.toEnumConstant(this.tipoDatabase);
  295.             try {
  296.                 this.semaphore = new Semaphore(this.semaphore_statistics, SemaphoreMapping.newInstance(this.timerLock.getIdLock()),
  297.                         config, databaseType, this.logTimer);
  298.             }catch(Exception e) {
  299.                 throw new TimerException(e.getMessage(),e);
  300.             }
  301.         }
  302.     }
  303.    
  304.     public void check() throws TimerException {
  305.        
  306.         // Controllo che il sistema non sia andando in shutdown
  307.         if(OpenSPCoop2Startup.contextDestroyed){
  308.             this.logTimer.error("["+TimerStatisticheThread.ID_MODULO+"] Rilevato sistema in shutdown");
  309.             return;
  310.         }

  311.         // Controllo che l'inizializzazione corretta delle risorse sia effettuata
  312.         if(!OpenSPCoop2Startup.initialize){
  313.             this.msgDiag.logFatalError("inizializzazione di OpenSPCoop non effettuata", "Check Inizializzazione");
  314.             String msgErrore = "Riscontrato errore: inizializzazione del Timer o di OpenSPCoop non effettuata";
  315.             this.logTimer.error(msgErrore);
  316.             throw new TimerException(msgErrore);
  317.         }

  318.         // Controllo risorse di sistema disponibili
  319.         if( !TimerMonitoraggioRisorseThread.isRisorseDisponibili()){
  320.             this.logTimer.error("["+TimerStatisticheThread.ID_MODULO+"] Risorse di sistema non disponibili: "+TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile().getMessage(),TimerMonitoraggioRisorseThread.getRisorsaNonDisponibile());
  321.             return;
  322.         }
  323.         if( !MsgDiagnostico.gestoreDiagnosticaDisponibile){
  324.             this.logTimer.error("["+TimerStatisticheThread.ID_MODULO+"] Sistema di diagnostica non disponibile: "+MsgDiagnostico.motivoMalfunzionamentoDiagnostici.getMessage(),MsgDiagnostico.motivoMalfunzionamentoDiagnostici);
  325.             return;
  326.         }
  327.        
  328.         boolean enabled = false;
  329.         switch (this.tipoStatistica) {
  330.         case STATISTICHE_ORARIE:
  331.             enabled = TimerState.ENABLED.equals(STATE_STATISTICHE_ORARIE);
  332.             break;
  333.         case STATISTICHE_GIORNALIERE:
  334.             enabled = TimerState.ENABLED.equals(STATE_STATISTICHE_GIORNALIERE);
  335.             break;
  336.         case STATISTICHE_SETTIMANALI:
  337.             enabled = TimerState.ENABLED.equals(STATE_STATISTICHE_SETTIMANALI);
  338.             break;
  339.         case STATISTICHE_MENSILI:
  340.             enabled = TimerState.ENABLED.equals(STATE_STATISTICHE_MENSILI);
  341.             break;
  342.         default:
  343.             break;
  344.         }
  345.         if(!enabled) {
  346.             this.msgDiag.logPersonalizzato("disabilitato");
  347.             this.logCore.info(this.msgDiag.getMessaggio_replaceKeywords("disabilitato"));
  348.             this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("disabilitato"));
  349.             return;
  350.         }
  351.        
  352.        
  353.        
  354.         this.msgDiag.logPersonalizzato("generazioneStatistiche");
  355.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("generazioneStatistiche"));
  356.         long startControlloTimer = DateManager.getTimeMillis();
  357.            
  358.            
  359.         DBTransazioniManager dbTransazioniManager = null;
  360.         DBStatisticheManager dbStatisticheManager = null;
  361.         Resource rTransazioni = null;
  362.         Resource rStatistiche = null;
  363.         Connection conConfig = null;
  364.         DriverConfigurazioneDB driverConfigurazioneDB = null;
  365.         try{

  366.             dbTransazioniManager = DBTransazioniManager.getInstance();
  367.             rTransazioni = dbTransazioniManager.getResource(this.op2Properties.getIdentitaPortaDefaultWithoutProtocol(), TimerStatisticheThread.ID_MODULO, null);
  368.             if(rTransazioni==null){
  369.                 throw new Exception("Risorsa al database delle transazioni non disponibile");
  370.             }
  371.             Connection conTransazioni = (Connection) rTransazioni.getResource();
  372.             if(conTransazioni == null)
  373.                 throw new Exception("Connessione al database delle transazioni non disponibile");  

  374.             Connection conStatistiche = null;
  375.             if(this.op2Properties.isStatisticheUseTransazioniDatasource()) {
  376.                 conStatistiche = conTransazioni; // non prendo due connessioni per "atterrare" sul solito database
  377.             }
  378.             else if(this.op2Properties.isStatisticheUsePddRuntimeDatasource() && this.op2Properties.isTransazioniUsePddRuntimeDatasource()) {
  379.                 conStatistiche = conTransazioni; // non prendo due connessioni per "atterrare" sul solito database
  380.             }
  381.             else {
  382.                 dbStatisticheManager = DBStatisticheManager.getInstance();
  383.                 rStatistiche = dbStatisticheManager.getResource(this.op2Properties.getIdentitaPortaDefaultWithoutProtocol(), TimerStatisticheThread.ID_MODULO, null);
  384.                 if(rStatistiche==null){
  385.                     throw new Exception("Risorsa al database delle statistiche non disponibile");
  386.                 }
  387.                 conStatistiche = (Connection) rStatistiche.getResource();
  388.                 if(conStatistiche == null)
  389.                     throw new Exception("Connessione al database delle statistiche non disponibile");  
  390.             }
  391.            
  392.             org.openspcoop2.core.statistiche.dao.IServiceManager statisticheSM =
  393.                     (org.openspcoop2.core.statistiche.dao.IServiceManager)
  394.                     this.daoFactory.getServiceManager(org.openspcoop2.core.statistiche.utils.ProjectInfo.getInstance(), conStatistiche,
  395.                         this.daoFactoryServiceManagerPropertiesStatistiche, this.daoFactoryLogger);
  396.                
  397.             org.openspcoop2.core.transazioni.dao.IServiceManager transazioniSM =
  398.                     (org.openspcoop2.core.transazioni.dao.IServiceManager)
  399.                         this.daoFactory.getServiceManager(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance(), conTransazioni,
  400.                         this.daoFactoryServiceManagerPropertiesTransazioni, this.daoFactoryLogger);
  401.            
  402.             org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager pluginsStatisticheSM = null;
  403.             org.openspcoop2.core.plugins.dao.IServiceManager pluginsBaseSM = null;
  404.             org.openspcoop2.core.commons.search.dao.IServiceManager utilsSM = null;
  405.             org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager pluginsTransazioniSM = null;
  406.             if(this.generazioneStatisticheCustom){
  407.                
  408.                 IDriverConfigurazioneGet driverConfigurazione = ConfigurazionePdDReader.getDriverConfigurazionePdD();
  409.                 if(driverConfigurazione instanceof DriverConfigurazioneDB) {
  410.                     driverConfigurazioneDB = (DriverConfigurazioneDB) driverConfigurazione;
  411.                 }
  412.                 else {
  413.                     throw new Exception("La generazione delle statistiche custom richiede una configurazione di tipo 'db', trovato: "+driverConfigurazione.getClass().getName());
  414.                 }
  415.                 conConfig = driverConfigurazioneDB.getConnection(TimerStatisticheThread.ID_MODULO+".customStats");
  416.                
  417.                 pluginsStatisticheSM = (org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager)
  418.                         this.daoFactory.getServiceManager(
  419.                                 org.openspcoop2.monitor.engine.config.statistiche.utils.ProjectInfo.getInstance(), conConfig,
  420.                                 this.daoFactoryServiceManagerPropertiesPluginsStatistiche, this.daoFactoryLogger);
  421.                 pluginsBaseSM = (org.openspcoop2.core.plugins.dao.IServiceManager)
  422.                         this.daoFactory.getServiceManager(
  423.                                 org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(), conConfig,
  424.                                 this.daoFactoryServiceManagerPropertiesPluginsBase, this.daoFactoryLogger);
  425.                 utilsSM = (org.openspcoop2.core.commons.search.dao.IServiceManager)
  426.                         this.daoFactory.getServiceManager(
  427.                                 org.openspcoop2.core.commons.search.utils.ProjectInfo.getInstance(), conConfig,
  428.                                 this.daoFactoryServiceManagerPropertiesUtils, this.daoFactoryLogger);
  429.                 if(this.analisiTransazioniCustom){
  430.                     pluginsTransazioniSM = (org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager)
  431.                             this.daoFactory.getServiceManager(
  432.                                     org.openspcoop2.monitor.engine.config.transazioni.utils.ProjectInfo.getInstance(), conConfig,
  433.                                     this.daoFactoryServiceManagerPropertiesPluginsTransazioni, this.daoFactoryLogger);
  434.                 }
  435.             }
  436.            
  437.             // aggiorno configurazione per forceIndex
  438.             this.statisticsConfig.setForceIndexConfig(this.op2Properties.getStatisticheGenerazioneExternalForceIndexRepository());
  439.            
  440.             String causa = "Generazione Statistiche";
  441.             try {
  442.                 GestoreMessaggi.acquireLock(
  443.                         this.semaphore, conStatistiche, this.timerLock,
  444.                         this.msgDiag, causa,
  445.                         this.op2Properties.getStatisticheGenerazioneTimerLockAttesaAttiva(),
  446.                         this.op2Properties.getStatisticheGenerazioneTimerLockCheckInterval());
  447.                
  448.                 StatisticsLibrary sLibrary = new StatisticsLibrary(this.statisticsConfig, statisticheSM, transazioniSM,
  449.                         pluginsStatisticheSM, pluginsBaseSM, utilsSM, pluginsTransazioniSM);
  450.                
  451.                
  452.                 switch (this.tipoStatistica) {
  453.                 case STATISTICHE_ORARIE:
  454.                     if(generaStatistica("orario", conStatistiche, sLibrary, TipoIntervalloStatistico.STATISTICHE_ORARIE) == false) {
  455.                         return; // problemi con il lock
  456.                     }
  457.                     break;
  458.                 case STATISTICHE_GIORNALIERE:
  459.                     if(generaStatistica("giornaliero", conStatistiche, sLibrary, TipoIntervalloStatistico.STATISTICHE_GIORNALIERE) == false) {
  460.                         return; // problemi con il lock
  461.                     }
  462.                     break;
  463.                 case STATISTICHE_SETTIMANALI:
  464.                     if(generaStatistica("settimanale", conStatistiche, sLibrary, TipoIntervalloStatistico.STATISTICHE_SETTIMANALI) == false) {
  465.                         return; // problemi con il lock
  466.                     }
  467.                     break;
  468.                 case STATISTICHE_MENSILI:
  469.                     if(generaStatistica("mensile", conStatistiche, sLibrary, TipoIntervalloStatistico.STATISTICHE_MENSILI) == false) {
  470.                         return; // problemi con il lock
  471.                     }
  472.                     break;
  473.                 default:
  474.                     break;
  475.                 }
  476.                
  477.             }finally{
  478.                 try{
  479.                     GestoreMessaggi.releaseLock(
  480.                             this.semaphore, conStatistiche, this.timerLock,
  481.                             this.msgDiag, causa);
  482.                 }catch(Exception e){}
  483.             }
  484.            
  485.            
  486.            
  487.             // end
  488.             long endControlloTimer = DateManager.getTimeMillis();
  489.             long diff = (endControlloTimer-startControlloTimer);
  490.             this.logTimer.info("Generazione '"+this.tipoStatistica.getValue()+"' terminato in "+Utilities.convertSystemTimeIntoStringMillisecondi(diff, true));
  491.            
  492.            
  493.         }
  494.         catch(TimerLockNotAvailableException t) {
  495.             // msg diagnostico emesso durante l'emissione dell'eccezione
  496.             this.logTimer.info(t.getMessage(),t);
  497.         }
  498.         catch (Exception e) {
  499.             this.msgDiag.logErroreGenerico(e,"GenerazioneStatistiche");
  500.             this.logTimer.error("Riscontrato errore durante la generazione delle statistiche ("+this.tipoStatistica.getValue()+"): "+ e.getMessage(),e);
  501.         }finally{
  502.             try{
  503.                 if(rTransazioni!=null)
  504.                     dbTransazioniManager.releaseResource(this.op2Properties.getIdentitaPortaDefaultWithoutProtocol(), TimerStatisticheThread.ID_MODULO, rTransazioni);
  505.             }catch(Throwable eClose){}
  506.             try{
  507.                 if(rStatistiche!=null)
  508.                     dbStatisticheManager.releaseResource(this.op2Properties.getIdentitaPortaDefaultWithoutProtocol(), TimerStatisticheThread.ID_MODULO, rStatistiche);
  509.             }catch(Throwable eClose){}
  510.             try{
  511.                 if(conConfig!=null)
  512.                     driverConfigurazioneDB.releaseConnection(conConfig);
  513.             }catch(Throwable eClose){}
  514.         }
  515.            
  516.     }

  517.    
  518.     private boolean generaStatistica(String intervallo, Connection conStatistiche, StatisticsLibrary sLibrary, TipoIntervalloStatistico tipoIntervalloStatistico) {
  519.        
  520.         long startGenerazione = DateManager.getTimeMillis();
  521.         this.msgDiag.addKeyword(CostantiPdD.KEY_TIPO_STATISTICA, intervallo); // riferito a intervallo
  522.         this.msgDiag.logPersonalizzato("generazioneStatistiche.inCorso");
  523.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("generazioneStatistiche.inCorso"));
  524.        
  525.         try{
  526.             GestoreMessaggi.updateLock(
  527.                     this.semaphore, conStatistiche, this.timerLock,
  528.                     this.msgDiag, "Generazione statistiche intervallo '"+intervallo+"' ...");
  529.         }catch(Throwable e){
  530.             this.msgDiag.logErroreGenerico(e,"TimerStatistiche-UpdateLock");
  531.             this.logTimer.error("TimerStatistiche-UpdateLock: "+e.getMessage(),e);
  532.             return false;
  533.         }
  534.        
  535.         switch (tipoIntervalloStatistico) {
  536.         case STATISTICHE_ORARIE:
  537.             sLibrary.generateStatisticaOraria();
  538.             break;
  539.         case STATISTICHE_GIORNALIERE:
  540.             sLibrary.generateStatisticaGiornaliera();
  541.             break;
  542.         case STATISTICHE_SETTIMANALI:
  543.             sLibrary.generateStatisticaSettimanale();
  544.             break;
  545.         case STATISTICHE_MENSILI:
  546.             sLibrary.generateStatisticaMensile();
  547.             break;
  548.         }  
  549.                
  550.         long endGenerazione = DateManager.getTimeMillis();
  551.         String tempoImpiegato = Utilities.convertSystemTimeIntoStringMillisecondi((endGenerazione-startGenerazione), true);
  552.         this.msgDiag.addKeyword(CostantiPdD.KEY_TEMPO_GENERAZIONE, tempoImpiegato);
  553.         this.msgDiag.logPersonalizzato("generazioneStatistiche.effettuata");
  554.         this.logTimer.info(this.msgDiag.getMessaggio_replaceKeywords("generazioneStatistiche.effettuata"));
  555.        
  556.         return true;
  557.     }
  558. }