* tsp-info endpoint added

* different collector services are now implemented
This commit is contained in:
Dávid Danyi
2017-09-05 19:15:17 +02:00
parent 88527e4ff1
commit efc6e7b0c4
16 changed files with 855 additions and 63 deletions

View File

@@ -2,12 +2,27 @@
namespace App\Service;
use Symfony\Component\CssSelector\CssSelectorConverter;
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
*/
@@ -18,15 +33,6 @@ class TrInfoCollectorService
*/
private $httpClient;
/**
* @var array
*/
private $tempSensors = [
'Temp 5' => 'back_left',
'Temp 4' => 'back_middle',
'Temp 3' => 'back_right',
];
/**
* JiraClientService constructor.
* @param Client $client
@@ -38,49 +44,139 @@ class TrInfoCollectorService
$this->config = $config;
}
public function getLabTemperatureData()
/**
* @return array
*/
public function getProgressInfo()
{
/** @var Config $labTemperatureUrl */
$labTemperatureUrl = $this->config->get('url.labTemperatureUrl');
$user = $this->config->get('mhweb.user');
$password = $this->config->get('mhweb.password');
/** @var string $trProgressUri */
$trProgressUri = $this->config->get('url.mhWebTrProgress');
$response = $this->httpClient
->setUri($labTemperatureUrl)
->setAuth($user, $password)
->setUri($trProgressUri)
->send();
if(!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad LAB result", $response->getStatusCode());
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad MHWEB result", $response->getStatusCode());
}
return $this->parseHtml($response->getBody());
$csvResponse = $response->getBody();
$csvReader = Reader::createFromString($csvResponse);
$csvReader->setHeaderOffset(0);
return $this->parseProgressCsvRecords($csvReader);
}
private function parseHtml($html): array
public function getPraGoals()
{
$cssToXpathConverter = new CssSelectorConverter();
$xpathLabelQuery = $cssToXpathConverter->toXPath('a.sensormenu.isnotpaused');
$xpathValueQuery = $cssToXpathConverter->toXPath('div.graphlabel2');
$user = $this->config->get('mhweb.user');
$password = $this->config->get('mhweb.password');
/** @var string $trProgressUri */
$trProgressUri = $this->config->get('url.mhWebPraGoals');
$xmlErrorHandling = libxml_use_internal_errors(TRUE);
$domDocument = new \DOMDocument();
$domDocument->loadHTML($html);
libxml_clear_errors();
libxml_use_internal_errors($xmlErrorHandling);
$response = $this->httpClient
->setAuth($user, $password)
->setUri($trProgressUri)
->send();
$documentXpath = new \DOMXPath($domDocument);
/** @var \DOMNodeList $element */
$element = $documentXpath->query($xpathLabelQuery);
if (!$response->isSuccess()) {
throw new \UnexpectedValueException("Bad MHWEB result", $response->getStatusCode());
}
$thing = [];
/** @var \DOMElement $item */
foreach($element as $item) {
$sensorName = trim($item->nodeValue);
if( in_array($sensorName, array_keys($this->tempSensors)) ){
/** @var \DOMNodeList $element */
$valueElement = $documentXpath->query($xpathValueQuery, $item->parentNode->parentNode);
$thing[$this->tempSensors[$sensorName]] = $valueElement->item(0)->nodeValue;
$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;
}
}
return $thing;
$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,
];
}
}