<commit_before>import _ from 'intl'
import assign from 'lodash/assign'
import Component from 'base-component'
import find from 'lodash/find'
import flatten from 'lodash/flatten'
import Icon from 'icon'
import map from 'lodash/map'
import mapValues from 'lodash/mapValues'
import Page from '../page'
import pick from 'lodash/pick'
import React, { cloneElement } from 'react'
import SrActionBar from './action-bar'
import { Container, Row, Col } from 'grid'
import { editSr } from 'xo'
import { NavLink, NavTabs } from 'nav'
import { Text } from 'editable'
import {
  connectStore,
  routes
} from 'utils'
import {
  createGetObject,
  createGetObjectMessages,
  createGetObjectsOfType,
  createSelector
} from 'selectors'

import TabAdvanced from './tab-advanced'
import TabGeneral from './tab-general'
import TabLogs from './tab-logs'
import TabHosts from './tab-host'
import TabDisks from './tab-disks'

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

@routes('general', {
  advanced: TabAdvanced,
  general: TabGeneral,
  logs: TabLogs,
  hosts: TabHosts,
  disks: TabDisks
})
@connectStore(() => {
  const getSr = createGetObject()

  const getContainer = createGetObject(
    (state, props) => getSr(state, props).$container
  )

  const getPbds = createGetObjectsOfType('PBD').pick(
    createSelector(getSr, sr => sr.$PBDs),
  )

  const getSrHosts = createGetObjectsOfType('host').pick(
    createSelector(
      getPbds,
      pbds => map(pbds, pbd => pbd.host)
    )
  )

  const getVdis = createGetObjectsOfType('VDI').pick(
    createSelector(getSr, sr => sr.VDIs),
  ).sort()

  const getLogs = createGetObjectMessages(getSr)

  const getVbdsByVdi = createGetObjectsOfType('VBD').pick(
    createSelector(
      getVdis,
      vdis => flatten(map(vdis, vdi => vdi.$VBDs))
    )
  ).groupBy('VDI')

  const getVdisToVmIds = createSelector(
    getVbdsByVdi,
    vbdsByVdi => mapValues(vbdsByVdi, vbds => {
      const vbd = find(vbds, 'VM')
      if (vbd) {
        return vbd.VM
      }
    })
  )

  return (state, props) => {
    const sr = getSr(state, props)
    if (!sr) {
      return {}
    }

    return {
      container: getContainer(state, props),
      hosts: getSrHosts(state, props),
      pbds: getPbds(state, props),
      logs: getLogs(state, props),
      vdis: getVdis(state, props),
      vdisToVmIds: getVdisToVmIds(state, props),
      sr
    }
  }
})
export default class Sr extends Component {
  header () {
    const { sr, container } = this.props
    if (!sr) {
      return <Icon icon='loading' />
    }
    return <Container>
      <Row>
        <Col mediumSize={6} className='header-title'>
          <h2>
            <Icon icon='sr' />
            {' '}
            <Text
              value={sr.name_label}
              onChange={nameLabel => editSr(sr, { nameLabel })}
            />
          </h2>
          <span>
            <Text
              value={sr.name_description}
              onChange={nameDescription => editSr(sr, { nameDescription })}
            />
            <span className='text-muted'>
              {' - '}{container.name_label}
            </span>
          </span>
        </Col>
        <Col mediumSize={6}>
          <div className='text-xs-center'>
            <SrActionBar sr={sr} />
          </div>
        </Col>
      </Row>
      <br />
      <Row>
        <Col>
          <NavTabs>
            <NavLink to={`/srs/${sr.id}/general`}>{_('generalTabName')}</NavLink>
            <NavLink to={`/srs/${sr.id}/disks`}>{_('disksTabName', { disks: sr.VDIs.length })}</NavLink><commit_msg>fix(sr): container var check if not defined<commit_after>import _ from 'intl'
import assign from 'lodash/assign'
import Component from 'base-component'
import find from 'lodash/find'
import flatten from 'lodash/flatten'
import Icon from 'icon'
import map from 'lodash/map'
import mapValues from 'lodash/mapValues'
import Page from '../page'
import pick from 'lodash/pick'
import React, { cloneElement } from 'react'
import SrActionBar from './action-bar'
import { Container, Row, Col } from 'grid'
import { editSr } from 'xo'
import { NavLink, NavTabs } from 'nav'
import { Text } from 'editable'
import {
  connectStore,
  routes
} from 'utils'
import {
  createGetObject,
  createGetObjectMessages,
  createGetObjectsOfType,
  createSelector
} from 'selectors'

import TabAdvanced from './tab-advanced'
import TabGeneral from './tab-general'
import TabLogs from './tab-logs'
import TabHosts from './tab-host'
import TabDisks from './tab-disks'

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

@routes('general', {
  advanced: TabAdvanced,
  general: TabGeneral,
  logs: TabLogs,
  hosts: TabHosts,
  disks: TabDisks
})
@connectStore(() => {
  const getSr = createGetObject()

  const getContainer = createGetObject(
    (state, props) => getSr(state, props).$container
  )

  const getPbds = createGetObjectsOfType('PBD').pick(
    createSelector(getSr, sr => sr.$PBDs),
  )

  const getSrHosts = createGetObjectsOfType('host').pick(
    createSelector(
      getPbds,
      pbds => map(pbds, pbd => pbd.host)
    )
  )

  const getVdis = createGetObjectsOfType('VDI').pick(
    createSelector(getSr, sr => sr.VDIs),
  ).sort()

  const getLogs = createGetObjectMessages(getSr)

  const getVbdsByVdi = createGetObjectsOfType('VBD').pick(
    createSelector(
      getVdis,
      vdis => flatten(map(vdis, vdi => vdi.$VBDs))
    )
  ).groupBy('VDI')

  const getVdisToVmIds = createSelector(
    getVbdsByVdi,
    vbdsByVdi => mapValues(vbdsByVdi, vbds => {
      const vbd = find(vbds, 'VM')
      if (vbd) {
        return vbd.VM
      }
    })
  )

  return (state, props) => {
    const sr = getSr(state, props)
    if (!sr) {
      return {}
    }

    return {
      container: getContainer(state, props),
      hosts: getSrHosts(state, props),
      pbds: getPbds(state, props),
      logs: getLogs(state, props),
      vdis: getVdis(state, props),
      vdisToVmIds: getVdisToVmIds(state, props),
      sr
    }
  }
})
export default class Sr extends Component {
  header () {
    const { sr, container } = this.props
    if (!sr) {
      return <Icon icon='loading' />
    }
    return <Container>
      <Row>
        <Col mediumSize={6} className='header-title'>
          <h2>
            <Icon icon='sr' />
            {' '}
            <Text
              value={sr.name_label}
              onChange={nameLabel => editSr(sr, { nameLabel })}
            />
          </h2>
          <span>
            <Text
              value={sr.name_description}
              onChange={nameDescription => editSr(sr, { nameDescription })}
            />
            {container &&
              <span className='text-muted'>
                {' - '}{container.name_label}
              </span>
            }
          </span>
        </Col>
        <Col mediumSize={6}>
          <div className='text-xs-center'>
            <SrActionBar sr={sr} />
          </div>
        </Col>
      </Row>
      <br />
      <Row>
        <Col>
          <NavTabs>
            <NavLink to={`/srs/${sr.id}/general`}>{_('generalTabName')}</NavLink>
            <NavLink to={`/srs/${sr.id}/disks`}>{_('disksTabName', { disks: sr.VDIs.length })}</NavLink>
{'fraction_non_alphanumeric': 0.1560430463576159, 'fraction_numerical': 0.0011037527593818985, 'mean_word_length': 2.52921129503408, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 85, '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': '3588841', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2346, 'n_words': 534}