Tal Nisan has uploaded a new change for review.

Change subject: core: Cleanup of GetSystemStatisticsQuery
......................................................................

core: Cleanup of GetSystemStatisticsQuery

Replaced all hard coded strings with contstants, extracted logic to separate
methods, renamed variables by Java standard and other minor changes

Change-Id: Ibb87448c24c6ac32838ae2e381856f478c27ea52
Signed-off-by: Tal Nisan <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetSystemStatisticsQuery.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/QueryConstants.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java
3 files changed, 87 insertions(+), 75 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/45/12145/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetSystemStatisticsQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetSystemStatisticsQuery.java
index ab8ca03..4bb354d 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetSystemStatisticsQuery.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetSystemStatisticsQuery.java
@@ -1,5 +1,8 @@
 package org.ovirt.engine.core.bll;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.commons.lang.StringUtils;
 import org.ovirt.engine.core.common.businessentities.StorageDomainStatus;
 import org.ovirt.engine.core.common.businessentities.VDSStatus;
@@ -13,53 +16,51 @@
         super(parameters);
     }
 
-    public java.util.HashMap<String, Integer> getSystemStatistics() {
-        //
-        // int max = (Parameters as GetSystemStatisticsQueryParameters).Max;
-        // Dictionary<String, Int32> res = new Dictionary<String, Int32>();
-        // QueriesCommandBase query;
-        // List<IVdcQueryable> tmp;
-        // query = CommandsFactory.CreateQueryCommand(
-        // VdcQueryType.Search, new SearchParameters("vms:",
-        // SearchType.VM){MaxCount = max});
-        // query.Execute();
-        // tmp = (List<IVdcQueryable>)query.QueryReturnValue.ReturnValue;
-        // int total_vms = tmp.Count;
-        // query = CommandsFactory.CreateQueryCommand(
-        // VdcQueryType.Search, new SearchParameters("vms: status != down",
-        // SearchType.VM) { MaxCount = max });
-        // query.Execute();
-        // tmp = (List<IVdcQueryable>)query.QueryReturnValue.ReturnValue;
-        // int active_vms = tmp.Count;
-        // List<DbUser> users = DbFacade.Instance.GetAllFromUsers();
-        // int total_users = users.Count, active_users = 0;
-        // foreach (DbUser user in users)
-        // {
-        // if (user.IsLogedin)
-        // {
-        // active_users++;
-        // }
-        // }
-        // query = CommandsFactory.CreateQueryCommand(
-        // VdcQueryType.Search, new SearchParameters("Hosts", SearchType.VDS) {
-        // MaxCount = max });
-        // query.Execute();
-        // tmp = (List<IVdcQueryable>)query.QueryReturnValue.ReturnValue;
-        // int total_vds = tmp.Count, active_vds = 0;
-        // foreach (VDS vds in tmp)
-        // {
-        // if ((vds.status == VDSStatus.Up) || (vds.status ==
-        // VDSStatus.PreparingForMaintenance))
-        // {
-        // active_vds++;
-        // }
-        // }
-        //
+    private static final char COMMA_DELIMITER = ',';
 
-        java.util.HashMap<String, Integer> res = new java.util.HashMap<String, 
Integer>();
+    private Map<String, Integer> getSystemStatistics() {
+        Map<String, Integer> res = new HashMap<String, Integer>();
 
         // VMs:
-        int total_vms = DbFacade.getInstance().getSystemStatisticsValue("VM", 
"");
+        int totalVMs = getTotalVMsStat();
+        int activeVMs = getActiveVMsStat();
+        int downVMs = (totalVMs - activeVMs) < 0 ? 0 : (totalVMs - activeVMs);
+
+        // Hosts:
+        int totalHosts = getTotalHostsStat();
+        int activeHosts = getActiveHostsStat();
+        int maintenanceHosts = getMaintenanceHostsStat();
+        int downHosts =
+                (totalHosts - activeHosts - maintenanceHosts) < 0 ? 0 : 
(totalHosts - activeHosts - maintenanceHosts);
+
+        // Users:
+        int totalUsers = getTotalUsersStat();
+        int activeUsers = getActiveUsersStat();
+
+        // Storage Domains:
+        int totalStorageDomains = getTotalStorageDomainsStat();
+        int activeStorageDomains = getActiveStorageDomainsStat();
+
+        res.put(QueryConstants.SYSTEM_STATS_TOTAL_VMS_FIELD, totalVMs);
+        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_VMS_FIELD, activeVMs);
+        res.put(QueryConstants.SYSTEM_STATS_DOWN_VMS_FIELD, downVMs);
+        res.put(QueryConstants.SYSTEM_STATS_TOTAL_HOSTS_FIELD, totalHosts);
+        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_HOSTS_FIELD, activeHosts);
+        res.put(QueryConstants.SYSTEM_STATS_MAINTENANCE_HOSTS_FIELD, 
maintenanceHosts);
+        res.put(QueryConstants.SYSTEM_STATS_DOWN_HOSTS_FIELD, downHosts);
+        res.put(QueryConstants.SYSTEM_STATS_TOTAL_USERS_FIELD, totalUsers);
+        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_USERS_FIELD, activeUsers);
+        res.put(QueryConstants.SYSTEM_STATS_TOTAL_STORAGE_DOMAINS_FIELD, 
totalStorageDomains);
+        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_STORAGE_DOMAINS_FIELD, 
activeStorageDomains);
+
+        return res;
+    }
+
+    private int getTotalVMsStat() {
+        return 
getDbFacade().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_VM_ENTITY);
+    }
+
+    private int getActiveVMsStat() {
         String[] activeVmStatuses = { (String.valueOf(VMStatus.Up.getValue())),
                 (String.valueOf(VMStatus.PoweringUp.getValue())),
                 (String.valueOf(VMStatus.PoweredDown.getValue())),
@@ -69,44 +70,44 @@
                 (String.valueOf(VMStatus.PoweringDown.getValue())),
                 (String.valueOf(VMStatus.Paused.getValue())),
                 (String.valueOf(VMStatus.Unknown.getValue())) };
-        int active_vms = DbFacade.getInstance()
-                .getSystemStatisticsValue("VM", 
StringUtils.join(activeVmStatuses, ','));
+        return 
getDbFacade().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_VM_ENTITY,
+                StringUtils.join(activeVmStatuses, COMMA_DELIMITER));
+    }
 
-        int down_vms = (total_vms - active_vms) < 0 ? 0 : (total_vms - 
active_vms);
+    private int getTotalHostsStat() {
+        return 
DbFacade.getInstance().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_HOST_ENTITY);
+    }
 
-        // Hosts:
-        int total_vds = 
DbFacade.getInstance().getSystemStatisticsValue("HOST", "");
+    private int getActiveHostsStat() {
+        String[] activeVdsStatuses =
+                { (String.valueOf(VDSStatus.Up.getValue())),
+                        
(String.valueOf(VDSStatus.PreparingForMaintenance.getValue())) };
+        return 
getDbFacade().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_HOST_ENTITY,
+                StringUtils.join(activeVdsStatuses, COMMA_DELIMITER));
+    }
 
-        String[] activeVdsStatuses = { 
(String.valueOf(VDSStatus.Up.getValue())),
-                
(String.valueOf(VDSStatus.PreparingForMaintenance.getValue()))};
-        int active_vds = 
DbFacade.getInstance().getSystemStatisticsValue("HOST",
-                StringUtils.join(activeVdsStatuses, ','));
-        int maintenance_vds = 
DbFacade.getInstance().getSystemStatisticsValue("HOST",
+    private int getMaintenanceHostsStat() {
+        return 
getDbFacade().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_HOST_ENTITY,
                 (String.valueOf(VDSStatus.Maintenance.getValue())));
-        int down_vds = (total_vds - active_vds - maintenance_vds) < 0 ? 0 : 
(total_vds - active_vds - maintenance_vds);
+    }
 
-        // Users:
-        int total_users = 
DbFacade.getInstance().getSystemStatisticsValue("USER", "");
-        int active_users = 
DbFacade.getInstance().getSystemStatisticsValue("USER", "1");
+    private int getTotalUsersStat() {
+        return 
DbFacade.getInstance().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_USER_ENTITY);
+    }
 
-        // Storage Domains:
-        int total_storage_domains = 
DbFacade.getInstance().getSystemStatisticsValue("TSD", "");
-        int active_storage_domains = 
DbFacade.getInstance().getSystemStatisticsValue("ASD",
-                (Integer.toString(StorageDomainStatus.Active.getValue())));
+    private int getActiveUsersStat() {
+        return 
DbFacade.getInstance().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_USER_ENTITY,
+                QueryConstants.SYSTEM_STATS_USER_ACTIVE_STATUS);
+    }
 
-        res.put(QueryConstants.SYSTEM_STATS_TOTAL_VMS_FIELD, total_vms);
-        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_VMS_FIELD, active_vms);
-        res.put(QueryConstants.SYSTEM_STATS_DOWN_VMS_FIELD, down_vms);
-        res.put(QueryConstants.SYSTEM_STATS_TOTAL_HOSTS_FIELD, total_vds);
-        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_HOSTS_FIELD, active_vds);
-        res.put(QueryConstants.SYSTEM_STATS_MAINTENANCE_HOSTS_FIELD, 
maintenance_vds);
-        res.put(QueryConstants.SYSTEM_STATS_DOWN_HOSTS_FIELD, down_vds);
-        res.put(QueryConstants.SYSTEM_STATS_TOTAL_USERS_FIELD, total_users);
-        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_USERS_FIELD, active_users);
-        res.put(QueryConstants.SYSTEM_STATS_TOTAL_STORAGE_DOMAINS_FIELD, 
total_storage_domains);
-        res.put(QueryConstants.SYSTEM_STATS_ACTIVE_STORAGE_DOMAINS_FIELD, 
active_storage_domains);
+    private int getTotalStorageDomainsStat() {
+        return 
DbFacade.getInstance().getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_TOTAL_STORAGE_DOMAIN_ENTITY);
+    }
 
-        return res;
+    private int getActiveStorageDomainsStat() {
+        return DbFacade.getInstance()
+                
.getSystemStatisticsValue(QueryConstants.SYSTEM_STATS_ACTIVE_STORAGE_DOMAIN_ENTITY,
+                (String.valueOf(StorageDomainStatus.Active.getValue())));
     }
 
     @Override
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/QueryConstants.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/QueryConstants.java
index 4368018..4d740fc 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/QueryConstants.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/constants/QueryConstants.java
@@ -16,4 +16,11 @@
     public static final String SYSTEM_STATS_TOTAL_STORAGE_DOMAINS_FIELD = 
"total_storage_domains";
     public static final String SYSTEM_STATS_ACTIVE_STORAGE_DOMAINS_FIELD = 
"active_storage_domains";
 
+    public static final String SYSTEM_STATS_VM_ENTITY = "VM";
+    public static final String SYSTEM_STATS_HOST_ENTITY = "HOST";
+    public static final String SYSTEM_STATS_USER_ENTITY = "USER";
+    public static final String SYSTEM_STATS_ACTIVE_STORAGE_DOMAIN_ENTITY = 
"ASD";
+    public static final String SYSTEM_STATS_TOTAL_STORAGE_DOMAIN_ENTITY = 
"TSD";
+
+    public static final String SYSTEM_STATS_USER_ACTIVE_STATUS = "1";
 }
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java
index a9ff611..2038484 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/DbFacade.java
@@ -18,6 +18,7 @@
 import org.ovirt.engine.core.common.businessentities.Image;
 import org.ovirt.engine.core.common.businessentities.Role;
 import org.ovirt.engine.core.common.businessentities.Snapshot;
+import org.ovirt.engine.core.common.businessentities.StorageDomain;
 import org.ovirt.engine.core.common.businessentities.StorageDomainDynamic;
 import org.ovirt.engine.core.common.businessentities.StorageDomainStatic;
 import org.ovirt.engine.core.common.businessentities.StorageDomainStatus;
@@ -33,7 +34,6 @@
 import org.ovirt.engine.core.common.businessentities.VmTemplate;
 import org.ovirt.engine.core.common.businessentities.image_storage_domain_map;
 import org.ovirt.engine.core.common.businessentities.permissions;
-import org.ovirt.engine.core.common.businessentities.StorageDomain;
 import org.ovirt.engine.core.common.businessentities.storage_pool;
 import org.ovirt.engine.core.common.businessentities.vds_spm_id_map;
 import org.ovirt.engine.core.common.businessentities.network.Network;
@@ -287,6 +287,10 @@
                 && (master.getStatus() == StorageDomainStatus.Active || 
master.getStatus() == StorageDomainStatus.Unknown);
     }
 
+    public Integer getSystemStatisticsValue(String entity) {
+        return getSystemStatisticsValue(entity, "");
+    }
+
     public Integer getSystemStatisticsValue(String entity, String status) {
         MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource().addValue("entity", entity).addValue(
                 "status", status);


--
To view, visit http://gerrit.ovirt.org/12145
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibb87448c24c6ac32838ae2e381856f478c27ea52
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Tal Nisan <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to