/* (non-Javadoc)
   * <p>Title: doMonitoring</p>
   * <p>Description: </p>
   * @see auto_scaling.monitor.Monitor#doMonitoring()
   */
  @Override
  public void doMonitoring() {
    SystemStatus systemStatus = SystemStatus.getSystemStatus();
    Collection<InstanceStatus> instances =
        InstanceFilter.getAttachedInstances(systemStatus.getAllInstances());

    ApplicationBrokerManager applicationBrokerManager =
        ApplicationBrokerManager.getApplicationBrokerManager();
    CloudSimBroker cloudSimBroker = applicationBrokerManager.getCloudSimBroker();

    String CPU = ResourceType.CPU.getName();
    for (InstanceStatus instanceStatus : instances) {
      String idString = instanceStatus.getId();
      MonitoredVMex vm = (MonitoredVMex) cloudSimBroker.getVmById(Integer.parseInt(idString));
      double utilization = vm.getCPUUtil();
      instanceStatus.setResourceConsumptionValue(CPU, utilization);
    }

    if (instances.size() > 0) {
      EventGenerator eventGenerator = EventGenerator.getEventGenerator();
      Map<String, Object> data = new HashMap<String, Object>();
      data.put(EventDataName.RESOURCE_TYPE, ResourceType.CPU);
      Event newEvent = eventGenerator.generateEvent(Events.RESOURCE_REQUIREMENT_UPDATE_EVENT, data);

      Queue<Event> eventQueue = EventQueueManager.getEventsQueue();
      eventQueue.add(newEvent);
      monitorLog.info(logFormatter.getGenerateEventLogString(newEvent, CPU));
    }
  }
  /**
   * @Title: getMaximumAvaliableCapacity @Description: get the number of maximum capacity
   *
   * @return the number of maximum capacity
   * @throws
   */
  public synchronized long getMaximumAvaliableCapacity() {
    long capacity = 0;
    Collection<InstanceStatus> attachedInstances =
        InstanceFilter.getAttachedInstances(allInstances);
    for (InstanceStatus instanceStatus : attachedInstances) {

      InstanceTemplate instanceTemplate = instanceStatus.getType();
      capacity += instanceTemplate.getMaximunCapacity();
    }
    return capacity;
  }
  /**
   * @Title: getAvailableCapacity @Description: get the number of current capacity
   *
   * @return the number of current capacity
   * @throws
   */
  public synchronized long getAvailableCapacity() {
    FaultTolerantLevel ftLevel = FaultTolerantLevel.ZERO;
    if (isSpotEnabled()) {
      ftLevel = this.faultTolerantLevel;
    }

    long capacity = 0;
    Collection<InstanceStatus> attachedInstances =
        InstanceFilter.getAttachedInstances(allInstances);
    for (InstanceStatus instanceStatus : attachedInstances) {

      InstanceTemplate instanceTemplate = instanceStatus.getType();
      if (instanceStatus instanceof OnDemandInstanceStatus) {
        capacity += instanceTemplate.getCapacity(FaultTolerantLevel.ZERO);
      } else {
        capacity += instanceTemplate.getCapacity(ftLevel);
      }
    }
    return capacity;
  }
 /**
  * @Title: getNumOfAttachedInstances @Description: get the number of instances attached to the
  * load balancer
  *
  * @return
  * @throws
  */
 public synchronized int getNumOfAttachedInstances() {
   Collection<InstanceStatus> attachedInstances =
       InstanceFilter.getAttachedInstances(allInstances);
   return attachedInstances.size();
 }