Skip to content

Commit

Permalink
fix: refactor and move format function to Date helper, pass deliveryE…
Browse files Browse the repository at this point in the history
…xecution->getFinishTime() on delivery finish
  • Loading branch information
Karol-Stelmaczonek committed Nov 24, 2023
1 parent e4d180e commit a9c8c4a
Showing 1 changed file with 3 additions and 29 deletions.
32 changes: 3 additions & 29 deletions models/Import/Service/SendCalculatedResultService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
namespace oat\taoResultServer\models\Import\Service;

use common_exception_Error;
use DateTimeInterface;
use DateTimeZone;
use oat\oatbox\event\EventManager;
use oat\oatbox\service\exception\InvalidServiceManagerException;
use oat\taoDelivery\model\execution\DeliveryExecutionService;
Expand All @@ -34,6 +32,7 @@
use taoResultServer_models_classes_ReadableResultStorage as ReadableResultStorage;
use taoResultServer_models_classes_Variable as ResultVariable;
use taoResultServer_models_classes_OutcomeVariable as OutcomeVariable;
use tao_helpers_Date as DateHelper;

class SendCalculatedResultService
{
Expand Down Expand Up @@ -68,10 +67,10 @@ public function sendByDeliveryExecutionId(string $deliveryExecutionId): array

$isFullyGraded = $this->checkIsFullyGraded($deliveryExecutionId, $outcomeVariables);

$timestamp = $this->formatTime($deliveryExecution->getFinishTime());
$timestamp = DateHelper::formatMicrotime($deliveryExecution->getFinishTime());

if ($isFullyGraded) {
$timestamp = $this->formatTime($this->getLatestOutcomesTimestamp($outcomeVariables));
$timestamp = DateHelper::formatMicrotime($this->getLatestOutcomesTimestamp($outcomeVariables));
}

$this->eventManager->trigger(
Expand Down Expand Up @@ -209,31 +208,6 @@ private function getLatestOutcomesTimestamp(array $outcomeVariables): ?string
return array_pop($sortedMicrotime);
}

/**
* Converts from microseconds seconds format to readable by Carbon seconds microseconds
* @param string|null $time
* @return string|null
* @example 0.47950700 1700135696 to 1700135696.47950700
*/
private function formatTime(?string $time): ?string
{
if ($time === null) {
return null;
}

// Split the string into microseconds and seconds
list($microseconds, $seconds) = explode(' ', $time);

// Show only the numbers after the dot without the integral part
list(, $decimalPart) = explode('.', sprintf('%0.6f', $microseconds));
//To preserve time zone we are not using createFromFormat()
$date = new \DateTime();
$date->setTimestamp((int)$seconds);
$date->modify('+ ' . $decimalPart . ' microseconds');

return $date->format(DateTimeInterface::RFC3339_EXTENDED);
}

private function sortMicrotimeList(array $microtimeList): array
{
usort($microtimeList, function ($a, $b) {
Expand Down

0 comments on commit a9c8c4a

Please sign in to comment.