<commit_before>/**
 * @author      <NAME> <<EMAIL>>
 * @version     0.1                 
 * @since       2012-12-09        
 */
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.*;
import java.lang.Math;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.DateTimeZone;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import com.vmware.vim25.mo.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class VMwareInventory 
{
  private final static Logger logger = Logger.getLogger("VMwareInventory");
  private ServiceInstance si = null;
  private Folder rootFolder = null;
  // Hash of host-morefID to host hash of attributes / values
  public HashMap<String, HashMap<String, Object>> hostMap = new HashMap<String, HashMap<String, Object>>();
  // Hash of vm-UUID to vm hash of attributes / values
  public HashMap<String, HashMap<String, Object>> vmMap = new HashMap<String, HashMap<String, Object>>();
  // Hash of PerfCounter name to Counter ID
  public HashMap<String, Integer> counterMap = new HashMap<String, Integer>();
  // Hash of PerfCounter name to Counter ID to name
  public HashMap<Integer, String> counterIdMap = new HashMap<Integer, String>();
  // Set of Metric Timestamps
  public TreeSet<String> tsSet = new TreeSet<String>();
  // List of VirtualMachine MORs 
  // Utility Constants
  public final static long KB = 1024;
  public final static double MB = Math.pow(1024,2);
  public final static double GB = Math.pow(1024,3);
  public final static double TB = Math.pow(1024,4);
  // used for registering VMware Plugin
  public final static String EXT_KEY = "com.6fusion.cloudresourcemeter";
  public final static String EXT_COMPANY = "6fusion USA";
  public final static String EXT_TYPE = "com.vmware.vim.viClientScripts";
  public final static String EXT_VERSION = "3.0";
  public final static String EXT_LABEL = "6fusionCloudResourceMeter";
  public final static String[] EXT_ADMIN_EMAIL = {"<EMAIL>"};

 /**
 * VMwareInventory - API adaptor between VI Java VMWARE API and JRUBY
 *
 * This class is a helper class to gather virtual machine info and
 * metric readings from VMware 4.1/5.0/5.1 vSphere
 * <p>
 * This class can be run standalone but the primary use is to provide
 * a bridge between JRUBY and the VI java VMWARE API to minimize the
 * passing of Java classes being utilized in Ruby.
 *
 * @param  url String vCenter API URL (Ex. https://192.168.100.110/sdk)
 * @param  username String vCenter username
 * @param  password String vCenter password
 */
  public VMwareInventory(String url, String username, String password ) throws Exception
  {
    ServiceInstance si = new ServiceInstance(new URL(url), username, password, true);
    this.si = si;
  }

  public void close()
  {
    this.si.getServerConnection().logout();
  }

  public Calendar currentTime() throws Exception
  {
    return this.si.currentTime();
  }

  private ExtensionManager getExtensionManager()
  {
    return si.getExtensionManager();
  }

  private PerformanceManager getPerformanceManager()
  {
    return si.getPerformanceManager();
  }

  public void register(String url) throws Exception
  {
    Extension extension = new Extension();
    Description description = new Description();
    ExtensionServerInfo serverInfo = new ExtensionServerInfo();
    DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
    Calendar lastHeartbeatTime = (Calendar) Calendar.getInstance(TimeZone.getTimeZone("GMT")).clone();
    lastHeartbeatTime.setTime(parser2.parseDateTime("2012-12-21T00:00:00Z").toDate());
    extension.setKey(VMwareInventory.EXT_KEY);
    extension.setCompany(VMwareInventory.EXT_COMPANY);
    extension.setType(VMwareInventory.EXT_TYPE);
    extension.setVersion(VMwareInventory.EXT_VERSION);
    extension.setLastHeartbeatTime(lastHeartbeatTime);
    description.setLabel(VMwareInventory.EXT_LABEL);
    description.setSummary(VMwareInventory.EXT_LABEL);
    extension.setDescription(description);
    serverInfo.setUrl(url);
    serverInfo.setDescription(description);
    serverInfo.setCompany(VMwareInventory.EXT_COMPANY);
    serverInfo.setType(VMwareInventory.EXT_TYPE);
    serverInfo.setAdminEmail(VMwareInventory.EXT_ADMIN_EMAIL);
    this.getExtensionManager().unregisterExtension(VMwareInventory.EXT_KEY);
    this.getExtensionManager().registerExtension(extension);
  }

  public HashMap<String, String>  getAboutInfo() throws Exception
  {
    logger.fine("Entering VMwareInventory.getAboutInfo()");
    AboutInfo about = this.si.getAboutInfo();
    HashMap<String, String> props = new HashMap<String, String>();
    props.put("name",about.getName());
    props.put("fullName",about.getFullName());
    props.put("vendor",about.getVendor());
    props.put("version",about.getVersion());
    props.put("build",about.getBuild());
    props.put("localeVersion",about.getLocaleVersion());
    props.put("localeBuild",about.getLocaleBuild());
    props.put("osType",about.getOsType());
    props.put("productLineId",about.getProductLineId());
    props.put("apiType",about.getApiType());
    props.put("apiVersion",about.getApiVersion());
    props.put("instanceUuid",about.getInstanceUuid());
    props.put("licenseProductVersion",about.getLicenseProductName());
    props.put("name",about.getName());

    for(int i=0; about.getDynamicProperty() !=null && i<about.getDynamicProperty().length; i++) {
      DynamicProperty dynamicProperty = about.getDynamicProperty()[i];
      props.put(dynamicProperty.getName(),dynamicProperty.getVal().toString());
    }
    logger.fine("Exiting VMwareInventory.getAboutInfo()");
    return props;
  }

  public List<HashMap<String, String>>  getStatisticLevels() throws Exception
  {
    logger.fine("Entering VMwareInventory.getStatisticLevels()");
    PerformanceManager perfMgr = this.si.getPerformanceManager();
    PerfInterval[] perfIntervals = perfMgr.getHistoricalInterval();
    ArrayList<HashMap<String, String>> stats = new ArrayList<HashMap<String, String>>();
    for(int i=0; perfIntervals !=null && i<perfIntervals.length; i++) {
      PerfInterval interval = perfIntervals[i];
      HashMap<String, String> props = new HashMap<String, String>();
      props.put("key",Integer.toString(interval.getKey()));
      props.put("samplingPeriod",Integer.toString(interval.getSamplingPeriod()));
      props.put("name",interval.getName());
      props.put("length",Integer.toString(interval.getLength()));
      props.put("level",interval.getLevel().toString());
      props.put("enabled",Boolean.toString(interval.isEnabled()));
      stats.add(props);
    }
    logger.fine("Exiting VMwareInventory.getStatisticLevels()");
    return stats;
  }

  public HashMap<String, Object> findByUuid(String uuid) throws Exception
  {
    logger.fine("Entering VMwareInventory.findByUuid(String uuid)");
    VirtualMachine[] vms = new VirtualMachine[1];
    VirtualMachine vm = (VirtualMachine) this.si.getSearchIndex().findByUuid(null,uuid,true,false);
    vms[0] = vm;
    gatherProperties(vms);
    logger.fine("Exiting VMwareInventory.findByUuid(String uuid)");
    return vmMap.get(vm.getMOR().get_value().toString());
  }

  public HashMap<String, Object>  findByUuidWithReadings(String uuid, String startIso8601, String endIso8601) throws Exception
  {
    logger.fine("Entering VMwareInventory.findByUuidWithReadings()");
    VirtualMachine[] vms = new VirtualMachine[1];
    VirtualMachine vm = (VirtualMachine) this.si.getSearchIndex().findByUuid(null,uuid,true,false);
    DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
    Calendar startTime = (Calendar) Calendar.getInstance(TimeZone.getTimeZone("GMT")).clone();
    Calendar endTime = (Calendar) Calendar.getInstance(TimeZone.getTimeZone("GMT")).clone();
    startTime.setTime(parser2.parseDateTime(startIso8601).toDate());
    endTime.setTime(parser2.parseDateTime(endIso8601).toDate());
    vms[0] = vm;
    gatherProperties(vms);
    List<VirtualMachine> vms_list = new ArrayList<VirtualMachine>(Arrays.asList(vms));
    readings(vms_list,startTime,endTime);
    logger.fine("Exiting VMwareInventory.findByUuidWithReadings()");
    return vmMap.get(vm.getMOR().get_value().toString());
  }

<commit_msg>Added error checking<commit_after>/**
 * @author      <NAME> <<EMAIL>>
 * @version     0.1                 
 * @since       2012-12-09        
 */
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.*;
import java.lang.Math;
import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.DateTimeZone;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.*;
import com.vmware.vim25.mo.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class VMwareInventory 
{
  private final static Logger logger = Logger.getLogger("VMwareInventory");
  private ServiceInstance si = null;
  private Folder rootFolder = null;
  // Hash of host-morefID to host hash of attributes / values
  public HashMap<String, HashMap<String, Object>> hostMap = new HashMap<String, HashMap<String, Object>>();
  // Hash of vm-UUID to vm hash of attributes / values
  public HashMap<String, HashMap<String, Object>> vmMap = new HashMap<String, HashMap<String, Object>>();
  // Hash of PerfCounter name to Counter ID
  public HashMap<String, Integer> counterMap = new HashMap<String, Integer>();
  // Hash of PerfCounter name to Counter ID to name
  public HashMap<Integer, String> counterIdMap = new HashMap<Integer, String>();
  // Set of Metric Timestamps
  public TreeSet<String> tsSet = new TreeSet<String>();
  // List of VirtualMachine MORs 
  // Utility Constants
  public final static long KB = 1024;
  public final static double MB = Math.pow(1024,2);
  public final static double GB = Math.pow(1024,3);
  public final static double TB = Math.pow(1024,4);
  // used for registering VMware Plugin
  public final static String EXT_KEY = "com.6fusion.cloudresourcemeter";
  public final static String EXT_COMPANY = "6fusion USA";
  public final static String EXT_TYPE = "com.vmware.vim.viClientScripts";
  public final static String EXT_VERSION = "3.0";
  public final static String EXT_LABEL = "6fusionCloudResourceMeter";
  public final static String[] EXT_ADMIN_EMAIL = {"<EMAIL>"};

 /**
 * VMwareInventory - API adaptor between VI Java VMWARE API and JRUBY
 *
 * This class is a helper class to gather virtual machine info and
 * metric readings from VMware 4.1/5.0/5.1 vSphere
 * <p>
 * This class can be run standalone but the primary use is to provide
 * a bridge between JRUBY and the VI java VMWARE API to minimize the
 * passing of Java classes being utilized in Ruby.
 *
 * @param  url String vCenter API URL (Ex. https://192.168.100.110/sdk)
 * @param  username String vCenter username
 * @param  password String vCenter password
 */
  public VMwareInventory(String url, String username, String password ) throws Exception
  {
    ServiceInstance si = new ServiceInstance(new URL(url), username, password, true);
    this.si = si;
  }

  public void close()
  {
    this.si.getServerConnection().logout();
  }

  public Calendar currentTime() throws Exception
  {
    return this.si.currentTime();
  }

  private ExtensionManager getExtensionManager()
  {
    return si.getExtensionManager();
  }

  private PerformanceManager getPerformanceManager()
  {
    return si.getPerformanceManager();
  }

  public void register(String url) throws Exception
  {
    Extension extension = new Extension();
    Description description = new Description();
    ExtensionServerInfo serverInfo = new ExtensionServerInfo();
    DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
    Calendar lastHeartbeatTime = (Calendar) Calendar.getInstance(TimeZone.getTimeZone("GMT")).clone();
    lastHeartbeatTime.setTime(parser2.parseDateTime("2012-12-21T00:00:00Z").toDate());
    extension.setKey(VMwareInventory.EXT_KEY);
    extension.setCompany(VMwareInventory.EXT_COMPANY);
    extension.setType(VMwareInventory.EXT_TYPE);
    extension.setVersion(VMwareInventory.EXT_VERSION);
    extension.setLastHeartbeatTime(lastHeartbeatTime);
    description.setLabel(VMwareInventory.EXT_LABEL);
    description.setSummary(VMwareInventory.EXT_LABEL);
    extension.setDescription(description);
    serverInfo.setUrl(url);
    serverInfo.setDescription(description);
    serverInfo.setCompany(VMwareInventory.EXT_COMPANY);
    serverInfo.setType(VMwareInventory.EXT_TYPE);
    serverInfo.setAdminEmail(VMwareInventory.EXT_ADMIN_EMAIL);
    this.getExtensionManager().unregisterExtension(VMwareInventory.EXT_KEY);
    this.getExtensionManager().registerExtension(extension);
  }

  public HashMap<String, String>  getAboutInfo() throws Exception
  {
    logger.fine("Entering VMwareInventory.getAboutInfo()");
    AboutInfo about = this.si.getAboutInfo();
    HashMap<String, String> props = new HashMap<String, String>();
    props.put("name",about.getName());
    props.put("fullName",about.getFullName());
    props.put("vendor",about.getVendor());
    props.put("version",about.getVersion());
    props.put("build",about.getBuild());
    props.put("localeVersion",about.getLocaleVersion());
    props.put("localeBuild",about.getLocaleBuild());
    props.put("osType",about.getOsType());
    props.put("productLineId",about.getProductLineId());
    props.put("apiType",about.getApiType());
    props.put("apiVersion",about.getApiVersion());
    props.put("instanceUuid",about.getInstanceUuid());
    props.put("licenseProductVersion",about.getLicenseProductName());
    props.put("name",about.getName());

    for(int i=0; about.getDynamicProperty() !=null && i<about.getDynamicProperty().length; i++) {
      DynamicProperty dynamicProperty = about.getDynamicProperty()[i];
      props.put(dynamicProperty.getName(),dynamicProperty.getVal().toString());
    }
    logger.fine("Exiting VMwareInventory.getAboutInfo()");
    return props;
  }

  public List<HashMap<String, String>>  getStatisticLevels() throws Exception
  {
    logger.fine("Entering VMwareInventory.getStatisticLevels()");
    PerformanceManager perfMgr = this.si.getPerformanceManager();
    PerfInterval[] perfIntervals = perfMgr.getHistoricalInterval();
    ArrayList<HashMap<String, String>> stats = new ArrayList<HashMap<String, String>>();
    for(int i=0; perfIntervals !=null && i<perfIntervals.length; i++) {
      PerfInterval interval = perfIntervals[i];
      HashMap<String, String> props = new HashMap<String, String>();
      props.put("key",Integer.toString(interval.getKey()));
      props.put("samplingPeriod",Integer.toString(interval.getSamplingPeriod()));
      props.put("name",interval.getName());
      props.put("length",Integer.toString(interval.getLength()));
      props.put("level",interval.getLevel().toString());
      props.put("enabled",Boolean.toString(interval.isEnabled()));
      stats.add(props);
    }
    logger.fine("Exiting VMwareInventory.getStatisticLevels()");
    return stats;
  }

  public HashMap<String, Object> findByUuid(String uuid) throws Exception
  {
    logger.fine("Entering VMwareInventory.findByUuid(String uuid)");
    VirtualMachine[] vms = new VirtualMachine[1];
    VirtualMachine vm = (VirtualMachine) this.si.getSearchIndex().findByUuid(null,uuid,true,false);
    if (vm == null) {
      logger.info("Machine UUID "+uuid+" not found");
      return null
    }
    vms[0] = vm;
    gatherProperties(vms);
    logger.fine("Exiting VMwareInventory.findByUuid(String uuid)");
    return vmMap.get(vm.getMOR().get_value().toString());
  }

  public HashMap<String, Object>  findByUuidWithReadings(String uuid, String startIso8601, String endIso8601) throws Exception
  {
    logger.fine("Entering VMwareInventory.findByUuidWithReadings()");
    VirtualMachine[] vms = new VirtualMachine[1];
    VirtualMachine vm = (VirtualMachine) this.si.getSearchIndex().findByUuid(null,uuid,true,false);
    DateTimeFormatter parser2 = ISODateTimeFormat.dateTimeNoMillis();
    Calendar startTime = (Calendar) Calendar.getInstance(TimeZone.getTimeZone("GMT")).clone();
    Calendar endTime = (Calendar) Calendar.getInstance(TimeZone.getTimeZone("GMT")).clone();
    startTime.setTime(parser2.parseDateTime(startIso8601).toDate());
    endTime.setTime(parser2.parseDateTime(endIso8601).toDate());
    if (vm == null) {
      logger.info("Machine UUID "+uuid+" not found");
      return null
    }
    vms[0] = vm;
    gatherProperties(vms);
    List<VirtualMachine> vms_list = new ArrayList<VirtualMachine>(Arrays.asList(vms));
    readings(vms_list,startTime,endTime);
    logger.fine("Exiting VMwareInventory.findByUuidWithReadings()");
    return vmMap.get(vm.getMOR().get_value().toString());
  }


{'fraction_non_alphanumeric': 0.12511117699377408, 'fraction_numerical': 0.01174029054254373, 'mean_word_length': 4.770099213137188, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 71, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3924970', 'n_tokens_mistral': 5109, 'n_tokens_neox': 4943, 'n_words': 1068}