StatisticsServiceLibrary.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.config;

  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  28. import org.openspcoop2.monitor.engine.config.statistiche.ConfigurazioneStatistica;
  29. import org.openspcoop2.monitor.engine.config.statistiche.InfoPlugin;
  30. import org.openspcoop2.monitor.engine.statistic.StatisticByResource;
  31. import org.openspcoop2.monitor.engine.statistic.StatisticByState;

  32. /**
  33.  * StatisticsServiceLibrary
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class StatisticsServiceLibrary implements Serializable {

  40.     /**
  41.      *
  42.      */
  43.     private static final long serialVersionUID = 1L;

  44.     private BasicServiceLibrary basicServiceLibrary;
  45.     private List<ConfigurazioneStatistica> statisticsActionLibrary;
  46.     private List<ConfigurazioneStatistica> statisticsActionAllLibrary;
  47.     private boolean pluginStatiTransazioniEnabled = false; // plugin org.openspcoop2.monitor.engine.plugins.statistic.StatisticByState
  48.     private List<String> pluginRisorseTransazioni = new ArrayList<>(); // plugin org.openspcoop2.monitor.engine.plugins.statistic.StatisticByResource

  49.     public BasicServiceLibrary getBasicServiceLibrary() {
  50.         return this.basicServiceLibrary;
  51.     }
  52.     public void setBasicServiceLibrary(BasicServiceLibrary basicServiceLibrary) {
  53.         this.basicServiceLibrary = basicServiceLibrary;
  54.     }
  55.     public List<ConfigurazioneStatistica> getStatisticsActionLibrary() {
  56.         return this.statisticsActionLibrary;
  57.     }
  58.     public void setStatisticsActionLibrary(List<ConfigurazioneStatistica> searchActionLibrary) {
  59.         this.statisticsActionLibrary = searchActionLibrary;
  60.     }
  61.     public List<ConfigurazioneStatistica> getStatisticsActionAllLibrary() {
  62.         return this.statisticsActionAllLibrary;
  63.     }
  64.     public void setStatisticsActionAllLibrary(
  65.             List<ConfigurazioneStatistica> searchActionAllLibrary) {
  66.         this.statisticsActionAllLibrary = searchActionAllLibrary;
  67.     }
  68.     public boolean isPluginStatiTransazioniEnabled() {
  69.         return this.pluginStatiTransazioniEnabled;
  70.     }
  71.     public void setPluginStatiTransazioniEnabled(boolean pluginStatiTransazioniEnabled) {
  72.         this.pluginStatiTransazioniEnabled = pluginStatiTransazioniEnabled;
  73.     }
  74.     public List<String> getPluginRisorseTransazioni() {
  75.         return this.pluginRisorseTransazioni;
  76.     }
  77.     public void setPluginRisorseTransazioni(List<String> pluginRisorseTransazioni) {
  78.         this.pluginRisorseTransazioni = pluginRisorseTransazioni;
  79.     }



  80.     // ****** UTILS ***************

  81.     @Override
  82.     public String toString(){
  83.         StringBuilder bf = new StringBuilder();

  84.         if(this.basicServiceLibrary==null){
  85.             bf.append("BasicServiceLibrary: notDefined");
  86.             bf.append("\n");
  87.         }
  88.         else{
  89.             bf.append(this.basicServiceLibrary.toString());
  90.         }

  91.            
  92.         if(this.getStatisticsActionLibrary()!=null){
  93.             for (int i = 0; i < this.getStatisticsActionLibrary().size(); i++) {
  94.                 bf.append("StatisticsActionLibrary["+i+"]: ");
  95.                 bf.append("id[").
  96.                 append(this.getStatisticsActionLibrary().get(i).getIdConfigurazioneStatistica()).
  97.                 append("] plugin.className[").
  98.                 append(this.getStatisticsActionLibrary().get(i).getPlugin().getClassName()).
  99.                 append("] plugin.description[").
  100.                 append(this.getStatisticsActionLibrary().get(i).getPlugin().getDescrizione()).
  101.                 append("] plugin.label[").
  102.                 append(this.getStatisticsActionLibrary().get(i).getPlugin().getLabel()).
  103.                 append("] label[").
  104.                 append(this.getStatisticsActionLibrary().get(i).getLabel()).
  105.                 append("] enabled[").
  106.                 append(this.getStatisticsActionLibrary().get(i).isEnabled()).
  107.                 append("]");
  108.                 bf.append("\n");
  109.             }
  110.         }else{
  111.             bf.append("StatisticsActionLibrary: ");
  112.             bf.append("-");
  113.         }
  114.         bf.append("\n");

  115.        
  116.         if(this.getStatisticsActionAllLibrary()!=null){
  117.             for (int i = 0; i < this.getStatisticsActionAllLibrary().size(); i++) {
  118.                 bf.append("StatisticsActionAllLibrary["+i+"]: ");
  119.                 bf.append("id[").
  120.                 append(this.getStatisticsActionAllLibrary().get(i).getIdConfigurazioneStatistica()).
  121.                 append("] plugin.className[").
  122.                 append(this.getStatisticsActionAllLibrary().get(i).getPlugin().getClassName()).
  123.                 append("] plugin.description[").
  124.                 append(this.getStatisticsActionAllLibrary().get(i).getPlugin().getDescrizione()).
  125.                 append("] plugin.label[").
  126.                 append(this.getStatisticsActionAllLibrary().get(i).getPlugin().getLabel()).
  127.                 append("] label[").
  128.                 append(this.getStatisticsActionAllLibrary().get(i).getLabel()).
  129.                 append("] enabled[").
  130.                 append(this.getStatisticsActionAllLibrary().get(i).isEnabled()).
  131.                 append("]");
  132.                 bf.append("\n");
  133.             }
  134.         }else{
  135.             bf.append("StatisticsActionAllLibrary: ");
  136.             bf.append("-");
  137.         }
  138.         bf.append("\n");


  139.         return bf.toString();
  140.     }


  141.     public List<ConfigurazioneStatistica> mergeServiceActionSearchLibrary(boolean onlyEnabled, boolean orderByLabel) throws Exception{

  142.         Map<String,ConfigurazioneStatistica> plugins = new HashMap<String,ConfigurazioneStatistica>();

  143.         if(this.pluginStatiTransazioniEnabled){
  144.             ConfigurazioneStatistica conf = this.buildNewConfigurazioneStatistica(StatisticByState.ID, StatisticByState.LABEL,
  145.                     StatisticByState.DESCRIZIONE,StatisticByState.class);
  146.             plugins.put(conf.getPlugin().getClassName(), conf);
  147.         }
  148.        
  149.         if(this.pluginRisorseTransazioni!=null && this.pluginRisorseTransazioni.size()>0){
  150.             ConfigurazioneStatistica conf = this.buildNewConfigurazioneStatistica(StatisticByResource.ID, StatisticByResource.LABEL,
  151.                     StatisticByResource.DESCRIZIONE,StatisticByResource.class);
  152.             plugins.put(conf.getPlugin().getClassName(), conf);
  153.         }
  154.        
  155.         // Leggo le risorse associate all'azione specifica
  156.         if(this.statisticsActionLibrary!=null && this.statisticsActionLibrary.size()>0){
  157.             for (ConfigurazioneStatistica configurazioneStatistica : this.statisticsActionLibrary) {
  158.                 if(!onlyEnabled || configurazioneStatistica.isEnabled()){
  159.                     plugins.put(configurazioneStatistica.getPlugin().getClassName(), configurazioneStatistica);
  160.                 }
  161.             }
  162.         }

  163.         // Leggo le risorse associate all'azione '*'
  164.         if(this.statisticsActionAllLibrary!=null && this.statisticsActionAllLibrary.size()>0){
  165.             for (ConfigurazioneStatistica configurazioneStatistica : this.statisticsActionAllLibrary) {

  166.                 String idRisorsa = configurazioneStatistica.getPlugin().getClassName();
  167.                 if(plugins.containsKey(idRisorsa)==false){

  168.                     // inserisco solo le risorse il cui idPlugin non siano giĆ  stati inseriti per l'azione specifica

  169.                     if(!onlyEnabled || configurazioneStatistica.isEnabled()){
  170.                         plugins.put(configurazioneStatistica.getPlugin().getClassName(), configurazioneStatistica);
  171.                     }
  172.                 }

  173.             }
  174.         }

  175.         List<ConfigurazioneStatistica> list = new ArrayList<ConfigurazioneStatistica>();
  176.         if(orderByLabel){
  177.             List<String> sortedKey = new ArrayList<>();
  178.             Map<String, ConfigurazioneStatistica> mapLabelToCorrelazioneStatistica = new HashMap<String, ConfigurazioneStatistica>();
  179.             int count = 0; // lo uso per gestire eventuali label identiche.
  180.             for (String className : plugins.keySet()) {
  181.                 ConfigurazioneStatistica cs = plugins.get(className);
  182.                 String labelKey = cs.getLabel()+count;
  183.                 sortedKey.add(labelKey);
  184.                 mapLabelToCorrelazioneStatistica.put(labelKey, cs);
  185.                 count++;
  186.             }
  187.             Collections.sort(sortedKey);
  188.             for (String sortKey : sortedKey) {
  189.                 list.add(mapLabelToCorrelazioneStatistica.get(sortKey));
  190.             }
  191.         }
  192.         else{
  193.             list.addAll(plugins.values());
  194.         }
  195.         return list;
  196.     }
  197.    

  198.     private ConfigurazioneStatistica buildNewConfigurazioneStatistica(String id,String label,String descrizione,Class<?> classPlugin){
  199.         ConfigurazioneStatistica conf = new ConfigurazioneStatistica();
  200.         conf.setEnabled(true);
  201.         conf.setIdConfigurazioneStatistica(id);
  202.         conf.setLabel(label);
  203.         InfoPlugin infoPlugin = new InfoPlugin();
  204.         infoPlugin.setClassName(classPlugin.getName());
  205.         infoPlugin.setDescrizione(descrizione);
  206.         infoPlugin.setLabel(label);
  207.         infoPlugin.setTipo(TipoPlugin.STATISTICA.getValue());
  208.         conf.setPlugin(infoPlugin);
  209.         return conf;
  210.     }
  211. }