Переглянути джерело

Poprawki do BiAuditGenerate

Mariusz Muszyński 8 роки тому
батько
коміт
d64960d976

+ 80 - 31
SE/se-lib/Route/UrlAction/BiAuditGenerate.php

@@ -1780,7 +1780,6 @@ Router::getRoute("UrlAction_BiAuditGenerate")->' . $function . ';
 			$BiAuditPowiazania = new BiAuditPowiazania($ID, $tasksDirLocation);
 			$BiAuditPowiazania = new BiAuditPowiazania($ID, $tasksDirLocation);
 			if (!$BiAuditPowiazania->powiazaniaFound()) throw new Exception("Nie znaleziono żadnych powiązań");
 			if (!$BiAuditPowiazania->powiazaniaFound()) throw new Exception("Nie znaleziono żadnych powiązań");
 			file_put_contents($xmlFile, $BiAuditPowiazania->asXml());
 			file_put_contents($xmlFile, $BiAuditPowiazania->asXml());
-			$BiAuditPowiazania->saveToDb();
 			$BiAuditPowiazania->generatePdfAndHtml();
 			$BiAuditPowiazania->generatePdfAndHtml();
 			file_put_contents($resultFile, "ok");
 			file_put_contents($resultFile, "ok");
 		} catch (Exception $e) {
 		} catch (Exception $e) {
@@ -1946,37 +1945,50 @@ class BiAuditPowiazania {
 	}
 	}
 
 
 	private function generateJson($type, $data) {
 	private function generateJson($type, $data) {
-		return json_encode(['ts' => microtime(true), 'type' => $type, 'item' => "{$this->i}/{$this->count}", 'data' => $data]);
+		return json_encode(['ts' => microtime(true), 'type' => $type, 'data' => $data]);
 	}
 	}
 
 
 	private function saveToLog($message) {
 	private function saveToLog($message) {
 		$messageJson = $this->generateJson('message', ['message' => $message]);
 		$messageJson = $this->generateJson('message', ['message' => $message]);
 		echo $messageJson . "\n";
 		echo $messageJson . "\n";
 	}
 	}
+
 	private function saveProgress($progress) {
 	private function saveProgress($progress) {
 		$this->lastProgress = $progress;
 		$this->lastProgress = $progress;
 		$timestamp = microtime(true);
 		$timestamp = microtime(true);
 		if ($progress) $estimated = ($timestamp - $this->startTimestamp) / $progress;
 		if ($progress) $estimated = ($timestamp - $this->startTimestamp) / $progress;
 		else $estimated = "N/A";
 		else $estimated = "N/A";
-		$progressJson = $this->generateJson('progress', ['progress' => $progress, 'results' => count($this->results), 'step' => $this->step, 'estimated' => $estimated]);
+		$progressJson = $this->generateJson('progress', ['progress' => $progress, 'item' => ($this->i + 1) . "/" . $this->count, 'results' => count($this->results), 'step' => $this->step, 'estimated' => $estimated]);
 		echo $progressJson . "\n";
 		echo $progressJson . "\n";
 		file_put_contents($this->progressFile, $progressJson);
 		file_put_contents($this->progressFile, $progressJson);
 	}
 	}
 
 
+	private function throwException($message) {
+		$this->saveToLog($message);
+		throw new Exception($message);
+	}
+
 	public function __construct($ID = 0, $tasksDirLocation = null) {
 	public function __construct($ID = 0, $tasksDirLocation = null) {
-		if (!$ID) throw new Exception("Wrong ID parameter");
-		if (!$tasksDirLocation) throw new Exception("Wrong directory of tasks location");
+		if (!$ID) $this->throwException("Wrong ID parameter");
+		if (!$tasksDirLocation) $this->throwException("Wrong directory of tasks location");
 		$this->tasksDirLocation = $tasksDirLocation;
 		$this->tasksDirLocation = $tasksDirLocation;
 
 
 		$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', 'Part')";
 		$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', 'Part')";
 		$result = DB::getPDO()->fetchAll($query);
 		$result = DB::getPDO()->fetchAll($query);
-		if (!$result) throw new Exception("Błąd danych");
+		if (!$result) $this->throwException("Błąd danych");
+
+		if ($this->loadResults()) {
+			$this->saveToLog("Wczytano wcześnie wyliczone dane");
+			return;
+		}
+
+		$this->saveToLog("Wczytuję parametry wyszukiwania powiązań");
 		$this->minDepth = (int) $result[0]['BI_analiza_minDepth'];
 		$this->minDepth = (int) $result[0]['BI_analiza_minDepth'];
 		$this->maxDepth = (int) $result[0]['BI_analiza_maxDepth'];
 		$this->maxDepth = (int) $result[0]['BI_analiza_maxDepth'];
 		$this->onlyTargets = ($result[0]['BI_analiza_onlyTargets'] != 'N');
 		$this->onlyTargets = ($result[0]['BI_analiza_onlyTargets'] != 'N');
 		if (!$this->minDepth) $this->minDepth = 1;
 		if (!$this->minDepth) $this->minDepth = 1;
-		if (!$this->maxDepth) throw new Exception("Błąd danych - nieokreślono maksymalnej głębokości analizy");
-		if ($this->minDepth > $this->maxDepth) throw new Exception("Wartość minimalnej głębokości analizy jest większa od wartości maksymalnej głębokości analizy");
+		if (!$this->maxDepth) $this->throwException("Błąd danych - nieokreślono maksymalnej głębokości analizy");
+		if ($this->minDepth > $this->maxDepth) $this->throwException("Wartość minimalnej głębokości analizy jest większa od wartości maksymalnej głębokości analizy");
 		$this->ID = $ID;
 		$this->ID = $ID;
 
 
 		$subQueries = [];
 		$subQueries = [];
@@ -1986,11 +1998,12 @@ class BiAuditPowiazania {
 		}
 		}
 		$query = implode(" union ", $subQueries);
 		$query = implode(" union ", $subQueries);
 		$result = DB::getPDO()->fetchAll($query);
 		$result = DB::getPDO()->fetchAll($query);
-		if (!$result) throw new Exception("Błąd danych - nie zdefinowano żadnego końcowego obiektu");
+		if (!$result) $this->throwException("Błąd danych - nie zdefinowano żadnego końcowego obiektu");
 		$this->endNodes = array_map('reset', $result);
 		$this->endNodes = array_map('reset', $result);
 
 
-		$this->progressFile = "{$this->tasksDirLocation}/generatePowiazania-{$ID}.progress";
+		$this->saveToLog("Rozpoczynam wyszukiwanie powiązań");
 
 
+		$this->progressFile = "{$this->tasksDirLocation}/generatePowiazania-{$ID}.progress";
 		$refPowiazaniaToPracownicy = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', 'BI_audit_ENERGA_PRACOWNICY');
 		$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
 		$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}' order by `all`.`ID` asc";
 			on `all`.REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY' and `all`.REMOTE_ID = ref.REMOTE_PRIMARY_KEY and ref.PRIMARY_KEY = '{$ID}' order by `all`.`ID` asc";
@@ -2003,11 +2016,14 @@ class BiAuditPowiazania {
 				$this->maxDepth = $this->origMaxDepth;
 				$this->maxDepth = $this->origMaxDepth;
 				$this->saveToLog("Przywrócono oryginalną maksymalną głębokość przeszukiwania ({$this->origMaxDepth})");
 				$this->saveToLog("Przywrócono oryginalną maksymalną głębokość przeszukiwania ({$this->origMaxDepth})");
 			}
 			}
-			//$this->findPowiazania($row['ID'], ($i / $count), $count);
 			$this->findPowiazania($row['ID']);
 			$this->findPowiazania($row['ID']);
-			
 			$this->saveProgress(1);
 			$this->saveProgress(1);
 		}
 		}
+		$this->nodes = [];
+		$this->saveToLog("Zakończono wyszukiwanie powiązań");
+		$this->saveResults();
+		$this->saveToLog("Zapisano wyliczone dane do pliku");
+		$this->saveToDb();
 	}
 	}
 
 
 	public function findPowiazania($ID, $progress = 0, $steps = 1, $relation = "", $end = 0) {
 	public function findPowiazania($ID, $progress = 0, $steps = 1, $relation = "", $end = 0) {
@@ -2018,7 +2034,7 @@ class BiAuditPowiazania {
 
 
 		if (((++$this->step) % 1000000) == 0) {
 		if (((++$this->step) % 1000000) == 0) {
 			$progressDiff = $progress - $this->lastProgress;
 			$progressDiff = $progress - $this->lastProgress;
-			$this->saveToLog("TEST: " . (number_format($progress - $this->lastProgress, 20)));
+//			$this->saveToLog("TEST: " . (number_format($progress - $this->lastProgress, 20)));
 			if ($progressDiff < 0.00005) {
 			if ($progressDiff < 0.00005) {
 				$this->lowProgressCount++;
 				$this->lowProgressCount++;
 				if ($this->lowProgressCount == 10) {
 				if ($this->lowProgressCount == 10) {
@@ -2050,34 +2066,51 @@ class BiAuditPowiazania {
 			return;
 			return;
 		}
 		}
 
 
-/*
-//		$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::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 / ($count * $steps))), ($count * $steps), $row['RELATION_ID'], $row['END']);
-*/
-
 		if (!isset($this->nodes[$ID][$relation])) {
 		if (!isset($this->nodes[$ID][$relation])) {
 			if ($relation) $where = "and ({$relation} & ref.RELATION_ID) != {$relation}";
 			if ($relation) $where = "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 = "select ref.ID2, RELATION_ID, END from BI_audit_ALL_ref ref where ref.ID1 = {$ID} {$where}";
 			$this->nodes[$ID][$relation] = DB::getPDO()->fetchAll($query);
 			$this->nodes[$ID][$relation] = DB::getPDO()->fetchAll($query);
 		}
 		}
 		$nodes = array_values(array_filter($this->nodes[$ID][$relation], function ($node) {
 		$nodes = array_values(array_filter($this->nodes[$ID][$relation], function ($node) {
-//			if ($relation && (($relation & $node['RELATION_ID']) == $relation)) return false;
 			if (isset($this->path[$node['ID2']])) return false;
 			if (isset($this->path[$node['ID2']])) return false;
 			return true;
 			return true;
 		}));
 		}));
-		$count = count($nodes);// $i = 0;
+		$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['ID2'], ($progress + ($i / ($count * $steps))), ($count * $steps), $node['RELATION_ID'], $node['END']);
 
 
 		array_pop($this->path);
 		array_pop($this->path);
 	}
 	}
 
 
+	private function saveResults() {
+		$this->saveToLog("Zapisuję wyliczone dane do pliku");
+		if (!$this->results) {
+			$this->saveToLog("Brak wyliczonych danych - niczego nie zapisaono");
+			return false;
+		}
+		$dataFile = "{$this->tasksDirLocation}/generatePowiazania-{$this->ID}.data";
+		$data = base64_encode(gzcompress(json_encode($this->results)));
+		file_put_contents($dataFile, $data);
+		$this->saveToLog("Zapisano wyliczone dane do pliku");
+		return true;
+	}
+
+	private function loadResults() {
+		$this->saveToLog("Próbuję wczytać wcześniej wyliczone dane");
+		$dataFile = "{$this->tasksDirLocation}/generatePowiazania-{$this->ID}.data";
+		if (!file_exists($dateFile)) {
+			$this->saveToLog("Nie znaleziono pliku z wyliczonymi danymi");
+			return false;
+		}
+		$results = @json_decode(gzuncompress(base64_decode($data)), true);
+		if (!$results) {
+			$this->saveToLog("Wystąpił błąd wczytywania wcześniej wyliczonych danych");
+			return false;
+		}
+		$this->results = $results;
+		$this->saveToLog("Wczytano wcześniej wyliczone dane");
+		return true;
+	}
+
 	private function relationName($ID) {
 	private function relationName($ID) {
 		if (!isset($this->relations[$ID])) {
 		if (!isset($this->relations[$ID])) {
 			$query = "select RELATION from BI_audit_ALL_ref_RELATIONS where ID & '{$ID}' order by ID";
 			$query = "select RELATION from BI_audit_ALL_ref_RELATIONS where ID & '{$ID}' order by ID";
@@ -2089,6 +2122,7 @@ class BiAuditPowiazania {
 	}
 	}
 
 
 	private function generateItemsResults() {
 	private function generateItemsResults() {
+		$this->saveToLog("Generuję dane na potrzeby utworzenia pliku XML");
 		$xmlRoot2 = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA";
 		$xmlRoot2 = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA";
 		$xmlElements = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row";
 		$xmlElements = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row";
 
 
@@ -2151,10 +2185,15 @@ class BiAuditPowiazania {
 		}
 		}
 		$this->items_results[$xmlRoot2]['BI_audit_ENERGA_RUM_KONTRAHENCI'] = $items_kontrahenci;
 		$this->items_results[$xmlRoot2]['BI_audit_ENERGA_RUM_KONTRAHENCI'] = $items_kontrahenci;
 		$this->items_results[$xmlRoot2]['BI_audit_KW_requested_person'] = $items_kw_person;
 		$this->items_results[$xmlRoot2]['BI_audit_KW_requested_person'] = $items_kw_person;
+		$this->saveToLog("Wygenerowano dane na potrzeby utworzenia pliku XML");
 	}
 	}
 
 
-	public function saveToDb() {
-		if (!$this->results) return null;
+	private function saveToDb() {
+		$this->saveToLog("Zapisuję wyliczone dane do bazy");
+		if (!$this->results) {
+			$this->saveToLog("Brak wyliczonych danych - nie zapisaono niczego do bazy");
+			return null;
+		}
 		$refPowiazaniaToPowiazaniaRow = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', 'BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
 		$refPowiazaniaToPowiazaniaRow = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA', 'BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row');
 		$refPowiazaniaRowToPowiazaniaRowObject = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row', 'BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
 		$refPowiazaniaRowToPowiazaniaRowObject = $this->getRefTable('BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row', 'BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object');
 		foreach ($this->results as $result) {
 		foreach ($this->results as $result) {
@@ -2169,13 +2208,16 @@ class BiAuditPowiazania {
 				DB::getPDO()->insert($ref, ['PRIMARY_KEY' => $idPowiazaniaRowObject, 'REMOTE_PRIMARY_KEY' => $object['REMOTE_ID']]);
 				DB::getPDO()->insert($ref, ['PRIMARY_KEY' => $idPowiazaniaRowObject, 'REMOTE_PRIMARY_KEY' => $object['REMOTE_ID']]);
 			}
 			}
 		}
 		}
+		$this->saveToLog("Zapisano wyliczone dane do bazy");
 	}
 	}
 
 
 	public function asXml() {
 	public function asXml() {
+		$this->saveToLog("Tworzę plik XML");
 		if (!$this->results) return null;
 		if (!$this->results) return null;
 		if (!$this->items_results) $this->generateItemsResults();
 		if (!$this->items_results) $this->generateItemsResults();
 		$xmlRoot = "RelatedFeatureRoot";
 		$xmlRoot = "RelatedFeatureRoot";
 		return V::arrayToXML($this->items_results, true, $xmlRoot);
 		return V::arrayToXML($this->items_results, true, $xmlRoot);
+		$this->saveToLog("Utworzono plik XML");
 	}
 	}
 
 
 	public function asArray($subArray = null) {
 	public function asArray($subArray = null) {
@@ -2206,15 +2248,22 @@ class BiAuditPowiazania {
 		if (!is_dir($antDir)) $sqlArr['FILE_STATUS_info'] .= ", ale nie udało się utworzyć pliku PDF";
 		if (!is_dir($antDir)) $sqlArr['FILE_STATUS_info'] .= ", ale nie udało się utworzyć pliku PDF";
 		else {
 		else {
 			copy($xmlFile, $antXmlFile);
 			copy($xmlFile, $antXmlFile);
+			$this->saveToLog("Tworzę raport w pliku PDF oraz HTML");
 			shell_exec("cd /Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree && /opt/local/bin/ant -f build_CRM_PROCES_tree.xml default_db:PROCES_INIT:tree:dita -Duuid=relations-{$this->ID}");
 			shell_exec("cd /Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree && /opt/local/bin/ant -f build_CRM_PROCES_tree.xml default_db:PROCES_INIT:tree:dita -Duuid=relations-{$this->ID}");
+			$this->saveToLog("Tworzę plik PDF");
 			shell_exec("cd \"/Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/temp/relations-{$this->ID}\" && /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3/bin/dita -o pdf -i relations-{$this->ID}.ditamap -f pdf");
 			shell_exec("cd \"/Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/temp/relations-{$this->ID}\" && /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3/bin/dita -o pdf -i relations-{$this->ID}.ditamap -f pdf");
-			if (file_exists($pdfFile)) rename($pdfFile, $pdfDestFile);
+			if (file_exists($pdfFile)) {
+				rename($pdfFile, $pdfDestFile);
+				$this->saveToLog("Utworzono plik PDF");
+			} $this->saveToLog("Nie udało się utworzyć pliku PDF");
+			$this->saveToLog("Tworzę pliki HTML");
 			shell_exec("cd \"/Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/temp/relations-{$this->ID}\" && /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3/bin/dita -o html -i relations-{$this->ID}.ditamap -f tocjs");
 			shell_exec("cd \"/Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/temp/relations-{$this->ID}\" && /Library/Server/Web/Data/Sites/Default/SE/stuff/dita-ot-2.3.3/bin/dita -o html -i relations-{$this->ID}.ditamap -f tocjs");
 			if (file_exists($htmlDir) && is_dir($htmlDir)) {
 			if (file_exists($htmlDir) && is_dir($htmlDir)) {
 				shell_exec("cd \"/Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/temp/relations-{$this->ID}\" && zip -r \"{$htmlZipDestFile}\" html");
 				shell_exec("cd \"/Library/Server/Web/Data/Sites/Default/SE/schema/WPS_Functions/default_db/CRM_PROCES_tree/temp/relations-{$this->ID}\" && zip -r \"{$htmlZipDestFile}\" html");
 				if (file_exists($htmlDestDir)) shell_exec("rm -rf \"{$htmlDestDir}\"");
 				if (file_exists($htmlDestDir)) shell_exec("rm -rf \"{$htmlDestDir}\"");
 				shell_exec("mv \"{$htmlDir}\" \"{$htmlDestDir}\"");
 				shell_exec("mv \"{$htmlDir}\" \"{$htmlDestDir}\"");
-			}
+				$this->saveToLog("Utworono pliki HTML");
+			} else $this->saveToLog("Nie udało się utworzyć plików HTML");
 		}
 		}
 	}
 	}
 }
 }

+ 12 - 0
SE/stuff/scripts/script.reload_cache_part.php

@@ -0,0 +1,12 @@
+#!/usr/bin/env php
+<?php
+error_reporting(E_ALL & ~E_NOTICE);
+$_SERVER['SERVER_NAME'] = gethostname();
+//require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
+require("/Library/Server/Web/Data/Sites/Default/SE/se-lib/bootstrap.php");
+date_default_timezone_set('Europe/Warsaw');
+
+Lib::loadClass('RouteBase');
+
+Lib::loadClass('Router');
+Router::getRoute('UrlAction_BiAuditGenerate')->doReloadCache();