<commit_before><?php
/**
 * This file is a part of Xen Orchestra Server.
 *
 * Xen Orchestra Server is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * Xen Orchestra Server is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Xen Orchestra Server. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * @author <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3
 *
 * @package Xen Orchestra Server
 */

use Bean\User;

/**
 *
 */
final class Application extends Base
{
	/**
	 *
	 */
	function __construct(DI $di)
	{
		parent::__construct();

		$this->_di = $di;
	}

	/**
	 * @todo Sort by pool.
	 */
	function api_network_getAll($id, array $params, Client $c)
	{
		$di = $this->_di;

		$mgr_pifs         = $di->get('pifs');
		$mgr_pifs_metrics = $di->get('pifs_metrics');

		$PIFs = array();
		foreach ($mgr_pifs->get() as $i => $PIF)
		{
			$PIF_metrics = $mgr_pifs_metrics->first($PIF->metrics);

			$PIFs[] = array(
				'currently_attached' => $PIF->currently_attached,
				'device'             => $PIF_metrics->device_name,
				'duplex'             => $PIF_metrics->duplex,
				'IP'                 => $PIF->IP,
				'MAC'                => $PIF->MAC,
				'name'               => 'PIF #'.$i, // @todo
				'speed'              => $PIF_metrics->speed,
				'uuid'               => $PIF->uuid,
				'vendor'             => $PIF_metrics->vendor_name,
			);
		}

		$pools = array(
			array(
				'uuid' => 'unknown',
				'name' => 'unknown pool',
				'PIFs'  => $PIFs,
			),
		);

		$c->respond($id, $pools);
	}

	/**
	 * @todo Sort by pool.
	 */
	function api_storage_getAll($id, array $params, Client $c)
	{
		$di = $this->_di;

		$mgr_srs = $di->get('srs');

		$SRs = array();
		foreach ($mgr_srs->get() as $SR)
		{
			$SRs[] = array(
				'allocated'   => $SR->virtual_allocation,
				'description' => $SR->name_description,
				'name'        => $SR->name_label,
				'shared'      => $SR->shared,
				'total'       => $SR->physical_size,
				'type'        => $SR->type,
				'used'        => $SR->physical_utilisation,
			);
		}

		$pools = array(
			array(
				'uuid' => 'unknown',
				'name' => 'unknown pool',
				'SRs'  => $SRs,
			),
		);

		$c->respond($id, $pools);
	}

	/**
	 * @todo Sort by pool.
	 */
	function api_host_getAll($id, array $params, Client $c)
	{
		$di = $this->_di;

		$mgr_hosts         = $di->get('hosts');
		$mgr_hosts_metrics = $di->get('hosts_metrics');
		$mgr_pifs          = $di->get('pifs');
		$mgr_vms           = $di->get('vms');
		$mgr_vms_metrics   = $di->get('vms_metrics');

		$hosts = array();
		foreach ($mgr_hosts->get() as $host)
		{
			$dom0 = $mgr_vms->first(array(
				'resident_on'       => $host->id,
				'is_control_domain' => true,
			));
			$dom0_metrics = $mgr_vms_metrics->first($dom0->metrics);

			$IPs = array();
			foreach ($host->PIFs as $PIF_ref)
			{
				$PIF = $mgr_pifs->first($PIF_ref);

				$IPs[] = $PIF->IP;
			}

			$host_metrics = $mgr_hosts_metrics->first($host->metrics);
			$memory = array(
				'free'  => $host_metrics->memory_free,
				'total' => $host_metrics->memory_total,
			);

			$hosts[] = array(
				'description' => $host->name_description,
				'id'          => $host->uuid,
				'IPs'         => $IPs,
				'memory'      => $memory,
				'name'        => $host->name_label,
				'start_time'  => $dom0_metrics->start_time['timestamp'],
			);
		}

		$pools = array(
			array(
				'uuid'  => 'unknown',
				'name'  => 'unknown pool',
				'hosts' => $hosts,
			),
		);

		$c->respond($id, $pools);
	}

	/**
	 *
	 */
	function api_host_get($id, array $params, Client $c)
	{
		// Checks parameter.
		if (!isset($params[0]))
		{
			return -32602; // Invalid params.
		}

		$di = $this->_di;

		$mgr_guest_metrics = $di->get('vms_guest_metrics');
		$mgr_hosts         = $di->get('hosts');
		$mgr_hosts_metrics = $di->get('hosts_metrics');
		$mgr_hosts_cpu     = $di->get('hosts_cpus');
		$mgr_pbds          = $di->get('pbds');
		$mgr_pifs          = $di->get('pifs');
		$mgr_pifs_metrics  = $di->get('pifs_metrics');
		$mgr_vms           = $di->get('vms');
		$mgr_vms_metrics   = $di->get('vms_metrics');
		$mgr_srs           = $di->get('srs');

		$host    = $mgr_hosts->first(array(
			'uuid' => $params[0],
		));
		$metrics = $mgr_hosts_metrics->first($host->metrics);

		// $mgr_messages      = $di->get('messages');
		// $mgr_networks      = $di->get('networks');
		// $mgr_vbds          = $di->get('vbds');
		// $mgr_vdis          = $di->get('vdis');
		// $mgr_vifs          = $di->get('vifs');

		$dom0 = $mgr_vms->first(array(
			'resident_on'       => $host->id,
			'is_control_domain' => true,
		));
		$dom0_metrics       = $mgr_vms_metrics->first($dom0->metrics);
		$dom0_guest_metrics = $mgr_guest_metrics->first($dom0->guest_metrics, false);

		$vms = $mgr_vms->get(array(
			'resident_on' => $host->id,
		));

		$CPUs = array();
		foreach ($mgr_hosts_cpu->get(array('host' => $host->id)) as $CPU)
		{
			$CPUs = array(
				'model'      => $CPU->model,
				'model_name' => $CPU->modelname,
				'number'     => $CPU->number,
				'speed'      => $CPU->speed,
				'vendor'     => $CPU->vendor,
			);
		}

		$memory = array(
			'free'   => $metrics->memory_free,
			'total'  => $metrics->memory_total,
			'per_VM' => array(),
		);

		$os_version = $dom0_guest_metrics
			? $dom0->guest_metrics->os_version
			: null;

		$PIFs = array();
		foreach ($mgr_pifs->get(array('host' => $host->id)) as $i => $PIF)
		{
			$PIF_metrics = $mgr_pifs_metrics->first($PIF->metrics);

			$PIFs[] = array(
				'currently_attached' => $PIF->currently_attached,
				'device'             => $PIF_metrics->device_name,
				'duplex'             => $PIF_metrics->duplex,
				'IP'                 => $PIF->IP,
				'MAC'                => $PIF->MAC,
				'name'               => 'PIF #'.$i, // @todo
				'speed'              => $PIF_metrics->speed,
				'uuid'               => $PIF->uuid,
				'vendor'             => $PIF_metrics->vendor_name,
			);
		}

		$SRs = array();
		foreach ($mgr_pbds->get(array('host' => $host->id)) as $PBD)
		{
			$SR = $mgr_srs->first($PBD->SR);
			$SRs[] = array(
				'allocated'   => $SR->virtual_allocation,
				'description' => $SR->name_description,
				'name'        => $SR->name_label,
				'shared'      => $SR->shared,
				'total'       => $SR->physical_size,
				'type'        => $SR->type,
				'used'        => $SR->physical_utilisation,
			);
		}

		$version = array( // major.minor.vendor
			'major'  => $host->API_version_major,
			'minor'  => $host->API_version_minor,
			'vendor' => $host->API_version_vendor,
		);

		$VMs = array();
		foreach ($vms as $VM)
		{
			$VMs[$VM->uuid] = array(
				'is_control_domain' => $VM->is_control_domain,
				'name'              => $VM->name_label,
			);
			$memory['per_VM'][$VM->uuid] = $VM->memory_dynamic_max;
		}

		$host = array(
			'CPUs'                  => $CPUs,
			'control_domain'        => $dom0->uuid,
			'description'           => $host->name_description,
			'enabled'               => $host->enabled,
			'hostname'              => $host->hostname,
			'is_pool_master'        => false, // @todo
			'iscsi_iqn'             => null, //@todo,
			'log_destination'       => 'local', // @todo
			'memory'                => $memory,
			'name'                  => $host->name_label,
			'os_version'            => $os_version,
			'PIFs'                  => $PIFs,
			'SRs'                   => $SRs,
			'start_time'            => $dom0_metrics->start_time['timestamp'],
			'tool_stack_start_time' => $dom0_metrics->start_time['timestamp'], // @todo
			'uuid'                  => $host->uuid,
			'version'               => $version,
			'VMs'                   => $VMs,
		);

		$c->respond($id, $host);
	}

	/**
	 *
	 */
	function api_session_signInWithPassword($id, array $params, Client $c)
	{
		// Checks parameters.
		if (!isset($params[0], $params[1]))
		{
			return -32602; // Invalid params.
		}
		list($name, $password) = $params;
<commit_msg>API[host.get]() returns software_version (related to vatesfr/xo-web#2).<commit_after><?php
/**
 * This file is a part of Xen Orchestra Server.
 *
 * Xen Orchestra Server is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * Xen Orchestra Server is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Xen Orchestra Server. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * @author <NAME> <<EMAIL>>
 * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html GPLv3
 *
 * @package Xen Orchestra Server
 */

use Bean\User;

/**
 *
 */
final class Application extends Base
{
	/**
	 *
	 */
	function __construct(DI $di)
	{
		parent::__construct();

		$this->_di = $di;
	}

	/**
	 * @todo Sort by pool.
	 */
	function api_network_getAll($id, array $params, Client $c)
	{
		$di = $this->_di;

		$mgr_pifs         = $di->get('pifs');
		$mgr_pifs_metrics = $di->get('pifs_metrics');

		$PIFs = array();
		foreach ($mgr_pifs->get() as $i => $PIF)
		{
			$PIF_metrics = $mgr_pifs_metrics->first($PIF->metrics);

			$PIFs[] = array(
				'currently_attached' => $PIF->currently_attached,
				'device'             => $PIF_metrics->device_name,
				'duplex'             => $PIF_metrics->duplex,
				'IP'                 => $PIF->IP,
				'MAC'                => $PIF->MAC,
				'name'               => 'PIF #'.$i, // @todo
				'speed'              => $PIF_metrics->speed,
				'uuid'               => $PIF->uuid,
				'vendor'             => $PIF_metrics->vendor_name,
			);
		}

		$pools = array(
			array(
				'uuid' => 'unknown',
				'name' => 'unknown pool',
				'PIFs'  => $PIFs,
			),
		);

		$c->respond($id, $pools);
	}

	/**
	 * @todo Sort by pool.
	 */
	function api_storage_getAll($id, array $params, Client $c)
	{
		$di = $this->_di;

		$mgr_srs = $di->get('srs');

		$SRs = array();
		foreach ($mgr_srs->get() as $SR)
		{
			$SRs[] = array(
				'allocated'   => $SR->virtual_allocation,
				'description' => $SR->name_description,
				'name'        => $SR->name_label,
				'shared'      => $SR->shared,
				'total'       => $SR->physical_size,
				'type'        => $SR->type,
				'used'        => $SR->physical_utilisation,
			);
		}

		$pools = array(
			array(
				'uuid' => 'unknown',
				'name' => 'unknown pool',
				'SRs'  => $SRs,
			),
		);

		$c->respond($id, $pools);
	}

	/**
	 * @todo Sort by pool.
	 */
	function api_host_getAll($id, array $params, Client $c)
	{
		$di = $this->_di;

		$mgr_hosts         = $di->get('hosts');
		$mgr_hosts_metrics = $di->get('hosts_metrics');
		$mgr_pifs          = $di->get('pifs');
		$mgr_vms           = $di->get('vms');
		$mgr_vms_metrics   = $di->get('vms_metrics');

		$hosts = array();
		foreach ($mgr_hosts->get() as $host)
		{
			$dom0 = $mgr_vms->first(array(
				'resident_on'       => $host->id,
				'is_control_domain' => true,
			));
			$dom0_metrics = $mgr_vms_metrics->first($dom0->metrics);

			$IPs = array();
			foreach ($host->PIFs as $PIF_ref)
			{
				$PIF = $mgr_pifs->first($PIF_ref);

				$IPs[] = $PIF->IP;
			}

			$host_metrics = $mgr_hosts_metrics->first($host->metrics);
			$memory = array(
				'free'  => $host_metrics->memory_free,
				'total' => $host_metrics->memory_total,
			);

			$hosts[] = array(
				'description' => $host->name_description,
				'id'          => $host->uuid,
				'IPs'         => $IPs,
				'memory'      => $memory,
				'name'        => $host->name_label,
				'start_time'  => $dom0_metrics->start_time['timestamp'],
			);
		}

		$pools = array(
			array(
				'uuid'  => 'unknown',
				'name'  => 'unknown pool',
				'hosts' => $hosts,
			),
		);

		$c->respond($id, $pools);
	}

	/**
	 *
	 */
	function api_host_get($id, array $params, Client $c)
	{
		// Checks parameter.
		if (!isset($params[0]))
		{
			return -32602; // Invalid params.
		}

		$di = $this->_di;

		$mgr_guest_metrics = $di->get('vms_guest_metrics');
		$mgr_hosts         = $di->get('hosts');
		$mgr_hosts_metrics = $di->get('hosts_metrics');
		$mgr_hosts_cpu     = $di->get('hosts_cpus');
		$mgr_pbds          = $di->get('pbds');
		$mgr_pifs          = $di->get('pifs');
		$mgr_pifs_metrics  = $di->get('pifs_metrics');
		$mgr_vms           = $di->get('vms');
		$mgr_vms_metrics   = $di->get('vms_metrics');
		$mgr_srs           = $di->get('srs');

		$host    = $mgr_hosts->first(array(
			'uuid' => $params[0],
		));
		$metrics = $mgr_hosts_metrics->first($host->metrics);

		// $mgr_messages      = $di->get('messages');
		// $mgr_networks      = $di->get('networks');
		// $mgr_vbds          = $di->get('vbds');
		// $mgr_vdis          = $di->get('vdis');
		// $mgr_vifs          = $di->get('vifs');

		$dom0 = $mgr_vms->first(array(
			'resident_on'       => $host->id,
			'is_control_domain' => true,
		));
		$dom0_metrics       = $mgr_vms_metrics->first($dom0->metrics);
		$dom0_guest_metrics = $mgr_guest_metrics->first($dom0->guest_metrics, false);

		$vms = $mgr_vms->get(array(
			'resident_on' => $host->id,
		));

		$CPUs = array();
		foreach ($mgr_hosts_cpu->get(array('host' => $host->id)) as $CPU)
		{
			$CPUs = array(
				'model'      => $CPU->model,
				'model_name' => $CPU->modelname,
				'number'     => $CPU->number,
				'speed'      => $CPU->speed,
				'vendor'     => $CPU->vendor,
			);
		}

		$memory = array(
			'free'   => $metrics->memory_free,
			'total'  => $metrics->memory_total,
			'per_VM' => array(),
		);

		$os_version = $dom0_guest_metrics
			? $dom0->guest_metrics->os_version
			: null;

		$PIFs = array();
		foreach ($mgr_pifs->get(array('host' => $host->id)) as $i => $PIF)
		{
			$PIF_metrics = $mgr_pifs_metrics->first($PIF->metrics);

			$PIFs[] = array(
				'currently_attached' => $PIF->currently_attached,
				'device'             => $PIF_metrics->device_name,
				'duplex'             => $PIF_metrics->duplex,
				'IP'                 => $PIF->IP,
				'MAC'                => $PIF->MAC,
				'name'               => 'PIF #'.$i, // @todo
				'speed'              => $PIF_metrics->speed,
				'uuid'               => $PIF->uuid,
				'vendor'             => $PIF_metrics->vendor_name,
			);
		}

		$SRs = array();
		foreach ($mgr_pbds->get(array('host' => $host->id)) as $PBD)
		{
			$SR = $mgr_srs->first($PBD->SR);
			$SRs[] = array(
				'allocated'   => $SR->virtual_allocation,
				'description' => $SR->name_description,
				'name'        => $SR->name_label,
				'shared'      => $SR->shared,
				'total'       => $SR->physical_size,
				'type'        => $SR->type,
				'used'        => $SR->physical_utilisation,
			);
		}

		$VMs = array();
		foreach ($vms as $VM)
		{
			$VMs[$VM->uuid] = array(
				'is_control_domain' => $VM->is_control_domain,
				'name'              => $VM->name_label,
			);
			$memory['per_VM'][$VM->uuid] = $VM->memory_dynamic_max;
		}

		$host = array(
			'CPUs'                  => $CPUs,
			'control_domain'        => $dom0->uuid,
			'description'           => $host->name_description,
			'enabled'               => $host->enabled,
			'hostname'              => $host->hostname,
			'is_pool_master'        => false, // @todo
			'iscsi_iqn'             => null, //@todo,
			'log_destination'       => 'local', // @todo
			'memory'                => $memory,
			'name'                  => $host->name_label,
			'os_version'            => $os_version,
			'PIFs'                  => $PIFs,
			'SRs'                   => $SRs,
			'start_time'            => $dom0_metrics->start_time['timestamp'],
			'tool_stack_start_time' => $dom0_metrics->start_time['timestamp'], // @todo
			'uuid'                  => $host->uuid,
			'software_version'      => $host->software_version,
			'VMs'                   => $VMs,
		);

		$c->respond($id, $host);
	}

	/**
	 *
	 */
	function api_session_signInWithPassword($id, array $params, Client $c)
	{
		// Checks parameters.
		if (!isset($params[0], $params[1]))
		{
			return -32602; // Invalid params.
		}
		list($name, $password) = $params;

{'fraction_non_alphanumeric': 0.1944627950273257, 'fraction_numerical': 0.0039036694492823256, 'mean_word_length': 2.3464630225080385, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 444, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3263112', 'n_tokens_mistral': 7124, 'n_tokens_neox': 6247, 'n_words': 1120}