From 1372b7e6d15747d9c801b05b10eadb3342181ad8 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Sat, 7 Jan 2017 21:53:31 +0300 Subject: [PATCH] Telegram bot users statistics in /stats response. --- .../Repository/Telegram/AccountRepository.php | 65 ++++++++++++++++++- .../Resources/views/Telegram/stats.md.twig | 2 + .../Telegram/PrivateMessageProcessor.php | 9 +++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Repository/Telegram/AccountRepository.php b/src/Skobkin/Bundle/PointToolsBundle/Repository/Telegram/AccountRepository.php index 04b5a22..e82a484 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Repository/Telegram/AccountRepository.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Repository/Telegram/AccountRepository.php @@ -2,10 +2,73 @@ namespace Skobkin\Bundle\PointToolsBundle\Repository\Telegram; - use Doctrine\ORM\EntityRepository; class AccountRepository extends EntityRepository { + /** + * @todo remove if not used + * + * @param array $criteria + * @param array|null $orderBy + * @param int|null $limit + * @param int|null $offset + * + * @return array + */ + public function findLinkedAccountsBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array + { + $qb = $this->createQueryBuilder('a'); + $i = 0; + foreach ($criteria as $property => $value) { + $qb + ->andWhere('a.'.$property.' = :criteria_'.$i) + ->setParameter('criteria_'.$i, $value) + ; + } + + if (null !== $orderBy) { + foreach ($orderBy as $property => $order) { + $qb->addOrderBy($property, $order); + } + } + + if (null !== $limit) { + $qb->setMaxResults($limit); + } + + if (null !== $offset) { + $qb->setFirstResult($offset); + } + + return $qb->getQuery()->getResult(); + } + + /** + * Returns total number of accounts + * + * @return int + */ + public function getAccountsCount(): int + { + return $this->createQueryBuilder('a') + ->select('COUNT(a)') + ->getQuery()->getSingleScalarResult() + ; + } + + /** + * Returns number of accounts with linked Point.im profile + * + * @return int + */ + public function getLinkedAccountsCount(): int + { + return $this->createQueryBuilder('a') + ->select('COUNT(a)') + ->where('a.user IS NOT NULL') + ->getQuery()->getSingleScalarResult() + ; + } } \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig index 2f0589a..132b86a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig @@ -2,6 +2,8 @@ Total users: {{ total_users }} Active users: {{ active_users }} +Telegram users: {{ telegram_accounts }} +Telegram linked users: {{ telegram_linked_accounts }} 24 hours events: {{ today_events }} Visit [Point Tools](https://point.skobk.in/) for more info. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php index 609fb39..043fdc8 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/Telegram/PrivateMessageProcessor.php @@ -8,6 +8,7 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Exception\Telegram\CommandProcessingException; use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository; use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository; +use Skobkin\Bundle\PointToolsBundle\Repository\Telegram\AccountRepository; use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository; use Skobkin\Bundle\PointToolsBundle\Service\Factory\Telegram\AccountFactory; use Skobkin\Bundle\PointToolsBundle\Service\UserApi; @@ -45,6 +46,11 @@ class PrivateMessageProcessor */ private $userRepo; + /** + * @var AccountRepository + */ + private $accountRepo; + /** * @var SubscriptionRepository */ @@ -70,6 +76,7 @@ class PrivateMessageProcessor $this->pointUserId = $pointUserId; $this->userRepo = $em->getRepository('SkobkinPointToolsBundle:User'); + $this->accountRepo = $em->getRepository('SkobkinPointToolsBundle:Telegram\Account'); $this->subscriptionRepo = $em->getRepository('SkobkinPointToolsBundle:Subscription'); $this->subscriptionEventRepo = $em->getRepository('SkobkinPointToolsBundle:SubscriptionEvent'); } @@ -336,6 +343,8 @@ class PrivateMessageProcessor [ 'total_users' => $this->userRepo->getUsersCount(), 'active_users' => $this->subscriptionRepo->getUserSubscribersCountById($this->pointUserId), + 'telegram_accounts' => $this->accountRepo->getAccountsCount(), + 'telegram_linked_accounts' => $this->accountRepo->getLinkedAccountsCount(), 'today_events' => $this->subscriptionEventRepo->getLastDayEventsCount(), ] );