/** * @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' => ['type' => '$type', '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'], 'type' => $item['_id']['type'], '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'], 'type' => $item['_id']['type'], 'year' => $item['_id']['timestamp']['year'], 'month' => $item['_id']['timestamp']['month']]; break; case self::GROUP_BY_YEAR: $stats[] = ['total' => $item['total'], 'type' => $item['_id']['type'], 'year' => $item['_id']['timestamp']['year']]; break; default: throw new InvalidGroupApiException(); } } return $stats; }
/** * @param string $topic * @param string $until * * @return int[] * * @throws InvalidGroupApiException */ public function compute($topic, $until) { $response = $this->connectToOperationStorageInternalWorker->connect()->aggregate([['$match' => ['topics' => $topic, 'timestamp' => ['$lt' => new \MongoDate($until)]]], ['$group' => ['_id' => ['type' => '$type'], 'total' => ['$sum' => 1]]], ['$sort' => ['_id' => 1]]]); $stats = []; foreach ($response['result'] as $item) { $stats[] = ['total' => $item['total'], 'type' => $item['_id']['type']]; } return $stats; }
/** * Collects operations. * * @return \Iterator */ public function collect() { $cursor = $this->connectToOperationStorageInternalWorker->connect()->find()->fields(['_id' => 0]); $operations = []; foreach ($cursor as $i => $operation) { /** @var \MongoDate $timestamp */ $timestamp = $operation['timestamp']; $operation['timestamp'] = $timestamp->sec; $operations[] = $operation; } return new \ArrayIterator($operations); }
/** * Logs a delete operation. * * @param string $mobile * @param string $uniqueness * @param string[] $topics * @param int $trial * @param int $balance * @param int $timestamp */ public function logDelete($mobile, $uniqueness, $topics, $trial, $balance, $timestamp) { $this->connectToOperationStorageInternalWorker->connect()->insert(['type' => 3, 'mobile' => $mobile, 'uniqueness' => $uniqueness, 'topics' => $topics, 'trial' => $trial, 'balance' => $balance, 'timestamp' => new \MongoDate($timestamp)]); }
/** * Checks a create operation. * * @param string $mobile * * @return boolean */ public function checkCreate($mobile) { $operation = $this->connectToOperationStorageInternalWorker->connect()->findOne(['type' => 1, 'mobile' => $mobile]); return $operation ? true : false; }