Esempio n. 1
0
 /**
  * method that outputs account, subscribers and usage of requested accounts and requested date usage
  * it's called automatically by the api main controller
  */
 public function execute()
 {
     Billrun_Factory::log()->log("Execute data triggers", Zend_Log::INFO);
     $request = $this->getRequest()->getRequest();
     // supports GET / POST requests
     $params = array('plan', 'data_usage', 'from_account_id', 'to_account_id', 'billrun');
     foreach ($params as $param) {
         if (!isset($request[$param])) {
             $msg = 'Missing required parameter: ' . $param;
             Billrun_Factory::log()->log($msg, Zend_Log::ERR);
             $this->getController()->setOutput(array(array('status' => 0, 'desc' => 'failed', 'output' => $msg)));
             return;
         }
     }
     Billrun_Factory::log()->log("Request params Received: plan-" . $request['plan'] . ", data_usage-" . $request['data_usage'] . ", from_account_id-" . $request['from_account_id'] . ", to_account_id-" . $request['to_account_id'] . ", billrun-" . $request['billrun'], Zend_Log::INFO);
     $balances = new BalancesModel(array('size' => Billrun_Factory::config()->getConfigValue('balances.accounts.limit', 50000)));
     $results = $balances->getBalancesVolume($request['plan'], $request['data_usage'], $request['from_account_id'], $request['to_account_id'], $request['billrun']);
     if (empty($results)) {
         Billrun_Factory::log()->log('Some error happen, no result, received parameters: ' . print_r($request, true), Zend_Log::ERR);
         return;
     }
     $counter = 0;
     $accounts = array();
     foreach ($results as $result) {
         $accounts['aid'][$result['aid']]['subs'][$result['sid']] = Billrun_Util::byteFormat($result['balance']['totals']['data']['usagev'], 'MB', 2, false, '.', '');
         $counter++;
     }
     $this->getController()->setOutput(array(array('status' => 1, 'desc' => 'success', 'subscribers_count' => $counter, 'output' => $accounts)));
     return true;
 }
Esempio n. 2
0
 /**
  * Get the data resource
  * 
  * @return Mongo Cursor
  */
 public function getData($filter_query = array())
 {
     //		print_R($filter_query);die;
     $cursor = $this->collection->query($filter_query)->cursor()->setReadPreference(Billrun_Factory::config()->getConfigValue('read_only_db_pref'));
     $this->_count = $cursor->count();
     $resource = $cursor->sort($this->sort)->skip($this->offset())->limit($this->size);
     $ret = array();
     foreach ($resource as $item) {
         if ($item->get('rates') && !$this->showprefix) {
             foreach ($item->get('rates') as $key => $rate) {
                 $added_columns = array('t' => $key, 'tprice' => $rate['rate'][0]['price'], 'taccess' => isset($rate['access']) ? $rate['access'] : 0);
                 if (strpos($key, 'call') !== FALSE) {
                     $added_columns['tduration'] = Billrun_Util::durationFormat($rate['rate'][0]['interval']);
                 } else {
                     if ($key == 'data') {
                         $added_columns['tduration'] = Billrun_Util::byteFormat($rate['rate'][0]['interval'], '', 0, true);
                     } else {
                         $added_columns['tduration'] = $rate['rate'][0]['interval'];
                     }
                 }
                 $ret[] = new Mongodloid_Entity(array_merge($item->getRawData(), $added_columns, $rate));
             }
         } else {
             if ($this->showprefix && (isset($filter_query['$and'][0]['key']) || isset($filter_query['$and'][0]['params.prefix']))) {
                 foreach ($item->get('params.prefix') as $prefix) {
                     $item_raw_data = $item->getRawData();
                     unset($item_raw_data['params']['prefix']);
                     // to prevent high memory usage
                     $entity = new Mongodloid_Entity(array_merge($item_raw_data, array('prefix' => $prefix)));
                     $ret[] = $entity;
                 }
             } else {
                 $ret[] = $item;
             }
         }
     }
     return $ret;
 }
Esempio n. 3
0
 /**
  * Get the data resource
  * 
  * @return Mongo Cursor
  */
 public function getData($filter_query = array(), $fields = false)
 {
     $cursor = $this->getRates($filter_query);
     $this->_count = $cursor->count();
     $resource = $cursor->sort($this->sort)->skip($this->offset())->limit($this->size);
     $ret = array();
     foreach ($resource as $item) {
         if ($fields) {
             foreach ($fields as $field) {
                 $row[$field] = $item->get($field);
             }
             if (isset($row['rates'])) {
                 // convert plan ref to plan name
                 foreach ($row['rates'] as &$rate) {
                     if (isset($rate['plans'])) {
                         $plans = array();
                         foreach ($rate['plans'] as $plan) {
                             $plan_id = $plan['$id'];
                             $plans[] = Billrun_Factory::plan(array('id' => $plan_id))->getName();
                         }
                         $rate['plans'] = $plans;
                     }
                 }
             }
             $ret[] = $row;
         } else {
             if ($item->get('rates') && !$this->showprefix) {
                 foreach ($item->get('rates') as $key => $rate) {
                     if (is_array($rate)) {
                         $added_columns = array('t' => $key, 'tprice' => $rate['rate'][0]['price'], 'taccess' => isset($rate['access']) ? $rate['access'] : 0);
                         if (strpos($key, 'call') !== FALSE) {
                             $added_columns['tduration'] = Billrun_Util::durationFormat($rate['rate'][0]['interval']);
                         } else {
                             if ($key == 'data') {
                                 $added_columns['tduration'] = Billrun_Util::byteFormat($rate['rate'][0]['interval'], '', 0, true);
                             } else {
                                 $added_columns['tduration'] = $rate['rate'][0]['interval'];
                             }
                         }
                         $ret[] = new Mongodloid_Entity(array_merge($item->getRawData(), $added_columns, $rate));
                     }
                 }
             } else {
                 if ($this->showprefix && (isset($filter_query['$and'][0]['key']) || isset($filter_query['$and'][0]['params.prefix']))) {
                     foreach ($item->get('params.prefix') as $prefix) {
                         $item_raw_data = $item->getRawData();
                         unset($item_raw_data['params']['prefix']);
                         // to prevent high memory usage
                         $entity = new Mongodloid_Entity(array_merge($item_raw_data, array('prefix' => $prefix)));
                         $ret[] = $entity;
                     }
                 } else {
                     $ret[] = $item;
                 }
             }
         }
     }
     return $ret;
 }