// Copyright 2014 Boundary, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.boundary.metrics.vmware.poller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import com.boundary.metrics.vmware.client.metrics.Metric;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.vmware.vim25.PerfCounterInfo;
import com.vmware.vim25.PerfMetricId;

/**
 * Contains all of the Performance Counter Metadata
 */
public class PerformanceCounterMetadata {
	

	
	private ImmutableMap.Builder<Integer,PerfCounterInfo> infoMap;
	private ImmutableMap.Builder<String,Integer> nameMap;

	private List<PerfMetricId> performanceMetricIds;
	
	/**
	 * Constructore
	 * @param infoMap Mapping of performance counter id and {@link PerfCounterInfo}
	 * @param nameMap Mapping
	 */
	public PerformanceCounterMetadata() {
		this.infoMap = ImmutableMap.builder();
		this.nameMap = ImmutableMap.builder();
	}
	
	/**
	 * Add a {@link PerfCounterInfo} instance to the collection.
	 * 
	 * @param counterInfo {@link PerfCounterInfo}
	 */
	public void put(PerfCounterInfo counterInfo) {
		infoMap.put(counterInfo.getKey(),counterInfo);
		nameMap.put(toFullName(counterInfo), counterInfo.getKey());
	}

	public Map<String,Integer> getNameMap() {
		return nameMap.build();
	}
	
	/**
	 * Returns a {@link Map} of the collection that maps performance
	 * counter id to {@link PerfCounterInfo}
	 * 
	 * @return Contents of the collection 
	 */
	public Map<Integer,PerfCounterInfo> getInfoMap() {
		return infoMap.build();
	}
	
	public List<PerfMetricId> getPerformanceMetricIds(Map<String,Metric> metrics) {
		Map<String,Integer> nameMap = this.getNameMap();
		this.performanceMetricIds = new ArrayList<PerfMetricId>();
		
		for (String counterName : metrics.keySet()) {
			if (nameMap.containsKey(counterName)) {
				PerfMetricId metricId = new PerfMetricId();
				// Get the ID for this counter.
				metricId.setCounterId(nameMap.get(counterName));
				metricId.setInstance("*");
				performanceMetricIds.add(metricId);
			}
		}
		return this.performanceMetricIds;
	}
	
	/**
	 * Helper method to generate a string use for the key for the {@link Map}
	 * @param perfCounterInfo {@link PerfCounterInfo}
	 * @return String full name of the counter.
	 */
	public static String toFullName(PerfCounterInfo perfCounterInfo) {
        return toFullName.apply(perfCounterInfo);
    }

	/**
	 * Static function name
	 */
    private static final Function<PerfCounterInfo, String> toFullName = new Function<PerfCounterInfo, String>() {
        @Nullable
        @Override
        public String apply(@Nullable PerfCounterInfo input) {
            return input == null ? null : String.format("%s.%s.%s", input.getGroupInfo().getKey(),
                    input.getNameInfo().getKey(), input.getRollupType().toString().toUpperCase());
        }
    };
    
    public static String toString(PerfMetricId input) {
    	return toStringFunction.apply(input);
    }
    
    public static final Function<PerfMetricId, String> toStringFunction = new Function<PerfMetricId, String>() {
      @Nullable
      @Override
      public String apply(@Nullable PerfMetricId input) {
          return input == null ? null : String.format("CounterID: %s, InstanceId: %s", input.getCounterId(), input.getInstance());
      }
  };
}

@@ -2692,24 +2692,712 @@
 icIds;%0A%09%7D%0A%09%0A
+%09 /**%0A     * Method to ratio the sampled value based on units%0A     * %0A     * @param metricInfo %7B@link PerfCounterInfo%7D Contains information about%0A     * @param value Sampled Value%0A     * @return %7B@link Number%7D New value%0A     */%0A    public static Number computeValue(PerfCounterInfo metricInfo,Number value) %7B%0A    %09String key = metricInfo.getUnitInfo().getKey();%0A    %09if (key.equalsIgnoreCase(%22kiloBytes%22)) %7B%0A    %09%09// Convert kilobytes to bytes%0A%09%09%09value = (long) value * 1024; %0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%0A%09%09%7D else if (key.equalsIgnoreCase(%22percent%22)) %7B%0A%09%09%09// Convert hundredth of a percent to a decimal percent%0A%09%09%09value = new Long((long) value).doubleValue() / 10000.0;%0A%09%09%7D%0A    %09return value;%0A    %7D%0A%09%0A
 %09/**%0A%09 * Hel

Add helper static method to compute sampled values