From ee9eadef472585edd3e7a69f589353ffd8eb3da5 Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Wed, 16 Nov 2011 17:00:22 -0800 Subject: [PATCH] bug 12054: added details level to make it faster if so desired by the admin. Reviewed-by: Alena, Will --- api/src/com/cloud/api/ResponseGenerator.java | 2 + .../com/cloud/api/commands/ListHostsCmd.java | 16 ++- .../src/com/cloud/api/ApiResponseHelper.java | 114 ++++++++++++++++++ 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/api/src/com/cloud/api/ResponseGenerator.java b/api/src/com/cloud/api/ResponseGenerator.java index 7180ba28568..4691e5ff67c 100755 --- a/api/src/com/cloud/api/ResponseGenerator.java +++ b/api/src/com/cloud/api/ResponseGenerator.java @@ -213,5 +213,7 @@ public interface ResponseGenerator { FirewallResponse createFirewallResponse(FirewallRule fwRule); SystemVmInstanceResponse createSystemVmInstanceResponse(VirtualMachine systemVM); + @Deprecated // This method is only a temporary solution. Do not use. + HostResponse createHostResponseTemporary(Host host, int details); } diff --git a/api/src/com/cloud/api/commands/ListHostsCmd.java b/api/src/com/cloud/api/commands/ListHostsCmd.java index e5d7c23f3df..3dbfa7f5f29 100644 --- a/api/src/com/cloud/api/commands/ListHostsCmd.java +++ b/api/src/com/cloud/api/commands/ListHostsCmd.java @@ -68,7 +68,10 @@ public class ListHostsCmd extends BaseListCmd { private Long virtualMachineId; @Parameter(name=ApiConstants.ALLOCATION_STATE, type=CommandType.STRING, description="list hosts by allocation state") - private String allocationState; + private String allocationState; + + @Parameter(name=ApiConstants.DETAILS, type=CommandType.INTEGER, description="give details. 1 = minimal; 2 = include static info; 3 = include events; 4 = include allocation and statistics") + private Integer details; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -109,6 +112,11 @@ public class ListHostsCmd extends BaseListCmd { public String getAllocationState() { return allocationState; } + + public Integer getDetails() { + return details; + } + ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// @@ -118,6 +126,7 @@ public class ListHostsCmd extends BaseListCmd { return s_name; } + @Override public AsyncJob.Type getInstanceType() { return AsyncJob.Type.Host; } @@ -137,8 +146,11 @@ public class ListHostsCmd extends BaseListCmd { ListResponse response = new ListResponse(); List hostResponses = new ArrayList(); + if (details == null) { + details = 5; + } for (Host host : result) { - HostResponse hostResponse = _responseGenerator.createHostResponse(host); + HostResponse hostResponse = _responseGenerator.createHostResponseTemporary(host, details); Boolean hasEnoughCapacity = false; if(hostIdsWithCapacity.contains(host.getId())){ hasEnoughCapacity = true; diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 3a3fad40cd7..9c57d887384 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -615,6 +615,120 @@ public class ApiResponseHelper implements ResponseGenerator { return hostResponse; } + @Deprecated // This method is only a temporary solution. + @Override + public HostResponse createHostResponseTemporary(Host host, int details) { + HostResponse hostResponse = new HostResponse(); + hostResponse.setId(host.getId()); + hostResponse.setCapabilities(host.getCapabilities()); + hostResponse.setClusterId(host.getClusterId()); + hostResponse.setCpuNumber(host.getCpus()); + hostResponse.setZoneId(host.getDataCenterId()); + hostResponse.setDisconnectedOn(host.getDisconnectedOn()); + hostResponse.setHypervisor(host.getHypervisorType()); + hostResponse.setHostType(host.getType()); + hostResponse.setLastPinged(new Date(host.getLastPinged())); + hostResponse.setManagementServerId(host.getManagementServerId()); + hostResponse.setName(host.getName()); + hostResponse.setPodId(host.getPodId()); + hostResponse.setRemoved(host.getRemoved()); + hostResponse.setCpuSpeed(host.getSpeed()); + hostResponse.setState(host.getStatus()); + hostResponse.setIpAddress(host.getPrivateIpAddress()); + hostResponse.setVersion(host.getVersion()); + hostResponse.setCreated(host.getCreated()); + + if (details > 1) { + GuestOSCategoryVO guestOSCategory = ApiDBUtils.getHostGuestOSCategory(host.getId()); + if (guestOSCategory != null) { + hostResponse.setOsCategoryId(guestOSCategory.getId()); + hostResponse.setOsCategoryName(guestOSCategory.getName()); + } + hostResponse.setZoneName(ApiDBUtils.findZoneById(host.getDataCenterId()).getName()); + + if (host.getPodId() != null) { + HostPodVO pod = ApiDBUtils.findPodById(host.getPodId()); + if (pod != null) { + hostResponse.setPodName(pod.getName()); + } + } + + if (host.getClusterId() != null) { + ClusterVO cluster = ApiDBUtils.findClusterById(host.getClusterId()); + hostResponse.setClusterName(cluster.getName()); + hostResponse.setClusterType(cluster.getClusterType().toString()); + } + + hostResponse.setLocalStorageActive(ApiDBUtils.isLocalStorageActiveOnHost(host)); + } + + if (details > 3) { + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + + // calculate cpu allocated by vm + if ((host.getCpus() != null) && (host.getSpeed() != null)) { + int cpu = 0; + String cpuAlloc = null; + List instances = ApiDBUtils.listUserVMsByHostId(host.getId()); + for (UserVmVO vm : instances) { + ServiceOffering so = ApiDBUtils.findServiceOfferingById(vm.getServiceOfferingId()); + cpu += so.getCpu() * so.getSpeed(); + } + cpuAlloc = decimalFormat.format(((float) cpu / (float) (host.getCpus() * host.getSpeed())) * 100f) + "%"; + hostResponse.setCpuAllocated(cpuAlloc); + + String cpuWithOverprovisioning = new Float(host.getCpus() * host.getSpeed() * ApiDBUtils.getCpuOverprovisioningFactor()).toString(); + hostResponse.setCpuWithOverprovisioning(cpuWithOverprovisioning); + } + + // calculate cpu utilized + String cpuUsed = null; + HostStats hostStats = ApiDBUtils.getHostStatistics(host.getId()); + if (hostStats != null) { + float cpuUtil = (float) hostStats.getCpuUtilization(); + cpuUsed = decimalFormat.format(cpuUtil) + "%"; + hostResponse.setCpuUsed(cpuUsed); + hostResponse.setNetworkKbsRead((new Double(hostStats.getNetworkReadKBs())).longValue()); + hostResponse.setNetworkKbsWrite((new Double(hostStats.getNetworkWriteKBs())).longValue()); + } + + if (host.getType() == Host.Type.Routing) { + hostResponse.setMemoryTotal(host.getTotalMemory()); + + // calculate memory allocated by systemVM and userVm + Long mem = ApiDBUtils.getMemoryUsagebyHost(host.getId()); + hostResponse.setMemoryAllocated(mem); + hostResponse.setMemoryUsed(mem); + hostResponse.setHostTags(ApiDBUtils.getHostTags(host.getId())); + } else if (host.getType().toString().equals("Storage")) { + hostResponse.setDiskSizeTotal(host.getTotalSize()); + hostResponse.setDiskSizeAllocated(0L); + } + } + + if (details > 2) { + Set possibleEvents = host.getStatus().getPossibleEvents(); + if ((possibleEvents != null) && !possibleEvents.isEmpty()) { + String events = ""; + Iterator iter = possibleEvents.iterator(); + while (iter.hasNext()) { + com.cloud.host.Status.Event event = iter.next(); + events += event.toString(); + if (iter.hasNext()) { + events += "; "; + } + } + hostResponse.setEvents(events); + } + } + + hostResponse.setAllocationState(host.getHostAllocationState().toString()); + + hostResponse.setObjectName("host"); + + return hostResponse; + } + @Override public VlanIpRangeResponse createVlanIpRangeResponse(Vlan vlan) { Long podId = ApiDBUtils.getPodIdForVlan(vlan.getId());