<commit_before>import {
  ensureArray,
  extractProperty,
  forEach,
  isArray,
  mapToArray,
  parseXml
} from './utils'
import {
  isHostRunning,
  isVmHvm,
  isVmRunning,
  parseDateTime
} from './xapi'

// ===================================================================

const {
  defineProperties,
  freeze
} = Object

function link (obj, prop, idField = '$id') {
  const dynamicValue = obj[`$${prop}`]
  if (dynamicValue == null) {
    return dynamicValue // Properly handles null and undefined.
  }

  if (isArray(dynamicValue)) {
    return mapToArray(dynamicValue, idField)
  }

  return dynamicValue[idField]
}

// Parse a string date time to a Unix timestamp (in seconds).
//
// If there are no data or if the timestamp is 0, returns null.
function toTimestamp (date) {
  if (!date) {
    return null
  }

  const ms = parseDateTime(date).getTime()
  if (!ms) {
    return null
  }

  return Math.round(ms / 1000)
}

// ===================================================================

const TRANSFORMS = {
  pool (obj) {
    return {
      default_SR: link(obj, 'default_SR'),
      HA_enabled: Boolean(obj.ha_enabled),
      master: link(obj, 'master'),
      tags: obj.tags,
      name_description: obj.name_description,
      name_label: obj.name_label || obj.$master.name_label

      // TODO
      // - ? networks = networksByPool.items[pool.id] (network.$pool.id)
      // - hosts = hostsByPool.items[pool.id] (host.$pool.$id)
      // - patches = poolPatchesByPool.items[pool.id] (poolPatch.$pool.id)
      // - SRs = srsByContainer.items[pool.id] (sr.$container.id)
      // - templates = vmTemplatesByContainer.items[pool.id] (vmTemplate.$container.$id)
      // - VMs = vmsByContainer.items[pool.id] (vm.$container.id)
      // - $running_hosts = runningHostsByPool.items[pool.id] (runningHost.$pool.id)
      // - $running_VMs = runningVmsByPool.items[pool.id] (runningHost.$pool.id)
      // - $VMs = vmsByPool.items[pool.id] (vm.$pool.id)
    }
  },

  // -----------------------------------------------------------------

  host (obj) {
    const {
      $metrics: metrics,
      other_config: otherConfig
    } = obj

    const isRunning = isHostRunning(obj)

    return {
      address: obj.address,
      bios_strings: obj.bios_strings,
      build: obj.software_version.build_number,
      CPUs: obj.cpu_info,
      enabled: Boolean(obj.enabled),
      current_operations: obj.current_operations,
      hostname: obj.hostname,
      iSCSI_name: otherConfig.iscsi_iqn || null,
      license_params: obj.license_params,
      license_server: obj.license_server,
      name_description: obj.name_description,
      name_label: obj.name_label,
      memory: (function () {
        if (metrics) {
          const free = +metrics.memory_free
          const total = +metrics.memory_total

          return {
            usage: total - free,
            size: total
          }
        }

        return {
          usage: 0,
          total: 0
        }
      })(),
      patches: link(obj, 'patches'),
      powerOnMode: obj.power_on_mode,
      power_state: isRunning ? 'Running' : 'Halted',
      tags: obj.tags,
      version: obj.software_version.product_version,

      // TODO: dedupe.
      PIFs: link(obj, 'PIFs'),
      $PIFs: link(obj, 'PIFs'),
      PCIs: link(obj, 'PCIs'),
      $PCIs: link(obj, 'PCIs'),
      PGPUs: link(obj, 'PGPUs'),
      $PGPUs: link(obj, 'PGPUs'),

      $PBDs: link(obj, 'PBDs')<commit_msg>New host state: Unknown.<commit_after>import {
  ensureArray,
  extractProperty,
  forEach,
  isArray,
  mapToArray,
  parseXml
} from './utils'
import {
  isHostRunning,
  isVmHvm,
  isVmRunning,
  parseDateTime
} from './xapi'

// ===================================================================

const {
  defineProperties,
  freeze
} = Object

function link (obj, prop, idField = '$id') {
  const dynamicValue = obj[`$${prop}`]
  if (dynamicValue == null) {
    return dynamicValue // Properly handles null and undefined.
  }

  if (isArray(dynamicValue)) {
    return mapToArray(dynamicValue, idField)
  }

  return dynamicValue[idField]
}

// Parse a string date time to a Unix timestamp (in seconds).
//
// If there are no data or if the timestamp is 0, returns null.
function toTimestamp (date) {
  if (!date) {
    return null
  }

  const ms = parseDateTime(date).getTime()
  if (!ms) {
    return null
  }

  return Math.round(ms / 1000)
}

// ===================================================================

const TRANSFORMS = {
  pool (obj) {
    return {
      default_SR: link(obj, 'default_SR'),
      HA_enabled: Boolean(obj.ha_enabled),
      master: link(obj, 'master'),
      tags: obj.tags,
      name_description: obj.name_description,
      name_label: obj.name_label || obj.$master.name_label

      // TODO
      // - ? networks = networksByPool.items[pool.id] (network.$pool.id)
      // - hosts = hostsByPool.items[pool.id] (host.$pool.$id)
      // - patches = poolPatchesByPool.items[pool.id] (poolPatch.$pool.id)
      // - SRs = srsByContainer.items[pool.id] (sr.$container.id)
      // - templates = vmTemplatesByContainer.items[pool.id] (vmTemplate.$container.$id)
      // - VMs = vmsByContainer.items[pool.id] (vm.$container.id)
      // - $running_hosts = runningHostsByPool.items[pool.id] (runningHost.$pool.id)
      // - $running_VMs = runningVmsByPool.items[pool.id] (runningHost.$pool.id)
      // - $VMs = vmsByPool.items[pool.id] (vm.$pool.id)
    }
  },

  // -----------------------------------------------------------------

  host (obj) {
    const {
      $metrics: metrics,
      other_config: otherConfig
    } = obj

    const isRunning = isHostRunning(obj)

    return {
      address: obj.address,
      bios_strings: obj.bios_strings,
      build: obj.software_version.build_number,
      CPUs: obj.cpu_info,
      enabled: Boolean(obj.enabled),
      current_operations: obj.current_operations,
      hostname: obj.hostname,
      iSCSI_name: otherConfig.iscsi_iqn || null,
      license_params: obj.license_params,
      license_server: obj.license_server,
      name_description: obj.name_description,
      name_label: obj.name_label,
      memory: (function () {
        if (metrics) {
          const free = +metrics.memory_free
          const total = +metrics.memory_total

          return {
            usage: total - free,
            size: total
          }
        }

        return {
          usage: 0,
          total: 0
        }
      })(),
      patches: link(obj, 'patches'),
      powerOnMode: obj.power_on_mode,
      power_state: metrics
        ? (isRunning ? 'Running' : 'Halted')
        : 'Unknown',
      tags: obj.tags,
      version: obj.software_version.product_version,

      // TODO: dedupe.
      PIFs: link(obj, 'PIFs'),
      $PIFs: link(obj, 'PIFs'),
      PCIs: link(obj, 'PCIs'),
      $PCIs: link(obj, 'PCIs'),
      PGPUs: link(obj, 'PGPUs'),
      $PGPUs: link(obj, 'PGPUs'),

      $PBDs: link(obj, 'PBDs')
{'fraction_non_alphanumeric': 0.1844785540094678, 'fraction_numerical': 0.0020083201836178454, 'mean_word_length': 2.429414658140679, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3361947', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2217, 'n_words': 462}