<commit_before><?php

/**
 * ColoCrossing Devices Controller for WHMCS Module.
 * Handles Responding to all Requests Related to Devices
 */
class ColoCrossing_DevicesController extends ColoCrossing_Controller {

	public function index(array $params) {
		$this->filters = array(
			'query' => isset($params['query']) ? $params['query'] : '',
		);

		$this->sort = isset($params['sort']) ? $params['sort'] : 'name';
		$this->order = isset($params['order']) && strtolower($params['order']) == 'desc' ? 'desc' : 'asc';
		$this->page = isset($params['page']) && is_numeric($params['page']) ? intval($params['page']) : 1;

		$this->devices = $this->api->devices->findAll(array(
			'filters' => $this->filters,
			'sort' => ($this->order == 'asc' ? '+' : '-') . $this->sort,
			'page_number' => $this->page,
			'page_size' => 30,
			'format' => 'paged'
		));
	}

	public function view(array $params) {
		$this->device = $this->api->devices->find($params['id']);

		if(empty($this->device)) {
			$this->setFlashMessage('The device was not found.', 'error');
			$this->redirectToModule($params['modulelink'], array(
				'controller' => 'devices',
				'action' => 'index'
			));
		}

		$this->assets = $this->device->getAssets();
		$this->type = $this->device->getType();
		$this->subnets = $this->type->isNetworkEndpoint() || $this->type->isVirtual() ? $this->device->getSubnets() : array();

		if($this->type->isRacked()) {
			$rack_name = $this->device->getRackName();
			$this->rack = isset($rack_name) ? $rack_name . ' (U Slot: ' . $this->device->getUSpace() . ')' : 'Unassigned';
		} else {
			$this->rack = 'Self';
		}

		if($this->type->isNetworkEndpoint()) {
			$this->switches = $this->device->getSwitches();
			$this->bandwidth_graphs = array();

			foreach ($this->switches as $i => $switch) {
				foreach ($switch->getPorts() as $j => $port) {
					if($port->isBandwidthGraphAvailable()) {
						$this->bandwidth_graphs[] = array(
							'device_id' => $this->device->getId(),
							'switch_id' => $switch->getId(),
							'port_id' => $port->getId()
						);
					}
				}
			}
		}

		if($this->type->isPowerEndpoint()) {
			$this->power_distribution_units = $this->device->getPowerDistributionUnits();
		}
	}


	public function bandwidthGraph(array $params) {
		$start = is_numeric($params['start']) ? intval($params['start']) : strtotime($params['start']);
		$end = is_numeric($params['end']) ? intval($params['end']) : strtotime($params['end']);

		$graph = $this->api->devices->switches->getBandwidthGraph($params['switch_id'], $params['port_id'], $params['id'], $start, $end);

		if (empty($graph))
		{
			http_response_code(404);
			return false;
		}

		ob_clean();
    	ob_start();

    	header('Content-Type: image/png');
    	http_response_code(imagepng($graph) ? 200 : 500);
    	imagedestroy($graph);

    	ob_end_flush();
    	exit;
	}


	public function updatePowerPorts(array $params) {
		$status = $params['status'];
		$status_description = $this->getPortStatusDescription($status);
		$comment = empty($params['comment']) ? null : $params['comment'];

		$success = true;

		foreach ($params['power_distribution_units'] as $pdu_id => $ports) {
			foreach ($ports as $port_id => $device_id) {
				$result = $this->api->devices->pdus->setPortStatus($pdu_id, $port_id, $device_id, $status, $comment);

				if($result) {
					ColoCrossing_Model_Event::log('Power port was ' . $status_description . '.');
				} else {
					$success = false;
				}
			}
		}

		if($success) {
			$this->setFlashMessage('The power ports have successfully been ' . $status_description . '.', 'success');
		} else {
			$this->setFlashMessage('An error occurred while controlling the power ports.', 'error');
		}

		$this->redirectToModule($params['modulelink'], array(
			'controller' => 'devices',
			'action' => 'view',
			'id' => isset($params['origin_device_id']) ? $params['origin_device_id'] : $params['device_id']
		));
	}

	public function updateNetworkPorts(array $params) {
		$status = $params['status'];
		$status_description = $this->getPortStatusDescription($status);
		$comment = empty($params['comment']) ? null : $params['comment'];

		$success = true;

		foreach ($params['switches'] as $pdu_id => $ports) {
			foreach ($ports as $port_id => $device_id) {
				$result = $this->api->devices->switches->setPortStatus($pdu_id, $port_id, $device_id, $status, $comment);

				if($result) {
					ColoCrossing_Model_Event::log('Network port was ' . $status_description . '.');
				} else {
					$success = false;
				}
			}
		}

		if($success) {
			$this->setFlashMessage('The network ports have successfully been ' . $status_description . '.', 'success');
		} else {
			$this->setFlashMessage('An error occurred while controlling the network ports.', 'error');
		}

		$this->redirectToModule($params['modulelink'], array(
			'controller' => 'devices',
			'action' => 'view',
			'id' => isset($params['origin_device_id']) ? $params['origin_device_id'] : $params['device_id']
		));
	}

	private function getPortStatusDescription($status) {
		switch ($status) {
			case 'on':
				return 'turned on';
			case 'off':
				return 'turned off';
			case 'restart':
				return 'restarted';
		}

		return 'controlled';
	}

}
<commit_msg>Refactor Switch/PDU Port Control to MAke More Descriptive Event Descriptions<commit_after><?php

/**
 * ColoCrossing Devices Controller for WHMCS Module.
 * Handles Responding to all Requests Related to Devices
 */
class ColoCrossing_DevicesController extends ColoCrossing_Controller {

	public function index(array $params) {
		$this->filters = array(
			'query' => isset($params['query']) ? $params['query'] : '',
		);

		$this->sort = isset($params['sort']) ? $params['sort'] : 'name';
		$this->order = isset($params['order']) && strtolower($params['order']) == 'desc' ? 'desc' : 'asc';
		$this->page = isset($params['page']) && is_numeric($params['page']) ? intval($params['page']) : 1;

		$this->devices = $this->api->devices->findAll(array(
			'filters' => $this->filters,
			'sort' => ($this->order == 'asc' ? '+' : '-') . $this->sort,
			'page_number' => $this->page,
			'page_size' => 30,
			'format' => 'paged'
		));
	}

	public function view(array $params) {
		$this->device = $this->api->devices->find($params['id']);

		if(empty($this->device)) {
			$this->setFlashMessage('The device was not found.', 'error');
			$this->redirectToModule($params['modulelink'], array(
				'controller' => 'devices',
				'action' => 'index'
			));
		}

		$this->assets = $this->device->getAssets();
		$this->type = $this->device->getType();
		$this->subnets = $this->type->isNetworkEndpoint() || $this->type->isVirtual() ? $this->device->getSubnets() : array();

		if($this->type->isRacked()) {
			$rack_name = $this->device->getRackName();
			$this->rack = isset($rack_name) ? $rack_name . ' (U Slot: ' . $this->device->getUSpace() . ')' : 'Unassigned';
		} else {
			$this->rack = 'Self';
		}

		if($this->type->isNetworkEndpoint()) {
			$this->switches = $this->device->getSwitches();
			$this->bandwidth_graphs = array();

			foreach ($this->switches as $i => $switch) {
				foreach ($switch->getPorts() as $j => $port) {
					if($port->isBandwidthGraphAvailable()) {
						$this->bandwidth_graphs[] = array(
							'device_id' => $this->device->getId(),
							'switch_id' => $switch->getId(),
							'port_id' => $port->getId()
						);
					}
				}
			}
		}

		if($this->type->isPowerEndpoint()) {
			$this->power_distribution_units = $this->device->getPowerDistributionUnits();
		}
	}


	public function bandwidthGraph(array $params) {
		$start = is_numeric($params['start']) ? intval($params['start']) : strtotime($params['start']);
		$end = is_numeric($params['end']) ? intval($params['end']) : strtotime($params['end']);

		$graph = $this->api->devices->switches->getBandwidthGraph($params['switch_id'], $params['port_id'], $params['id'], $start, $end);

		if (empty($graph))
		{
			http_response_code(404);
			return false;
		}

		ob_clean();
    	ob_start();

    	header('Content-Type: image/png');
    	http_response_code(imagepng($graph) ? 200 : 500);
    	imagedestroy($graph);

    	ob_end_flush();
    	exit;
	}


	public function updatePowerPorts(array $params) {
		$status = $params['status'];
		$status_description = $this->getPortStatusDescription($status);
		$comment = empty($params['comment']) ? null : $params['comment'];

		$success = true;

		foreach ($params['power_distribution_units'] as $pdu_id => $ports) {
			foreach ($ports as $port_id => $device_id) {
				$device = $this->api->devices->find($device_id);

				if(empty($device) || !$device->getType()->isPowerEndpoint()) {
					$success = false;
					continue;
				}

				$pdu = $device->getPowerDistributionUnit($pdu_id);

				if(empty($pdu)) {
					$success = false;
					continue;
				}

				$port = $pdu->getPort($port_id);

				if(empty($port) || !$port->isControllable()) {
					$success = false;
					continue;
				}

				$result = $this->api->devices->pdus->setPortStatus($pdu, $port, $device, $status, $comment);

				if($result) {
					$description = 'Power port ' . $port_id . ' of ' . $pdu->getName() . ' assigned to ' . $device->getName() . ' was '  . $status_description . '.';
					ColoCrossing_Model_Event::log($description);
				} else {
					$success = false;
				}
			}
		}

		if($success) {
			$this->setFlashMessage('The power ports have successfully been ' . $status_description . '.', 'success');
		} else {
			$this->setFlashMessage('An error occurred while controlling the power ports.', 'error');
		}

		$this->redirectToModule($params['modulelink'], array(
			'controller' => 'devices',
			'action' => 'view',
			'id' => isset($params['origin_device_id']) ? $params['origin_device_id'] : $params['device_id']
		));
	}

	public function updateNetworkPorts(array $params) {
		$status = $params['status'];
		$status_description = $this->getPortStatusDescription($status);
		$comment = empty($params['comment']) ? null : $params['comment'];

		$success = true;

		foreach ($params['switches'] as $switch_id => $ports) {
			foreach ($ports as $port_id => $device_id) {
				$device = $this->api->devices->find($device_id);

				if(empty($device) || !$device->getType()->isNetworkEndpoint()) {
					$success = false;
					continue;
				}

				$switch = $device->getSwitch($switch_id);

				if(empty($switch)) {
					$success = false;
					continue;
				}

				$port = $switch->getPort($port_id);

				if(empty($port) || !$port->isControllable()) {
					$success = false;
					continue;
				}

				$result = $this->api->devices->switches->setPortStatus($switch, $port, $device, $status, $comment);

				if($result) {
					$description = 'Network port ' . $port_id . ' of ' . $switch->getName() . ' assigned to ' . $device->getName() . ' was '  . $status_description . '.';
					ColoCrossing_Model_Event::log($description);
				} else {
					$success = false;
				}
			}
		}

		if($success) {
			$this->setFlashMessage('The network ports have successfully been ' . $status_description . '.', 'success');
		} else {
			$this->setFlashMessage('An error occurred while controlling the network ports.', 'error');
		}

		$this->redirectToModule($params['modulelink'], array(
			'controller' => 'devices',
			'action' => 'view',
			'id' => isset($params['origin_device_id']) ? $params['origin_device_id'] : $params['device_id']
		));
	}

	private function getPortStatusDescription($status) {
		switch ($status) {
			case 'on':
				return 'turned on';
			case 'off':
				return 'turned off';
			case 'restart':
				return 'restarted';
		}

		return 'controlled';
	}

}

{'fraction_non_alphanumeric': 0.20403761539125184, 'fraction_numerical': 0.0020705719955137606, 'mean_word_length': 3.9687098156879554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 227, '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': '136513', 'n_tokens_mistral': 4484, 'n_tokens_neox': 3819, 'n_words': 691}