2727
2828import org .apache .log4j .Logger ;
2929import org .springframework .stereotype .Component ;
30-
3130import org .apache .cloudstack .usage .UsageTypes ;
3231
3332import com .cloud .usage .UsageVMInstanceVO ;
3635import com .cloud .usage .dao .UsageVMInstanceDao ;
3736import com .cloud .user .AccountVO ;
3837import com .cloud .utils .Pair ;
38+ import com .cloud .utils .StringUtils ;
3939
4040@ Component
4141public class VMInstanceUsageParser {
@@ -75,7 +75,7 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
7575 Map <String , Pair <String , Long >> usageVMUptimeMap = new HashMap <String , Pair <String , Long >>();
7676 Map <String , Pair <String , Long >> allocatedVMMap = new HashMap <String , Pair <String , Long >>();
7777
78- Map <String , VMInfo > vmServiceOfferingMap = new HashMap <String , VMInfo >();
78+ Map <String , VMInfo > vmInfosMap = new HashMap <String , VMInfo >();
7979
8080 // loop through all the usage instances, create a usage record for each
8181 for (UsageVMInstanceVO usageInstance : usageInstances ) {
@@ -84,10 +84,13 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
8484 long zoneId = usageInstance .getZoneId ();
8585 long tId = usageInstance .getTemplateId ();
8686 int usageType = usageInstance .getUsageType ();
87- String key = vmId + "-" + soId + "-" + usageType ;
87+ Long cpuCores = usageInstance .getCpuCores ();
88+ Long cpuSpeed = usageInstance .getCpuSpeed ();
89+ Long memory = usageInstance .getMemory ();
90+ String key = StringUtils .join ("-" , vmId , soId , usageType , cpuCores , cpuSpeed , memory );
8891
89- // store the info in the service offering map
90- vmServiceOfferingMap .put (key , new VMInfo (vmId , zoneId , soId , tId , usageInstance .getHypervisorType ()));
92+ // store the info in the VMs map
93+ vmInfosMap .put (key , new VMInfo (vmId , zoneId , soId , tId , usageInstance .getHypervisorType (), cpuCores , cpuSpeed , memory ));
9194
9295 Date vmStartDate = usageInstance .getStartDate ();
9396 Date vmEndDate = usageInstance .getEndDate ();
@@ -119,9 +122,9 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
119122
120123 // Only create a usage record if we have a runningTime of bigger than zero.
121124 if (runningTime > 0L ) {
122- VMInfo info = vmServiceOfferingMap .get (vmIdKey );
125+ VMInfo info = vmInfosMap .get (vmIdKey );
123126 createUsageRecord (UsageTypes .RUNNING_VM , runningTime , startDate , endDate , account , info .getVirtualMachineId (), vmUptimeInfo .first (), info .getZoneId (),
124- info .getServiceOfferingId (), info .getTemplateId (), info .getHypervisorType ());
127+ info .getServiceOfferingId (), info .getTemplateId (), info .getHypervisorType (), info . getCpuCores (), info . getCpuSpeed (), info . getMemory () );
125128 }
126129 }
127130
@@ -131,9 +134,9 @@ public static boolean parse(AccountVO account, Date startDate, Date endDate) {
131134
132135 // Only create a usage record if we have a runningTime of bigger than zero.
133136 if (allocatedTime > 0L ) {
134- VMInfo info = vmServiceOfferingMap .get (vmIdKey );
137+ VMInfo info = vmInfosMap .get (vmIdKey );
135138 createUsageRecord (UsageTypes .ALLOCATED_VM , allocatedTime , startDate , endDate , account , info .getVirtualMachineId (), vmAllocInfo .first (), info .getZoneId (),
136- info .getServiceOfferingId (), info .getTemplateId (), info .getHypervisorType ());
139+ info .getServiceOfferingId (), info .getTemplateId (), info .getHypervisorType (), info . getCpuCores (), info . getCpuSpeed (), info . getMemory () );
137140 }
138141 }
139142
@@ -153,7 +156,7 @@ private static void updateVmUsageData(Map<String, Pair<String, Long>> usageDataM
153156 }
154157
155158 private static void createUsageRecord (int type , long runningTime , Date startDate , Date endDate , AccountVO account , long vmId , String vmName , long zoneId ,
156- long serviceOfferingId , long templateId , String hypervisorType ) {
159+ long serviceOfferingId , long templateId , String hypervisorType , Long cpuCores , Long cpuSpeed , Long memory ) {
157160 // Our smallest increment is hourly for now
158161 if (s_logger .isDebugEnabled ()) {
159162 s_logger .debug ("Total running time " + runningTime + "ms" );
@@ -179,23 +182,29 @@ private static void createUsageRecord(int type, long runningTime, Date startDate
179182 usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")" ;
180183 UsageVO usageRecord =
181184 new UsageVO (Long .valueOf (zoneId ), account .getId (), account .getDomainId (), usageDesc , usageDisplay + " Hrs" , type , new Double (usage ), Long .valueOf (vmId ),
182- vmName , Long .valueOf (serviceOfferingId ), Long .valueOf (templateId ), Long .valueOf (vmId ), startDate , endDate , hypervisorType );
185+ vmName , cpuCores , cpuSpeed , memory , Long .valueOf (serviceOfferingId ), Long .valueOf (templateId ), Long .valueOf (vmId ), startDate , endDate , hypervisorType );
183186 s_usageDao .persist (usageRecord );
184187 }
185188
186189 private static class VMInfo {
187- private long virtualMachineId ;
188- private long zoneId ;
189- private long serviceOfferingId ;
190- private long templateId ;
191- private String hypervisorType ;
192-
193- public VMInfo (long vmId , long zId , long soId , long tId , String hypervisorType ) {
190+ private final long virtualMachineId ;
191+ private final long zoneId ;
192+ private final long serviceOfferingId ;
193+ private final long templateId ;
194+ private final String hypervisorType ;
195+ private final Long cpuCores ;
196+ private final Long cpuSpeed ;
197+ private final Long memory ;
198+
199+ public VMInfo (long vmId , long zId , long soId , long tId , String hypervisorType , Long cpuCores , Long cpuSpeed , Long memory ) {
194200 virtualMachineId = vmId ;
195201 zoneId = zId ;
196202 serviceOfferingId = soId ;
197203 templateId = tId ;
198204 this .hypervisorType = hypervisorType ;
205+ this .cpuCores = cpuCores ;
206+ this .cpuSpeed = cpuSpeed ;
207+ this .memory = memory ;
199208 }
200209
201210 public long getZoneId () {
@@ -217,5 +226,17 @@ public long getTemplateId() {
217226 private String getHypervisorType () {
218227 return hypervisorType ;
219228 }
229+
230+ public Long getCpuCores () {
231+ return cpuCores ;
232+ }
233+
234+ public Long getCpuSpeed () {
235+ return cpuSpeed ;
236+ }
237+
238+ public Long getMemory () {
239+ return memory ;
240+ }
220241 }
221242}
0 commit comments