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

188 lines
5.3 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;
}
/**
2017-09-08 09:44:30 +02:00
* < 700 daily progress
* * weekly
* @return array
2017-09-08 09:44:30 +02:00
* @todo calculate progress late stuff
*/
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
{
2017-09-08 09:44:30 +02:00
$praBaseData = $this->config->get('pra.baseData')->toArray();
foreach ($goalCounter as $mho => &$counters) {
foreach (['A', 'B', 'C'] as $prio) {
2017-09-08 09:44:30 +02:00
$counters[$prio] = $counters[$prio] - $praBaseData[$prio][$mho];
}
}
return $goalCounter;
}
private function parseProgressCsvRecords(Reader $csvReader)
{
2017-09-08 09:44:30 +02:00
$teamMembers = $this->config->get('team.members')->toArray();
$statement = new Statement();
$csvRecords = $statement
2017-09-08 09:44:30 +02:00
->where(function($record) use ($teamMembers) {
return in_array(strtolower($record['owner']), $teamMembers);
})
->process($csvReader);
$trProgressList = [];
foreach ($csvRecords as $csvRecord) {
$trProgress = new TrProgress();
$trProgress->setEriref($csvRecord["eriref"])
->setHeading($csvRecord["heading"])
->setPrio($csvRecord["prio"])
2017-09-08 09:44:30 +02:00
->setLastProgressInDays($this->getLastProgressInDay($csvRecord))
;
$trProgressList[] = $trProgress;
}
usort($trProgressList, function(TrProgress $a, TrProgress $b){
2017-09-08 09:44:30 +02:00
return $b->getLastProgressInDays() <=> $a->getLastProgressInDays();
});
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,
];
}
}