taurus-api/src/App/Service/TrInfoCollectorService.php

183 lines
5.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Service;
use App\Entity\TrProgress;
use League\Csv\Reader;
use League\Csv\Statement;
use Zend\Config\Config;
use Zend\Http\Client;
class TrInfoCollectorService
{
const UNIT_CORE = 'core';
const UNIT_SIG = 'sig';
const UNIT_TADE = 'tade';
const MHO_MAP = [
'ETH-TSPCW-D' => self::UNIT_CORE,
'ETH-TSPCORE' => self::UNIT_CORE,
'XTS-TSP-SIG' => self::UNIT_SIG,
'XTS-TSPSIGD' => self::UNIT_SIG,
'ETH-TADE-DE' => self::UNIT_TADE,
'ETH-TADE-MA' => self::UNIT_TADE,
];
/**
* @var Config
*/
private $config;
/**
* @var Client
*/
private $httpClient;
/**
* JiraClientService constructor.
* @param Client $client
* @param Config $config
*/
public function __construct(Client $client, Config $config)
{
$this->httpClient = $client;
$this->config = $config;
}
/**
* @return array
*/
public function getProgressInfo()
{
$user = $this->config->get('mhweb.user');
$password = $this->config->get('mhweb.password');
/** @var string $trProgressUri */
$trProgressUri = $this->config->get('url.mhWebTrProgress');
$response = $this->httpClient
->setAuth($user, $password)
->setUri($trProgressUri)
->send();
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad MHWEB result", $response->getStatusCode());
}
$csvResponse = $response->getBody();
$csvReader = Reader::createFromString($csvResponse);
$csvReader->setHeaderOffset(0);
return $this->parseProgressCsvRecords($csvReader);
}
public function getPraGoals()
{
$user = $this->config->get('mhweb.user');
$password = $this->config->get('mhweb.password');
/** @var string $trProgressUri */
$trProgressUri = $this->config->get('url.mhWebPraGoals');
$response = $this->httpClient
->setAuth($user, $password)
->setUri($trProgressUri)
->send();
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad MHWEB result", $response->getStatusCode());
}
$csvResponse = $response->getBody();
$csvReader = Reader::createFromString($csvResponse);
$csvReader->setHeaderOffset(0);
$statement = new Statement();
$csvRecords = $statement
->process($csvReader);
$goalCounter = $this->initGoalCounter();
foreach ($csvRecords as $record) {
$goalCounter[self::MHO_MAP[$record["mho"]]][$record["prio"]]++;
}
return $this->caltulatePraBaseDiff($goalCounter);
}
private function caltulatePraBaseDiff($goalCounter): array
{
$praBaseData = $this->config->get('pra.baseData');
foreach ($goalCounter as $mho => &$counters) {
foreach (['A', 'B', 'C'] as $prio) {
$counters[$prio] = $counters[$prio] - $praBaseData[$mho][$prio];
}
}
return $goalCounter;
}
private function parseProgressCsvRecords(Reader $csvReader)
{
$statement = new Statement();
$csvRecords = $statement
->process($csvReader);
$trProgressList = [];
foreach ($csvRecords as $csvRecord) {
$trProgress = new TrProgress();
$trProgress->setEriref($csvRecord["eriref"])
->setHeading($csvRecord["heading"])
->setPrio($csvRecord["prio"])
->setLastProgress($this->getLastProgressInDay($csvRecord))
;
$trProgressList[] = $trProgress;
}
usort($trProgressList, function(TrProgress $a, TrProgress $b){
return $b->getLastProgress() <=> $a->getLastProgress();
});
return $trProgressList;
}
/**
* @param array $csvRecord
* @return int
* @todo fix the BS with tuesday or whatever
*/
private function getLastProgressInDay(array $csvRecord): int
{
$lastProgressDate = null;
$hasNoProgressDate = false;
try {
$lastProgressDate = new \DateTime(str_replace(" - "," ", $csvRecord["lastprogressdate"]));
} catch(\Exception $e) {
$hasNoProgressDate = true;
}
try {
$lastDesignDate = new \DateTime($csvRecord["lastdesigndate"]);
if($hasNoProgressDate || $lastDesignDate > $lastProgressDate) {
$lastProgressDate = $lastDesignDate;
}
} catch (\Exception $e) {
if($hasNoProgressDate) {
return 0;
}
}
$now = new \DateTime();
$dateDiff = $now->diff($lastProgressDate);
return $dateDiff->days;
}
private function initGoalCounter(): array
{
$emptyPrios = ['A' => 0, 'B' => 0, 'C' => 0];
return [
self::UNIT_CORE => $emptyPrios,
self::UNIT_SIG => $emptyPrios,
self::UNIT_TADE => $emptyPrios,
];
}
}