public function __construct(array $params = array()) { $params['collection'] = Billrun_Factory::db()->queue; parent::__construct($params); $this->search_key = "stamp"; $this->lines_coll = Billrun_Factory::db()->queueCollection(); }
/** * method to convert plans names into their refs * triggered before save the rate entity for edit * * @param Mongodloid collection $collection * @param array $data * * @return void * @todo move to model */ public function update($data) { if (isset($data['rates'])) { $plansColl = Billrun_Factory::db()->plansCollection(); $currentDate = new MongoDate(); $rates = $data['rates']; //convert plans foreach ($rates as &$rate) { if (isset($rate['plans'])) { $sourcePlans = (array) $rate['plans']; // this is array of strings (retreive from client) $newRefPlans = array(); // this will be the new array of DBRefs unset($rate['plans']); foreach ($sourcePlans as &$plan) { $planEntity = $plansColl->query('name', $plan)->lessEq('from', $currentDate)->greaterEq('to', $currentDate)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref'))->current(); $newRefPlans[] = $planEntity->createRef($plansColl); } $rate['plans'] = $newRefPlans; } } $data['rates'] = $rates; } return parent::update($data); }
public function __construct() { // load the config data from db $this->collection = Billrun_Factory::db()->configCollection(); $this->options = array('receive', 'process', 'calculate'); $this->loadConfig(); }
public function calc() { Billrun_Factory::log()->log("Execute reset", Zend_Log::INFO); $rebalance_queue = Billrun_Factory::db()->rebalance_queueCollection(); $limit = Billrun_Config::getInstance()->getConfigValue('resetlines.limit', 10); $offset = Billrun_Config::getInstance()->getConfigValue('resetlines.offset', '1 hour'); $query = array('creation_date' => array('$lt' => new MongoDate(strtotime($offset . ' ago')))); $sort = array('creation_date' => 1); $results = $rebalance_queue->find($query)->sort($sort)->limit($limit); $billruns = array(); $all_sids = array(); foreach ($results as $result) { $billruns[$result['billrun_key']][] = $result['sid']; $all_sids[] = $result['sid']; } foreach ($billruns as $billrun_key => $sids) { $model = new ResetLinesModel($sids, $billrun_key); try { $ret = $model->reset(); if (isset($ret['err']) && !is_null($ret['err'])) { return FALSE; } $rebalance_queue->remove(array('sid' => array('$in' => $sids))); } catch (Exception $exc) { Billrun_Factory::log()->log('Error resetting sids ' . implode(',', $sids) . ' of billrun ' . $billrun_key . '. Error was ' . $exc->getTraceAsString(), Zend_Log::ALERT); return $this->setError($exc->getTraceAsString(), array('sids' => $sids, 'billrun_key' => $billrun_key)); } } Billrun_Factory::log()->log("Success resetting sids " . implode(',', $all_sids), Zend_Log::INFO); return true; }
/** * Process a given file and create a temporary response file to it. * @param type $filePath the location of the file that need to be proceesed * @param type $logLine the log line that associated with the file to process. * @return boolean|string return the temporary file path if the file should be responded to. * or false if the file wasn't processed into the DB yet. */ protected function processFileForResponse($filePath, $logLine) { $logLine = $logLine->getRawData(); $this->linesCount = $this->linesErrors = $this->totalChargeAmount = 0; $linesCollection = Billrun_Factory::db()->linesCollection(); $dbLines = $linesCollection->query()->equals('file', $logLine['file']); //run only after the lines were processed by the billrun. if ($dbLines->count() == 0 || $linesCollection->query()->equals('file', $logLine['file'])->exists('billrun')->count() == 0) { return false; } //save file to a temporary location $responsePath = $this->workspace . rand(); $srcFile = fopen($filePath, "r+"); $file = fopen($responsePath, "w"); $lines = ""; foreach ($dbLines as $dbLine) { //alter data line $line = $this->updateLine($dbLine->getRawData(), $logLine); if ($line) { $this->linesCount++; $this->totalChargeAmount += floatval($dbLine->get('call_charge')); $lines .= $line . "\n"; } } //alter lines fputs($file, $this->updateHeader(fgets($srcFile), $logLine) . "\n"); fputs($file, $lines); //alter trailer fputs($file, $this->updateTrailer($logLine) . "\n"); fclose($file); return $responsePath; }
public function execute() { Billrun_Factory::log()->log("Execute reset", Zend_Log::INFO); $request = $this->getRequest()->getRequest(); // supports GET / POST requests if (empty($request['sid'])) { return $this->setError('Please supply at least one sid', $request); } // remove the aids from current balance cache - on next current balance it will be recalculated and avoid to take it from cache if (isset($request['aid'])) { $this->cleanAccountCache($request['aid']); } $billrun_key = Billrun_Util::getBillrunKey(time()); // Warning: will convert half numeric strings / floats to integers $sids = array_unique(array_diff(Billrun_Util::verify_array($request['sid'], 'int'), array(0))); if ($sids) { try { $rebalance_queue = Billrun_Factory::db()->rebalance_queueCollection(); foreach ($sids as $sid) { $rebalance_queue->insert(array('sid' => $sid, 'billrun_key' => $billrun_key, 'creation_date' => new MongoDate())); } } catch (Exception $exc) { Billrun_Util::logFailedResetLines($sids, $billrun_key); return FALSE; } } else { return $this->setError('Illegal sid', $request); } $this->getController()->setOutput(array(array('status' => 1, 'desc' => 'success', 'input' => $request))); return TRUE; }
public function __construct() { $this->record_types = Billrun_Factory::config()->getConfigValue('016_one_way.identifications.record_types', array('30')); $this->filename = date('Ymd', time()) . '.TXT'; $this->output_path = Billrun_Factory::config()->getConfigValue('016_one_way.export.path', '/var/www/billrun/workspace/016_one_way/Treated/') . DIRECTORY_SEPARATOR . $this->filename; $this->access_price = round(Billrun_Factory::config()->getConfigValue('016_one_way.access_price', 1.0), 2); $this->lines_coll = Billrun_Factory::db()->linesCollection(); }
/** * Execute write down the calculation output */ public function write() { Billrun_Factory::dispatcher()->trigger('beforeCalculatorWriteData', array('data' => $this->data)); $lines = Billrun_Factory::db()->linesCollection(); foreach ($this->data as $item) { $item->save($lines); } Billrun_Factory::dispatcher()->trigger('afterCalculatorWriteData', array('data' => $this->data)); }
protected function processFileForResponse($filePath, $logLine) { $tmpLogLine = $logLine->getRawData(); $unprocessDBLines = Billrun_Factory::db()->linesCollection()->query()->notExists('billrun')->equals('file', $tmpLogLine['file']); //run only if theres promlematic lines in the file. if ($unprocessDBLines->count() == 0) { return false; } return parent::processFileForResponse($filePath, $logLine); }
/** * load the ggsn rates to be used later. */ protected function loadRates() { $rates_coll = Billrun_Factory::db()->ratesCollection(); $rates = $rates_coll->query($this->rateKeyMapping)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); $this->rates = array(); foreach ($rates as $value) { $value->collection($rates_coll); $this->rates[] = $value; } }
public function __construct($options = array()) { $this->alertServer = isset($options['alertHost']) ? $options['alertHost'] : Billrun_Factory::config()->getConfigValue('fraudAlerts.alert.host', '127.0.0.1'); $this->alertPath = isset($options['alertPath']) ? $options['alertPath'] : Billrun_Factory::config()->getConfigValue('fraudAlerts.alert.path', '/'); $this->alertTypes = isset($options['alertTypes']) ? $options['alertTypes'] : Billrun_Factory::config()->getConfigValue('fraudAlerts.alert.types', array('nrtrde', 'ggsn', 'deposit', 'ilds', 'nsn')); $this->isDryRun = isset($options['dryRun']) ? $options['dryRun'] : Billrun_Factory::config()->getConfigValue('fraudAlerts.alert.dry_run', false); $this->startTime = time(); $this->eventsCol = Billrun_Factory::db()->eventsCollection(); $this->linesCol = Billrun_Factory::db()->linesCollection(); }
protected function insertToQueue($entity) { $queue = Billrun_Factory::db()->queueCollection(); if (!is_object($queue)) { Billrun_Factory::log()->log('Queue collection is not defined', Zend_Log::ALERT); return false; } else { return $queue->insert(array('stamp' => $entity['stamp'], 'type' => $entity['type'], 'urt' => $entity['urt'], 'calc_name' => false, 'calc_time' => false), array('w' => 1)); } }
public function __construct(array $params = array()) { if (isset($params['collection'])) { unset($params['collection']); } parent::__construct($params); $this->collection = Billrun_Factory::db(Billrun_Factory::config()->getConfigValue('fraud.db'))->eventsCollection(); $this->collection_name = 'events'; $this->search_key = "stamp"; }
/** * method to receive the balances lines that over requested date usage * * @return Mongodloid_Cursor Mongo cursor for iteration */ public function getBalancesVolume($plan, $data_usage, $from_account_id, $to_account_id, $billrun) { $params = array('name' => $plan, 'time' => Billrun_Util::getStartTime($billrun)); $plan_id = Billrun_Factory::plan($params); $id = $plan_id->get('_id')->getMongoID(); $data_usage_bytes = Billrun_Util::megabytesToBytesFormat((int) $data_usage); $query = array('aid' => array('$gte' => (int) $from_account_id, '$lte' => (int) $to_account_id), 'billrun_month' => $billrun, 'balance.totals.data.usagev' => array('$gt' => (double) $data_usage_bytes), 'current_plan' => Billrun_Factory::db()->plansCollection()->createRef($id)); // print_R($query);die; return $this->collection->query($query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref'))->hint(array('aid' => 1, 'billrun_month' => 1))->limit($this->size); }
/** * Caches the rates in the memory for fast computations */ protected function loadRates() { $rates_coll = Billrun_Factory::db()->ratesCollection(); $query = array('rates.service' => array('$exists' => 1)); $rates = $rates_coll->query($query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); $this->rates = array(); foreach ($rates as $rate) { $rate->collection($rates_coll); $this->rates[] = $rate; } }
protected function getProcessedFilesForType($type) { $files = array(); $log = Billrun_Factory::db()->logCollection(); $logLines = $log->query()->equals('type', $type)->exists('process_time')->notExists('response_time'); foreach ($logLines as $logEntry) { $logEntry->collection($log); $files[$logEntry->get('file')] = $logEntry; } return $files; }
/** * Caches the rates in the memory for fast computations */ protected function loadRates() { $rates_coll = Billrun_Factory::db()->ratesCollection(); $query = array('key' => array('$in' => array('CREDIT_VATABLE', 'CREDIT_VAT_FREE'))); $rates = $rates_coll->query($query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); $this->rates = array(); foreach ($rates as $rate) { $rate->collection($rates_coll); $this->rates[$rate['key']] = $rate; } }
/** * method to markdown all the lines that triggered the event * * @param array $items the lines * @param string $pluginName the plugin name which triggered the event * * @return array affected lines */ public function handlerMarkDown(&$items, $pluginName) { if ($pluginName != $this->getName() || !$items) { return; } $ret = array(); $lines = Billrun_Factory::db()->linesCollection(); foreach ($items as &$item) { $ret[] = $lines->update(array('stamp' => array('$in' => $item['lines_stamps'])), array('$set' => array('event_stamp' => $item['event_stamp'])), array('multiple' => 1)); } return $ret; }
protected static function initCollection($collection_name) { if (isset(self::$keys[$collection_name])) { $coll = Billrun_Factory::db()->{$collection_name . "Collection"}(); $resource = $coll->query()->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); foreach ($resource as $entity) { $entity->collection($coll); self::$entities[$collection_name]['by_id'][strval($entity->getId())] = $entity; self::$entities[$collection_name]['by_key'][$entity[self::$keys[$collection_name]]][] = $entity; } } }
protected function loadSidLines($sid, $limits, $plan, $groupSelected, $dayKey) { $line_year = date('Y', strtotime($this->line_time)); $from = date('YmdHis', strtotime(str_replace('%Y', $line_year, $limits['period']['from']) . ' 00:00:00')); $to = date('YmdHis', strtotime(str_replace('%Y', $line_year, $limits['period']['to']) . ' 23:59:59')); $match = array('$match' => array('sid' => $sid, 'type' => 'tap3', 'plan' => $plan->getData()->get('name'), 'basicCallInformation.CallEventStartTimeStamp.localTimeStamp' => array('$gte' => $from, '$lte' => $to), 'arategroup' => $groupSelected, 'in_group' => array('$gt' => 0), 'billrun' => array('$exists' => true))); $group = array('$group' => array('_id' => array('day_key' => array('$substr' => array('$basicCallInformation.CallEventStartTimeStamp.localTimeStamp', 0, 8))))); $match2 = array('$match' => array('_id.day_key' => array('$lte' => $dayKey))); $results = Billrun_Factory::db()->linesCollection()->aggregate($match, $group, $match2); return array_map(function ($res) { return $res['_id']['day_key']; }, $results); }
public function init() { $forceUser = Billrun_Factory::config()->getConfigValue('cliForceUser', ''); $systemExecuterUser = trim(shell_exec('whoami')); if (!empty($forceUser) && $systemExecuterUser != $forceUser && $systemExecuterUser != 'apache') { Billrun_Log::getInstance()->addWriter(new Zend_Log_Writer_Stream('php://stdout')); $this->addOutput('Cannot run cli command with the system user ' . $systemExecuterUser . '. Please use ' . $forceUser . ' for CLI operations'); exit; } $this->setActions(); $this->setOptions(); // this will verify db config will load into main config Billrun_Factory::db(); }
/** * for every rate who has ref to original plan add ref to new plan * @param type $source_id * @param type $new_id */ public function duplicate_rates($source_id, $new_id) { $rates_col = Billrun_Factory::db()->ratesCollection(); $source_ref = MongoDBRef::create("plans", $source_id); $dest_ref = MongoDBRef::create("plans", $new_id); $usage_types = Billrun_Factory::config()->getConfigValue('admin_panel.line_usages'); foreach ($usage_types as $type => $string) { $attribute = "rates." . $type . ".plans"; $query = array($attribute => $source_ref); $update = array('$push' => array($attribute => $dest_ref)); $params = array("multiple" => 1); $rates_col->update($query, $update, $params); } }
public function loadDbConfig() { try { $configColl = Billrun_Factory::db()->configCollection(); if ($configColl) { $dbConfig = $configColl->query()->cursor()->sort(array('_id' => -1))->limit(1)->current()->getRawData(); unset($dbConfig['_id']); $iniConfig = $this->config->toArray(); $this->config = new Yaf_Config_Simple(array_merge($iniConfig, $dbConfig)); } } catch (Exception $e) { Billrun_Factory::log('Cannot load database config', Zend_Log::CRIT); return false; } }
/** * method to collect data which need to be handle by event */ public function handlerCollect($options) { if ($options['type'] != 'roaming') { return FALSE; } Billrun_Factory::log()->log("Collect deposits fraud (deposits plugin)", Zend_Log::INFO); $eventsCol = Billrun_Factory::db()->eventsCollection(); $timeWindow = strtotime("-" . Billrun_Factory::config()->getConfigValue('deposit.hourly.timespan', '4 hours')); $where = array('$match' => array('event_stamp' => array('$exists' => false), 'event_type' => array('$ne' => 'DEPOSITS'), 'nofity_time' => array('$gte' => new MongoDate($timeWindow)))); $group = array('$group' => array("_id" => '$imsi', 'deposits' => array('$sum' => 1), 'events_ids' => array('$addToSet' => '$_id'), 'imsi' => array('$first' => '$imsi'), 'msisdn' => array('$first' => '$msisdn'), 'events_stamps' => array('$addToSet' => '$stamp'))); $project = array('$project' => array("_id" => 1, 'deposits' => 1, 'events_ids' => 1, 'imsi' => 1, 'msisdn' => 1, 'events_stamps' => 1)); $having = array('$match' => array('deposits' => array('$gte' => floatval(Billrun_Factory::config()->getConfigValue('deposit.hourly.thresholds.deposits', 3))))); $items = $eventsCol->aggregate($where, $group, $project, $having); Billrun_Factory::log()->log("Deposits fraud found " . count($items) . " items", Zend_Log::INFO); return $items; }
/** * method to collect data which need to be handle by event */ public function handlerCollect($options) { if ($this->getName() != $options['type']) { return FALSE; } Billrun_Factory::log()->log("ILDS fraud collect handler triggered", Zend_Log::DEBUG); $lines = Billrun_Factory::db()->linesCollection(); $charge_time = Billrun_Util::getLastChargeTime(true, Billrun_Factory::config()->getConfigValue('ilds.billrun.charging_day', 20)); $base_match = array('$match' => array('source' => 'ilds')); $where = array('$match' => array('event_stamp' => array('$exists' => false), 'deposit_stamp' => array('$exists' => false), 'urt' => array('$gte' => new MongoDate($charge_time)), 'aprice' => array('$exists' => true), 'billrun' => array('$exists' => false))); $group = array('$group' => array("_id" => '$caller_phone_no', 'msisdn' => array('$first' => '$caller_phone_no'), "total" => array('$sum' => '$aprice'), 'lines_stamps' => array('$addToSet' => '$stamp'))); $project = array('$project' => array('caller_phone_no' => '$_id', '_id' => 0, 'msidsn' => 1, 'total' => 1, 'lines_stamps' => 1)); $having = array('$match' => array('total' => array('$gte' => floatval(Billrun_Factory::config()->getConfigValue('ilds.threshold', 100))))); $ret = $lines->aggregate($base_match, $where, $group, $project, $having); Billrun_Factory::log()->log("ILDS fraud plugin found " . count($ret) . " items", Zend_Log::DEBUG); return $ret; }
/** * method to log the processing * * @todo refactoring this method */ protected function logDB($fileData) { $log = Billrun_Factory::db()->logCollection(); Billrun_Factory::dispatcher()->trigger('beforeLogReceiveFile', array(&$fileData, $this)); $query = array('stamp' => $fileData['stamp'], 'received_time' => array('$exists' => false)); $addData = array('received_hostname' => Billrun_Util::getHostName(), 'received_time' => date(self::base_dateformat)); $update = array('$set' => array_merge($fileData, $addData)); if (empty($query['stamp'])) { Billrun_Factory::log()->log("Billrun_Receiver::logDB - got file with empty stamp : {$fileData['stamp']}", Zend_Log::NOTICE); return FALSE; } $result = $log->update($query, $update, array('w' => 1)); if ($result['ok'] != 1 || $result['n'] != 1) { Billrun_Factory::log()->log("Billrun_Receiver::logDB - Failed when trying to update a file log record " . $fileData['file_name'] . " with stamp of : {$fileData['stamp']}", Zend_Log::NOTICE); } return $result['n'] == 1 && $result['ok'] == 1; }
public function load() { $this->date = date(Billrun_Base::base_dateformat, $this->now); $subscriber = Billrun_Factory::subscriber(); $this->account_data = array(); $res = $subscriber->getList(0, 1, $this->date, $this->aid); if (!empty($res)) { $this->account_data = current($res); } $billrun_params = array('aid' => $this->aid, 'billrun_key' => $this->stamp, 'autoload' => false); $billrun = Billrun_Factory::billrun($billrun_params); $manual_lines = array(); $deactivated_subscribers = array(); foreach ($this->account_data as $subscriber) { if (!Billrun_Factory::db()->rebalance_queueCollection()->query(array('sid' => $subscriber->sid), array('sid' => 1))->cursor()->current()->isEmpty()) { $subscriber_status = "REBALANCE"; $billrun->addSubscriber($subscriber, $subscriber_status); continue; } if ($billrun->subscriberExists($subscriber->sid)) { Billrun_Factory::log()->log("Billrun " . $this->stamp . " already exists for subscriber " . $subscriber->sid, Zend_Log::ALERT); continue; } $next_plan_name = $subscriber->getNextPlanName(); if (is_null($next_plan_name) || $next_plan_name == "NULL") { $subscriber_status = "closed"; $current_plan_name = $subscriber->getCurrentPlanName(); if (is_null($current_plan_name) || $current_plan_name == "NULL") { $deactivated_subscribers[] = array("sid" => $subscriber->sid); } } else { $subscriber_status = "open"; $flat_entry = $subscriber->getFlatEntry($this->stamp, true); $manual_lines = array_merge($manual_lines, array($flat_entry['stamp'] => $flat_entry)); } $manual_lines = array_merge($manual_lines, $subscriber->getCredits($this->stamp, true)); $billrun->addSubscriber($subscriber, $subscriber_status); } // print_R($manual_lines);die; $this->lines = $billrun->addLines($manual_lines, $deactivated_subscribers); $billrun->filter_disconected_subscribers($deactivated_subscribers); $this->data = $billrun->getRawData(); }
/** * make the calculation */ public function updateRow($row) { Billrun_Factory::dispatcher()->trigger('beforeCalculatorUpdateRow', array($row, $this)); $pricingData = array(); $row->collection(Billrun_Factory::db()->linesCollection()); $zoneKey = $this->isLineIncoming($row) ? 'incoming' : $this->loadDBRef($row->get(Billrun_Calculator_Wholesale_Nsn::MAIN_DB_FIELD, true))['key']; if (isset($row['usagev']) && $zoneKey) { $rates = $this->getCarrierRateForZoneAndType($this->loadDBRef($row->get($this->isLineIncoming($row) ? 'wsc_in' : 'wsc', true)), $zoneKey, $row['usaget'], $this->isPeak($row) ? 'peak' : 'off_peak'); if ($rates) { $pricingData = $this->getLinePricingData($row['usagev'], $rates); //todo add peak/off peak to the data. $row->setRawData(array_merge($row->getRawData(), $pricingData)); } else { Billrun_Factory::log()->log(" Failed finding rate for row : " . print_r($row['stamp'], 1), Zend_Log::DEBUG); } } else { Billrun_Factory::log()->log($this->count++ . " no usagev or zone : {$row['usagev']} && {$zoneKey} for line with stamp: " . $row['stamp'], Zend_Log::NOTICE); return false; } Billrun_Factory::dispatcher()->trigger('afterCalculatorUpdateRow', array($row, $this)); return $row; }
public function updateRow($row) { Billrun_Factory::dispatcher()->trigger('beforeCalculatorUpdateRow', array($row, $this)); //@TODO change this be be configurable. $pricingData = array(); $row->collection(Billrun_Factory::db()->linesCollection()); $zoneKey = $this->isLineIncoming($row) ? 'incoming' : $this->loadDBRef($row->get(Billrun_Calculator_Wholesale_Nsn::MAIN_DB_FIELD, true))['key']; if (isset($row['usagev']) && $zoneKey) { $carir = $this->loadDBRef($row->get(in_array($row->get('wsc', true), $this->nrCarriers) ? 'wsc' : 'wsc_in', true)); $rates = $this->getCarrierRateForZoneAndType($carir, $zoneKey, $row['usaget']); if (!$rates) { Billrun_Factory::log()->log(" Failed finding rate for row : " . print_r($row['stamp'], 1), Zend_Log::DEBUG); return false; } $pricingData = $this->getLinePricingData($row['usagev'], $rates); $row->setRawData(array_merge($row->getRawData(), $pricingData)); } else { Billrun_Factory::log()->log(" No usagev or zone : {$row['usagev']} && {$zoneKey} for line with stamp: " . $row['stamp'], Zend_Log::NOTICE); return false; } Billrun_Factory::dispatcher()->trigger('afterCalculatorUpdateRow', array($row, $this)); return $row; }
/** * constructor * * @param array $params of parameters to preset the object */ public function __construct(array $params = array()) { if (isset($params['collection'])) { if (isset($params['db'])) { $this->collection = Billrun_Factory::db(array('name' => $params['db']))->balancesCollection(); } else { $this->collection = call_user_func(array(Billrun_Factory::db(), $params['collection'] . 'Collection')); // $this->collection->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref')); } $this->collection_name = $params['collection']; } if (isset($params['page'])) { $this->page = $params['page']; } if (isset($params['size'])) { $this->size = $params['size']; } if (isset($params['sort'])) { $this->sort = $params['sort']; } if (isset($params['extra_columns'])) { $this->extra_columns = $params['extra_columns']; } }