diff --git a/sge-backend/message.json b/sge-backend/message.json index d6a55ed..f109f80 100644 --- a/sge-backend/message.json +++ b/sge-backend/message.json @@ -1,493 +1,548 @@ { - "158f073fbd554d4a9c32309ea7d1865e":{ - "main_optimization_space":{ - "vms":{ - "10.150.1.131":{ - "vm_name":"BLC-AYPOS-Development-20", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"aypos.test.flavour.10", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-20", - 2, - 1, - 8, - "compute2" - ] - }, - "10.150.1.186":{ - "vm_name":"BLC-AYPOS-Development-21", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"aypos.test.flavour.10", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-21", - 2, - 1, - 8, - "compute2" - ] - } - }, - "pms":{ - "10.150.1.33":{ - "tag":"None", - "name":"compute2", - "power":177.83822950819666, - "confg":[ - "compute2", - 3, - 94, - 59, - 114 - ] - }, - "10.150.1.34":{ - "tag":"None", - "name":"compute3", - "power":21.3385737704918, - "confg":[ - "compute3", - 24, - 115, - 151, - 114 - ] - }, - "10.150.1.35":{ - "tag":"None", - "name":"compute4", - "power":152.66185245901653, - "confg":[ - "compute4", - -13, - 28, - 103, - 114 - ] - }, - "10.150.1.32":{ - "tag":"None", - "name":"compute1", - "power":36.34004918032787, - "confg":[ - "compute1", - 10, - 97, - 211, - 114 - ] - } - } - }, - "sub_optimization_space":{ - - } - }, - "48966d34ff274def88db7f7b6d9f5cdc":{ - "main_optimization_space":{ - "vms":{ - "10.150.1.101":{ - "vm_name":"Bgreen", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"m1.medium", - "tag":"None", - "confg":[ - "Bgreen", - 2, - 4, - 40, - "compute4" - ] - }, - "10.150.1.107":{ - "vm_name":"BLC-AYPOS-Development-1", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.34", - "host":"compute3", - "flavor_name":"aypos.test.flavour.8", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-1", - 3, - 3, - 8, - "compute3" - ] - }, - "10.150.1.112":{ - "vm_name":"nginx-proxyV2", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"m1.small", - "tag":"None", - "confg":[ - "nginx-proxyV2", - 1, - 2, - 20, - "compute2" - ] - }, - "10.150.1.114":{ - "vm_name":"BLC-AYPOS-Development-5", - "power":8.541673488756713, - "state":"active", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"aypos.test.flavour.19", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-5", - 2, - 3, - 8, - "compute4" - ] - }, - "10.150.1.123":{ - "vm_name":"SGE", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"ForJAVA", - "tag":"None", - "confg":[ - "SGE", - 8, - 32, - 150, - "compute4" - ] - }, - "10.150.1.130":{ - "vm_name":"aybuke", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"m1.public", - "tag":"None", - "confg":[ - "aybuke", - 8, - 8, - 50, - "compute4" - ] - }, - "10.150.1.133":{ - "vm_name":"BLC-AYPOS-Development-2", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.34", - "host":"compute3", - "flavor_name":"aypos.test.flavour.15", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-2", - 2, - 4, - 8, - "compute3" - ] - }, - "10.150.1.141":{ - "vm_name":"BLC-AYPOS-Development-0", - "power":10.146380530416682, - "state":"active", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"aypos.test.flavour.11", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-0", - 3, - 3, - 8, - "compute2" - ] - }, - "10.150.1.145":{ - "vm_name":"BLC-AYPOS-Development-9", - "power":12.464360819208926, - "state":"active", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"aypos.test.flavour.0", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-9", - 2, - 1, - 8, - "compute2" - ] - }, - "10.150.1.150":{ - "vm_name":"blc-cloud", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.32", - "host":"compute1", - "flavor_name":"m1.xlarge", - "tag":"None", - "confg":[ - "blc-cloud", - 8, - 16, - 160, - "compute1" - ] - }, - "10.150.1.155":{ - "vm_name":"Aypos-Ulak", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"m1.vrealize", - "tag":"None", - "confg":[ - "Aypos-Ulak", - 8, - 16, - 50, - "compute4" - ] - }, - "10.150.1.167":{ - "vm_name":"prometheus_serverV3", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"ayposYedek", - "tag":"None", - "confg":[ - "prometheus_serverV3", - 2, - 6, - 60, - "compute2" - ] - }, - "10.150.1.171":{ - "vm_name":"blc-meet", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"m1.devstack", - "tag":"None", - "confg":[ - "blc-meet", - 4, - 7, - 60, - "compute4" - ] - }, - "10.150.1.172":{ - "vm_name":"blc-office", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.32", - "host":"compute1", - "flavor_name":"m1.large", - "tag":"None", - "confg":[ - "blc-office", - 4, - 8, - 80, - "compute1" - ] - }, - "10.150.1.174":{ - "vm_name":"mail-server", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"mail-server", - "tag":"None", - "confg":[ - "mail-server", - 4, - 8, - 100, - "compute4" - ] - }, - "10.150.1.175":{ - "vm_name":"BLC-AYPOS-Development-7", - "power":16.084780100161176, - "emissionsource":{ - "dizel":30, - "coal":40, - "solar energy":30 + "id":111, + "data_center":"new lab blc", + "projects":{ + "48966d34ff274def88db7f7b6d9f5cdc":{ + "main_optimization_space":{ + "vms":{ + "10.150.1.101":{ + "vm_name":"Bgreen", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"m1.medium", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "Bgreen", + 2, + 4, + 40, + "compute4" + ] }, - "state":"active", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"aypos.test.flavour.19", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-7", - 2, - 3, - 8, - "compute2" - ] + "10.150.1.107":{ + "vm_name":"BLC-AYPOS-Development-1", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.34", + "host":"compute3", + "flavor_name":"aypos.test.flavour.8", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-1", + 3, + 3, + 8, + "compute3" + ] + }, + "10.150.1.112":{ + "vm_name":"nginx-proxyV2", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.33", + "host":"compute2", + "flavor_name":"m1.small", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "nginx-proxyV2", + 1, + 2, + 20, + "compute2" + ] + }, + "10.150.1.114":{ + "vm_name":"BLC-AYPOS-Development-5", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.33", + "host":"compute2", + "flavor_name":"aypos.test.flavour.19", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-5", + 2, + 3, + 8, + "compute2" + ] + }, + "10.150.1.123":{ + "vm_name":"SGE", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"ForJAVA", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "SGE", + 8, + 32, + 150, + "compute4" + ] + }, + "10.150.1.130":{ + "vm_name":"aybuke", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"m1.public", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "aybuke", + 8, + 8, + 50, + "compute4" + ] + }, + "10.150.1.133":{ + "vm_name":"BLC-AYPOS-Development-2", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.34", + "host":"compute3", + "flavor_name":"aypos.test.flavour.15", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-2", + 2, + 4, + 8, + "compute3" + ] + }, + "10.150.1.141":{ + "vm_name":"BLC-AYPOS-Development-0", + "power":12.178434411667777, + "state":"active", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"aypos.test.flavour.11", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-0", + 3, + 3, + 8, + "compute4" + ] + }, + "10.150.1.145":{ + "vm_name":"BLC-AYPOS-Development-9", + "power":14.950904196341009, + "state":"active", + "hosting_pm":"10.150.1.33", + "host":"compute2", + "flavor_name":"aypos.test.flavour.0", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-9", + 2, + 1, + 8, + "compute2" + ] + }, + "10.150.1.150":{ + "vm_name":"blc-cloud", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.32", + "host":"compute1", + "flavor_name":"m1.xlarge", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "blc-cloud", + 8, + 16, + 160, + "compute1" + ] + }, + "10.150.1.155":{ + "vm_name":"Aypos-Ulak", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"m1.vrealize", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "Aypos-Ulak", + 8, + 16, + 50, + "compute4" + ] + }, + "10.150.1.167":{ + "vm_name":"prometheus_serverV3", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.33", + "host":"compute2", + "flavor_name":"ayposYedek", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "prometheus_serverV3", + 2, + 6, + 60, + "compute2" + ] + }, + "10.150.1.171":{ + "vm_name":"blc-meet", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"m1.devstack", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "blc-meet", + 4, + 7, + 60, + "compute4" + ] + }, + "10.150.1.172":{ + "vm_name":"blc-office", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.32", + "host":"compute1", + "flavor_name":"m1.large", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "blc-office", + 4, + 8, + 80, + "compute1" + ] + }, + "10.150.1.174":{ + "vm_name":"mail-server", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"mail-server", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "mail-server", + 4, + 8, + 100, + "compute4" + ] + }, + "10.150.1.175":{ + "vm_name":"BLC-AYPOS-Development-7", + "power":19.256314103409117, + "state":"active", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"aypos.test.flavour.19", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-7", + 2, + 3, + 8, + "compute4" + ] + }, + "10.150.1.180":{ + "vm_name":"BLC-AYPOS-Development-8", + "power":26.738567147437937, + "state":"active", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"aypos.test.flavour.6", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-8", + 1, + 2, + 8, + "compute4" + ] + }, + "10.150.1.190":{ + "vm_name":"BLC-AYPOS-Development-6", + "power":42.36942549990342, + "state":"active", + "hosting_pm":"10.150.1.33", + "host":"compute2", + "flavor_name":"aypos.test.flavour.16", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-6", + 2, + 4, + 8, + "compute2" + ] + }, + "10.150.1.193":{ + "vm_name":"BLC-AYPOS-Development-4", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.34", + "host":"compute3", + "flavor_name":"aypos.test.flavour.4", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "BLC-AYPOS-Development-4", + 2, + 1, + 8, + "compute3" + ] + }, + "10.150.1.199":{ + "vm_name":"gitea-server", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.33", + "host":"compute2", + "flavor_name":"m1.large", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "gitea-server", + 4, + 8, + 80, + "compute2" + ] + }, + "10.150.1.200":{ + "vm_name":"ollama", + "power":0, + "state":"inactive", + "hosting_pm":"10.150.1.35", + "host":"compute4", + "flavor_name":"m1.vrealize", + "emissionsource":{ + "Dizel":10, + "Ham petrol":5, + "Orimulsion":15, + "Motor benzini":30, + "Jet benzini":40 + }, + "tag":"main_optimization_space", + "confg":[ + "ollama", + 8, + 16, + 50, + "compute4" + ] + } }, - "10.150.1.180":{ - "vm_name":"BLC-AYPOS-Development-8", - "power":22.454381667855632, - "state":"active", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"aypos.test.flavour.6", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-8", - 1, - 2, - 8, - "compute2" - ] - }, - "10.150.1.190":{ - "vm_name":"BLC-AYPOS-Development-6", - "power":36.17691364518403, - "state":"active", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"aypos.test.flavour.16", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-6", - 2, - 4, - 8, - "compute2" - ] - }, - "10.150.1.193":{ - "vm_name":"BLC-AYPOS-Development-4", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.34", - "host":"compute3", - "flavor_name":"aypos.test.flavour.4", - "tag":"None", - "confg":[ - "BLC-AYPOS-Development-4", - 2, - 1, - 8, - "compute3" - ] - }, - "10.150.1.199":{ - "vm_name":"gitea-server", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.33", - "host":"compute2", - "flavor_name":"m1.large", - "tag":"None", - "confg":[ - "gitea-server", - 4, - 8, - 80, - "compute2" - ] - }, - "10.150.1.200":{ - "vm_name":"ollama", - "power":0, - "state":"inactive", - "hosting_pm":"10.150.1.35", - "host":"compute4", - "flavor_name":"m1.vrealize", - "tag":"None", - "confg":[ - "ollama", - 8, - 16, - 50, - "compute4" - ] + "pms":{ + "10.150.1.33":{ + "tag":"main_optimization_space", + "name":"compute2", + "power":159.86075409836062, + "confg":[ + "compute2", + 11, + 101, + 91, + 114 + ] + }, + "10.150.1.34":{ + "tag":"main_optimization_space", + "name":"compute3", + "power":21.31772131147542, + "confg":[ + "compute3", + 24, + 115, + 151, + 114 + ] + }, + "10.150.1.35":{ + "tag":"main_optimization_space", + "name":"compute4", + "power":189.92359016393448, + "confg":[ + "compute4", + -17, + 23, + 87, + 114 + ] + }, + "10.150.1.32":{ + "tag":"main_optimization_space", + "name":"compute1", + "power":36.45219672131149, + "confg":[ + "compute1", + 10, + 97, + 211, + 114 + ] + } } }, - "pms":{ - "10.150.1.33":{ - "tag":"None", - "name":"compute2", - "power":177.83822950819666, - "confg":[ - "compute2", - 3, - 94, - 59, - 114 - ] - }, - "10.150.1.34":{ - "tag":"None", - "name":"compute3", - "power":21.3385737704918, - "confg":[ - "compute3", - 24, - 115, - 151, - 114 - ] - }, - "10.150.1.35":{ - "tag":"None", - "name":"compute4", - "power":152.66185245901653, - "confg":[ - "compute4", - -13, - 28, - 103, - 114 - ] - }, - "10.150.1.32":{ - "tag":"None", - "name":"compute1", - "power":36.34004918032787, - "confg":[ - "compute1", - 10, - 97, - 211, - 114 - ] - } + "sub_optimization_space":{ + } - }, - "sub_optimization_space":{ - } } } \ No newline at end of file diff --git a/sge-backend/src/main/java/com/sgs/graphql/dataCenter/domain/VM.java b/sge-backend/src/main/java/com/sgs/graphql/dataCenter/domain/VM.java index 6aa5dc3..9305f63 100644 --- a/sge-backend/src/main/java/com/sgs/graphql/dataCenter/domain/VM.java +++ b/sge-backend/src/main/java/com/sgs/graphql/dataCenter/domain/VM.java @@ -2,8 +2,13 @@ package com.sgs.graphql.dataCenter.domain; import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonManagedReference; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import com.sgs.lib.dao.domain.BaseDomain; +import java.util.Map; +import java.util.HashMap; + import javax.persistence.*; @Entity @@ -18,9 +23,11 @@ public class VM extends BaseDomain { private String host; private String flavorName; private String tag; + private String emissionSourceData; // JSON string to store emission source map private Config config; private PhysicalMachine physicalMachine; + @Column(name = "state") public String getState() { return state; @@ -102,6 +109,15 @@ public class VM extends BaseDomain { this.ip = ip; } + @Column(name = "emission_source_data", columnDefinition = "text") + public String getEmissionSourceData() { + return emissionSourceData; + } + + public void setEmissionSourceData(String emissionSourceData) { + this.emissionSourceData = emissionSourceData; + } + @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "vm") @JsonManagedReference public Config getConfig() { @@ -123,4 +139,33 @@ public class VM extends BaseDomain { this.physicalMachine = physicalMachine; } + // Helper methods for emission source map + @Transient + public Map getEmissionSource() { + if (emissionSourceData == null || emissionSourceData.trim().isEmpty()) { + return new HashMap<>(); + } + try { + ObjectMapper mapper = new ObjectMapper(); + return mapper.readValue(emissionSourceData, new TypeReference>() {}); + } catch (Exception e) { + System.err.println("Error parsing emission source data: " + e.getMessage()); + return new HashMap<>(); + } + } + + public void setEmissionSource(Map emissionSource) { + if (emissionSource == null) { + this.emissionSourceData = null; + return; + } + try { + ObjectMapper mapper = new ObjectMapper(); + this.emissionSourceData = mapper.writeValueAsString(emissionSource); + } catch (Exception e) { + System.err.println("Error serializing emission source data: " + e.getMessage()); + this.emissionSourceData = null; + } + } + } \ No newline at end of file diff --git a/sge-backend/src/main/java/com/sgs/graphql/dataCenter/dto/VMDto.java b/sge-backend/src/main/java/com/sgs/graphql/dataCenter/dto/VMDto.java index 0084be8..4a106b0 100644 --- a/sge-backend/src/main/java/com/sgs/graphql/dataCenter/dto/VMDto.java +++ b/sge-backend/src/main/java/com/sgs/graphql/dataCenter/dto/VMDto.java @@ -2,6 +2,7 @@ package com.sgs.graphql.dataCenter.dto; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; +import java.util.Map; public class VMDto { @JsonProperty("vm_name") @@ -28,6 +29,9 @@ public class VMDto { @JsonProperty("confg") // Note: keeping the typo from JSON private List confg; + @JsonProperty("emissionsource") + private Map emissionSource; + private String ip; // IP address from the message key private boolean calcOn = false; // Derived from state: active = true, inactive = false @@ -113,6 +117,14 @@ public class VMDto { this.calcOn = calcOn; } + public Map getEmissionSource() { + return emissionSource; + } + + public void setEmissionSource(Map emissionSource) { + this.emissionSource = emissionSource; + } + // Helper method to get config as ConfigDto public ConfigDto getConfig() { if (confg != null && confg.size() >= 5) { diff --git a/sge-backend/src/main/java/com/sgs/utils/rabbitMQ/MessageListener.java b/sge-backend/src/main/java/com/sgs/utils/rabbitMQ/MessageListener.java index 3ee1e5e..43af254 100644 --- a/sge-backend/src/main/java/com/sgs/utils/rabbitMQ/MessageListener.java +++ b/sge-backend/src/main/java/com/sgs/utils/rabbitMQ/MessageListener.java @@ -92,7 +92,14 @@ public class MessageListener { DataCenter entity = toDataCenterEntity(dto); DataCenter dataCenter = createDataCenter(entity); - processEmissionCalculations(dataCenter); + + // Save message processing success log + systemLogger.createSystemLog(LogType.INFO, + "VM data processed successfully for DataCenter ID: " + dataCenter.getExternalId()); + + // Process emission calculations in a separate transaction to avoid rollback issues + // This will be done asynchronously after the VM data is committed + processEmissionCalculationsAsync(dataCenter); // System.out.println(entity.toString()); System.out.println("✅ Raw JSON message:\n" + message); @@ -117,7 +124,11 @@ public class MessageListener { //System.out.println("✅ Saved DataCenter:\n" + objectMapper.writeValueAsString(dataCenter)); } catch (Exception e) { - System.err.println("Mesaj işlenirken hata oluştu: " + e.getMessage()); + System.err.println("❌ Mesaj işlenirken hata oluştu: " + e.getMessage()); + e.printStackTrace(); + systemLogger.createSystemLog(LogType.ERROR, + "Error processing VM message: " + e.getMessage()); + // Don't rethrow the exception to prevent transaction rollback } } @@ -153,8 +164,14 @@ public class MessageListener { // Handle config from confg array entity.setConfig(toConfigEntity(dto.getConfig())); + // Handle emission source map + entity.setEmissionSource(dto.getEmissionSource()); + // Debug logging System.out.println("🔍 VM Entity Created: " + dto.getVmName() + " - state = " + dto.getState() + " - calcOn = " + dto.isCalcOn()); + if (dto.getEmissionSource() != null && !dto.getEmissionSource().isEmpty()) { + System.out.println(" Emission Sources: " + dto.getEmissionSource()); + } return entity; } @@ -401,6 +418,7 @@ public class MessageListener { vm.setHost(newVm.getHost()); vm.setFlavorName(newVm.getFlavorName()); vm.setTag(newVm.getTag()); + vm.setEmissionSource(newVm.getEmissionSource()); System.out.println("✅ Updated existing VM: " + vm.getVmName() + " (IP: " + vm.getIp() + ") - calcOn = " + vm.getCalcOn()); } else { @@ -450,6 +468,32 @@ public class MessageListener { } + /** + * Process emission calculations asynchronously to avoid transaction conflicts + */ + public void processEmissionCalculationsAsync(DataCenter dataCenter) { + // Run emission calculations in a separate thread to avoid transaction issues + new Thread(() -> { + try { + // Wait a bit to ensure the transaction is committed and VMs have IDs + Thread.sleep(2000); + + // Refresh the DataCenter to get the latest state with VM IDs + Optional refreshedDcOpt = dataCenterRepo.findByExternalId(dataCenter.getExternalId()); + if (refreshedDcOpt.isPresent()) { + DataCenter refreshedDc = refreshedDcOpt.get(); + System.out.println("🔄 Processing emission calculations asynchronously for DataCenter: " + refreshedDc.getDataCenter()); + processEmissionCalculations(refreshedDc); + } else { + System.err.println("❌ Could not find DataCenter for emission calculations: " + dataCenter.getExternalId()); + } + } catch (Exception e) { + System.err.println("❌ Error in async emission calculations: " + e.getMessage()); + e.printStackTrace(); + } + }).start(); + } + public void processEmissionCalculations(DataCenter dataCenter) { System.out.println("🔍 Starting VM-level emission calculations for DataCenter ID: " + dataCenter.getId() + " (Name: " + dataCenter.getDataCenter() + ")"); @@ -476,7 +520,10 @@ public class MessageListener { " (Name: " + dataCenter.getDataCenter() + ")"); int totalVMs = 0; int eligibleVMs = 0; + int processedVMs = 0; int successfulCalculations = 0; + int failedCalculations = 0; + int totalEmissionSources = 0; for (Project project : dataCenter.getProjects()) { for (PhysicalMachine pm : project.getPhysicalMachines()) { @@ -488,8 +535,37 @@ public class MessageListener { eligibleVMs++; if (vm.getPower() != null && vm.getPower() > 0) { System.out.println("✅ Processing VM " + vm.getVmName() + " (calcOn = true)"); - boolean success = createVMEmissionRecord(dataCenter, vm, project, pm); - if (success) successfulCalculations++; + processedVMs++; + + // Check if VM has emission sources + Map emissionSources = vm.getEmissionSource(); + if (emissionSources != null && !emissionSources.isEmpty()) { + // Create separate emission record for each emission source + System.out.println("🔍 VM has " + emissionSources.size() + " emission sources"); + totalEmissionSources += emissionSources.size(); + for (Map.Entry sourceEntry : emissionSources.entrySet()) { + String sourceName = sourceEntry.getKey(); + Integer percentage = sourceEntry.getValue(); + System.out.println(" - " + sourceName + ": " + percentage + "%"); + + boolean success = createVMEmissionRecordForSource(dataCenter, vm, project, pm, sourceName, percentage); + if (success) { + successfulCalculations++; + } else { + failedCalculations++; + } + } + } else { + // Fallback to default emission source if VM has no emission sources + System.out.println("⚠️ VM has no emission sources, using default"); + totalEmissionSources++; + boolean success = createVMEmissionRecord(dataCenter, vm, project, pm); + if (success) { + successfulCalculations++; + } else { + failedCalculations++; + } + } } else { System.out.println("⚠️ Skipping VM " + vm.getVmName() + " (calcOn = true) - no power consumption data"); } @@ -504,13 +580,21 @@ public class MessageListener { System.out.println("🎯 VM Emission Calculation Summary:"); System.out.println(" Total VMs found: " + totalVMs); System.out.println(" VMs with calcOn = true: " + eligibleVMs); + System.out.println(" VMs processed for emissions: " + processedVMs); System.out.println(" VMs skipped (calcOn = false or null): " + (totalVMs - eligibleVMs)); + System.out.println(" Total emission source calculations: " + totalEmissionSources); System.out.println(" Successful emission calculations: " + successfulCalculations); - System.out.println(" Failed calculations: " + (eligibleVMs - successfulCalculations)); + System.out.println(" Failed emission calculations: " + failedCalculations); } private boolean createVMEmissionRecord(DataCenter dataCenter, VM vm, Project project, PhysicalMachine pm) { try { + // Check if VM has an ID (is persisted) + if (vm.getId() == null) { + System.err.println("❌ VM " + vm.getVmName() + " has no ID - cannot create emission record"); + return false; + } + MainDataTableCreateInput input = createVMMainDataTableInput(dataCenter, vm, project, pm); System.out.println("🔍 Creating emission record for VM: " + vm.getVmName() + " (Power: " + vm.getPower() + "W)"); @@ -535,6 +619,52 @@ public class MessageListener { } } + private boolean createVMEmissionRecordForSource(DataCenter dataCenter, VM vm, Project project, PhysicalMachine pm, String emissionSourceName, Integer percentage) { + try { + // Check if VM has an ID (is persisted) + if (vm.getId() == null) { + System.err.println("❌ VM " + vm.getVmName() + " has no ID - cannot create emission record for source " + emissionSourceName); + return false; + } + + // Find the emission source by name/tag + List emissionSources = emissionSourceRepo.findByTag(emissionSourceName); + if (emissionSources.isEmpty()) { + System.err.println("❌ Could not find emission source: " + emissionSourceName); + return false; + } + + EmissionSource emissionSource = emissionSources.get(0); + + // Calculate power consumption for this emission source (percentage of total VM power) + double sourceSpecificPower = vm.getPower() * (percentage / 100.0); + + MainDataTableCreateInput input = createVMMainDataTableInputForSource(dataCenter, vm, project, pm, emissionSource, sourceSpecificPower, percentage); + System.out.println("🔍 Creating emission record for VM: " + vm.getVmName() + + " - Source: " + emissionSourceName + " (" + percentage + "%) - Power: " + sourceSpecificPower + "W"); + + MainDataTable result = callMainDataTableMutation(input); + + if (result != null) { + System.out.println("✅ VM Emission calculation completed for source " + emissionSourceName + ":"); + System.out.println(" - VM: " + vm.getVmName()); + System.out.println(" - Emission Source: " + emissionSourceName + " (" + percentage + "%)"); + System.out.println(" - Power: " + sourceSpecificPower + "W"); + System.out.println(" - CO2: " + result.getCo2()); + System.out.println(" - Total Emission: " + result.getTotalEmission()); + System.out.println(" - Record ID: " + result.getId()); + return true; + } else { + System.err.println("❌ Failed to create emission record for VM: " + vm.getVmName() + " - Source: " + emissionSourceName); + return false; + } + + } catch (Exception e) { + System.err.println("❌ Error calculating emissions for VM " + vm.getVmName() + " - Source: " + emissionSourceName + ": " + e.getMessage()); + return false; + } + } + private MainDataTableCreateInput createVMMainDataTableInput(DataCenter dataCenter, VM vm, Project project, PhysicalMachine pm) { MainDataTableCreateInput input = new MainDataTableCreateInput(); @@ -615,6 +745,76 @@ public class MessageListener { return input; } + private MainDataTableCreateInput createVMMainDataTableInputForSource(DataCenter dataCenter, VM vm, Project project, PhysicalMachine pm, EmissionSource emissionSource, double sourceSpecificPower, Integer percentage) { + MainDataTableCreateInput input = new MainDataTableCreateInput(); + + // Copy datacenter-level information (if available) + input.setYear("2025"); + input.setMonth("07"); + + // Note: These fields are no longer received in the message + // They need to be set via DataCenter CRUD operations first + if (dataCenter.getArea() != null && !dataCenter.getArea().getCities().isEmpty()) { + input.setCity(dataCenter.getArea().getCities().get(0).getId()); + } + + if (dataCenter.getArea() != null && !dataCenter.getArea().getDistricts().isEmpty()) { + input.setDistrict(dataCenter.getArea().getDistricts().get(0).getId()); + } + + if (dataCenter.getSector() != null) { + input.setSector(dataCenter.getSector().getId()); + } else { + System.out.println("⚠️ Warning: DataCenter has no sector set - emission calculation may fail"); + } + + if (dataCenter.getSubSector() != null) { + input.setSubSector(dataCenter.getSubSector().getId()); + } + + // Use the specific emission source for this calculation + input.setEmissionSource(emissionSource.getId()); + + if (dataCenter.getActivitySubUnit() != null) { + input.setActivitySubUnit(dataCenter.getActivitySubUnit().getId()); + } + + if (dataCenter.getConsuptionUnit() != null) { + input.setConsuptionUnit(dataCenter.getConsuptionUnit().getId()); + } + + // Default to Kapsam-3 if no emission scope is set + input.setScope(dataCenter.getEmissionScope() != null ? + dataCenter.getEmissionScope().getTag().equals("Kapsam-3") : true); + + try { + List organizations = organizationRepo.findAll(); + if (!organizations.isEmpty()) { + input.setOrganization(organizations.get(0).getId()); + } + } catch (Exception e) { + System.err.println("❌ Error finding organization: " + e.getMessage()); + } + + // Set VM-specific fields + input.setVmId(vm.getId()); + + // Use the source-specific power consumption (percentage of total VM power) + input.setConsuptionAmount(String.valueOf(sourceSpecificPower)); + + System.out.println("🔍 VM Emission Input for Source:"); + System.out.println(" VM ID: " + vm.getId()); + System.out.println(" VM Name: " + vm.getVmName()); + System.out.println(" Emission Source: " + emissionSource.getTag()); + System.out.println(" Percentage: " + percentage + "%"); + System.out.println(" Source Power: " + sourceSpecificPower + "W"); + System.out.println(" Physical Machine: " + pm.getName()); + System.out.println(" Project: " + project.getName()); + System.out.println(" DataCenter Sector: " + (dataCenter.getSector() != null ? dataCenter.getSector().getTag() : "NOT SET")); + + return input; + } + private void setupSecurityContext() { User systemUser = new User("system", "", Collections.singletonList(new SimpleGrantedAuthority("dataset_create"))); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(systemUser, null, systemUser.getAuthorities()); diff --git a/sge-backend/src/main/resources/application.properties b/sge-backend/src/main/resources/application.properties index b82dd6c..e057094 100644 --- a/sge-backend/src/main/resources/application.properties +++ b/sge-backend/src/main/resources/application.properties @@ -17,14 +17,14 @@ security.jwt.token.secret-key=secret app.survey.base-url=http://localhost.com -spring.rabbitmq.host=188.132.198.145 -spring.rabbitmq.port=5672 -spring.rabbitmq.username=testuser -spring.rabbitmq.password=JGasF24561AZv2894De -# spring.rabbitmq.host=localhost +# spring.rabbitmq.host=188.132.198.145 # spring.rabbitmq.port=5672 -# spring.rabbitmq.username=guest -# spring.rabbitmq.password=guest +# spring.rabbitmq.username=testuser +# spring.rabbitmq.password=JGasF24561AZv2894De +spring.rabbitmq.host=localhost +spring.rabbitmq.port=5672 +spring.rabbitmq.username=guest +spring.rabbitmq.password=guest spring.rabbitmq.virtual-host=/ spring.rabbitmq.connection-timeout=20000 spring.rabbitmq.template.retry.enabled=true diff --git a/sge-backend/src/main/resources/graphql/dataCenter/type.graphqls b/sge-backend/src/main/resources/graphql/dataCenter/type.graphqls index f92f563..a96fb1e 100644 --- a/sge-backend/src/main/resources/graphql/dataCenter/type.graphqls +++ b/sge-backend/src/main/resources/graphql/dataCenter/type.graphqls @@ -47,9 +47,12 @@ type Vm { host: String flavorName: String tag: String + emissionSource: EmissionSourceMap config: Config } +scalar EmissionSourceMap + type Config { id: ID cpu: Int diff --git a/sge-backend/src/main/resources/graphql/mainDataTable/type.graphqls b/sge-backend/src/main/resources/graphql/mainDataTable/type.graphqls index f322d94..416bd06 100644 --- a/sge-backend/src/main/resources/graphql/mainDataTable/type.graphqls +++ b/sge-backend/src/main/resources/graphql/mainDataTable/type.graphqls @@ -26,6 +26,7 @@ type MainDataTable { proteinAmount:Float burnOrOpenBurn:Boolean scopeCheck:Boolean + vm: Vm }