CheckStatoPdDHealthCheckStatsUtils.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.services.connector;

  21. import java.sql.Connection;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.List;

  25. import org.openspcoop2.core.commons.CoreException;
  26. import org.openspcoop2.core.commons.dao.DAOFactory;
  27. import org.openspcoop2.core.commons.dao.DAOFactoryProperties;
  28. import org.openspcoop2.core.id.IDSoggetto;
  29. import org.openspcoop2.core.statistiche.StatisticaInfo;
  30. import org.openspcoop2.core.statistiche.dao.IStatisticaInfoServiceSearch;
  31. import org.openspcoop2.generic_project.exception.NotImplementedException;
  32. import org.openspcoop2.generic_project.exception.ServiceException;
  33. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  34. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  35. import org.openspcoop2.monitor.engine.statistic.StatisticheGiornaliere;
  36. import org.openspcoop2.monitor.engine.statistic.StatisticheMensili;
  37. import org.openspcoop2.monitor.engine.statistic.StatisticheOrarie;
  38. import org.openspcoop2.monitor.engine.statistic.StatisticheSettimanali;
  39. import org.openspcoop2.pdd.config.DBStatisticheManager;
  40. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  41. import org.openspcoop2.pdd.config.Resource;
  42. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  43. import org.openspcoop2.utils.date.DateManager;
  44. import org.openspcoop2.utils.date.DateUtils;
  45. import org.slf4j.Logger;

  46. /**
  47.  * CheckStatoPdDHealthCheckStatsUtils
  48.  *
  49.  * @author Poli Andrea (apoli@link.it)
  50.  * @author $Author$
  51.  * @version $Rev$, $Date$
  52.  */
  53. public class CheckStatoPdDHealthCheckStatsUtils {
  54.    
  55.     private CheckStatoPdDHealthCheckStatsUtils() {}

  56.     public static void verificaInformazioniStatistiche(Logger log, OpenSPCoop2Properties properties) throws CoreException {
  57.         CheckStatoPdDHealthCheckStats check = CheckStatoPdDHealthCheckStats.readProprietaVerificaInformazioniStatistiche(properties);
  58.         verificaInformazioniStatistiche(log, properties, check);
  59.     }
  60.    
  61.     public static void verificaInformazioniStatistiche(Logger log, OpenSPCoop2Properties properties, CheckStatoPdDHealthCheckStats check) throws CoreException {
  62.        
  63.         boolean verificaStatisticaOraria = check.isVerificaStatisticaOraria();
  64.         boolean verificaStatisticaGiornaliera = check.isVerificaStatisticaGiornaliera();
  65.         boolean verificaStatisticaSettimanale = check.isVerificaStatisticaSettimanale();
  66.         boolean verificaStatisticaMensile = check.isVerificaStatisticaMensile();
  67.        
  68.         if(!verificaStatisticaOraria && !verificaStatisticaGiornaliera && !verificaStatisticaSettimanale && !verificaStatisticaMensile) {
  69.             return;
  70.         }
  71.        
  72.         DAOFactoryProperties daoFactoryProperties = null;
  73.         Logger daoFactoryLogger = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  74.         DAOFactory daoFactory = DAOFactory.getInstance(daoFactoryLogger);
  75.         try {
  76.             daoFactoryProperties = DAOFactoryProperties.getInstance(daoFactoryLogger);
  77.         }catch(Exception e) {
  78.             throw new CoreException(e.getMessage(),e);
  79.         }
  80.         String tipoDatabase = properties.getDatabaseType();
  81.         if(tipoDatabase==null){
  82.             try {
  83.                 tipoDatabase = daoFactoryProperties.getTipoDatabase(new org.openspcoop2.core.statistiche.utils.ProjectInfo());
  84.             }catch(Exception e) {
  85.                 throw new CoreException(e.getMessage(),e);
  86.             }
  87.         }
  88.         if(tipoDatabase==null){
  89.             throw new CoreException("Database type undefined");
  90.         }
  91.        
  92.         ServiceManagerProperties daoFactoryServiceManagerPropertiesStatistiche = null;
  93.         try {
  94.             daoFactoryServiceManagerPropertiesStatistiche = daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.statistiche.utils.ProjectInfo.getInstance());
  95.         }catch(Exception e) {
  96.             throw new CoreException(e.getMessage(),e);
  97.         }
  98.         daoFactoryServiceManagerPropertiesStatistiche.setShowSql(false);    
  99.         daoFactoryServiceManagerPropertiesStatistiche.setDatabaseType(tipoDatabase);
  100.        
  101.         DBStatisticheManager dbStatisticheManager = DBStatisticheManager.getInstance();
  102.                
  103.         Resource r = null;
  104.         IDSoggetto dominio = properties.getIdentitaPortaDefaultWithoutProtocol();
  105.         String idModulo = "GovWayHealthCheck";
  106.         try{    
  107.             Connection con = null;
  108.             r = dbStatisticheManager.getResource(dominio, idModulo, null);
  109.             if(r==null){
  110.                 throw new CoreException("Database resource not available");
  111.             }
  112.             con = (Connection) r.getResource();
  113.            
  114.             org.openspcoop2.core.statistiche.dao.IServiceManager statisticheSM =
  115.                     (org.openspcoop2.core.statistiche.dao.IServiceManager) daoFactory.getServiceManager(org.openspcoop2.core.statistiche.utils.ProjectInfo.getInstance(),
  116.                             con, daoFactoryServiceManagerPropertiesStatistiche, daoFactoryLogger);
  117.             IStatisticaInfoServiceSearch search = statisticheSM.getStatisticaInfoServiceSearch();
  118.             verificaInformazioniStatistiche(log, search, check);
  119.         }
  120.         catch(CoreException e) {
  121.             throw e;
  122.         }catch(Exception e) {
  123.             throw new CoreException(e.getMessage(),e);
  124.         }
  125.         finally {
  126.             try{
  127.                 if(r!=null) {
  128.                     dbStatisticheManager.releaseResource(dominio, idModulo, r);
  129.                 }
  130.             }catch(Exception eClose){
  131.                 // ignore
  132.             }
  133.         }
  134.            
  135.     }
  136.    
  137.     public static void verificaInformazioniStatistiche(Logger log, IStatisticaInfoServiceSearch search, CheckStatoPdDHealthCheckStats check) throws ServiceException, NotImplementedException, CoreException {
  138.        
  139.         boolean verificaStatisticaOraria = check.isVerificaStatisticaOraria();
  140.         boolean verificaStatisticaGiornaliera = check.isVerificaStatisticaGiornaliera();
  141.         boolean verificaStatisticaSettimanale = check.isVerificaStatisticaSettimanale();
  142.         boolean verificaStatisticaMensile = check.isVerificaStatisticaMensile();
  143.         int verificaStatisticaOrariaSoglia = check.getVerificaStatisticaOrariaSoglia();
  144.         int verificaStatisticaGiornalieraSoglia = check.getVerificaStatisticaGiornalieraSoglia();
  145.         int verificaStatisticaSettimanaleSoglia = check.getVerificaStatisticaSettimanaleSoglia();
  146.         int verificaStatisticaMensileSoglia = check.getVerificaStatisticaMensileSoglia();
  147.        
  148.         IPaginatedExpression pagExpr = search.newPaginatedExpression();
  149.         List<StatisticaInfo> list = search.findAll(pagExpr);
  150.         if(list==null || list.isEmpty()) {
  151.             throw new CoreException("No statistical report available");
  152.         }
  153.         Date dataUltimaGenerazioneOraria = null;
  154.         Date dataUltimaGenerazioneGiornaliera = null;
  155.         Date dataUltimaGenerazioneSettimanale = null;
  156.         Date dataUltimaGenerazioneMensile = null;
  157.         for (StatisticaInfo statisticaInfo : list) {
  158.             if(statisticaInfo.getTipoStatistica()==null) {
  159.                 throw new CoreException("Found row without associated report type");
  160.             }
  161.             switch (statisticaInfo.getTipoStatistica()) {
  162.             case STATISTICHE_ORARIE:
  163.                 dataUltimaGenerazioneOraria = statisticaInfo.getDataUltimaGenerazione();
  164.                 break;
  165.             case STATISTICHE_GIORNALIERE:
  166.                 dataUltimaGenerazioneGiornaliera = statisticaInfo.getDataUltimaGenerazione();
  167.                 break;
  168.             case STATISTICHE_SETTIMANALI:
  169.                 dataUltimaGenerazioneSettimanale = statisticaInfo.getDataUltimaGenerazione();
  170.                 break;
  171.             case STATISTICHE_MENSILI:
  172.                 dataUltimaGenerazioneMensile = statisticaInfo.getDataUltimaGenerazione();
  173.                 break;
  174.             }
  175.         }
  176.        
  177.         // Se viene rilevata come ultimo intervallo temporale delle statistiche una data più vecchia dell'unità indicata nella soglia (relativamente al tipo di statistica) viene ritornato un errore
  178.         // Se si indica un valore '1' si richiede ad esempio che le statistiche siano aggiornate all'intervallo precedente.
  179.         // Quindi ad es. all'ora precedente per l'orario, al giorno precedente per il giornaliero e cosi via...
  180.         // Se si indica 0 si vuole imporre che le statistiche siano aggiornate (anche parzialmente) all'ultimo intervallo
  181.         // NOTA: (potrebbe creare dei falsi negativi se la generazione delle statistiche avviene dopo il check)
  182.         StringBuilder bf = new StringBuilder();
  183.         Date now = DateManager.getDate();
  184.        
  185.         if(verificaStatisticaOraria) {
  186.             if(dataUltimaGenerazioneOraria==null) {
  187.                 if(bf.length()>0) {
  188.                     bf.append("\n");
  189.                 }
  190.                 bf.append("No hourly report detected");
  191.             }
  192.             else {
  193.                 Date nowTruncate = StatisticheOrarie.getInstanceForUtils().truncDate(now, false);
  194.                 Date dataUltimaGenerazioneTruncate = StatisticheOrarie.getInstanceForUtils().truncDate(dataUltimaGenerazioneOraria, false);
  195.                 Date dataUltimaGenerazioneTruncateConAggiuntaSoglia = new Date(dataUltimaGenerazioneTruncate.getTime());
  196.                 for (int i = 0; i < verificaStatisticaOrariaSoglia; i++) {
  197.                     dataUltimaGenerazioneTruncateConAggiuntaSoglia = StatisticheOrarie.getInstanceForUtils().incrementDate(dataUltimaGenerazioneTruncateConAggiuntaSoglia, false);  
  198.                 }
  199.                 boolean sogliaNonRispettata = dataUltimaGenerazioneTruncateConAggiuntaSoglia.before(nowTruncate);
  200.                 SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  201.                 String msg = getLogMessageStats("oraria",dataUltimaGenerazioneOraria, verificaStatisticaOrariaSoglia,
  202.                         dataUltimaGenerazioneTruncate, dataUltimaGenerazioneTruncateConAggiuntaSoglia,
  203.                         nowTruncate, sogliaNonRispettata);
  204.                 log.debug(msg);

  205.                 if(sogliaNonRispettata) {
  206.                     if(bf.length()>0) {
  207.                         bf.append("\n");
  208.                     }
  209.                     bf.append("Hourly statistical information found whose last update is older than the allowed threshold ("+verificaStatisticaOrariaSoglia+"); last generation date: "+dateformat.format(dataUltimaGenerazioneOraria));
  210.                 }
  211.             }
  212.         }
  213.        
  214.         if(verificaStatisticaGiornaliera) {
  215.             if(dataUltimaGenerazioneGiornaliera==null) {
  216.                 if(bf.length()>0) {
  217.                     bf.append("\n");
  218.                 }
  219.                 bf.append("No daily report detected");
  220.             }
  221.             else {
  222.                 Date nowTruncate = StatisticheGiornaliere.getInstanceForUtils().truncDate(now, false);
  223.                 Date dataUltimaGenerazioneTruncate = StatisticheGiornaliere.getInstanceForUtils().truncDate(dataUltimaGenerazioneGiornaliera, false);
  224.                 Date dataUltimaGenerazioneTruncateConAggiuntaSoglia = new Date(dataUltimaGenerazioneTruncate.getTime());
  225.                 for (int i = 0; i < verificaStatisticaGiornalieraSoglia; i++) {
  226.                     dataUltimaGenerazioneTruncateConAggiuntaSoglia = StatisticheGiornaliere.getInstanceForUtils().incrementDate(dataUltimaGenerazioneTruncateConAggiuntaSoglia, false);
  227.                 }
  228.                 boolean sogliaNonRispettata = dataUltimaGenerazioneTruncateConAggiuntaSoglia.before(nowTruncate);
  229.                 SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  230.                 String msg = getLogMessageStats("giornaliera",dataUltimaGenerazioneGiornaliera, verificaStatisticaGiornalieraSoglia,
  231.                         dataUltimaGenerazioneTruncate, dataUltimaGenerazioneTruncateConAggiuntaSoglia,
  232.                         nowTruncate, sogliaNonRispettata);
  233.                 log.debug(msg);

  234.                 if(sogliaNonRispettata) {
  235.                     if(bf.length()>0) {
  236.                         bf.append("\n");
  237.                     }
  238.                     bf.append("Daily statistical information found whose last update is older than the allowed threshold ("+verificaStatisticaGiornalieraSoglia+"); last generation date: "+dateformat.format(dataUltimaGenerazioneGiornaliera));
  239.                 }
  240.             }
  241.         }
  242.        
  243.         if(verificaStatisticaSettimanale) {
  244.             if(dataUltimaGenerazioneSettimanale==null) {
  245.                 if(bf.length()>0) {
  246.                     bf.append("\n");
  247.                 }
  248.                 bf.append("No weekly report detected");
  249.             }
  250.             else {
  251.                 Date nowTruncate = StatisticheSettimanali.getInstanceForUtils().truncDate(now, false);
  252.                 Date dataUltimaGenerazioneTruncate = StatisticheSettimanali.getInstanceForUtils().truncDate(dataUltimaGenerazioneSettimanale, false);
  253.                 Date dataUltimaGenerazioneTruncateConAggiuntaSoglia = new Date(dataUltimaGenerazioneTruncate.getTime());
  254.                 for (int i = 0; i < verificaStatisticaSettimanaleSoglia; i++) {
  255.                     dataUltimaGenerazioneTruncateConAggiuntaSoglia = StatisticheSettimanali.getInstanceForUtils().incrementDate(dataUltimaGenerazioneTruncateConAggiuntaSoglia, false);
  256.                 }
  257.                 boolean sogliaNonRispettata = dataUltimaGenerazioneTruncateConAggiuntaSoglia.before(nowTruncate);
  258.                 SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  259.                 String msg = getLogMessageStats("settimanale",dataUltimaGenerazioneSettimanale, verificaStatisticaSettimanaleSoglia,
  260.                         dataUltimaGenerazioneTruncate, dataUltimaGenerazioneTruncateConAggiuntaSoglia,
  261.                         nowTruncate, sogliaNonRispettata);
  262.                 log.debug(msg);

  263.                 if(sogliaNonRispettata) {
  264.                     if(bf.length()>0) {
  265.                         bf.append("\n");
  266.                     }
  267.                     bf.append("Weekly statistical information found whose last update is older than the allowed threshold ("+verificaStatisticaSettimanaleSoglia+"); last-generation: "+dateformat.format(dataUltimaGenerazioneSettimanale));
  268.                 }
  269.             }
  270.         }
  271.        
  272.         if(verificaStatisticaMensile) {
  273.             if(dataUltimaGenerazioneMensile==null) {
  274.                 if(bf.length()>0) {
  275.                     bf.append("\n");
  276.                 }
  277.                 bf.append("No monthly report detected");
  278.             }
  279.             else {
  280.                 Date nowTruncate = StatisticheMensili.getInstanceForUtils().truncDate(now, false);
  281.                 Date dataUltimaGenerazioneTruncate = StatisticheMensili.getInstanceForUtils().truncDate(dataUltimaGenerazioneMensile, false);
  282.                 Date dataUltimaGenerazioneTruncateConAggiuntaSoglia = new Date(dataUltimaGenerazioneTruncate.getTime());
  283.                 for (int i = 0; i < verificaStatisticaMensileSoglia; i++) {
  284.                     dataUltimaGenerazioneTruncateConAggiuntaSoglia = StatisticheMensili.getInstanceForUtils().incrementDate(dataUltimaGenerazioneTruncateConAggiuntaSoglia, false);
  285.                 }
  286.                 boolean sogliaNonRispettata = dataUltimaGenerazioneTruncateConAggiuntaSoglia.before(nowTruncate);
  287.                 SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  288.                 String msg = getLogMessageStats("mensile",dataUltimaGenerazioneMensile, verificaStatisticaMensileSoglia,
  289.                         dataUltimaGenerazioneTruncate, dataUltimaGenerazioneTruncateConAggiuntaSoglia,
  290.                         nowTruncate, sogliaNonRispettata);
  291.                 log.debug(msg);

  292.                 if(sogliaNonRispettata) {
  293.                     if(bf.length()>0) {
  294.                         bf.append("\n");
  295.                     }
  296.                     bf.append("Monthly statistical information found whose last update is older than the allowed threshold ("+verificaStatisticaMensileSoglia+"); last-generation: "+dateformat.format(dataUltimaGenerazioneMensile));
  297.                 }
  298.             }
  299.         }
  300.        
  301.         if(bf.length()>0) {
  302.             throw new CoreException(bf.toString());
  303.         }
  304.     }
  305.    
  306.     private static String getLogMessageStats(String tipo, Date dataUltimaGenerazione, int verificaStatisticaSoglia,
  307.             Date dataUltimaGenerazioneTruncate, Date dataUltimaGenerazioneTruncateConAggiuntaSoglia,
  308.             Date nowTruncate, boolean sogliaNonRispettata) {
  309.         SimpleDateFormat dateformat = DateUtils.getSimpleDateFormatMs();
  310.         return "Check [Statistica "+tipo+"], ultima data di generazione ["+dateformat.format(dataUltimaGenerazione)+
  311.                 "] dataUltimaGenerazioneTruncate["+dateformat.format(dataUltimaGenerazioneTruncate)+"] soglia["+verificaStatisticaSoglia+
  312.                 "] dataUltimaGenerazioneTruncateConAggiuntaSoglia["+dateformat.format(dataUltimaGenerazioneTruncateConAggiuntaSoglia)+
  313.                 "] nowTruncate["+dateformat.format(nowTruncate)+"] sogliaNonRispettata["+sogliaNonRispettata+"]";
  314.     }
  315.    
  316. }