Fix su clear internal sqlite db
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good
This commit is contained in:
@@ -21,7 +21,6 @@ import org.springframework.stereotype.Service;
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -253,41 +252,53 @@ public class LogService {
|
||||
@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.MINUTES, zone = "Europe/Rome")
|
||||
public void checkLogSize() throws Exception {
|
||||
String maxSize = settingsModel.getLoggerConfiguration().getDbMaxSize();
|
||||
if (UtilityString.isNullOrEmpty(maxSize)) {
|
||||
maxSize = "1G"; // Valore predefinito
|
||||
}
|
||||
|
||||
// Trova il valore numerico e l'unità di misura
|
||||
String pattern = "([\\d.]+)\\s*(M|MB|G|GB)?";
|
||||
java.util.regex.Pattern r = java.util.regex.Pattern.compile(pattern);
|
||||
java.util.regex.Matcher m = r.matcher(maxSize);
|
||||
|
||||
BigDecimal numericValue = BigDecimal.ONE;
|
||||
String um = "G";
|
||||
if (!UtilityString.isNullOrEmpty(maxSize)) {
|
||||
um = Arrays.stream(maxSize.split("[0-9-.]")).filter(x -> !UtilityString.isNullOrEmpty(x)).findFirst().orElse(null);
|
||||
if (UtilityString.isNullOrEmpty(um)) {
|
||||
numericValue = new BigDecimal(maxSize);
|
||||
um = "G";
|
||||
} else if (!Arrays.asList("M", "MB", "G", "GB").contains(um.toUpperCase().trim()) || maxSize.indexOf(um) == 0) {
|
||||
um = "G";
|
||||
} else {
|
||||
numericValue = new BigDecimal(maxSize.substring(0, maxSize.indexOf(um)));
|
||||
String um = "G"; // Default
|
||||
|
||||
if (m.find()) {
|
||||
numericValue = new BigDecimal(m.group(1));
|
||||
if (m.group(2) != null) {
|
||||
um = m.group(2).toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
int pow = 3;
|
||||
if (Arrays.asList("M", "MB").contains(um.toUpperCase().trim())) {
|
||||
pow = 2;
|
||||
}
|
||||
// Calcola il fattore di moltiplicazione in base all'unità
|
||||
int pow = um.startsWith("M") ? 2 : 3;
|
||||
|
||||
// Calcola il massimo numero di byte
|
||||
long maxBytes = numericValue.multiply(BigDecimal.valueOf(1024).pow(pow)).longValue();
|
||||
|
||||
// Verifica la dimensione del file e pulisci se necessario
|
||||
File logDbFile = new File(UtilityDirs.getConfigPath() + "logs.db");
|
||||
if (logDbFile.length() > maxBytes) {
|
||||
if (logDbFile.exists() && logDbFile.length() > maxBytes) {
|
||||
int deletedRows = cleanOlderLogs();
|
||||
logger.trace(String.format("Rimossi %d record dal log_db per eccesso di spazio", deletedRows));
|
||||
logger.trace("Rimossi {} record dal log_db per eccesso di spazio", deletedRows);
|
||||
}
|
||||
}
|
||||
|
||||
private int cleanOlderLogs() throws Exception {
|
||||
int hours = 6;
|
||||
int hours = 7 * 24; // 7 giorni in ore
|
||||
String sql = String.format("DELETE FROM app_logs\n" +
|
||||
" WHERE entry_date < (SELECT entry_date FROM app_logs ORDER BY entry_date LIMIT 1) + (%d * 3600000)", hours);
|
||||
try (Connection connection = ConnectionFactory.getDatabaseConnection();
|
||||
PreparedStatement ps = connection.prepareStatement(sql)) {
|
||||
return ps.executeUpdate();
|
||||
try (Connection connection = ConnectionFactory.getDatabaseConnection()) {
|
||||
PreparedStatement ps = connection.prepareStatement(sql);
|
||||
int deletedRows = ps.executeUpdate();
|
||||
ps.close();
|
||||
|
||||
ps = connection.prepareStatement("pragma wal_checkpoint(full); VACUUM;");
|
||||
ps.execute();
|
||||
ps.close();
|
||||
|
||||
return deletedRows;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user