Przeglądaj źródła

BiAuditGenerate - drobne poprawki + test generowania postępu działania

Mariusz Muszyński 8 lat temu
rodzic
commit
5a895c0d5b
1 zmienionych plików z 33 dodań i 17 usunięć
  1. 33 17
      SE/se-lib/Route/UrlAction/BiAuditGenerate.php

+ 33 - 17
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -224,7 +224,7 @@ function toggle(source) {
 
 		$sqlUpdate['ID'] = $powiazanieID;
 
-		$this->truncatePowiazaniaFromDB($powiazanieID);
+		self::truncatePowiazaniaFromDB($powiazanieID);
 
 		$refPowiazaniaToPracownicy = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
 			'default_db__x3A__BI_audit_ENERGA_PRACOWNICY:BI_audit_ENERGA_PRACOWNICY');
@@ -925,7 +925,7 @@ function validateCompany(source) {
 		SE_Layout::dol();
 	}
 
-	private function getRefsTables($namespace) {
+	private static function getRefsTables($namespace) {
 		try {
 			$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($namespace, [ 'propertyName' => '*,field' ]);
 			if (!($item['hasStruct'] && $item['isStructInstalled'] && $item['isObjectActive'])) throw new Exception("Object error");
@@ -943,8 +943,8 @@ function validateCompany(source) {
 		}
 	}
 
-	private function truncatePowiazaniaFromDB($ID) {
-		$refsTables = $this->getRefsTables('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA');
+	private static function truncatePowiazaniaFromDB($ID) {
+		$refsTables = self::getRefsTables('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA');
 		if (!$refsTables) return null;
 		$refPowiazaniaToPowiazaniaRow = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
 			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
@@ -957,12 +957,12 @@ function validateCompany(source) {
 		DB::getPDO()->query($query);
 	}
 
-	private function deleteResultsFromDB($ID) {
+	private static function deleteResultsFromDB($ID) {
 		$refPowiazaniaToPowiazaniaRow = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA',
 			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
 		$refPowiazaniaRowToPowiazaniaRowObject = ACL::getRefTable('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row',
 			'default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
-		$refsTables = $this->getRefsTables('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
+		$refsTables = self::getRefsTables('default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
 
 		$sqlDeleteFrom = "delete from
 `{$refPowiazaniaToPowiazaniaRow}`,
@@ -1127,7 +1127,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 						'FILE_STATUS_info' => 'W trakcie generowania powiązań',
 					];
 					DB::getDB()->UPDATE_OBJ('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', $sqlArr);
-					$this->deleteResultsFromDB($row['ID']);
+					self::deleteResultsFromDB($row['ID']);
 
 					$generatePowiazaniaResultFile = "{$tasksDirLocation}/generatePowiazania-{$row['ID']}.result";
 					$generatePowiazaniaPhpFile = "{$tasksDirLocation}/generatePowiazania-{$row['ID']}.php";
@@ -1178,7 +1178,7 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 						'FILE_STATUS' => 'ERROR',
 						'FILE_STATUS_info' => $error,
 					];
-					$this->deleteResultsFromDB($row['ID']);
+					self::deleteResultsFromDB($row['ID']);
 					if (file_exists($xmlFile)) unlink($xmlFile);
 				} elseif ($success) {
 					$sqlArr = [
@@ -1924,6 +1924,8 @@ class BiAuditPowiazania {
 	private $relations = [];
 	private $destTables = ['BI_audit_ENERGA_RUM_KONTRAHENCI', 'BI_audit_KW_requested_person'];
 	private $refTables = [];
+	private $step = 0;
+	private $progressFile;
 
 	private function getRefTable($tableFrom, $tableTo, $returnException = true) {
 		if (!isset($this->refTables[$tableFrom][$tableTo])) {
@@ -1960,38 +1962,52 @@ class BiAuditPowiazania {
 		if (!$result) throw new Exception("Błąd danych - nie zdefinowano żadnego końcowego obiektu");
 		$this->endNodes = array_map('reset', $result);
 
+		$this->progressFile = dirname(__FILE__) . "/generatePowiazania-{$ID}.progress";
+
 		$refPowiazaniaToPracownicy = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', 'BI_audit_ENERGA_PRACOWNICY');
 		$query = "select `all`.ID as 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 = '{$ID}'";
 		$result = DB::getPDO()->fetchAll($query);
-		foreach ($result as $row) $this->findPowiazania($row['ID']);
+		$count = count($result);
+		foreach ($result as $i => $row) $this->findPowiazania($row['ID'], (($i + 1) / $count));
 	}
 
-	public function findPowiazania($ID, $relation = "", $end = 0) {
+	public function findPowiazania($ID, $progress, $relation = "", $end = 0) {
 		if (isset($this->path[$ID])) return false;
 
 		$this->path[$ID] = $relation;
 		if (!$relation) $relation = 0;
 
+		$finish = false;
+
 		if ($end) {
 			if (in_array($ID, $this->endNodes) && count($this->path) > $this->minDepth) $this->results[] = $this->path;
-			array_pop($this->path);
-			return true;
+			$finish = true;
 		}
 
 		if (count($this->path) > $this->maxDepth) {
 			if (!$this->onlyTargets) $this->results[] = $this->path;
+			$finish = true;
+		}
+
+		if ($finish) {
 			array_pop($this->path);
-			return false;
+			if (((++$this->step) % 1000) == 0) {
+				$progressJson = json_encode(['progress' => $progress, 'results' => count($this->path), 'step' => $this->step]);
+				file_put_contents($this->progressFile, $progressJson);
+			}
+			return;
 		}
 
-		$nodes = [];
+//		$nodes = [];
 		if ($relation) $where = "and ({$relation} & ref.RELATION_ID) != {$relation}";
 		else $where = "";
 		$query = "select ref.ID2, RELATION_ID, END from BI_audit_ALL_ref ref where ref.ID1 = '{$ID}' {$where}";
-		$result = DB::query($query);
-		while ($row = DB::fetch($result)) $nodes[$row->ID2] = ['relation' => (int) $row->RELATION_ID, 'end' => $row->END];
-		foreach ($nodes as $node => $data) $this->findPowiazania($node, $data['relation'], $data['end']);
+		$result = DB::getPDO()->fetchAll($query);
+//		foreach ($result as $row) $nodes[$row['ID2']] = ['relation' => (int) $row['RELATION_ID'], 'end' => $row['END']];
+//		foreach ($nodes as $node => $data) $this->findPowiazania($node, $data['relation'], $data['end']);
+		$count = count($result);
+		foreach ($result as $i => $row) $this->findPowiazania($row['ID2'], ($progress * ($i + 1) / $count), $row['RELATION_ID'], $row['END']);
 		array_pop($this->path);
 	}