SondaBatch.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.utils.sonde.impl;

  21. import java.sql.Connection;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. import java.util.HashSet;
  25. import java.util.Properties;
  26. import java.util.Set;

  27. import org.openspcoop2.utils.TipiDatabase;
  28. import org.openspcoop2.utils.io.Base64Utilities;
  29. import org.openspcoop2.utils.sonde.ParametriSonda;
  30. import org.openspcoop2.utils.sonde.Sonda;
  31. import org.openspcoop2.utils.sonde.SondaException;

  32. /**
  33.  * Classe di implementazione della Sonda per i batch
  34.  *
  35.  *
  36.  * @author Bussu Giovanni (bussu@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class SondaBatch extends Sonda {

  41.     /**
  42.      * Costruttore per la classe SondaBatch
  43.      * @param param parametri costruttivi della sonda
  44.      * @throws Exception
  45.      */
  46.     public SondaBatch(ParametriSonda param) {
  47.         super(param);
  48.         Set<String> reserved = new HashSet<String>();
  49.         reserved.add("data_ultimo_batch");
  50.         reserved.add("esito_batch");
  51.         reserved.add("interazioni_fallite");
  52.         reserved.add("descrizione_errore");
  53.         this.getParam().setReserved(reserved);

  54.     }

  55.     @Override
  56.     public StatoSonda getStatoSonda(){


  57.         Date now = new Date();
  58.         Date data_warn = new Date(now.getTime() - super.getParam().getSogliaWarn());
  59.         Date data_err = new Date(now.getTime() - super.getParam().getSogliaError());

  60.         StatoSonda statoSonda = new StatoSonda();
  61.         boolean esito_batch = false;
  62.         if(super.getParam().getDatiCheck().containsKey("esito_batch")) {
  63.             String esitoBatchString = (String) super.getParam().getDatiCheck().getProperty("esito_batch");
  64.             try{
  65.                 esito_batch = Boolean.parseBoolean(esitoBatchString);
  66.             } catch(NumberFormatException e){
  67.                 e.printStackTrace(System.err);
  68.                 System.err.println("Errore durante il parsing del parametro esito_batch: " + super.getParam().getDatiCheck().getProperty("esito_batch") + ". Elimino il valore");
  69.                 super.getParam().getDatiCheck().remove("esito_batch");
  70.             }
  71.         } else {
  72.             System.err.println("Parametro esito_batch non trovato");
  73.         }

  74.         Long dataUltimoBatchLong = null;
  75.         if(super.getParam().getDatiCheck().containsKey("data_ultimo_batch")) {
  76.             try {
  77.                
  78.                 dataUltimoBatchLong = Long.valueOf(super.getParam().getDatiCheck().getProperty("data_ultimo_batch"));
  79.             } catch(NumberFormatException e) {
  80.                 e.printStackTrace(System.err);
  81.                 System.err.println("Errore durante il parsing del parametro data_ultimo_batch: " + super.getParam().getDatiCheck().getProperty("data_ultimo_batch") + ". Elimino il valore");
  82.                 super.getParam().getDatiCheck().remove("data_ultimo_batch");
  83.             }
  84.         } else {
  85.             System.err.println("Parametro data_ultimo_batch non trovato");
  86.         }

  87.         if(dataUltimoBatchLong == null) {
  88.             statoSonda.setStato(2);
  89.             statoSonda.setDescrizione("Il batch "+super.getParam().getNome()+" risulta non essere eseguito.");
  90.             return statoSonda;
  91.         }

  92.         Date data_ultimo_batch = new Date(dataUltimoBatchLong);

  93.         SimpleDateFormat format = new SimpleDateFormat(PATTERN);
  94.         String dataUltimoBatchString = format.format(data_ultimo_batch);
  95.        
  96.         if(esito_batch) {

  97.             //Valuto l'eventuale superamento delle soglie e calcolo lo stato
  98.             if(data_ultimo_batch.before(data_err)) {
  99.                 String errorDateFormatted = format.format(data_err);
  100.                 statoSonda.setStato(2);
  101.                 statoSonda.setDescrizione("Il batch "+super.getParam().getNome()+" risulta non essere eseguito dal "+dataUltimoBatchString+". Data di error ("+errorDateFormatted+") superata.");
  102.             } else if(data_ultimo_batch.before(data_warn)) {
  103.                 String warnDateFormatted = format.format(data_warn);
  104.                 statoSonda.setStato(1);
  105.                 statoSonda.setDescrizione("Il batch "+super.getParam().getNome()+" risulta non essere eseguito dal "+dataUltimoBatchString+". Data di warn ("+warnDateFormatted+") superata.");
  106.             } else {
  107.                 statoSonda.setStato(0);
  108.                 statoSonda.setDescrizione("Batch "+super.getParam().getNome()+" eseguito con successo il "+dataUltimoBatchString+".");
  109.             }
  110.      
  111.             return statoSonda;
  112.         } else {
  113.            
  114.             Integer interazioniFallite = -1;
  115.             if(super.getParam().getDatiCheck().containsKey("interazioni_fallite")) {
  116.                 try{
  117.                     interazioniFallite = Integer.parseInt(super.getParam().getDatiCheck().getProperty("interazioni_fallite"));
  118.                 } catch(NumberFormatException e) {
  119.                     e.printStackTrace(System.err);
  120.                     System.err.println("Errore durante il parsing del parametro interazioni_fallite: " + super.getParam().getDatiCheck().getProperty("interazioni_fallite") + ". Elimino il valore");
  121.                     super.getParam().getDatiCheck().remove("interazioni_fallite");
  122.                 }
  123.             } else {
  124.                 System.err.println("Parametro interazioni_fallite non trovato");
  125.             }

  126.             statoSonda.setStato(2);
  127.             String descr = null;
  128.             if(super.getParam().getDatiCheck().containsKey("descrizione_errore")) {
  129.                 descr = new String(Base64Utilities.decode((String) super.getParam().getDatiCheck().get("descrizione_errore")));
  130.             }
  131.             statoSonda.setDescrizione("Il batch "+super.getParam().getNome()+" risulta fallire dal "+dataUltimoBatchString+" (fallite "+interazioniFallite+" iterazioni). Descrizione dell'ultimo errore:" + descr);
  132.             return statoSonda;
  133.         }
  134.     }

  135.    
  136.     //retrocompatibilita
  137.     public StatoSonda aggiornaStatoSonda(boolean esito_batch, Date data_ultimo_batch, String descrizioneErrore, Connection connection, TipiDatabase tipoDatabase) throws SondaException {
  138.         return this.aggiornaStatoSonda(esito_batch, null, data_ultimo_batch, descrizioneErrore, connection, tipoDatabase);
  139.     }
  140.    
  141.     /**
  142.      * @param esito_batch true se l'esito e' positivo, false altrimenti
  143.      * @param data_ultimo_batch ultima data di esecuzione del batch
  144.      * @param descrizioneErrore eventuale descrizione dell'errore (in caso di esito negativo)
  145.      * @param connection connessione per il DB
  146.      * @param tipoDatabase tipo database
  147.      * @return lo stato attuale della sonda
  148.      * @throws SondaException
  149.      */
  150.     public StatoSonda aggiornaStatoSonda(boolean esito_batch, Properties params, Date data_ultimo_batch, String descrizioneErrore, Connection connection, TipiDatabase tipoDatabase) throws SondaException {
  151.         // inserisce i dati nel properties
  152.        
  153.         super.getParam().putAllCheck(params);
  154.        
  155.         super.getParam().getDatiCheck().put("data_ultimo_batch", data_ultimo_batch.getTime()+ "");
  156.         super.getParam().getDatiCheck().put("esito_batch", String.valueOf(esito_batch));
  157.         if(!esito_batch) {
  158.             if(super.getParam().getDatiCheck().containsKey("interazioni_fallite")) {
  159.                 Integer interazioniFallite = Integer.parseInt(super.getParam().getDatiCheck().getProperty("interazioni_fallite"));
  160.                 super.getParam().getDatiCheck().put("interazioni_fallite", (interazioniFallite+1) + "");
  161.             } else {
  162.                 super.getParam().getDatiCheck().put("interazioni_fallite", 1 + "");
  163.             }
  164.             if(descrizioneErrore != null) {
  165.                 super.getParam().getDatiCheck().put("descrizione_errore", Base64Utilities.encodeAsString(descrizioneErrore.getBytes()));
  166.             }
  167.         } else {
  168.             if(super.getParam().getDatiCheck().containsKey("descrizione_errore"))
  169.                 super.getParam().getDatiCheck().remove("descrizione_errore");
  170.             if(super.getParam().getDatiCheck().containsKey("interazioni_fallite"))
  171.                 super.getParam().getDatiCheck().remove("interazioni_fallite");
  172.         }
  173.         return updateSonda(connection, tipoDatabase);
  174.     }

  175. }