4444import com .cloud .alert .AlertManager ;
4545import com .cloud .event .EventTypes ;
4646import com .cloud .event .UsageEventVO ;
47+ import com .cloud .event .UsageEventDetailsVO ;
4748import com .cloud .event .dao .UsageEventDao ;
4849import com .cloud .event .dao .UsageEventDetailsDao ;
4950import com .cloud .usage .dao .UsageDao ;
@@ -1067,7 +1068,7 @@ private void createVMHelperEvent(UsageEventVO event) {
10671068 usageInstance .setServiceOfferingId (soId );
10681069 usageInstance .setStartDate (event .getCreateDate ());
10691070 usageInstance .setEndDate (null );
1070- _usageInstanceDao . persist (usageInstance );
1071+ populateDynamicComputeOfferingDetailsAndPersist (usageInstance , event . getId () );
10711072 }
10721073 }
10731074
@@ -1078,7 +1079,7 @@ private void createVMHelperEvent(UsageEventVO event) {
10781079 UsageVMInstanceVO usageInstanceNew =
10791080 new UsageVMInstanceVO (UsageTypes .RUNNING_VM , zoneId , event .getAccountId (), vmId , vmName , soId , templateId , hypervisorType , event .getCreateDate (),
10801081 null );
1081- _usageInstanceDao . persist (usageInstanceNew );
1082+ populateDynamicComputeOfferingDetailsAndPersist (usageInstanceNew , event . getId () );
10821083 } catch (Exception ex ) {
10831084 s_logger .error ("Error saving usage instance for vm: " + vmId , ex );
10841085 }
@@ -1105,38 +1106,11 @@ private void createVMHelperEvent(UsageEventVO event) {
11051106 try {
11061107 Long templateId = event .getTemplateId ();
11071108 String hypervisorType = event .getResourceType ();
1108- Long cpuCores = null ;
1109- Long memory = null ;
1110- Long cpuSpeed = null ;
1111-
1112- //populate the cpu, memory and cpuSpeed of the vm when created from a dynamic offering.
1113- Map <String , String > usageDetails = _usageEventDetailsDao .findDetails (event .getId ());
1114-
1115- if (usageDetails != null && usageDetails .size () != 0 ) {
1116- if (usageDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ()) != null ) {
1117- cpuCores = Long .parseLong (usageDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ()));
1118- }
1119- if (usageDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ()) != null ) {
1120- cpuSpeed = Long .parseLong (usageDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ()));
1121- }
1122- if (usageDetails .get (UsageEventVO .DynamicParameters .memory .name ()) != null ) {
1123- memory = Long .parseLong (usageDetails .get (UsageEventVO .DynamicParameters .memory .name ()));
1124- }
1125- }
11261109
11271110 // add this VM to the usage helper table
11281111 UsageVMInstanceVO usageInstanceNew = new UsageVMInstanceVO (UsageTypes .ALLOCATED_VM , zoneId , event .getAccountId (), vmId , vmName ,
11291112 soId , templateId , hypervisorType , event .getCreateDate (), null );
1130- if (cpuCores != null ) {
1131- usageInstanceNew .setCpuCores (cpuCores );
1132- }
1133- if (cpuSpeed != null ) {
1134- usageInstanceNew .setCpuSpeed (cpuSpeed );
1135- }
1136- if (memory != null ) {
1137- usageInstanceNew .setMemory (memory );
1138- }
1139- _usageInstanceDao .persist (usageInstanceNew );
1113+ populateDynamicComputeOfferingDetailsAndPersist (usageInstanceNew , event .getId ());
11401114 } catch (Exception ex ) {
11411115 s_logger .error ("Error saving usage instance for vm: " + vmId , ex );
11421116 }
@@ -1176,7 +1150,7 @@ private void createVMHelperEvent(UsageEventVO event) {
11761150 // add this VM to the usage helper table
11771151 UsageVMInstanceVO usageInstanceNew =
11781152 new UsageVMInstanceVO (UsageTypes .ALLOCATED_VM , zoneId , event .getAccountId (), vmId , vmName , soId , templateId , hypervisorType , event .getCreateDate (), null );
1179- _usageInstanceDao . persist (usageInstanceNew );
1153+ populateDynamicComputeOfferingDetailsAndPersist (usageInstanceNew , event . getId () );
11801154 } else if (EventTypes .EVENT_VM_DYNAMIC_SCALE .equals (event .getType ())) {
11811155 // Ending the running vm event
11821156 SearchCriteria <UsageVMInstanceVO > sc = _usageInstanceDao .createSearchCriteria ();
@@ -1211,7 +1185,7 @@ private void createVMHelperEvent(UsageEventVO event) {
12111185 usageInstance .setServiceOfferingId (soId );
12121186 usageInstance .setStartDate (event .getCreateDate ());
12131187 usageInstance .setEndDate (null );
1214- _usageInstanceDao . persist (usageInstance );
1188+ populateDynamicComputeOfferingDetailsAndPersist (usageInstance , event . getId () );
12151189 }
12161190 }
12171191
@@ -1221,8 +1195,28 @@ private void createVMHelperEvent(UsageEventVO event) {
12211195 // add this VM to the usage helper table with new service offering Id
12221196 UsageVMInstanceVO usageInstanceNew =
12231197 new UsageVMInstanceVO (UsageTypes .RUNNING_VM , zoneId , event .getAccountId (), vmId , vmName , soId , templateId , hypervisorType , event .getCreateDate (), null );
1224- _usageInstanceDao .persist (usageInstanceNew );
1198+ populateDynamicComputeOfferingDetailsAndPersist (usageInstanceNew , event .getId ());
1199+ }
1200+ }
1201+
1202+ private void populateDynamicComputeOfferingDetailsAndPersist (UsageVMInstanceVO usageInstance , Long eventId ) {
1203+
1204+ //populate the cpu, memory and cpuSpeed of the vm when created from a dynamic offering.
1205+ UsageEventDetailsVO cpuNumber = _usageEventDetailsDao .findDetail (eventId , UsageEventVO .DynamicParameters .cpuNumber .name ());
1206+ if (cpuNumber != null ) {
1207+ usageInstance .setCpuCores (Long .parseLong (cpuNumber .getValue ()));
1208+ }
1209+
1210+ UsageEventDetailsVO cpuSpeed = _usageEventDetailsDao .findDetail (eventId , UsageEventVO .DynamicParameters .cpuSpeed .name ());
1211+ if (cpuSpeed != null ) {
1212+ usageInstance .setCpuSpeed (Long .parseLong (cpuSpeed .getValue ()));
1213+ }
1214+
1215+ UsageEventDetailsVO memory = _usageEventDetailsDao .findDetail (eventId , UsageEventVO .DynamicParameters .memory .name ());
1216+ if (memory != null ) {
1217+ usageInstance .setMemory (Long .parseLong (memory .getValue ()));
12251218 }
1219+ _usageInstanceDao .persist (usageInstance );
12261220 }
12271221
12281222 private void createNetworkHelperEntry (UserStatisticsVO userStat , UsageNetworkVO usageNetworkStats , long timestamp ) {
0 commit comments