<commit_before>import forEach from 'lodash.foreach'
import isArray from 'lodash.isarray'
import map from 'lodash.map'

import {
  ensureArray,
  extractProperty,
  parseXml
} from './utils'
import {
  isHostRunning,
  isVmRunning
} from './xapi'

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

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

  if (isArray(dynamicValue)) {
    return map(dynamicValue, '$id')
  }

  return dynamicValue.$id
}

// The JSON interface of XAPI format dates incorrectly.
const JSON_DATE_RE = /^(\d{4})(\d{2})(\d{2})T(.+)$/
function fixJsonDate (date) {
  const matches = JSON_DATE_RE.exec(date)

  if (!matches) {
    return date
  }

  const [, year, month, day, time] = matches
  return `${year}-${month}-${day}T${time}`
}

function toTimestamp (date) {
  if (!date) {
    return null
  }

  return Math.round(Date.parse(fixJsonDate(date)) / 1000)
}

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

export function pool (obj) {
  return {
    default_SR: link(obj, 'default_SR'),
    HA_enabled: Boolean(obj.ha_enabled),
    master: link(obj, 'master'),
    name_description: obj.name_description,
    name_label: obj.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)
  }
}

// -------------------------------------------------------------------
<commit_msg>A pool’s name defaults to its master’s name.<commit_after>import forEach from 'lodash.foreach'
import isArray from 'lodash.isarray'
import map from 'lodash.map'

import {
  ensureArray,
  extractProperty,
  parseXml
} from './utils'
import {
  isHostRunning,
  isVmRunning
} from './xapi'

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

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

  if (isArray(dynamicValue)) {
    return map(dynamicValue, '$id')
  }

  return dynamicValue.$id
}

// The JSON interface of XAPI format dates incorrectly.
const JSON_DATE_RE = /^(\d{4})(\d{2})(\d{2})T(.+)$/
function fixJsonDate (date) {
  const matches = JSON_DATE_RE.exec(date)

  if (!matches) {
    return date
  }

  const [, year, month, day, time] = matches
  return `${year}-${month}-${day}T${time}`
}

function toTimestamp (date) {
  if (!date) {
    return null
  }

  return Math.round(Date.parse(fixJsonDate(date)) / 1000)
}

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

export function pool (obj) {
  return {
    default_SR: link(obj, 'default_SR'),
    HA_enabled: Boolean(obj.ha_enabled),
    master: link(obj, 'master'),
    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)
  }
}

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

{'fraction_non_alphanumeric': 0.24397230842683218, 'fraction_numerical': 0.0033420864168059203, 'mean_word_length': 3.1691542288557213, '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': '5493249', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1353, 'n_words': 270}