You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ro...@apache.org on 2021/09/03 08:00:22 UTC

[cloudstack] branch 4.15 updated: usage: fix backup usage (#5259)

This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch 4.15
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.15 by this push:
     new 7a7e449  usage: fix backup usage (#5259)
7a7e449 is described below

commit 7a7e449af67a1d7e18708b9d32e298cdf84fbf91
Author: Wei Zhou <we...@apache.org>
AuthorDate: Fri Sep 3 09:59:56 2021 +0200

    usage: fix backup usage (#5259)
    
    when creating usage record for backup usages in a period, we need to check if vm offering is changed. if vm offering is changed, create a new cloud usage record, otherwise update existing usage record.
    
    This PR fixes #4982
---
 .../com/cloud/usage/parser/BackupUsageParser.java  | 68 ++++++----------------
 1 file changed, 17 insertions(+), 51 deletions(-)

diff --git a/usage/src/main/java/com/cloud/usage/parser/BackupUsageParser.java b/usage/src/main/java/com/cloud/usage/parser/BackupUsageParser.java
index bacf706..d0f25c0 100644
--- a/usage/src/main/java/com/cloud/usage/parser/BackupUsageParser.java
+++ b/usage/src/main/java/com/cloud/usage/parser/BackupUsageParser.java
@@ -17,15 +17,13 @@
 
 package com.cloud.usage.parser;
 
+import java.text.DecimalFormat;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 
-import org.apache.cloudstack.backup.Backup;
 import org.apache.cloudstack.usage.UsageTypes;
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
@@ -68,65 +66,33 @@ public class BackupUsageParser {
             return true;
         }
 
-        final Map<Long, BackupInfo> vmUsageMap = new HashMap<>();
         for (final UsageBackupVO usageBackup : usageBackups) {
             final Long vmId = usageBackup.getVmId();
             final Long zoneId = usageBackup.getZoneId();
             final Long offeringId = usageBackup.getBackupOfferingId();
-            if (vmUsageMap.get(vmId) == null) {
-                vmUsageMap.put(vmId, new BackupUsageParser.BackupInfo(new Backup.Metric(0L, 0L), zoneId, vmId, offeringId));
+            Date createdDate = usageBackup.getCreated();
+            Date removedDate = usageBackup.getRemoved();
+            if (createdDate.before(startDate)) {
+                createdDate = startDate;
             }
-            final Backup.Metric metric = vmUsageMap.get(vmId).getMetric();
-            metric.setBackupSize(metric.getBackupSize() + usageBackup.getSize());
-            metric.setDataSize(metric.getDataSize() + usageBackup.getProtectedSize());
-        }
+            if (removedDate == null || removedDate.after(endDate)) {
+                removedDate = endDate;
+            }
+            final long duration = (removedDate.getTime() - createdDate.getTime()) + 1;
+            final float usage = duration / 1000f / 60f / 60f;
+            DecimalFormat dFormat = new DecimalFormat("#.######");
+            String usageDisplay = dFormat.format(usage);
 
-        for (final BackupInfo backupInfo : vmUsageMap.values()) {
-            final Long vmId = backupInfo.getVmId();
-            final Long zoneId = backupInfo.getZoneId();
-            final Long offeringId = backupInfo.getOfferingId();
-            final Double rawUsage = (double) backupInfo.getMetric().getBackupSize();
-            final Double sizeGib = rawUsage / (1024.0 * 1024.0 * 1024.0);
-            final String description = String.format("Backup usage VM ID: %d", vmId);
-            final String usageDisplay = String.format("%.4f GiB", sizeGib);
+            final Double rawUsage = (double) usageBackup.getSize();
+            final String description = String.format("Backup usage VM ID: %d, backup offering: %d", vmId, offeringId);
 
             final UsageVO usageRecord =
-                    new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay,
-                            UsageTypes.BACKUP, rawUsage, vmId, null, offeringId, null, vmId,
-                            backupInfo.getMetric().getBackupSize(), backupInfo.getMetric().getDataSize(), startDate, endDate);
+                    new UsageVO(zoneId, account.getAccountId(), account.getDomainId(), description, usageDisplay + " Hrs",
+                            UsageTypes.BACKUP, new Double(usage), vmId, null, offeringId, null, vmId,
+                            usageBackup.getSize(), usageBackup.getProtectedSize(), startDate, endDate);
             s_usageDao.persist(usageRecord);
         }
 
         return true;
     }
-
-    static class BackupInfo {
-        Backup.Metric metric;
-        Long zoneId;
-        Long vmId;
-        Long offeringId;
-
-        public BackupInfo(Backup.Metric metric, Long zoneId, Long vmId, Long offeringId) {
-            this.metric = metric;
-            this.zoneId = zoneId;
-            this.vmId = vmId;
-            this.offeringId = offeringId;
-        }
-
-        public Backup.Metric getMetric() {
-            return metric;
-        }
-
-        public Long getZoneId() {
-            return zoneId;
-        }
-
-        public Long getVmId() {
-            return vmId;
-        }
-
-        public Long getOfferingId() {
-            return offeringId;
-        }
-    }
 }
\ No newline at end of file