Bladeren bron

BiAudit reverse search

Mariusz Muszyński 8 jaren geleden
bovenliggende
commit
29e675d2f9
1 gewijzigde bestanden met toevoegingen van 42 en 18 verwijderingen
  1. 42 18
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 42 - 18
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -2205,7 +2205,9 @@ class BiAuditPowiazania {
 	private $splittedResults = [];
 	private $maxResults = 1000000;
 	private $fidRow = [];
-	private $pracownikID = 0;
+	private $itemID = 0;
+	private $reverseSearch = false;
+	private $query = "";
 
 	private static function generateJson($type, $data) {
 		return json_encode(['ts' => microtime(true), 'type' => $type, 'data' => $data]);
@@ -2234,7 +2236,7 @@ class BiAuditPowiazania {
 			$this->lastTimestamp = $timestamp;
 			$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' => $args['progress'], 'results' => (count($this->results) - $this->lastResults), 'elapsed' => $elapsed, 'estimated' => $estimated];
+			$this->progress['details'][$this->itemID] = ['progress' => $args['progress'], 'results' => (count($this->results) - $this->lastResults), 'elapsed' => $elapsed, 'estimated' => $estimated];
 		}
 		if (isset($args['message'])) $this->progress['summary']['message'] = $args['message'];
 		if (isset($args['step'])) $this->progress['summary']['step'] = $args['step'];
@@ -2281,16 +2283,29 @@ class BiAuditPowiazania {
 			$subQueries[] = "select `all`.ID from BI_audit_ALL `all` join `{$refTable}` ref on `all`.REMOTE_TABLE = '{$destTable}' and `all`.REMOTE_ID = ref.REMOTE_PRIMARY_KEY and ref.PRIMARY_KEY = '{$this->ID}'";
 		}
 		$query = implode(" union ", $subQueries);
-		$result = DB::getPDO()->fetchAll($query);
-		if (!$result) self::throwException("Błąd danych - nie zdefinowano żadnego końcowego obiektu");
-		$this->endNodes = array_map('reset', $result);
-
-		self::saveToLog("Rozpoczynam wyszukiwanie powiązań");
+		$resultDst = DB::getPDO()->fetchAll($query);
+		if (!$resultDst) self::throwException("Błąd danych - nie zdefinowano żadnego końcowego obiektu");
 
 		$refPowiazaniaToPracownicy = BiAuditRefTables::getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', 'BI_audit_ENERGA_PRACOWNICY', true);
 		$query = "select `all`.`ID` from `BI_audit_ALL` `all` join `{$refPowiazaniaToPracownicy}` `ref`
 			on `all`.`REMOTE_TABLE` = 'BI_audit_ENERGA_PRACOWNICY' and `all`.`REMOTE_ID` = `ref`.`REMOTE_PRIMARY_KEY` and `ref`.`PRIMARY_KEY` = '{$this->ID}' order by `all`.`ID` asc";
-		$result = DB::getPDO()->fetchAll($query);
+		$resultSrc = DB::getPDO()->fetchAll($query);
+		if (!$resultSrc) self::throwException("Błąd danych - nie zdefinowano żadnego początkowego obiektu");
+
+		if (count($resultSrc) >= count($resultDst)) {
+			$result = $resultSrc;
+			$this->endNodes = array_map('reset', $resultDst);
+			$this->query = "select ref.ID2 as ID, RELATION_ID from BI_audit_ALL_ref ref where ref.ID1";
+		} else {
+			$result = $resultDst;
+			$this->endNodes = array_map('reset', $resultSrc);
+			$this->reverseSearch = true;
+			$this->query = "select ref.ID1 as ID, RELATION_ID from BI_audit_ALL_ref ref where ref.ID2";
+			self::saveToLog("Docelowych obiektów jest mniej, niż początkowych - włączono mechanizm odwrotnego przeszukiwania");
+		}
+
+		self::saveToLog("Rozpoczynam wyszukiwanie powiązań");
+
 		$this->count = count($result);
 		$this->progress['summary']['count'] = $this->count;
 		$this->origMaxDepth = $this->maxDepth;
@@ -2299,7 +2314,7 @@ class BiAuditPowiazania {
 		try {
 			foreach ($result as $i => $row) {
 				$this->i = $i + 1;
-				$this->pracownikID = DB::getPDO()->fetchValue("select `REMOTE_ID` from `BI_audit_ALL` where `ID` = :ID", [":ID" => $row['ID']]);
+				$this->itemID = 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(['progress' => 0, 'message' => "Wyszukiwanie powiązań dla obiektu {$this->i}/{$this->count}"]);
@@ -2328,6 +2343,13 @@ class BiAuditPowiazania {
 		$this->splitResults();
 	}
 
+	private static function reversePath($path) {
+		$arr = array_reverse($path, true);
+		$keys = array_keys($arr);
+		array_unshift($arr, array_pop($arr));
+		return array_combine($keys, $arr);
+	}
+
 	private function addResult($progress) {
 		if (count($this->results) == $this->maxResults) {
 			$message = "Przerwano wyszukiwanie powiązań - osiągnięto maksymalną liczbę wyników {$this->maxResults}";
@@ -2335,18 +2357,20 @@ class BiAuditPowiazania {
 			$this->saveProgress(['progress' => $progress, 'message' => $message]);
 			throw new Exception('');
 		}
-		$this->results[] = $this->path;
-		$this->saveToDb($this->path);           
+		if ($this->reverseSearch) $path = self::reversePath($this->path);
+		else $path = $this->path;
+		$this->results[] = $path;
+		$this->saveToDb($path);           
 	}
 
-	public function findPowiazania($ID, $progress = 0, $steps = 1, $relation = "", $end = 0) {
+	public function findPowiazania($ID, $progress = 0, $steps = 1, $relation = "") {
 		if (((++$this->step) % 1000000) == 0) {
 			$progressDiff = $progress - $this->lastProgress;
 			if ($progressDiff < 0.00005) {
 				$this->lowProgressCount++;
 				if ($this->lowProgressCount == 10) {
 					$this->maxDepth--;
-					self::saveToLog("Zbyt dużo rekurencji, zmniejszono maksymalną głębokość poszukiwań do {$this->maxDepth} dla bieżącego pracownika");
+					self::saveToLog("Zbyt dużo rekurencji, zmniejszono maksymalną głębokość poszukiwań do {$this->maxDepth} dla bieżącego obiektu");
 				}
 			} else $this->lowProgressCount = 0;
 		}
@@ -2367,8 +2391,8 @@ class BiAuditPowiazania {
 		$this->path[$ID] = $relation;
 		if (!$relation) $relation = 0;
 
-		if ($end) {
-			if (in_array($ID, $this->endNodes) && count($this->path) > $this->minDepth) $this->addResult($progress);
+		if (in_array($ID, $this->endNodes)) {
+			if (count($this->path) > $this->minDepth) $this->addResult($progress);
 			array_pop($this->path);
 			return;
 		}
@@ -2381,15 +2405,15 @@ class BiAuditPowiazania {
 
 		if (!isset($this->nodes[$ID][$relation])) {
 			$where = $relation ? "and ({$relation} & ref.RELATION_ID) != {$relation}" : "";
-			$query = "select ref.ID2, RELATION_ID, END from BI_audit_ALL_ref ref where ref.ID1 = {$ID} {$where}";
+			$query = "{$this->query} = {$ID} {$where}";
 			$this->nodes[$ID][$relation] = DB::getPDO()->fetchAll($query);
 		}
 		$nodes = array_values(array_filter($this->nodes[$ID][$relation], function ($node) {
-			if (isset($this->path[$node['ID2']])) return false;
+			if (isset($this->path[$node['ID']])) return false;
 			return true;
 		}));
 		$count = count($nodes);
-		foreach ($nodes as $i => $node) $this->findPowiazania($node['ID2'], ($progress + ($i / ($count * $steps))), ($count * $steps), $node['RELATION_ID'], $node['END']);
+		foreach ($nodes as $i => $node) $this->findPowiazania($node['ID'], ($progress + ($i / ($count * $steps))), ($count * $steps), $node['RELATION_ID']);
 
 		array_pop($this->path);
 	}