Skip to content

Commit

Permalink
Store region last update in db (instead of cache) (#803)
Browse files Browse the repository at this point in the history
* Create Region entity

* Add migration

* Update REgionProvider.php

* Update NewMapperCommand.php

* Update UpdateCommand.php

* Apply fixes from StyleCI

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
jbelien and StyleCIBot authored Nov 15, 2022
1 parent 149106e commit 8dd02f3
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 16 deletions.
31 changes: 31 additions & 0 deletions migrations/Version20221115184831.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20221115184831 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create table "region"';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE region (id VARCHAR(255) NOT NULL, last_update DATETIME NOT NULL, PRIMARY KEY(id))');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE region');
}
}
11 changes: 11 additions & 0 deletions src/Command/NewMapperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\Changeset;
use App\Entity\Mapper;
use App\Entity\Region;
use App\Service\ChangesetProvider;
use App\Service\MapperProvider;
use App\Service\OpenStreetMapAPI;
Expand Down Expand Up @@ -150,6 +151,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

/** @var Region|null */
$r = $this->entityManager->find(Region::class, $key);
if (null === $r) {
$r = new Region();
$r->setId($key);
}
$r->setLastUpdate(new \DateTime());
$this->entityManager->persist($r);
$this->entityManager->flush();

return Command::SUCCESS;
} catch (ClientException $e) {
$io->error($e->getMessage());
Expand Down
23 changes: 12 additions & 11 deletions src/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Command;

use App\Entity\Region;
use App\Service\RegionsProvider;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -20,6 +22,7 @@ class UpdateCommand extends Command
{
public function __construct(
private RegionsProvider $provider,
private EntityManagerInterface $entityManager,
private CacheItemPoolInterface $cache
) {
parent::__construct();
Expand All @@ -44,31 +47,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($regions as $continent => $group) {
foreach ($group as $key => $region) {
$cacheKey = sprintf('last_update.%s', $key);
/** @var Region|null */
$r = $this->entityManager->find(Region::class, $key);
$lastUpdate = null === $r ? null : $r->getLastUpdate();

$io->title(sprintf('%s (%s)', $region['name'], date('Y-m-d')));

$lastUpdate = $this->cache->getItem($cacheKey);
if (true === $input->getOption('force') || !$lastUpdate->isHit() || $lastUpdate->get() < date('Y-m-d')) {
if (!$lastUpdate->isHit()) {
// If cache is not set, get new mappers from the last 5 days
if (true === $input->getOption('force') || null === $lastUpdate || $lastUpdate->format('Y-m-d') < date('Y-m-d')) {
if (null === $lastUpdate) {
// If there never was an update, get new mappers from the last 5 days
$date = (new \DateTime())->sub(new \DateInterval('P5D'))->format('Y-m-d');
$io->note(sprintf('Cache is not set, get new mappers from %s.', $date));
} elseif (true === $input->getOption('force') && $lastUpdate->get() === date('Y-m-d')) {
} elseif (true === $input->getOption('force') && $lastUpdate->format('Y-m-d') === date('Y-m-d')) {
// If last update was today and process is forced, get new mappers from yesterday
$date = (new \DateTime($lastUpdate->get()))->sub(new \DateInterval('P1D'))->format('Y-m-d');
$date = $lastUpdate->sub(new \DateInterval('P1D'))->format('Y-m-d');
$io->note(sprintf('Get new mappers from %s (forced).', $date));
} else {
// Get new mappers from the last update date
$date = (new \DateTime($lastUpdate->get()))->format('Y-m-d');
$date = $lastUpdate->format('Y-m-d');
$io->note(sprintf('Get new mappers from %s.', $date));
}

try {
$this->process($key, $date, $output);

$lastUpdate->set(date('c'));
$this->cache->save($lastUpdate);
} catch (\Exception $e) {
$io->error($e->getMessage());
}
Expand Down
47 changes: 47 additions & 0 deletions src/Entity/Region.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Entity;

use App\Repository\RegionRepository;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass=RegionRepository::class)
*/
class Region
{
/**
* @ORM\Id
* @ORM\Column(type="string")
*/
private ?string $id = null;

/**
* @ORM\Column(type="datetime")
*/
private $lastUpdate;

public function getId(): ?string
{
return $this->id;
}

public function setId(string $id): self
{
$this->id = $id;

return $this;
}

public function getLastUpdate(): \DateTime
{
return $this->lastUpdate;
}

public function setLastUpdate(\DateTime $lastUpdate): self
{
$this->lastUpdate = $lastUpdate;

return $this;
}
}
66 changes: 66 additions & 0 deletions src/Repository/RegionRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Repository;

use App\Entity\Region;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<Region>
*
* @method Region|null find($id, $lockMode = null, $lockVersion = null)
* @method Region|null findOneBy(array $criteria, array $orderBy = null)
* @method Region[] findAll()
* @method Region[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class RegionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Region::class);
}

public function save(Region $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);

if ($flush) {
$this->getEntityManager()->flush();
}
}

public function remove(Region $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);

if ($flush) {
$this->getEntityManager()->flush();
}
}

// /**
// * @return Region[] Returns an array of Region objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('r')
// ->andWhere('r.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('r.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }

// public function findOneBySomeField($value): ?Region
// {
// return $this->createQueryBuilder('r')
// ->andWhere('r.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}
10 changes: 5 additions & 5 deletions src/Service/RegionsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

use App\Entity\Mapper;
use App\Repository\MapperRepository;
use App\Repository\RegionRepository;
use App\Repository\WelcomeRepository;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Yaml\Yaml;

class RegionsProvider
{
private array $regions = [];

public function __construct(
private CacheItemPoolInterface $cache,
private RegionRepository $regionRepository,
private MapperRepository $mapperRepository,
private WelcomeRepository $welcomeRepository,
private string $projectDirectory
Expand Down Expand Up @@ -72,13 +72,13 @@ public function getGeometry(string $continent, string $key): array

public function getLastUpdate(string $key): ?\DateTime
{
$cacheKey = sprintf('last_update.%s', $key);
$region = $this->regionRepository->find($key);

if (true !== $this->cache->hasItem($cacheKey)) {
if (null === $region) {
return null;
}

return new \DateTime($this->cache->getItem($cacheKey)->get());
return $region->getLastUpdate();
}

public function getPercentage(string $key): array
Expand Down

0 comments on commit 8dd02f3

Please sign in to comment.