src/Controller/Search/DashboardSearchController.php line 47

  1. <?php
  2. namespace App\Controller\Search;
  3. use App\Entity\Advertiser;
  4. use App\Entity\Comment;
  5. use App\Entity\Device;
  6. use App\Entity\Host;
  7. use App\Entity\ProxyUser;
  8. use App\Entity\SearchCampaign;
  9. use App\Entity\SearchFeed;
  10. use App\Entity\SearchFeedIsps;
  11. use App\Entity\User;
  12. use App\Repository\SearchCampaignRepository;
  13. use App\Repository\SearchFeedRepository;
  14. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  15. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  16. use EasyCorp\Bundle\EasyAdminBundle\Config\Assets;
  17. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  18. use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
  19. use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
  20. use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
  21. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
  22. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  23. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Security\Core\User\UserInterface;
  27. use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
  28. use Symfony\UX\Chartjs\Model\Chart;
  29. class DashboardSearchController extends AbstractDashboardController
  30. {
  31.     private SearchFeedRepository $searchFeedRepository;
  32.     private SearchCampaignRepository $campaignRepository;
  33.     private ChartBuilderInterface $chartBuilder;
  34.     public function __construct(SearchCampaignRepository $campaignRepositorySearchFeedRepository $searchFeedRepositoryChartBuilderInterface $chartBuilder, private AdminUrlGenerator $adminUrlGenerator)
  35.     {
  36.         $this->campaignRepository $campaignRepository;
  37.         $this->searchFeedRepository $searchFeedRepository;
  38.         $this->chartBuilder $chartBuilder;
  39.     }
  40.     #[IsGranted('ROLE_USER')]
  41.     #[Route('/'name'search')]
  42.     public function index(): Response
  43.     {
  44.         $runningFeeds $this->searchFeedRepository->findLatest();
  45.         $enabledFeeds $this->campaignRepository->findBy(['enabled' => true]);
  46.         $lastSevenDaysUsages $this->searchFeedRepository->lastSevenDaysUsages();
  47.         // current month
  48.         $firstDayOfMonth = (new \DateTime('first day of this month'))->format('Y-m-d');
  49.         $today = (new \DateTime('now'))->format('Y-m-d');
  50.         $currentMonthUsages $this->searchFeedRepository->monthUsages($firstDayOfMonth$today);
  51.         // last month
  52.         $firstDayOfLastMonth = (new \DateTime('first day of last month'))->format('Y-m-d');
  53.         $lastDayOfLastMonth = (new \DateTime('last day of last month'))->format('Y-m-d');
  54.         $lastMonthUsages $this->searchFeedRepository->monthUsages($firstDayOfLastMonth$lastDayOfLastMonth);
  55.         $currentMonthUsagesByAdv $this->searchFeedRepository->monthUsagesByAdv($firstDayOfMonth$today);
  56.         $lastMonthUsagesByAdv $this->searchFeedRepository->monthUsagesByAdv($firstDayOfLastMonth$lastDayOfLastMonth);
  57.         return $this->render('admin/index_search.html.twig', [
  58.             'enabledFeeds'            => $enabledFeeds,
  59.             'runningFeeds'            => $runningFeeds,
  60.             'lastSevenDaysUsages'     => $lastSevenDaysUsages,
  61.             'currentMonthUsages'      => $currentMonthUsages,
  62.             'lastMonthUsages'         => $lastMonthUsages,
  63.             'currentMonthUsagesByAdv' => $currentMonthUsagesByAdv,
  64.             'lastMonthUsagesByAdv'    => $lastMonthUsagesByAdv,
  65.             'chart'                   => $this->createChart($currentMonthUsagesByAdv),
  66.         ]);
  67.     }
  68.     public function configureDashboard(): Dashboard
  69.     {
  70.         return Dashboard::new()
  71.             ->setTitle('Overflow Admin Search');
  72.     }
  73.     public function configureUserMenu(UserInterface $user): UserMenu
  74.     {
  75.         if (!$user instanceof User) {
  76.             throw new \Exception('Wrong User');
  77.         }
  78.         return parent::configureUserMenu($user)
  79.             ->setAvatarUrl($user->getAvatarUrl())
  80.             ->
  81.             setMenuItems([
  82.                 MenuItem::linkToUrl('My Profile''fa fa-user'$this->generateUrl('app_profile_show'))
  83.             ]);
  84.     }
  85.     public function configureCrud(): Crud
  86.     {
  87.         return parent::configureCrud()
  88.             ->setDefaultSort([
  89.                 'id' => 'DESC',
  90.             ])
  91.             ->overrideTemplate('crud/field/id''admin/field/id_with_icon.html.twig');
  92.     }
  93.     public function configureMenuItems(): iterable
  94.     {
  95.         yield MenuItem::linkToDashboard('Dashboard Search''fa fa-dashboard');
  96.         yield MenuItem::section('Settings');
  97.         yield MenuItem::linkToCrud('Advertisers''fa fa-users'Advertiser::class);
  98.         yield MenuItem::linkToCrud('Search Campaings''fa fa-users'SearchCampaign::class);
  99.         yield MenuItem::section();
  100.         yield MenuItem::section('Search Campaings');
  101.         yield MenuItem::linkToCrud('SearchFeed Today''fa fa-users'SearchFeed::class)
  102.             ->setController(SearchFeedCrudTodayController::class);
  103.         yield MenuItem::linkToCrud('SearchFeed Yesterday''fa fa-users'SearchFeed::class)
  104.             ->setController(SearchFeedCrudYesterdayController::class);
  105.         yield MenuItem::linkToCrud('SearchFeed''fa fa-users'SearchFeed::class)
  106.             ->setController(SearchFeedCrudController::class);
  107.         yield MenuItem::section();
  108.         yield MenuItem::linkToCrud('SearchFeed Isp\'s''fa fa-tags'SearchFeedIsps::class);
  109.         yield MenuItem::linkToCrud('Hosts''fa fa-tags'Host::class);
  110.         yield MenuItem::linkToCrud('Available Proxy Users''fa fa-users'ProxyUser::class);
  111.         yield MenuItem::linkToCrud('Devices''fa fa-users'Device::class);
  112.         yield MenuItem::linkToCrud('Comments''fa fa-users'Comment::class);
  113.         yield MenuItem::section();
  114.         yield MenuItem::linkToUrl('Apag''''/admin');
  115.     }
  116.     public function configureActions(): Actions
  117.     {
  118.         return parent::configureActions()
  119.             ->add(Crud::PAGE_INDEXAction::DETAIL)
  120.             ->update(Crud::PAGE_DETAILAction::EDIT, static function (Action $action) {
  121.                 return $action->setIcon('fa fa-edit');
  122.             })
  123.             ->update(Crud::PAGE_DETAILAction::INDEX, static function (Action $action) {
  124.                 return $action->setIcon('fa fa-list');
  125.             });
  126.     }
  127.     public function configureAssets(): Assets
  128.     {
  129.         return parent::configureAssets()
  130.             ->addWebpackEncoreEntry('admin');
  131.     }
  132.     private function createChart(array $currentMonthUsagesByAdv): Chart
  133.     {
  134.         $labels = [];
  135.         $sumClicks = [];
  136.         $sumRevenue = [];
  137.         $sumCosts = [];
  138.         $sumProfit = [];
  139.         foreach ($currentMonthUsagesByAdv as $data) {
  140.             // Assuming all keys are always present, if not, use null coalescing like $data['sumClicks'] ?? 0
  141.             $labels[] = $data['advertiserName'];
  142.             $sumClicks[] = (int)$data['sumClicks'];
  143.             $sumRevenue[] = (float)$data['sumCosts'];
  144.             $sumCosts[] = (float)$data['sumUsages'] * 2.7;
  145.             $sumProfit[] = (float)$data['sumCosts'] - (float)$data['sumUsages'] * 2.7;
  146.         }
  147.         $chart $this->chartBuilder->createChart(Chart::TYPE_RADAR);
  148.         $chart->setOptions([
  149.             'plugins' => [
  150.                 'title' => [
  151.                     'display'   => true,
  152.                     'text'      => strtoupper('Usages Adv This Month'),
  153.                     'fontColor' => '#ce1111',
  154.                     'fontSize'  => 16
  155.                     //'padding' => 50
  156.                 ],
  157.             ],
  158.             'scales'  => [
  159.                 'y' => [
  160.                     'suggestedMin' => 0,
  161.                     'suggestedMax' => 100,
  162.                 ],
  163.             ],
  164.         ]);
  165.         $chart->setData([
  166.             'labels'   => $labels,
  167.             'datasets' => [
  168.                 [
  169.                     'label'           => 'Sum Clicks',
  170.                     'backgroundColor' => 'rgb(22, 99, 132, 0.2)',
  171.                     'borderColor'     => 'rgb(22, 99, 132)',
  172.                     'data'            => $sumClicks,
  173.                     'hidden'          => true,
  174.                 ],
  175.                 [
  176.                     'label'           => 'Sum Revenue',
  177.                     'backgroundColor' => 'rgb(45, 220, 126, 0.2)',
  178.                     'borderColor'     => 'rgb(45, 220, 126)',
  179.                     'data'            => $sumRevenue,
  180.                 ],
  181.                 [
  182.                     'label'           => 'Sum Costs',
  183.                     'backgroundColor' => 'rgb(255, 99, 132, 0.2)',
  184.                     'borderColor'     => 'rgb(255, 99, 132)',
  185.                     'data'            => $sumCosts,
  186.                 ],
  187.                 [
  188.                     'label' => 'Sum Profit',
  189.                     //  'backgroundColor' => 'rgb(255, 99, 132)',
  190.                     //  'borderColor'     => 'rgb(255, 99, 132)',
  191.                     'data'  => $sumProfit,
  192.                 ],
  193.             ],
  194.         ]);
  195.         return $chart;
  196.     }
  197. }