cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alena1...@apache.org
Subject [1/2] git commit: updated refs/heads/master to 78b1ac4
Date Thu, 31 Oct 2013 22:37:05 GMT
Updated Branches:
  refs/heads/master b436a8239 -> 78b1ac412


CLOUDSTACK-3868: fixed "count" param in listUsageRecords API to return the actual number of
records matching the search criteria. Used to return the wrong value - the number of records
per page

Conflicts:
	server/src/com/cloud/usage/UsageServiceImpl.java
	utils/src/com/cloud/utils/db/GenericDaoBase.java


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/78b1ac41
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/78b1ac41
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/78b1ac41

Branch: refs/heads/master
Commit: 78b1ac4126aa1d6192846a4e4934d2a79d29cbff
Parents: 9c773fb
Author: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Authored: Fri Jul 26 15:03:36 2013 -0700
Committer: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Committed: Thu Oct 31 15:25:57 2013 -0700

----------------------------------------------------------------------
 .../command/admin/usage/GetUsageRecordsCmd.java  | 19 +++++++++++--------
 .../apache/cloudstack/usage/UsageService.java    |  4 +++-
 .../api/command/test/UsageCmdTest.java           |  9 ++++++---
 .../schema/src/com/cloud/usage/dao/UsageDao.java |  3 ++-
 .../src/com/cloud/usage/dao/UsageDaoImpl.java    |  5 +++--
 .../src/com/cloud/utils/db/GenericDaoBase.java   |  7 +++++++
 server/src/com/cloud/usage/UsageServiceImpl.java | 15 ++++++++-------
 7 files changed, 40 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/78b1ac41/api/src/org/apache/cloudstack/api/command/admin/usage/GetUsageRecordsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/command/admin/usage/GetUsageRecordsCmd.java
b/api/src/org/apache/cloudstack/api/command/admin/usage/GetUsageRecordsCmd.java
index f4960dd..59220ee 100644
--- a/api/src/org/apache/cloudstack/api/command/admin/usage/GetUsageRecordsCmd.java
+++ b/api/src/org/apache/cloudstack/api/command/admin/usage/GetUsageRecordsCmd.java
@@ -19,7 +19,6 @@ package org.apache.cloudstack.api.command.admin.usage;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.TimeZone;
 
 import org.apache.cloudstack.api.APICommand;
 import org.apache.cloudstack.api.ApiConstants;
@@ -33,6 +32,8 @@ import org.apache.cloudstack.api.response.UsageRecordResponse;
 import org.apache.cloudstack.usage.Usage;
 import org.apache.log4j.Logger;
 
+import com.cloud.utils.Pair;
+
 @APICommand(name = "listUsageRecords", description="Lists usage records for accounts", responseObject=UsageRecordResponse.class)
 public class GetUsageRecordsCmd extends BaseListCmd {
     public static final Logger s_logger = Logger.getLogger(GetUsageRecordsCmd.class.getName());
@@ -110,16 +111,18 @@ public class GetUsageRecordsCmd extends BaseListCmd {
 
     @Override
     public void execute(){
-        List<? extends Usage> usageRecords = _usageService.getUsageRecords(this);
+        Pair<List<? extends Usage>, Integer> usageRecords = _usageService.getUsageRecords(this);
         ListResponse<UsageRecordResponse> response = new ListResponse<UsageRecordResponse>();
         List<UsageRecordResponse> usageResponses = new ArrayList<UsageRecordResponse>();
-        for(Usage usageRecord: usageRecords){
-        	UsageRecordResponse usageResponse = _responseGenerator.createUsageResponse(usageRecord);
-        	usageResponse.setObjectName("usagerecord");
-        	usageResponses.add(usageResponse);
+        if (usageRecords != null) {
+            for(Usage usageRecord: usageRecords.first()){
+                UsageRecordResponse usageResponse = _responseGenerator.createUsageResponse(usageRecord);
+                usageResponse.setObjectName("usagerecord");
+                usageResponses.add(usageResponse);
+            }
+            response.setResponses(usageResponses, usageRecords.second());
         }
-        
-        response.setResponses(usageResponses);
+                
         response.setResponseName(getCommandName());
         this.setResponseObject(response);
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/78b1ac41/api/src/org/apache/cloudstack/usage/UsageService.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/usage/UsageService.java b/api/src/org/apache/cloudstack/usage/UsageService.java
index 3838225..e203c3f 100755
--- a/api/src/org/apache/cloudstack/usage/UsageService.java
+++ b/api/src/org/apache/cloudstack/usage/UsageService.java
@@ -23,6 +23,8 @@ import org.apache.cloudstack.api.command.admin.usage.GenerateUsageRecordsCmd;
 import org.apache.cloudstack.api.command.admin.usage.GetUsageRecordsCmd;
 import org.apache.cloudstack.api.response.UsageTypeResponse;
 
+import com.cloud.utils.Pair;
+
 public interface UsageService {
     /**
      * Generate Billing Records from the last time it was generated to the
@@ -50,7 +52,7 @@ public interface UsageService {
      *             the appropriate page number)
      * @return a list of usage records
      */
-    List<? extends Usage> getUsageRecords(GetUsageRecordsCmd cmd);
+    Pair<List<? extends Usage>, Integer> getUsageRecords(GetUsageRecordsCmd cmd);
 
     /**
      * Retrieves the timezone used for usage aggregation.  One day is represented as midnight
to 11:59:59pm

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/78b1ac41/api/test/org/apache/cloudstack/api/command/test/UsageCmdTest.java
----------------------------------------------------------------------
diff --git a/api/test/org/apache/cloudstack/api/command/test/UsageCmdTest.java b/api/test/org/apache/cloudstack/api/command/test/UsageCmdTest.java
index 1f218f4..b8dfb0a 100644
--- a/api/test/org/apache/cloudstack/api/command/test/UsageCmdTest.java
+++ b/api/test/org/apache/cloudstack/api/command/test/UsageCmdTest.java
@@ -16,7 +16,11 @@
 // under the License.
 package org.apache.cloudstack.api.command.test;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import junit.framework.TestCase;
+
 import org.apache.cloudstack.api.command.admin.usage.GetUsageRecordsCmd;
 import org.apache.cloudstack.usage.Usage;
 import org.apache.cloudstack.usage.UsageService;
@@ -26,8 +30,7 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.mockito.Mockito;
 
-import java.util.ArrayList;
-import java.util.List;
+import com.cloud.utils.Pair;
 
 public class UsageCmdTest extends TestCase {
 
@@ -56,7 +59,7 @@ public class UsageCmdTest extends TestCase {
 
         UsageService usageService = Mockito.mock(UsageService.class);
 
-        List usageRecords = new ArrayList<Usage>();
+        Pair<List<? extends Usage>, Integer> usageRecords = new Pair<List<?
extends Usage>, Integer>(new ArrayList<Usage>(), new Integer(0));
 
         Mockito.when(usageService.getUsageRecords(getUsageRecordsCmd)).thenReturn(
                 usageRecords);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/78b1ac41/engine/schema/src/com/cloud/usage/dao/UsageDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageDao.java b/engine/schema/src/com/cloud/usage/dao/UsageDao.java
index f571b63..b833577 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageDao.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageDao.java
@@ -22,13 +22,14 @@ import com.cloud.usage.UsageVO;
 import com.cloud.user.AccountVO;
 import com.cloud.user.UserStatisticsVO;
 import com.cloud.user.VmDiskStatisticsVO;
+import com.cloud.utils.Pair;
 import com.cloud.utils.db.Filter;
 import com.cloud.utils.db.GenericDao;
 import com.cloud.utils.db.SearchCriteria;
 
 public interface UsageDao extends GenericDao<UsageVO, Long> {
     void deleteRecordsForAccount(Long accountId);
-    List<UsageVO> searchAllRecords(SearchCriteria<UsageVO> sc, Filter filter);
+    Pair<List<UsageVO>, Integer> searchAndCountAllRecords(SearchCriteria<UsageVO>
sc, Filter filter);
 
     void saveAccounts(List<AccountVO> accounts);
     void updateAccounts(List<AccountVO> accounts);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/78b1ac41/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java
index e1e843e..c4d8ec4 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageDaoImpl.java
@@ -35,6 +35,7 @@ import com.cloud.user.AccountVO;
 import com.cloud.user.UserStatisticsVO;
 import com.cloud.user.VmDiskStatisticsVO;
 import com.cloud.utils.DateUtil;
+import com.cloud.utils.Pair;
 import com.cloud.utils.db.Filter;
 import com.cloud.utils.db.GenericDaoBase;
 import com.cloud.utils.db.SearchCriteria;
@@ -93,8 +94,8 @@ public class UsageDaoImpl extends GenericDaoBase<UsageVO, Long> implements
Usage
 	}
 
 	@Override
-	public List<UsageVO> searchAllRecords(SearchCriteria<UsageVO> sc, Filter filter)
{
-	    return listIncludingRemovedBy(sc, filter);
+	public Pair<List<UsageVO>, Integer> searchAndCountAllRecords(SearchCriteria<UsageVO>
sc, Filter filter) {
+	    return listAndCountIncludingRemovedBy(sc, filter);
 	}
 
 	@Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/78b1ac41/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
----------------------------------------------------------------------
diff --git a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
index ba5200e..177cd5b 100755
--- a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
+++ b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
@@ -1864,4 +1864,11 @@ public abstract class GenericDaoBase<T, ID extends Serializable>
extends Compone
 
         return sql;
     }
+    
+    @DB()
+    protected Pair<List<T>, Integer> listAndCountIncludingRemovedBy(final SearchCriteria<T>
sc, final Filter filter) {
+        List<T> objects = searchIncludingRemoved(sc, filter, null, false);
+        Integer count = getCount(sc);
+        return new Pair<List<T>, Integer>(objects, count);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/78b1ac41/server/src/com/cloud/usage/UsageServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/usage/UsageServiceImpl.java b/server/src/com/cloud/usage/UsageServiceImpl.java
index d16d36d..e36ce44 100755
--- a/server/src/com/cloud/usage/UsageServiceImpl.java
+++ b/server/src/com/cloud/usage/UsageServiceImpl.java
@@ -32,6 +32,7 @@ import org.apache.cloudstack.api.command.admin.usage.GetUsageRecordsCmd;
 import org.apache.cloudstack.api.response.UsageTypeResponse;
 import org.apache.cloudstack.context.CallContext;
 import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
+import org.apache.cloudstack.usage.Usage;
 import org.apache.cloudstack.usage.UsageService;
 import org.apache.cloudstack.usage.UsageTypes;
 import org.apache.log4j.Logger;
@@ -49,11 +50,11 @@ import com.cloud.usage.dao.UsageJobDao;
 import com.cloud.user.Account;
 import com.cloud.user.AccountVO;
 import com.cloud.user.dao.AccountDao;
+import com.cloud.utils.Pair;
 import com.cloud.utils.component.Manager;
 import com.cloud.utils.component.ManagerBase;
 import com.cloud.utils.db.Filter;
 import com.cloud.utils.db.SearchCriteria;
-import com.cloud.utils.db.Transaction;
 import com.cloud.utils.db.TransactionLegacy;
 
 @Component
@@ -112,7 +113,7 @@ public class UsageServiceImpl extends ManagerBase implements UsageService,
Manag
     }
 
     @Override
-    public List<UsageVO> getUsageRecords(GetUsageRecordsCmd cmd) {
+    public Pair<List<? extends Usage>, Integer> getUsageRecords(GetUsageRecordsCmd
cmd) {
         Long accountId = cmd.getAccountId();
         Long domainId = cmd.getDomainId();
         String accountName = cmd.getAccountName();
@@ -209,13 +210,13 @@ public class UsageServiceImpl extends ManagerBase implements UsageService,
Manag
             sc.addAnd("startDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate);
             sc.addAnd("endDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate);
         } else {
-            return new ArrayList<UsageVO>(); // return an empty list if we fail to
validate the dates
+            return new Pair<List<? extends Usage>, Integer>(new ArrayList<Usage>(),
new Integer(0)); // return an empty list if we fail to validate the dates
         }
 
-        List<UsageVO> usageRecords = null;
+        Pair<List<UsageVO>, Integer> usageRecords = null;
         TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB);
         try {
-            usageRecords = _usageDao.searchAllRecords(sc, usageFilter);
+            usageRecords = _usageDao.searchAndCountAllRecords(sc, usageFilter);
         } finally {
             txn.close();
 
@@ -223,8 +224,8 @@ public class UsageServiceImpl extends ManagerBase implements UsageService,
Manag
             TransactionLegacy swap = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
             swap.close();
         }
-
-        return usageRecords;
+        
+        return new Pair<List<? extends Usage>, Integer>(usageRecords.first(),
usageRecords.second());
     }
 
     @Override


Mime
View raw message