/** * Increases the not delivered stat. * * @param string $info * * @throws NonExistentIdInternalException */ public function increase($info) { $result = $this->connectToStorageInternalWorker->connect()->update(array('id' => $info), array('$inc' => array('notDelivered' => 1))); if ($result['n'] == 0) { throw new NonExistentIdInternalException(); } }
/** * @param string $topic * @param string $from * @param string $to * @param int $group * * @return int[] * * @throws InvalidGroupApiException */ public function compute($topic, $from, $to, $group) { switch ($group) { case self::GROUP_BY_DAY: $timestamp = ['year' => ['$year' => '$timestamp'], 'month' => ['$month' => '$timestamp'], 'day' => ['$dayOfMonth' => '$timestamp']]; break; case self::GROUP_BY_MONTH: $timestamp = ['year' => ['$year' => '$timestamp'], 'month' => ['$month' => '$timestamp']]; break; case self::GROUP_BY_YEAR: $timestamp = ['year' => ['$year' => '$timestamp']]; break; default: throw new InvalidGroupApiException(); } $response = $this->connectToOperationStorageInternalWorker->connect()->aggregate([['$match' => ['topics' => $topic, 'timestamp' => ['$gte' => new \MongoDate($from), '$lte' => new \MongoDate($to)]]], ['$group' => ['_id' => ['timestamp' => $timestamp], 'total' => ['$sum' => 1]]], ['$sort' => ['_id' => 1]]]); $stats = []; foreach ($response['result'] as $item) { switch ($group) { case self::GROUP_BY_DAY: $stats[] = ['total' => $item['total'], 'year' => $item['_id']['timestamp']['year'], 'month' => $item['_id']['timestamp']['month'], 'day' => $item['_id']['timestamp']['day']]; break; case self::GROUP_BY_MONTH: $stats[] = ['total' => $item['total'], 'year' => $item['_id']['timestamp']['year'], 'month' => $item['_id']['timestamp']['month']]; break; case self::GROUP_BY_YEAR: $stats[] = ['total' => $item['total'], 'year' => $item['_id']['timestamp']['year']]; break; default: throw new InvalidGroupApiException(); } } return $stats; }
/** * Collects latest stats grouped by topic. */ public function collect() { $topics = $this->collectTopicsApiWorker->collect(); $stats = []; foreach ($topics as $topic) { $stats = array_merge($stats, iterator_to_array($this->connectToStorageInternalWorker->connect()->find(['topics' => $topic['id']])->fields(['_id' => 0])->sort(['_id' => -1])->limit(5))); } return $stats; }
/** * Collects stats. * * @return \Iterator */ public function collect() { $cursor = $this->connectToStatStorageInternalWorker->connect()->find()->fields(['_id' => 0])->sort(['_id' => -1]); $stats = []; foreach ($cursor as $i => $stat) { /** @var \MongoDate $timestamp */ $timestamp = $stat['timestamp']; $stat['timestamp'] = $timestamp->sec; $stats[] = $stat; } return new \ArrayIterator($stats); }
/** * Creates the stat for given info. * * @param array $info * @param string $total * @param int $timestamp */ public function create($info, $total, $timestamp) { $this->connectToStorageInternalWorker->connect()->insert(['id' => $info['id'], 'body' => $info['body'], 'topics' => $info['topics'], 'timestamp' => new \MongoDate($timestamp), 'total' => $total, 'delivered' => 0, 'notDelivered' => 0]); }