<commit_before><?php

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2015 S.D.O.C. LTD. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */
require_once APPLICATION_PATH . '/application/controllers/Action/Api.php';

/**
 * Realtime event action controller class
 *
 * @package     Controllers
 * @subpackage  Action
 * @since       4.0
 */
class RealtimeeventAction extends ApiAction {

	protected $event = null;
	protected $usaget = null;

	/**
	 * method to execute realtime event
	 */
	public function execute() {
		Billrun_Factory::log("Execute realtime event", Zend_Log::INFO);
		$this->event = $this->getRequestData();
		$this->setEventData();
		$data = $this->process();
		return $this->respond($data);
	}

	/**
	 * make simple sanity to the event input
	 */
	protected function preCheck() {
		
	}

	protected function customer() {
		if (!empty($this->event['imsi'])) {
			
		} else if (!empty($this->event['msisdn'])) {
			
		} else {
			// die no customer identifcation
			return FALSE;
		}
		return TRUE;
	}

	protected function rate() {
		$this->event['arate'] = MongoDBRef::create($collection, $id);
	}

	protected function charge() {
		
	}

	protected function saveEvent() {
		
	}
	
	/**
	 * Gets the data sent to the api
	 * @todo get real data from request (now it's only mock-up)
	 */
	protected function getRequestData() {
		$request = $this->getRequest()->getRequest();
		$this->usaget = $request['usaget'];
		$decoder = Billrun_Decoder_Manager::getDecoder(array(
			'usaget' => $this->usaget
		));
		if (!$decoder) {
			Billrun_Factory::log('Cannot get decoder', Zend_Log::ALERT);
			return false;
		}

		if (!empty($request['request'])) {
			$requestBody = $request['request'];
		} else {
			$requestBody = file_get_contents("PHP://input");
		}

		return Billrun_Util::parseDataToBillrunConvention($decoder->decode($requestBody));
	}
	
	/**
	 * Sets the data of $this->event
	 */
	protected function setEventData() {
		$this->event['source'] = 'realtime';
		$this->event['type'] = $this->getEventType();
		$this->event['rand'] = rand(1,1000000);
		$this->event['stamp'] = Billrun_Util::generateArrayStamp($this->event);
		$this->event['record_type'] = $this->getDataRecordType($this->usaget, $this->event);
		if ($this->usaget === 'data') {
			$this->event['sgsn_address'] = $this->getSgsn($this->event);
		}
				
		$this->event['billrun_pretend'] = $this->isPretend($this->event);
		// we are on real time -> the time is now
		$this->event['urt'] = new MongoDate();
	}
	
	protected function getSgsn($event) {
		$sgsn = 0;
		if (isset($event['service']['sgsn_address'])) {
			$sgsn = $event['service']['sgsn_address'];
		} else if(isset ($event['sgsn_address'])) {
			$sgsn = $event['sgsn_address'];
		} else if(isset ($event['sgsnaddress'])) {
			$sgsn = $event['sgsnaddress'];
		}
		return long2ip(hexdec($sgsn));
	}
	
	protected function getDataRecordType($usaget, $data) {
		switch ($usaget){
			case('data'):
				$requestCode = $data['request_type'];
				$requestTypes = Billrun_Factory::config()->getConfigValue('realtimeevent.data.requestType',array());
				foreach ($requestTypes as $requestTypeDesc => $requestTypeCode) {
					if ($requestCode == $requestTypeCode) {
						return strtolower($requestTypeDesc);
					}
				}
				return false;
			case('call'):
				return $data['api_name'];
		}
	}
	
	/**
	 * Gets the event type for rates calculator
	 * 
	 * @return string event type
	 * @todo Get values from config
	 */
	protected function getEventType() {
		//TODO: move to config
		switch ($this->usaget) {
			case ('sms'):
				return 'smsrt';
			case ('data'):
				return 'gy';
			case ('call'):
				return 'callrt'; //TODO: change name of rate calculator
			case ('service'):
				return 'service';
		}<commit_msg>Real-time sms and service - no need to send record_type<commit_after><?php

/**
 * @package         Billing
 * @copyright       Copyright (C) 2012-2015 S.D.O.C. LTD. All rights reserved.
 * @license         GNU Affero General Public License Version 3; see LICENSE.txt
 */
require_once APPLICATION_PATH . '/application/controllers/Action/Api.php';

/**
 * Realtime event action controller class
 *
 * @package     Controllers
 * @subpackage  Action
 * @since       4.0
 */
class RealtimeeventAction extends ApiAction {

	protected $event = null;
	protected $usaget = null;

	/**
	 * method to execute realtime event
	 */
	public function execute() {
		Billrun_Factory::log("Execute realtime event", Zend_Log::INFO);
		$this->event = $this->getRequestData();
		$this->setEventData();
		$data = $this->process();
		return $this->respond($data);
	}

	/**
	 * make simple sanity to the event input
	 */
	protected function preCheck() {
		
	}

	protected function customer() {
		if (!empty($this->event['imsi'])) {
			
		} else if (!empty($this->event['msisdn'])) {
			
		} else {
			// die no customer identifcation
			return FALSE;
		}
		return TRUE;
	}

	protected function rate() {
		$this->event['arate'] = MongoDBRef::create($collection, $id);
	}

	protected function charge() {
		
	}

	protected function saveEvent() {
		
	}
	
	/**
	 * Gets the data sent to the api
	 * @todo get real data from request (now it's only mock-up)
	 */
	protected function getRequestData() {
		$request = $this->getRequest()->getRequest();
		$this->usaget = $request['usaget'];
		$decoder = Billrun_Decoder_Manager::getDecoder(array(
			'usaget' => $this->usaget
		));
		if (!$decoder) {
			Billrun_Factory::log('Cannot get decoder', Zend_Log::ALERT);
			return false;
		}

		if (!empty($request['request'])) {
			$requestBody = $request['request'];
		} else {
			$requestBody = file_get_contents("PHP://input");
		}

		return Billrun_Util::parseDataToBillrunConvention($decoder->decode($requestBody));
	}
	
	/**
	 * Sets the data of $this->event
	 */
	protected function setEventData() {
		$this->event['source'] = 'realtime';
		$this->event['type'] = $this->getEventType();
		$this->event['rand'] = rand(1,1000000);
		$this->event['stamp'] = Billrun_Util::generateArrayStamp($this->event);
		$this->event['record_type'] = $this->getDataRecordType($this->usaget, $this->event);
		if ($this->usaget === 'data') {
			$this->event['sgsn_address'] = $this->getSgsn($this->event);
		}
				
		$this->event['billrun_pretend'] = $this->isPretend($this->event);
		// we are on real time -> the time is now
		$this->event['urt'] = new MongoDate();
	}
	
	protected function getSgsn($event) {
		$sgsn = 0;
		if (isset($event['service']['sgsn_address'])) {
			$sgsn = $event['service']['sgsn_address'];
		} else if(isset ($event['sgsn_address'])) {
			$sgsn = $event['sgsn_address'];
		} else if(isset ($event['sgsnaddress'])) {
			$sgsn = $event['sgsnaddress'];
		}
		return long2ip(hexdec($sgsn));
	}
	
	protected function getDataRecordType($usaget, $data) {
		switch ($usaget){
			case('data'):
				$requestCode = $data['request_type'];
				$requestTypes = Billrun_Factory::config()->getConfigValue('realtimeevent.data.requestType',array());
				foreach ($requestTypes as $requestTypeDesc => $requestTypeCode) {
					if ($requestCode == $requestTypeCode) {
						return strtolower($requestTypeDesc);
					}
				}
				return false;
			case('call'):
				return $data['api_name'];
			case('sms'):
				return 'sms';
			case('service'):
				return 'service';
		}
	}
	
	/**
	 * Gets the event type for rates calculator
	 * 
	 * @return string event type
	 * @todo Get values from config
	 */
	protected function getEventType() {
		//TODO: move to config
		switch ($this->usaget) {
			case ('sms'):
				return 'smsrt';
			case ('data'):
				return 'gy';
			case ('call'):
				return 'callrt'; //TODO: change name of rate calculator
			case ('service'):
				return 'service';
		}
{'fraction_non_alphanumeric': 0.17228946690703065, 'fraction_numerical': 0.005408189544166882, 'mean_word_length': 3.7359756097560974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '246961', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2527, 'n_words': 576}