cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alena1...@apache.org
Subject git commit: updated refs/heads/4.2 to 07bf2b2
Date Fri, 26 Jul 2013 22:37:32 GMT
Updated Branches:
  refs/heads/4.2 daa95bab2 -> 07bf2b222


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


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

Branch: refs/heads/4.2
Commit: 07bf2b2227df6dd4f64f58e50db177c3f59acdaf
Parents: daa95ba
Author: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Authored: Fri Jul 26 15:03:36 2013 -0700
Committer: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Committed: Fri Jul 26 15:37:20 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 +++--
 server/src/com/cloud/usage/UsageServiceImpl.java | 14 ++++++++------
 utils/src/com/cloud/utils/db/GenericDaoBase.java |  8 +++++++-
 7 files changed, 40 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/07bf2b22/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/07bf2b22/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/07bf2b22/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/07bf2b22/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/07bf2b22/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 bd7b6b7..9dd1c59 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/07bf2b22/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 ae0a585..836a505 100755
--- a/server/src/com/cloud/usage/UsageServiceImpl.java
+++ b/server/src/com/cloud/usage/UsageServiceImpl.java
@@ -30,6 +30,7 @@ import javax.naming.ConfigurationException;
 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 org.apache.cloudstack.usage.Usage;
 import org.apache.cloudstack.usage.UsageService;
 import org.apache.cloudstack.usage.UsageTypes;
 import org.apache.log4j.Logger;
@@ -48,6 +49,7 @@ import com.cloud.user.Account;
 import com.cloud.user.AccountVO;
 import com.cloud.user.UserContext;
 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;
@@ -110,7 +112,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();
@@ -194,13 +196,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;
         Transaction txn = Transaction.open(Transaction.USAGE_DB);
         try {
-            usageRecords = _usageDao.searchAllRecords(sc, usageFilter);
+            usageRecords = _usageDao.searchAndCountAllRecords(sc, usageFilter);
         } finally {
             txn.close();
 
@@ -208,8 +210,8 @@ public class UsageServiceImpl extends ManagerBase implements UsageService,
Manag
             Transaction swap = Transaction.open(Transaction.CLOUD_DB);
             swap.close();
         }
-
-        return usageRecords;
+        
+        return new Pair<List<? extends Usage>, Integer>(usageRecords.first(),
usageRecords.second());
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/07bf2b22/utils/src/com/cloud/utils/db/GenericDaoBase.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/GenericDaoBase.java b/utils/src/com/cloud/utils/db/GenericDaoBase.java
index f0fc700..8bb088b 100755
--- a/utils/src/com/cloud/utils/db/GenericDaoBase.java
+++ b/utils/src/com/cloud/utils/db/GenericDaoBase.java
@@ -68,7 +68,6 @@ import com.cloud.utils.DateUtil;
 import com.cloud.utils.NumbersUtil;
 import com.cloud.utils.Pair;
 import com.cloud.utils.Ternary;
-import com.cloud.utils.component.ComponentContext;
 import com.cloud.utils.component.ComponentLifecycle;
 import com.cloud.utils.component.ComponentLifecycleBase;
 import com.cloud.utils.component.ComponentMethodInterceptable;
@@ -901,6 +900,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable>
extends Compone
     protected List<T> listIncludingRemovedBy(final SearchCriteria<T> sc, final
Filter filter) {
         return searchIncludingRemoved(sc, filter, null, false);
     }
+    
+    @DB(txn=false)
+    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);
+    }
 
     @DB(txn=false)
     protected List<T> listIncludingRemovedBy(final SearchCriteria<T> sc) {


Mime
View raw message