<commit_before>
export function calculateTotals(resources, selectedNamespaces) {
  let countsByKind = {}
  let namespacesFiltered = (Object.keys(selectedNamespaces).length > 0)
  for (let key in resources) {
    let resource = resources[key]
    if (!namespacesFiltered || resource.metadata.namespace in selectedNamespaces) {
      countsByKind[resource.kind] = (countsByKind[resource.kind] || 0) + 1
    }
  }
  return countsByKind
}

const statUnits = {
  cpu: {
    baseUnit: 'millicores',
    targetUnit: 'cores',
  },
  memory: {
    baseUnit: 'bytes',
    targetUnit: 'gibibytes',
  },
  disk: {
    baseUnit: 'bytes',
    targetUnit: 'gibibytes',
  },
  volumes: {
    baseUnit: 'bytes',
    targetUnit: 'gibibytes',
  },
  netRx: {
    baseUnit: 'bytes/sec',
    targetUnit: 'kibibytes/sec',
  },
  netTx: {
    baseUnit: 'bytes/sec',
    targetUnit: 'kibibytes/sec',
  },
}

export function calculateMetrics(clusterMetrics, namespaceMetrics, selectedNamespaces) {
  
  let namespacesFiltered = (Object.keys(selectedNamespaces).length > 0)
  
  const stats = {
    cpu: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    memory: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    disk: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    volumes: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
    },
    netRx: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    netTx: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
  }

  if (!!namespaceMetrics) {
    for (let ns in namespaceMetrics) {
      if (!namespacesFiltered || ns in selectedNamespaces) {
        let m = namespaceMetrics[ns]
        for (let s in stats) {
          accumulateStats(stats, s, m)
        }
      }
    }
  } else {
    return stats
  }

  for (let s in stats) {
    let stat = stats[s]
    stat.ratio = (stat.usage / stat.total)

    let statUnit = statUnits[s]
    if ('targetUnit' in statUnit) {
      if (statUnit.targetUnit.includes('/')) {
        stat.ratio = convert(stat.ratio, statUnit.baseUnit, statUnit.targetUnit)
      } else {
        stat.usage = convert(stat.usage, statUnit.baseUnit, statUnit.targetUnit)
        stat.total = convert(stat.total, statUnit.baseUnit, statUnit.targetUnit)
      }
      stat.units = statUnit.targetUnit
    }
  }
  return stats
}

function accumulateStats(stats, stat, m) {<commit_msg>default ratio to 0 instead of NaN<commit_after>
export function calculateTotals(resources, selectedNamespaces) {
  let countsByKind = {}
  let namespacesFiltered = (Object.keys(selectedNamespaces).length > 0)
  for (let key in resources) {
    let resource = resources[key]
    if (!namespacesFiltered || resource.metadata.namespace in selectedNamespaces) {
      countsByKind[resource.kind] = (countsByKind[resource.kind] || 0) + 1
    }
  }
  return countsByKind
}

const statUnits = {
  cpu: {
    baseUnit: 'millicores',
    targetUnit: 'cores',
  },
  memory: {
    baseUnit: 'bytes',
    targetUnit: 'gibibytes',
  },
  disk: {
    baseUnit: 'bytes',
    targetUnit: 'gibibytes',
  },
  volumes: {
    baseUnit: 'bytes',
    targetUnit: 'gibibytes',
  },
  netRx: {
    baseUnit: 'bytes/sec',
    targetUnit: 'kibibytes/sec',
  },
  netTx: {
    baseUnit: 'bytes/sec',
    targetUnit: 'kibibytes/sec',
  },
}

export function calculateMetrics(clusterMetrics, namespaceMetrics, selectedNamespaces, allNamespaces) {
  
  let namespacesFiltered = (Object.keys(selectedNamespaces).length > 0)
  
  const stats = {
    cpu: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    memory: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    disk: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    volumes: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
    },
    netRx: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
    netTx: {
      usage: 0,
      total: 0,
      units: '',
      ratio: 0,
      shareTotal: true,
    },
  }

  if (!!namespaceMetrics) {
    for (let ns in namespaceMetrics) {
      if (!namespacesFiltered || ns in selectedNamespaces) {
        let m = namespaceMetrics[ns]
        for (let s in stats) {
          accumulateStats(stats, s, m)
        }
      }
    }
  } else {
    return stats
  }

  for (let s in stats) {
    let stat = stats[s]
    stat.ratio = stat.total === 0 ? 0 : (stat.usage / stat.total)

    let statUnit = statUnits[s]
    if ('targetUnit' in statUnit) {
      if (statUnit.targetUnit.includes('/')) {
        stat.ratio = convert(stat.ratio, statUnit.baseUnit, statUnit.targetUnit)
      } else {
        stat.usage = convert(stat.usage, statUnit.baseUnit, statUnit.targetUnit)
        stat.total = convert(stat.total, statUnit.baseUnit, statUnit.targetUnit)
      }
      stat.units = statUnit.targetUnit
    }
  }
  return stats
}

function accumulateStats(stats, stat, m) {
{'fraction_non_alphanumeric': 0.13034351145038167, 'fraction_numerical': 0.008969465648854962, 'mean_word_length': 2.2776735459662287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3083401', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1750, 'n_words': 406}