Skip to content

Commit

Permalink
Merge pull request #112 from digio-ch/issue/hc-241
Browse files Browse the repository at this point in the history
Adjust date calculation for "Abteilungen im Vergleich"
  • Loading branch information
SebastianStorz authored Feb 15, 2024
2 parents 04db146 + 199c85e commit 9841524
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ services:
$apiToken: '%env(GROUP_STRUCTURE_TOKEN)%'
$url: '%env(GROUP_STRUCTURE_URL)%'

App\Service\CensusAPIService:
App\Service\Census\CensusAPIService:
arguments:
$apiToken: '%env(CENSUS_TOKEN)%'
$url: '%env(CENSUS_URL)%'
3 changes: 1 addition & 2 deletions src/Command/FetchCensusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use App\Model\CommandStatistics;
use App\Repository\Midata\CensusGroupRepository;
use App\Repository\Midata\GroupTypeRepository;
use App\Service\CensusAPIService;
use App\Service\GroupStructureAPIService;
use App\Service\Census\CensusAPIService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
11 changes: 11 additions & 0 deletions src/Repository/Midata/CensusGroupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Id\AssignedGenerator;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\ManagerRegistry;

/**
Expand Down Expand Up @@ -48,6 +49,16 @@ public function remove(CensusGroup $entity, bool $flush = true): void
}
}

public function getLatestYear(): int
{
$rsm = new ResultSetMapping();
$rsm->addScalarResult('max', 'max', 'integer');
$query = $this->_em->createNativeQuery('SELECT MAX(year) FROM census_group;', $rsm);
$result = $query->getSingleScalarResult();
if (is_null($result)) throw new \Exception("No date found in census table.");
return $result;
}

// /**
// * @return CensusGroup[] Returns an array of CensusGroup objects
// */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace App\Service;
namespace App\Service\Census;

use App\Service\Http;
use App\Service\Http\GuzzleWrapper;

class CensusAPIService
Expand Down
32 changes: 32 additions & 0 deletions src/Service/Census/CensusDateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Service\Census;

use App\Repository\Midata\CensusGroupRepository;

class CensusDateProvider
{
private CensusGroupRepository $censusGroupRepository;

public function __construct(CensusGroupRepository $censusGroupRepository)
{
$this->censusGroupRepository = $censusGroupRepository;
}

/**
* This funciton returns the latest year we have census data for in the database.
* It was created so that census widgets are still usable in the period between the start of a new year until
* the census data is updated.
* @return int
* @throws \Exception
*/
public function getLatestYear(): int
{
return $this->censusGroupRepository->getLatestYear();
}

public function getRelevantDateRange(): array
{
return range($this->getLatestYear() - 5, $this->getLatestYear());
}
}
20 changes: 12 additions & 8 deletions src/Service/DataProvider/CensusDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Repository\Midata\GroupTypeRepository;
use App\Repository\Statistics\StatisticGroupRepository;
use App\Service\Apps\Census\CensusFilter;
use App\Service\Census\CensusDateProvider;
use Doctrine\DBAL\Schema\Table;
use Sentry\Util\JSON;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -26,15 +27,18 @@ class CensusDataProvider extends WidgetDataProvider
{
private CensusGroupRepository $censusGroupRepository;
private StatisticGroupRepository $statisticGroupRepository;
private CensusDateProvider $censusDateProvider;
public function __construct(
GroupRepository $groupRepository,
GroupTypeRepository $groupTypeRepository,
TranslatorInterface $translator,
CensusGroupRepository $censusGroupRepository,
StatisticGroupRepository $statisticGroupRepository
StatisticGroupRepository $statisticGroupRepository,
CensusDateProvider $censusDateProvider
) {
$this->censusGroupRepository = $censusGroupRepository;
$this->statisticGroupRepository = $statisticGroupRepository;
$this->censusDateProvider = $censusDateProvider;
parent::__construct(
$groupRepository,
$groupTypeRepository,
Expand Down Expand Up @@ -66,7 +70,7 @@ public function getPreviewData(Group $group)
]
];
foreach ($flattenedGroups as $group) {
$censusGroup = $this->censusGroupRepository->findOneBy(['group_id' => $group->getId(), 'year' => date('Y')]);
$censusGroup = $this->censusGroupRepository->findOneBy(['group_id' => $group->getId(), 'year' => $this->censusDateProvider->getLatestYear()]);
if (!is_null($censusGroup)) {
$return['m']['leiter'] += $censusGroup->getLeiterMCount();
$return['m']['biber'] += $censusGroup->getBiberMCount();
Expand Down Expand Up @@ -211,7 +215,7 @@ public function getTableData(Group $group, CensusRequestData $censusRequestData)
$flattenedGroups = $this->getRelevantGroups($group);
$flattenedGroups = $this->sortGroups($flattenedGroups);
$dataTransferObjects = [];
$relevantYears = range(date('Y') - 5, date('Y'));
$relevantYears = $this->censusDateProvider->getRelevantDateRange();
foreach ($flattenedGroups as $flattenedGroup) {
$dataTransferObjects[] = CensusMapper::mapToCensusTable($flattenedGroup, $this->censusGroupRepository->findBy(['group_id' => $flattenedGroup->getId()]), $relevantYears, $censusRequestData);
}
Expand All @@ -229,7 +233,7 @@ public function getDevelopmentData(Group $group, CensusRequestData $censusReques

$absolute = [];
$relative = [];
$relevantYears = range(date('Y') - 5, date('Y'));
$relevantYears = $this->censusDateProvider->getRelevantDateRange();
foreach ($relevantGroups as $relevantGroup) {
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId()]);
if (!sizeof($data) == 0) {
Expand All @@ -253,7 +257,7 @@ public function getMembersData(Group $group, CensusRequestData $censusRequestDat

$rawResults = [];
foreach ($relevantGroups as $relevantGroup) {
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId(), 'year' => date('Y')]);
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId(), 'year' => $this->censusDateProvider->getLatestYear()]);
if (!sizeof($data) == 0) {
CensusMapper::filterCensusGroup($data[0], $censusRequestData);
$biber = $data[0]->getBiberMCount() + $data[0]->getBiberFCount();
Expand All @@ -278,7 +282,7 @@ public function getMembersData(Group $group, CensusRequestData $censusRequestDat
$dto->setData($rawResult);
$return[] = $dto;
}
return $return;
return ['data' =>$return, 'year' => $this->censusDateProvider->getLatestYear()];
}

public function getTreemapData(Group $group, CensusRequestData $censusRequestData)
Expand All @@ -288,7 +292,7 @@ public function getTreemapData(Group $group, CensusRequestData $censusRequestDat

$return = [];
foreach ($relevantGroups as $relevantGroup) {
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId(), 'year' => date('Y')]);
$data = $this->censusGroupRepository->findBy(['group_id' => $relevantGroup->getId(), 'year' => $this->censusDateProvider->getLatestYear()]);
if (!sizeof($data) == 0) {
CensusMapper::filterCensusGroup($data[0], $censusRequestData);
$dto = new TreemapWidgetDTO();
Expand All @@ -300,7 +304,7 @@ public function getTreemapData(Group $group, CensusRequestData $censusRequestDat
$return[] = $dto;
}
}
return $return;
return ['data' =>$return, 'year' => $this->censusDateProvider->getLatestYear()];
}

/**
Expand Down

0 comments on commit 9841524

Please sign in to comment.