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