Fix Backend error:

Error loading data: Exception while fetching data (/vmEmissionSummary) : org.hibernate.exception.SQLGrammarException: could not extract ResultSet
This commit is contained in:
2025-08-18 08:03:40 +03:00
parent a66b01334d
commit 92d88df213

View File

@@ -22,7 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MainDataTableService extends BaseService<MainDataTable, MainDataTableRepo, MainDataTableCriteria, MainDataTableCriteriaSpec> {
public class MainDataTableService
extends BaseService<MainDataTable, MainDataTableRepo, MainDataTableCriteria, MainDataTableCriteriaSpec> {
@PersistenceContext
private EntityManager entityManager;
@@ -35,96 +36,95 @@ public class MainDataTableService extends BaseService<MainDataTable, MainDataTab
public List<VMEmissionSummary> getVMEmissionSummaries() {
return getVMEmissionSummaries(null, null);
}
public List<VMEmissionSummary> getVMEmissionSummaries(UUID datacenterId) {
return getVMEmissionSummaries(datacenterId, null);
}
public List<VMEmissionSummary> getVMEmissionSummaries(UUID datacenterId, UUID projectId) {
List<String> whereConditions = new ArrayList<>();
if (datacenterId != null) {
whereConditions.add("dc.id = decode(replace(:datacenterId, '-', ''), 'hex')");
}
if (projectId != null) {
whereConditions.add("p.id = decode(replace(:projectId, '-', ''), 'hex')");
whereConditions.add("v.project = :projectId");
}
String whereClause = whereConditions.isEmpty() ? "" :
"WHERE " + String.join(" AND ", whereConditions) + " ";
String whereClause = whereConditions.isEmpty() ? "" : "WHERE " + String.join(" AND ", whereConditions) + " ";
String sql = """
SELECT
CAST(v.id AS VARCHAR) as vm_id,
v.vm_name as vm_name,
v.power as vm_power,
v.state as vm_status,
mdt.total_emission,
mdt.created_date,
pm.name as physical_machine_name,
p.name as project_name,
dc.data_center_name as datacenter_name,
mdt.co2,
mdt.ch4,
mdt.n2o
FROM main_data_table mdt
JOIN vm v ON mdt.vm_id = v.id
LEFT JOIN physical_machine pm ON v.physical_machine_id = pm.id
LEFT JOIN project p ON pm.project_id = p.id
LEFT JOIN data_center dc ON p.data_center_id = dc.id
""" + whereClause + """
ORDER BY mdt.created_date DESC, v.vm_name
""";
SELECT
CAST(v.id AS VARCHAR) as vm_id,
v.vm_name as vm_name,
v.power as vm_power,
v.state as vm_status,
mdt.total_emission,
mdt.created_date,
pm.name as physical_machine_name,
v.project as project_name,
dc.data_center_name as datacenter_name,
mdt.co2,
mdt.ch4,
mdt.n2o
FROM main_data_table mdt
JOIN vm v ON mdt.vm_id = v.id
LEFT JOIN physical_machine pm ON v.physical_machine_id = pm.id
LEFT JOIN data_center dc ON pm.data_center_id = dc.id
""" + whereClause + """
ORDER BY mdt.created_date DESC, v.vm_name
""";
Query query = entityManager.createNativeQuery(sql);
// Add parameters if provided
if (datacenterId != null) {
query.setParameter("datacenterId", datacenterId.toString());
}
if (projectId != null) {
query.setParameter("projectId", projectId.toString());
}
@SuppressWarnings("unchecked")
List<Object[]> results = query.getResultList();
return results.stream().map(row -> {
VMEmissionSummary summary = new VMEmissionSummary();
// Handle UUID conversion from hex format
String uuidStr = (String) row[0];
UUID vmId = convertHexToUUID(uuidStr);
summary.setVmId(vmId);
summary.setVmName((String) row[1]);
summary.setVmPower((Double) row[2]);
summary.setVmStatus((String) row[3]);
summary.setTotalEmission((Double) row[4]);
// Convert Timestamp to LocalDateTime for created_date
Timestamp timestamp = (Timestamp) row[5];
if (timestamp != null) {
summary.setCreatedDate(timestamp.toLocalDateTime());
}
summary.setPhysicalMachine((String) row[6]);
summary.setProject((String) row[7]);
summary.setDataCenter((String) row[8]);
// Individual emission values
summary.setCo2((Double) row[9]);
summary.setCh4((Double) row[10]);
summary.setN2o((Double) row[11]);
return summary;
}).collect(Collectors.toList());
}
/**
* Converts PostgreSQL hex format UUID to proper UUID format
*
* @param hexUuid UUID in hex format (e.g., \x6205c18b8d1e4f0fa5154212fb44050b)
* @return UUID object
*/
@@ -134,11 +134,11 @@ public class MainDataTableService extends BaseService<MainDataTable, MainDataTab
String hex = hexUuid.substring(2);
// Insert hyphens to make it a proper UUID format
// UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
String formatted = hex.substring(0, 8) + "-" +
hex.substring(8, 12) + "-" +
hex.substring(12, 16) + "-" +
hex.substring(16, 20) + "-" +
hex.substring(20);
String formatted = hex.substring(0, 8) + "-" +
hex.substring(8, 12) + "-" +
hex.substring(12, 16) + "-" +
hex.substring(16, 20) + "-" +
hex.substring(20);
return UUID.fromString(formatted);
} else {
// If it's already in proper format, parse directly