StatisticheUtils.java
/*
* GovWay - A customizable API Gateway
* https://govway.org
*
* Copyright (c) 2005-2025 Link.it srl (https://link.it).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openspcoop2.core.statistiche.utils;
import org.openspcoop2.core.constants.CostantiDB;
import org.openspcoop2.core.statistiche.model.StatisticaModel;
import org.openspcoop2.generic_project.beans.FunctionField;
import org.openspcoop2.generic_project.beans.IField;
import org.openspcoop2.generic_project.exception.ExpressionException;
import org.openspcoop2.generic_project.exception.ExpressionNotImplementedException;
import org.openspcoop2.generic_project.expression.IExpression;
import org.openspcoop2.generic_project.expression.impl.sql.ISQLFieldConverter;
/**
* StatisticheUtils
*
* @author Poli Andrea (poli@link.it)
* @author $Author$
* @version $Rev$, $Date$
*/
public class StatisticheUtils {
private StatisticheUtils() {}
public static void selezionaRecordValidi(IExpression expr, StatisticaModel model) throws ExpressionNotImplementedException, ExpressionException{
// Seleziona record stabili: stato >= 1 (include stato 1, 2 e 3)
// stato 1: intervalli precedenti stabili
// stato 2: record ancora validi in fase di aggiornamento
// stato 3: intervallo corrente stabile
expr.greaterEquals(model.STATO_RECORD, CostantiDB.STATISTICHE_STATO_RECORD_VALIDO);
}
public static FunctionField calcolaMedia(ISQLFieldConverter fieldConverter, IField latenza, IField numeroRichieste, String alias) throws ExpressionException{
// (SUM(latenza_servizio*richieste))/SUM(richieste)
String columnLatenza = fieldConverter.toColumn(latenza, false);
String columnRichieste = fieldConverter.toColumn(numeroRichieste, false);
String sbFunctionValue = getSqlCalcolaMedia(columnLatenza, columnRichieste);
Class<?> functionValueType = Long.class;
return new FunctionField(sbFunctionValue,functionValueType,"","",alias);
}
public static String getSqlCalcolaMedia(String columnLatenza, String columnRichieste) {
// Calcola la media ponderata escludendo i record con latenza NULL o < 0
// Formula: SUM(CASE WHEN latenza >= 0 THEN latenza*richieste ELSE 0 END) /
// SUM(CASE WHEN latenza >= 0 THEN richieste ELSE 0 END)
// Compatibile con: PostgreSQL, MySQL, HSQL, Oracle, SQL Server
StringBuilder sbFunctionValue = new StringBuilder("(");
// Numeratore: somma solo le latenze valide (>= 0) moltiplicate per le richieste
sbFunctionValue.append("SUM(CASE WHEN ").append(columnLatenza).append(" >= 0 THEN ");
sbFunctionValue.append(columnLatenza).append("*").append(columnRichieste);
sbFunctionValue.append(" ELSE 0 END)");
sbFunctionValue.append("/");
// Denominatore: somma solo le richieste con latenza valida (>= 0)
sbFunctionValue.append("SUM(CASE WHEN ").append(columnLatenza).append(" >= 0 THEN ");
sbFunctionValue.append(columnRichieste);
sbFunctionValue.append(" ELSE 0 END)");
sbFunctionValue.append(")");
return sbFunctionValue.toString();
}
}