Przeglądaj źródła

BiAuditAll - poprawienie pliku progress

Mariusz Muszyński 8 lat temu
rodzic
commit
06f02f6d46
1 zmienionych plików z 35 dodań i 15 usunięć
  1. 35 15
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 35 - 15
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -1971,13 +1971,17 @@ SQL;
 
 			$sqlArr = [
 				'BI_analiza_reloadCache' => 'No',
-				'FILE_STATUS_info' => 'Zakończono odświeżanie cache i wyzwolono funkcję szukania powiązań',
+				'FILE_STATUS_info' => 'Szukam powiązań',
 			];
 			DB::getPDO()->update('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', "ID", $ID, $sqlArr);
 
 			$BiAuditPowiazania = new BiAuditPowiazania($ID, $tasksDirLocation);
 			if (!$BiAuditPowiazania->powiazaniaFound()) throw new Exception("Nie znaleziono żadnych powiązań");
 			//file_put_contents($xmlFile, $BiAuditPowiazania->asXml());
+
+			$sqlArr = ['FILE_STATUS_info' => 'Generuję raporty PDF i HTML'];
+			DB::getPDO()->update('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', "ID", $ID, $sqlArr);
+			
 			$BiAuditPowiazania->saveXml();
 			$BiAuditPowiazania->generatePdfAndHtml();
 			file_put_contents($resultFile, json_encode(['result' => 'ok', 'parts' => $BiAuditPowiazania->getPartsCount()]));
@@ -2178,7 +2182,16 @@ class BiAuditPowiazania {
 	private $step = 0;
 	private $tasksDirLocation;
 	private $progressFile;
-	private $progress = [];
+	private $progress = [
+		'summary' => [
+			'count' => null,
+			'message' => 'Inicjacja',
+			'ts' => null,
+			'step' => 'initialize',
+			'created' => 0,
+		],
+		'details' => [],
+	];
 	private $lastProgress = 0;
 	private $lastResults = 0;
 	private $startTimestamp = null;
@@ -2204,23 +2217,25 @@ class BiAuditPowiazania {
 		shell_exec($cmd);
 	}
 
-	private function saveProgress($progress, $message = null) {
+	private function saveProgress($args) {
 		$timestamp = microtime(true);
-		if ($progress !== null && $this->i) {
+		if (isset($args['progress']) && $this->i) {
 			$elapsed = $timestamp - $this->startTimestamp;
-			if ($progress) {
+			if ($args['progress']) {
 				//$lastElapsed = $timestamp - $this->lastTimestamp;
 				//$lastProgress = $progress - $this->lastProgress;
 				//$estimated = (($elapsed * (1 - $progress)) / $progress + ($lastElapsed * (1 - $lastProgress)) / $lastProgress) / 2;
-				$estimated = ($elapsed * (1 - $progress)) / $progress;
+				$estimated = ($elapsed * (1 - $args['progress'])) / $args['progress'];
 			} else $estimated = "N/A";
-			$this->lastProgress = $progress;
+			$this->lastProgress = $args['progress'];
 			$this->lastTimestamp = $timestamp;
-			$progressJson = self::generateJson('progress', ['progress' => $progress, 'item' => $this->i . "/" . $this->count, 'results' => count($this->results), 'step' => $this->step, 'elapsed' => $elapsed, 'estimated' => $estimated]);
+			$progressJson = self::generateJson('progress', ['progress' => $args['progress'], 'item' => $this->i . "/" . $this->count, 'results' => count($this->results), 'step' => $this->step, 'elapsed' => $elapsed, 'estimated' => $estimated]);
 			echo $progressJson . "\n";
-			$this->progress['details'][$this->pracownikID] = ['progress' => $progress, 'results' => (count($this->results) - $this->lastResults), 'elapsed' => $elapsed, 'estimated' => $estimated];
+			$this->progress['details'][$this->pracownikID] = ['progress' => $args['progress'], 'results' => (count($this->results) - $this->lastResults), 'elapsed' => $elapsed, 'estimated' => $estimated];
 		}
-		if ($message !== null) $this->progress['summary']['message'] = $message;
+		if (isset($args['message'])) $this->progress['summary']['message'] = $args['message'];
+		if (isset($args['step'])) $this->progress['summary']['step'] = $args['step'];
+		if (isset($args['created'])) $this->progress['summary']['created'] += $args['created'];
 		$this->progress['summary']['ts'] = $timestamp;
 		file_put_contents($this->progressFile, json_encode($this->progress));
 	}
@@ -2235,6 +2250,8 @@ class BiAuditPowiazania {
 		if (!$tasksDirLocation) self::throwException("Wrong directory of tasks location");
 		$this->tasksDirLocation = $tasksDirLocation;
 
+		$this->saveProgress();
+
 		$query = "select BI_analiza_minDepth, BI_analiza_maxDepth, BI_analiza_onlyTargets from BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA where ID = '{$ID}' and FILE_STATUS = 'IN_PROGRESS' and BI_analiza_reloadCache not in ('Full')";
 		$result = DB::getPDO()->fetchAll($query);
 		if (!$result) self::throwException("Błąd danych");
@@ -2273,6 +2290,7 @@ class BiAuditPowiazania {
 		$this->count = count($result);
 		$this->progress['summary']['count'] = $this->count;
 		$this->origMaxDepth = $this->maxDepth;
+		$this->saveProgress(['step' => 'relations']);
 
 		try {
 			foreach ($result as $i => $row) {
@@ -2280,13 +2298,13 @@ class BiAuditPowiazania {
 				$this->pracownikID = DB::getPDO()->fetchValue("select `REMOTE_ID` from `BI_audit_ALL` where `ID` = :ID", [":ID" => $row['ID']]);
 				$this->startTimestamp = microtime(true);
 				$this->lastProgress = 0;
-				$this->saveProgress(0, "Wyszukiwanie powiązań dla obiektu {$this->i}/{$this->count}");
+				$this->saveProgress(['progress' => 0, 'message' => "Wyszukiwanie powiązań dla obiektu {$this->i}/{$this->count}"]);
 				if ($this->maxDepth != $this->origMaxDepth) {
 					$this->maxDepth = $this->origMaxDepth;
 					self::saveToLog("Przywrócono oryginalną maksymalną głębokość przeszukiwania ({$this->origMaxDepth})");
 				}
 				$this->findPowiazania($row['ID']);
-				$this->saveProgress(1, "Zakończono wyszukiwanie powiązań dla obiektu {$this->i}/{$this->count}");
+				$this->saveProgress(['progress' => 1, 'message' => "Zakończono wyszukiwanie powiązań dla obiektu {$this->i}/{$this->count}"]);
 				$this->lastResults = count($this->results);
 			}
 		} catch (Exception $e) {
@@ -2294,7 +2312,7 @@ class BiAuditPowiazania {
 				$this->startTimestamp = microtime(true);
 				$this->lastProgress = 0;
 				$this->lastResults = count($this->results);
-				while (++$this->i <= $this->count) $this->saveProgress(0);
+				while (++$this->i <= $this->count) $this->saveProgress(['progress' => 0]);
 			}
 			if ($message = $e->getMessage()) self::saveToLog("Wystąpił błąd podczas wyszukiwania powiązań - {$message}");
 		}
@@ -2310,7 +2328,7 @@ class BiAuditPowiazania {
 		if (count($this->results) == $this->maxResults) {
 			$message = "Przerwano wyszukiwanie powiązań - osiągnięto maksymalną liczbę wyników {$this->maxResults}";
 			self::saveToLog($message);
-			$this->saveProgress($progress, $message);
+			$this->saveProgress(['progress' => $progress, 'message' => $message]);
 			throw new Exception('');
 		}
 		$this->results[] = $this->path;
@@ -2327,7 +2345,7 @@ class BiAuditPowiazania {
 					self::saveToLog("Zbyt dużo rekurencji, zmniejszono maksymalną głębokość poszukiwań do {$this->maxDepth} dla bieżącego pracownika");
 				}
 			} else $this->lowProgressCount = 0;
-			$this->saveProgress($progress);
+			$this->saveProgress(['progress' => $progress]);
 		}
 
 		if ((($this->step) % 10000) == 0) {
@@ -2632,6 +2650,7 @@ class BiAuditPowiazania {
 	}
 
 	public function generatePdfAndHtml() {
+		$this->saveProgress(['step' => 'reports']);
 		if ($this->getPartsCount()) {
 			self::saveToLog("Generuję raporty w plikach PDF oraz HTML (liczba raportów do wygenerowania: {$this->getPartsCount()})");
 			for ($i =0; $i < $this->getPartsCount(); $i++) $this->_generatePdfAndHtml($i);
@@ -2674,5 +2693,6 @@ class BiAuditPowiazania {
 				self::saveToLog("Wygenerowano pliki HTML ({$resultsPart})");
 			} else self::saveToLog("Nie udało się wygenerować plików HTML {$resultsPart})");
 		}
+		$this->saveProgress(['created' => 1]);
 	}
 }