forked from BLC/sgeUpdated
Add filtering capability for showing VMEmissionSummary
This commit is contained in:
@@ -865,7 +865,7 @@ public class SgsApplication implements CommandLineRunner {
|
|||||||
if (neighborhoodService.findAll().isEmpty()) {
|
if (neighborhoodService.findAll().isEmpty()) {
|
||||||
createNeighborhoodsFromJson();
|
createNeighborhoodsFromJson();
|
||||||
}
|
}
|
||||||
if (areaService.findAll().isEmpty()) {
|
if (!cityService.findAll().isEmpty()) {
|
||||||
createDefaultArea();
|
createDefaultArea();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
import com.sgs.graphql.auth.service.AuthorizationService;
|
import com.sgs.graphql.auth.service.AuthorizationService;
|
||||||
import com.sgs.graphql.dataCenter.domain.DataCenter;
|
import com.sgs.graphql.dataCenter.domain.DataCenter;
|
||||||
|
import com.sgs.graphql.dataCenter.domain.PhysicalMachine;
|
||||||
import com.sgs.graphql.dataCenter.query.pagination.DataCenterPageable;
|
import com.sgs.graphql.dataCenter.query.pagination.DataCenterPageable;
|
||||||
import com.sgs.graphql.dataCenter.repo.DataCenterRepo;
|
import com.sgs.graphql.dataCenter.repo.DataCenterRepo;
|
||||||
|
import com.sgs.graphql.dataCenter.repo.PhysicalMachineRepo;
|
||||||
import com.sgs.graphql.dataCenter.repo.criteria.DataCenterCriteria;
|
import com.sgs.graphql.dataCenter.repo.criteria.DataCenterCriteria;
|
||||||
import com.sgs.graphql.dataCenter.service.DataCenterService;
|
import com.sgs.graphql.dataCenter.service.DataCenterService;
|
||||||
import com.sgs.graphql.systemHistory.mutation.SystemLogger;
|
import com.sgs.graphql.systemHistory.mutation.SystemLogger;
|
||||||
@@ -30,13 +32,15 @@ public class DataCenterQueryResolver implements GraphQLQueryResolver {
|
|||||||
|
|
||||||
private final DataCenterService DataCenterService;
|
private final DataCenterService DataCenterService;
|
||||||
private final DataCenterRepo dataCenterRepo;
|
private final DataCenterRepo dataCenterRepo;
|
||||||
|
private final PhysicalMachineRepo physicalMachineRepo;
|
||||||
private final AuthorizationService authorizationService;
|
private final AuthorizationService authorizationService;
|
||||||
private final SystemLogger systemLogger;
|
private final SystemLogger systemLogger;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public DataCenterQueryResolver(AuthorizationService authorizationService, SystemLogger systemLogger, DataCenterService DataCenterService, DataCenterRepo dataCenterRepo) {
|
public DataCenterQueryResolver(AuthorizationService authorizationService, SystemLogger systemLogger, DataCenterService DataCenterService, DataCenterRepo dataCenterRepo, PhysicalMachineRepo physicalMachineRepo) {
|
||||||
this.DataCenterService = DataCenterService;
|
this.DataCenterService = DataCenterService;
|
||||||
this.dataCenterRepo = dataCenterRepo;
|
this.dataCenterRepo = dataCenterRepo;
|
||||||
|
this.physicalMachineRepo = physicalMachineRepo;
|
||||||
this.authorizationService = authorizationService;
|
this.authorizationService = authorizationService;
|
||||||
this.systemLogger = systemLogger;
|
this.systemLogger = systemLogger;
|
||||||
}
|
}
|
||||||
@@ -66,4 +70,8 @@ public class DataCenterQueryResolver implements GraphQLQueryResolver {
|
|||||||
return dataCenterRepo.findByNumber(id).stream().findFirst();
|
return dataCenterRepo.findByNumber(id).stream().findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<PhysicalMachine> physicalMachines(UUID datacenterId, UUID projectId) {
|
||||||
|
return physicalMachineRepo.findByDatacenterIdAndProjectId(datacenterId, projectId);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.sgs.graphql.dataCenter.repo;
|
||||||
|
|
||||||
|
import com.sgs.graphql.dataCenter.domain.PhysicalMachine;
|
||||||
|
import com.sgs.lib.dao.repo.BaseRepo;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface PhysicalMachineRepo extends BaseRepo<PhysicalMachine> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all physical machines by datacenter ID and project ID
|
||||||
|
* @param datacenterId Datacenter ID
|
||||||
|
* @param projectId Project ID
|
||||||
|
* @return List of physical machines
|
||||||
|
*/
|
||||||
|
@Query("SELECT pm FROM PhysicalMachine pm " +
|
||||||
|
"JOIN pm.project p " +
|
||||||
|
"JOIN p.dataCenter dc " +
|
||||||
|
"WHERE dc.id = :datacenterId AND p.id = :projectId")
|
||||||
|
List<PhysicalMachine> findByDatacenterIdAndProjectId(@Param("datacenterId") UUID datacenterId,
|
||||||
|
@Param("projectId") UUID projectId);
|
||||||
|
}
|
||||||
@@ -51,9 +51,11 @@ public class MainDataTableQueryResolver implements GraphQLQueryResolver {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* GraphQL query to get VM emission summaries with hierarchy information
|
* GraphQL query to get VM emission summaries with hierarchy information
|
||||||
|
* @param datacenterId Optional datacenter ID to filter VMs by datacenter
|
||||||
|
* @param projectId Optional project ID to filter VMs by project
|
||||||
* @return List of VM emission summaries including datacenter, project, aggregate, and physical machine info
|
* @return List of VM emission summaries including datacenter, project, aggregate, and physical machine info
|
||||||
*/
|
*/
|
||||||
public List<VMEmissionSummary> vmEmissionSummary() {
|
public List<VMEmissionSummary> vmEmissionSummary(UUID datacenterId, UUID projectId) {
|
||||||
return mainDataTableService.getVMEmissionSummaries();
|
return mainDataTableService.getVMEmissionSummaries(datacenterId, projectId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import com.sgs.lib.dao.service.BaseService;
|
|||||||
import com.sgs.graphql.mainDataTable.dto.VMEmissionSummary;
|
import com.sgs.graphql.mainDataTable.dto.VMEmissionSummary;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -32,12 +33,33 @@ public class MainDataTableService extends BaseService<MainDataTable, MainDataTab
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<VMEmissionSummary> getVMEmissionSummaries() {
|
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')");
|
||||||
|
}
|
||||||
|
|
||||||
|
String whereClause = whereConditions.isEmpty() ? "" :
|
||||||
|
"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.name as vm_name,
|
v.vm_name as vm_name,
|
||||||
v.power as vm_power,
|
v.power as vm_power,
|
||||||
v.status 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,
|
||||||
@@ -48,15 +70,24 @@ public class MainDataTableService extends BaseService<MainDataTable, MainDataTab
|
|||||||
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 vms vms_active ON v.active_vms_id = vms_active.id
|
LEFT JOIN physical_machine pm ON v.physical_machine_id = pm.id
|
||||||
LEFT JOIN vms vms_inactive ON v.inactive_vms_id = vms_inactive.id
|
|
||||||
LEFT JOIN physical_machine pm ON (pm.vms_id = vms_active.id OR pm.vms_id = vms_inactive.id)
|
|
||||||
LEFT JOIN project p ON pm.project_id = p.id
|
LEFT JOIN project p ON pm.project_id = p.id
|
||||||
LEFT JOIN data_center dc ON p.data_center_id = dc.id
|
LEFT JOIN data_center dc ON p.data_center_id = dc.id
|
||||||
ORDER BY mdt.created_date DESC, v.name
|
""" + whereClause + """
|
||||||
|
ORDER BY mdt.created_date DESC, v.vm_name
|
||||||
""";
|
""";
|
||||||
|
|
||||||
Query query = entityManager.createNativeQuery(sql);
|
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")
|
@SuppressWarnings("unchecked")
|
||||||
List<Object[]> results = query.getResultList();
|
List<Object[]> results = query.getResultList();
|
||||||
|
|
||||||
|
|||||||
@@ -3,4 +3,5 @@ extend type Query{
|
|||||||
dataCenters(criteria: DataCenterCriteria, sortBy: [SortBy!]): [DataCenter!]
|
dataCenters(criteria: DataCenterCriteria, sortBy: [SortBy!]): [DataCenter!]
|
||||||
paginateDataCenters(pagination : Pagination!, criteria: DataCenterCriteria, sortBy:[SortBy!] ) : DataCenterPageable!
|
paginateDataCenters(pagination : Pagination!, criteria: DataCenterCriteria, sortBy:[SortBy!] ) : DataCenterPageable!
|
||||||
getByNumber(number: Int!): DataCenter
|
getByNumber(number: Int!): DataCenter
|
||||||
|
physicalMachines(datacenterId: ID!, projectId: ID!): [PhysicalMachine!]!
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,7 @@ type Project {
|
|||||||
id: ID
|
id: ID
|
||||||
name: String
|
name: String
|
||||||
physicalMachines: [PhysicalMachine]
|
physicalMachines: [PhysicalMachine]
|
||||||
|
dataCenter: DataCenter
|
||||||
}
|
}
|
||||||
|
|
||||||
type PhysicalMachine {
|
type PhysicalMachine {
|
||||||
@@ -34,6 +35,7 @@ type PhysicalMachine {
|
|||||||
tag: String
|
tag: String
|
||||||
power: Float
|
power: Float
|
||||||
vms: [Vm]
|
vms: [Vm]
|
||||||
|
project: Project
|
||||||
}
|
}
|
||||||
|
|
||||||
type Vm {
|
type Vm {
|
||||||
@@ -49,6 +51,7 @@ type Vm {
|
|||||||
tag: String
|
tag: String
|
||||||
emissionSource: EmissionSourceMap
|
emissionSource: EmissionSourceMap
|
||||||
config: Config
|
config: Config
|
||||||
|
physicalMachine: PhysicalMachine
|
||||||
}
|
}
|
||||||
|
|
||||||
scalar EmissionSourceMap
|
scalar EmissionSourceMap
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ extend type Query{
|
|||||||
mainDataTable(id: ID!): MainDataTable!
|
mainDataTable(id: ID!): MainDataTable!
|
||||||
mainDataTables(criteria: MainDataTableCriteria, sortBy: [SortBy!]): [MainDataTable!]
|
mainDataTables(criteria: MainDataTableCriteria, sortBy: [SortBy!]): [MainDataTable!]
|
||||||
paginateMainDataTables(pagination : Pagination!, criteria: MainDataTableCriteria, sortBy:[SortBy!] ) : MainDataTablePageable!
|
paginateMainDataTables(pagination : Pagination!, criteria: MainDataTableCriteria, sortBy:[SortBy!] ) : MainDataTablePageable!
|
||||||
vmEmissionSummary: [VMEmissionSummary!]!
|
vmEmissionSummary(datacenterId: ID, projectId: ID): [VMEmissionSummary!]!
|
||||||
}
|
}
|
||||||
@@ -51,9 +51,9 @@ type Config {
|
|||||||
disk: Int
|
disk: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
type VMEmissionSummary {
|
type VMEmissionSummary {
|
||||||
vmId: ID!
|
vmId: ID!
|
||||||
vmName: String!
|
vmName: String
|
||||||
vmPower: Float
|
vmPower: Float
|
||||||
vmStatus: String
|
vmStatus: String
|
||||||
totalEmission: Float!
|
totalEmission: Float!
|
||||||
@@ -66,7 +66,7 @@ type VMEmissionSummary {
|
|||||||
ch4: Float!
|
ch4: Float!
|
||||||
n2o: Float!
|
n2o: Float!
|
||||||
reportGeneratedTime: LocalDateTime
|
reportGeneratedTime: LocalDateTime
|
||||||
}
|
}
|
||||||
|
|
||||||
type SolidWasteSupplement {
|
type SolidWasteSupplement {
|
||||||
id: ID!
|
id: ID!
|
||||||
|
|||||||
Reference in New Issue
Block a user