<commit_before><?php /** @noinspection PhpMissingBreakStatementInspection */

namespace Rancherize\Blueprint\ResourceLimit\EventListener;

use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterServicePreparedEvent;
use Rancherize\Blueprint\Infrastructure\Service\ExtraInformationNotFoundException;
use Rancherize\Blueprint\ResourceLimit\ExtraInformation\ExtraInformation as ResourceLimitExtraInformation;

/**
 * Class ServiceWriteListener
 * @package Rancherize\Blueprint\ResourceLimit\EventListener
 */
class ServiceWriteListener {

	/**
	 * @param ServiceWriterServicePreparedEvent $event
	 */
	public function writeService( ServiceWriterServicePreparedEvent $event ) {
		$dockerData = $event->getDockerContent();
		$rancherData = $event->getRancherContent();

		$service = $event->getService();
		try {
			$extraInformation = $service->getExtraInformation( ResourceLimitExtraInformation::IDENTIFIER );
		} catch ( ExtraInformationNotFoundException $e ) {
			return;
		}

		if ( !$extraInformation instanceof ResourceLimitExtraInformation )
			return;

		$rancherData = $this->addCpuReservation( $rancherData, $extraInformation );
		$dockerData = $this->addCpuLImit( $dockerData, $extraInformation );
		$dockerData = $this->addMemoryLimit( $dockerData, $extraInformation );
		$dockerData = $this->addMemoryReservation( $dockerData, $extraInformation );

		$event->setDockerContent( $dockerData );
		$event->setRancherContent( $rancherData );
	}

	/**
	 * @param $dockerData
	 * @param ResourceLimitExtraInformation $extraInformation
	 * @return mixed
	 */
	private function addCpuLImit( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

		if ( $extraInformation->getCpuPeriod() === null || $extraInformation->getCpuQuota() === null )
			return $dockerData;

		$dockerData['cpu_period'] = $extraInformation->getCpuPeriod();
		$dockerData['cpu_quota'] = $extraInformation->getCpuQuota();

		return $dockerData;
	}

	/**
	 * @param $dockerData
	 * @param ResourceLImitExtraInformation $extraInformation
	 * @return mixed
	 */
	private function addMemoryLimit( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

		if ( $extraInformation->getCpuPeriod() === null || $extraInformation->getCpuQuota() === null )
			return $dockerData;

		$memory = $extraInformation->getMemoryLimit();
		preg_match( '~(\d+)([gGmM]?)~', $memory, $matches );
		$memory = (int)$matches[1];
		$modifier = $matches[2];
		switch ( $modifier ) {
			case 'g':
			case 'G':
				$memory *= 1024;

			case 'm':
			case 'M':
				$memory *= 1024;

			case 'k':
			case 'K':
				$memory *= 1024;

			default:
				break;
		}
		$dockerData['mem_limit'] = $memory;

		return $dockerData;
	}

	/**
	 * @param array $rancherData
	 * @param $extraInformation
	 * @return array
	 */
	private function addCpuReservation( array $rancherData, ResourceLimitExtraInformation $extraInformation ) {

		if ( $extraInformation->getCpuReservation() === null )
			return $rancherData;

		$memory = $extraInformation->getCpuReservation();
		preg_match( '~(\d+)([gGmM]?)~', $memory, $matches );
		$memory = (int)$matches[1];
		$modifier = $matches[2];
		switch ( $modifier ) {
			case 'g':
			case 'G':
				$memory *= 1024;

			case 'm':
			case 'M':
				$memory *= 1024;

			case 'k':
			case 'K':
				$memory *= 1024;

			default:
				break;
		}
		$rancherData['milli_cpu_reservation'] = $memory;

		return $rancherData;
	}

	private function addMemoryReservation( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

		if( $extraInformation->getMemoryReservation() === null)
			return $dockerData;

		$dockerData['mem_reservation'] = $extraInformation->getMemoryReservation();<commit_msg>memory reservation in bytes<commit_after><?php /** @noinspection PhpMissingBreakStatementInspection */

namespace Rancherize\Blueprint\ResourceLimit\EventListener;

use Rancherize\Blueprint\Infrastructure\Service\Events\ServiceWriterServicePreparedEvent;
use Rancherize\Blueprint\Infrastructure\Service\ExtraInformationNotFoundException;
use Rancherize\Blueprint\ResourceLimit\ExtraInformation\ExtraInformation as ResourceLimitExtraInformation;

/**
 * Class ServiceWriteListener
 * @package Rancherize\Blueprint\ResourceLimit\EventListener
 */
class ServiceWriteListener {

	/**
	 * @param ServiceWriterServicePreparedEvent $event
	 */
	public function writeService( ServiceWriterServicePreparedEvent $event ) {
		$dockerData = $event->getDockerContent();
		$rancherData = $event->getRancherContent();

		$service = $event->getService();
		try {
			$extraInformation = $service->getExtraInformation( ResourceLimitExtraInformation::IDENTIFIER );
		} catch ( ExtraInformationNotFoundException $e ) {
			return;
		}

		if ( !$extraInformation instanceof ResourceLimitExtraInformation )
			return;

		$rancherData = $this->addCpuReservation( $rancherData, $extraInformation );
		$dockerData = $this->addCpuLImit( $dockerData, $extraInformation );
		$dockerData = $this->addMemoryLimit( $dockerData, $extraInformation );
		$dockerData = $this->addMemoryReservation( $dockerData, $extraInformation );

		$event->setDockerContent( $dockerData );
		$event->setRancherContent( $rancherData );
	}

	/**
	 * @param $dockerData
	 * @param ResourceLimitExtraInformation $extraInformation
	 * @return mixed
	 */
	private function addCpuLImit( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

		if ( $extraInformation->getCpuPeriod() === null || $extraInformation->getCpuQuota() === null )
			return $dockerData;

		$dockerData['cpu_period'] = $extraInformation->getCpuPeriod();
		$dockerData['cpu_quota'] = $extraInformation->getCpuQuota();

		return $dockerData;
	}

	/**
	 * @param $dockerData
	 * @param ResourceLImitExtraInformation $extraInformation
	 * @return mixed
	 */
	private function addMemoryLimit( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

		if ( $extraInformation->getCpuPeriod() === null || $extraInformation->getCpuQuota() === null )
			return $dockerData;

		$memory = $extraInformation->getMemoryLimit();
		preg_match( '~(\d+)([gGmM]?)~', $memory, $matches );
		$memory = (int)$matches[1];
		$modifier = $matches[2];
		switch ( $modifier ) {
			case 'g':
			case 'G':
				$memory *= 1024;

			case 'm':
			case 'M':
				$memory *= 1024;

			case 'k':
			case 'K':
				$memory *= 1024;

			default:
				break;
		}
		$dockerData['mem_limit'] = $memory;

		return $dockerData;
	}

	/**
	 * @param array $rancherData
	 * @param $extraInformation
	 * @return array
	 */
	private function addCpuReservation( array $rancherData, ResourceLimitExtraInformation $extraInformation ) {

		if ( $extraInformation->getCpuReservation() === null )
			return $rancherData;

		$rancherData['milli_cpu_reservation'] = $extraInformation->getCpuReservation();

		return $rancherData;
	}

	private function addMemoryReservation( $dockerData, ResourceLimitExtraInformation $extraInformation ) {

		if( $extraInformation->getMemoryReservation() === null)
			return $dockerData;

		$memory = $extraInformation->getMemoryReservation();
		preg_match( '~(\d+)([gGmM]?)~', $memory, $matches );
		$memory = (int)$matches[1];
		$modifier = $matches[2];
		switch ( $modifier ) {
			case 'g':
			case 'G':
				$memory *= 1024;

			case 'm':
			case 'M':
				$memory *= 1024;

			case 'k':
			case 'K':
				$memory *= 1024;

			default:
				break;
		}
		$dockerData['mem_reservation'] = $memory;
{'fraction_non_alphanumeric': 0.13807079167792488, 'fraction_numerical': 0.007565522831667117, 'mean_word_length': 5.008928571428571, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '7391754', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2237, 'n_words': 434}