Skip to content

Instantly share code, notes, and snippets.

@patpawlowski
Created March 25, 2015 16:38
Show Gist options
  • Save patpawlowski/c3c6743aafe913c7b7e7 to your computer and use it in GitHub Desktop.
Save patpawlowski/c3c6743aafe913c7b7e7 to your computer and use it in GitHub Desktop.
<?php
/*
* Be sure to set the timezone in the calling script
* date_default_timezone_set('America/Chicago');
*
* require_once('ProgressTracker.class.php');
*
*
*/
class progressTracker{
private $StartDateTime = '';
private $TotalCount = 0;
private $Errors = array();
public function __construct($TotalCount = 0)
{
echo date(DATE_W3C)." starting . . .\n";
$this->TotalCount = $TotalCount;
$this->StartDateTime = new DateTime();
$this->update();
}
public function setTotalCount($TotalCount = 0)
{
$this->TotalCount = $TotalCount;
echo "Total to be processed:".$TotalCount."\n";
}
public function logError($Error)
{
array_push($this->Errors, $Error);
}
public function update($Message = '', $Processed = 0)
{
//echo "\n\n";
$Now = new DateTime();
$Duration = date_diff($this->StartDateTime, $Now, true);
$RecordsPerMinute = 0;
$EstimatedTimeLeft = 0;
$ETLHours = 0;
$ETLDays = 0;
$EstimatedFinishTime = new DateTime();
echo $Duration->format("\tYears:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s \n");
if ($this->TotalCount)
{
$PercentDone = round($Processed*100/$this->TotalCount);
$RecordsLeft = $this->TotalCount - $Processed;
echo "\t".$PercentDone."% completed, ".$RecordsLeft." records remaining, ".$Processed." records processed\n";
$TotalMinutes = $Duration->i + $Duration->h * 60 + $Duration->d * 1440 + $Duration->s / 60;
$TotalMinutes = round($TotalMinutes);
if ($TotalMinutes > 0)
{
$RecordsPerMinute = round($Processed/$TotalMinutes);
$EstimatedFinishTime->add(new DateInterval('PT'.$TotalMinutes.'M'));
}
if ($RecordsPerMinute > 0) $EstimatedTimeLeft = round($RecordsLeft/$RecordsPerMinute);
if ($EstimatedTimeLeft > 0) $ETLHours = round($EstimatedTimeLeft/60, 2);
if ($ETLHours > 0) $ETLDays = round($ETLDays/24, 2);
echo "\t".$RecordsPerMinute." records per minute, ".$EstimatedTimeLeft." minutes or ".$ETLHours." hours or ".$ETLDays." days left\n";
echo "\tEstimated Completion Time: ".$EstimatedFinishTime->format('Y-m-d H:i:s')."\n";
}
if ($Message)
{
echo $Message."\n";
}
echo "\n";
}
public function __destruct()
{
echo date(DATE_W3C)." finished . . .\nTotal Duration:\n";
$Now = new DateTime();
$Duration = date_diff($this->StartDateTime, $Now, true);
echo $Duration->format("Years:%Y,Months:%M,Days:%d,Hours:%H,Minutes:%i,Seconds:%s \n");
echo count($this->Errors)." errors\n";
print_r($this->Errors);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment