<?php
namespace App\Controller;
use App\Service\{HTTPStatusProd,HTTPStatusDev,GarageStatus,CarStock, CebiaReportTable, OldNewCRM, XmlCheck, FileCheck};
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class DefaultController extends AbstractController
{
private string $projectDir;
public function __construct(ParameterBagInterface $params)
{
$this->projectDir = $params->get('kernel.project_dir');
}
#[Route('/', name: 'default')]
public function index(): Response
{
return $this->render('default/index.html.twig', [
'oldvsnew' => OldNewCRM::getStatusCode($this->projectDir),
'xmlcheck' => XmlCheck::getStatusCode($this->projectDir),
'httpstatusprod' => HTTPStatusProd::getStatusCode($this->projectDir),
'httpstatusdev' => HTTPStatusDev::getStatusCode($this->projectDir),
'carstockstatus' => CarStock::getStatusCode($this->projectDir),
'garagestatus' => GarageStatus::getStatusCode($this->projectDir),
'filestatus' => FileCheck::getStatusCode($this->projectDir),
'xmlcheck_message' => XmlCheck::getStatusMessage($this->projectDir),
'carstockstatus_message' => CarStock::getStatusMessage($this->projectDir),
'garagestatus_message' => GarageStatus::getStatusMessage($this->projectDir)
]);
}
#[Route('/file-check', name:'file_check')]
public function fileCheck(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/file-check.json';
if(!file_exists($file)) { return $this->redirectToRoute('default'); }
return $this->render('default/file_check.html.twig', [
'data' => json_decode(file_get_contents($file), true),
'last_update' => date('Y-m-d H:i:s', filemtime($file)),
]);
}
#[Route('/old-vs-new', name: 'old_vs_new')]
public function oldVsNew(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/1_old_vs_new_crm.json';
if(!file_exists($file)) { return $this->redirectToRoute('default'); }
$oldvsnew = json_decode(file_get_contents($file));
return $this->render('default/old_vs_new.html.twig', [
'oldvsnew' => $oldvsnew,
'last_update' => date('Y-m-d H:i:s', filemtime($file)),
]);
}
#[Route('/xml-monitor', name: 'xml_monitor')]
public function xmlMonitor(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/2_xml_monitor.html';
if(!file_exists($file)){ return $this->redirectToRoute('default'); }
$data = file_get_contents($file);
return $this->render('default/xml_check.html.twig', [
'data' => $data,
'last_update' => date('Y-m-d H:i:s', filemtime($file)),
'message' => XmlCheck::getStatusMessage($this->projectDir)
]);
}
#[Route('/xml-monitor-group', name: 'xml_group')]
public function xmlGroup(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/2_xml_monitor-group.html';
if(!file_exists($file)){ return $this->redirectToRoute('default'); }
$data = file_get_contents($file);
return $this->render('default/xml_check_group.html.twig', [
'data' => $data,
'last_update' => date('Y-m-d H:i:s', filemtime($file))
]);
}
#[Route('/json-monitor-group', name: 'json_group')]
public function jsonGroup(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/2_json_monitor-group.html';
if(!file_exists($file)){ return $this->redirectToRoute('default'); }
$data = file_get_contents($file);
return $this->render('default/json_check_group.html.twig', [
'data' => $data,
'last_update' => date('Y-m-d H:i:s', filemtime($file))
]);
}
#[Route('/json-monitor', name: 'json_monitor')]
public function jsonMonitor(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/2_json_monitor.html';
if(!file_exists($file)){ return $this->redirectToRoute('default'); }
$data = file_get_contents($file);
return $this->render('default/json_check.html.twig', [
'data' => $data,
'last_update' => date('Y-m-d H:i:s', filemtime($file)),
'message' => null,
]);
}
#[Route('/http-status/prod', name: 'http_status_prod')]
public function httpStatusProd(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/http_status_prod.json';
if(!file_exists($file)){ return $this->redirectToRoute('default'); }
$jsonData = json_decode(file_get_contents($file), true);
return $this->render('default/httpstatus_prod.html.twig', [
'data' => $jsonData,
'last_update' => date("Y-m-d H:i:s", filemtime($file))
]);
}
#[Route('/http-status/dev', name: 'http_status_dev')]
public function httpStatusDev(): Response
{
$file = $this->getParameter('kernel.project_dir'). '/var/status/http_status_dev.json';
if(!file_exists($file)){ return $this->redirectToRoute('default'); }
$jsonData = json_decode(file_get_contents($file), true);
return $this->render('default/httpstatus_dev.html.twig', [
'data' => $jsonData,
'last_update' => date("Y-m-d H:i:s", filemtime($file))
]);
}
#[Route('/car-stock', name: 'car_stock')]
public function carStock(): Response
{
$data = $this->getParameter('kernel.project_dir') . '/var/status/car-stock.json';
$data2 = $this->getParameter('kernel.project_dir') . '/var/status/car-stock-2.json';
$status = $this->getParameter('kernel.project_dir') . '/var/status/car-stock-status.json';
if(!file_exists($data) OR !file_exists($status) OR !file_exists($data2)){ return $this->redirectToRoute('default'); }
return $this->render('default/car_stock.html.twig', [
'data' => json_decode(file_get_contents($data), true),
'data2' => json_decode(file_get_contents($data2), true),
'last_update' => date('Y-m-d H:i:s', filemtime($data)),
'message' => CarStock::getStatusMessage($this->projectDir)
]);
}
#[Route('/garage-status', name: 'garage_status')]
public function garageStatus(): Response
{
$data = $this->getParameter('kernel.project_dir') . '/var/status/garage_status_all.json';
if(!file_exists($data)){ return $this->redirectToRoute('default'); }
return $this->render('default/garage_status.html.twig', [
'data' => json_decode(file_get_contents($data), true),
'last_update' => date('Y-m-d H:i:s', filemtime($data)),
'message' => GarageStatus::getStatusMessage($this->projectDir)
]);
}
#[Route('/cebia-report', name: 'cebia_report')]
public function cerbiaReport(CebiaReportTable $cebiaReportTable, CacheInterface $cache): Response
{
/* $data = $this->getParameter('kernel.project_dir') . '/var/status/cebia-report.csv';
if(!file_exists($data)){ return $this->redirectToRoute('default'); }
$csvArray = [];
if(($handle = fopen($data, 'r')) !== false)
{
while (($data = fgetcsv($handle)) !== false)
{
if (end($data) === '') {
array_pop($data);
}
$csvArray[] = $data;
}
fclose($handle);
}
return $this->render('default/cebia_report.html.twig', [
'data' => $csvArray,
'last_update' => date('Y-m-d H:i:s', filemtime($data)),
]); */
$htmlTable = $cache->get('cebia_report_table', function (ItemInterface $item) use ($cebiaReportTable) {
$item->expiresAfter(3600);
return $cebiaReportTable->execute();
});
return $this->render('default/cebia_report.html.twig', [
'htmlTable' => $htmlTable,
]);
}
#[Route('/exponea', name: 'app_exponea')]
public function exponea(): Response
{
$dataPath = $this->getParameter('kernel.project_dir') . '/var/status/exponea.json';
if (!file_exists($dataPath)) {
return $this->redirectToRoute('default');
}
$jsonData = json_decode(file_get_contents($dataPath), true);
return $this->render('default/exponea.html.twig', [
'data' => $jsonData,
'last_update' => (new \DateTime())->setTimestamp(filemtime($dataPath))->format('Y-m-d H:i:s'),
]);
}
#[Route('/api/getLastUpdate/{name}')]
public function apiGetLastUpdate(string $name): JsonResponse
{
$projectDir = $this->getParameter('kernel.project_dir');
$dataPath = '';
switch ($name) {
case 'exponea':
$dataPath = $projectDir . '/var/status/exponea.json';
break;
case 'cebia-report':
$dataPath = $projectDir . '/var/status/cebia-report.csv';
break;
case 'garage_status_all':
$dataPath = $projectDir . '/var/status/garage_status_all.json';
break;
case 'car-stock':
$dataPath = $projectDir . '/var/status/car-stock.json';
break;
case 'http_status_dev':
$dataPath = $projectDir . '/var/status/http_status_dev.json';
break;
case 'http_status_prod':
$dataPath = $projectDir . '/var/status/http_status_prod.json';
break;
case 'xml_monitor':
$dataPath = $projectDir . '/var/status/2_xml_monitor.html';
break;
case 'old_vs_new':
$dataPath = $projectDir . '/var/status/1_old_vs_new_crm.json';
break;
case 'file_check':
$dataPath = $projectDir . '/var/status/file-check.json';
break;
default:
return new JsonResponse(['error' => 'invalid name'], 400);
}
if (!file_exists($dataPath)) {
return new JsonResponse(['error' => 'file not found'], 404);
}
$lastUpdate = date('Y-m-d H:i:s', filemtime($dataPath));
return new JsonResponse(['last_update' => $lastUpdate]);
}
#[Route('/database-sync', name: 'database_sync')]
public function databaseSync(): Response
{
$filePath = $this->getParameter('kernel.project_dir') . '/var/status/database-sync-stats.json';
if (!file_exists($filePath)) {
return $this->redirectToRoute('default');
}
$data = json_decode(file_get_contents($filePath), true);
$lastUpdate = date('Y-m-d H:i:s', filemtime($filePath));
$brandMapping = [
1 => 'aaaauto',
2 => 'mototechna',
3 => 'auto-diskont'
];
$countryMapping = [
23 => 'cs',
81 => 'pl',
92 => 'sk'
];
$comparisonData = [];
foreach ($brandMapping as $brandId => $company) {
foreach ($countryMapping as $countryId => $countryCode) {
$comparisonData[] = [
'brand_id' => $brandId,
'company' => $company,
'country_id' => $countryId,
'country_code' => $countryCode,
'production' => $this->findCount($data['production'], $brandId, $countryId),
'resultmatic' => $this->findCount($data['resultmatic'], $brandId, $countryId),
'exponea' => $this->findCountExponea($data['exponea'], $company, $countryCode)
];
}
}
return $this->render('default/database_sync.html.twig', [
'comparison_data' => $comparisonData,
'last_update' => $lastUpdate,
]);
}
private function findCount(array $data, int $brandId, int $countryId): ?int
{
foreach ($data as $row) {
if ($row['brand_id'] == $brandId && $row['country_id'] == $countryId) {
return (int) $row['count'];
}
}
return null;
}
private function findCountExponea(array $data, string $company, string $countryCode): ?int
{
foreach ($data as $row) {
if ($row['company'] == $company && $row['country_code'] == $countryCode) {
return (int) $row['count'];
}
}
return null;
}
}