Status.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('UI');
  4. Lib::loadClass('Response');
  5. Lib::loadClass('SchemaFactory');
  6. class Route_Status extends RouteBase {
  7. public function defaultAction() {
  8. UI::gora();
  9. UI::startTag('div', ['class' => "container"]);
  10. echo UI::h('h1', [], [
  11. UI::h('a', ['href'=>"index.php"], "SE"),
  12. " &raquo; ",
  13. " Status systemu procesy5"
  14. ]);
  15. try {
  16. DB::getPDO();
  17. if ($postTask = V::get('_postTask', '', $_REQUEST)) {
  18. DBG::log($args, 'array', "exec '{$postTask}'");
  19. if (!method_exists($this, "{$postTask}PostTask")) throw new Exception("Post Task not exists!");
  20. ob_start();
  21. $this->{"{$postTask}PostTask"}($args);
  22. $outputPostTask = ob_get_clean();
  23. if ($outputPostTask) {
  24. echo UI::h('details', [], [
  25. UI::h('summary', [], "Wynik '{$postTask}' <i>(rozwiń)</i>"),
  26. $outputPostTask,
  27. ]);
  28. }
  29. }
  30. $this->viewStatusDatabase();
  31. if (in_array(User::get('ADM_ADMIN_LEVEL'), ['0', '1'])) {
  32. $this->viewStatusUsers();
  33. }
  34. // UI::table([
  35. // 'caption' => 'Baza danych',
  36. // 'rows' => DB::getPDO()->fetchAll(" SHOW VARIABLES ")
  37. // ]);
  38. } catch (Exception $e) {
  39. UI::alert('danger', $e->getMessage());
  40. }
  41. UI::endTag('div');// .container
  42. UI::dol();
  43. }
  44. public function viewStatusDatabase() {
  45. $dbEvents = DB::getPDO()->fetchFirst(" SHOW VARIABLES WHERE VARIABLE_NAME = 'event_scheduler' ");
  46. // DBG::nicePrint($dbEvents, '$dbEvents');
  47. // [Variable_name] => event_scheduler
  48. // [Value] => ON
  49. $aclObjectCacheExists = false;
  50. $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  51. $aclCacheTableName = $objectStorage->getRootTableName();
  52. if ($aclCacheTableName) {
  53. try {
  54. $totalObjects = DB::getPDO()->fetchValue("select count(*) as cnt from `{$aclCacheTableName}`");
  55. if ($totalObjects > 0) $aclObjectCacheExists = true;
  56. } catch (Exception $e) {
  57. DBG::log($e);
  58. }
  59. }
  60. DBG::log($objectStorage, 'array', "\$objectStorage");
  61. DBG::log($objectStorage->getRootTableName(), 'array', "\$objectStorage->getRootTableName()");
  62. UI::table([
  63. 'caption' => UI::h('b', ['style' => "color:#000"], 'Baza danych'),
  64. 'rows' => [
  65. [
  66. 'nazwa' => "Event Scheduler (generowanie Grafika, itp.)",
  67. 'wartość' => ('ON' == $dbEvents['Value'])
  68. ? UI::h('span', ['class' => "label label-success"], "ON")
  69. : UI::h('span', ['class' => "label label-danger"], "OFF"),
  70. '#' => UI::hButtonPost("Włącz", [
  71. 'class' => "btn btn-xs btn-default",
  72. 'data' => [
  73. '_postTask' => 'fixEventSheduler',
  74. ]
  75. ])
  76. ],
  77. [
  78. 'nazwa' => "System obiektów (xsd)",
  79. 'wartość' => ($aclObjectCacheExists)
  80. ? UI::h('span', ['class' => "label label-success"], "ON")
  81. : UI::h('span', ['class' => "label label-danger"], "OFF"),
  82. '#' => UI::hButtonPost("Aktualizuj cache", [
  83. 'class' => "btn btn-xs btn-default",
  84. 'data' => [
  85. '_postTask' => 'updateObjectCache'
  86. ]
  87. ])
  88. ],
  89. ]
  90. ]);
  91. }
  92. public function fixEventShedulerPostTask() {
  93. DB::getPDO()->execSql(" SET GLOBAL event_scheduler='ON' ");
  94. }
  95. public function updateObjectCachePostTask() {
  96. DBG::log("updateObjectCachePostTask...");
  97. SchemaFactory::loadDefaultObject('SystemSource')->updateCache();
  98. SchemaFactory::loadDefaultObject('SystemObject')->updateCache();
  99. SchemaFactory::loadDefaultObject('SystemObjectField')->updateCache();
  100. UI::alert('info', "Lista obiketów zaktualizowana");
  101. }
  102. public function viewStatusUsers() {
  103. $nowMinus3Months = date("Y-m-d", mktime(0,0,0, date('m') - 1, date('d'), date('Y')));
  104. $sesUsers = DB::getPDO()->fetchAll("
  105. select c.ID, c.CONF_KEY, c.CONF_VAL
  106. from CRM_CONFIG c
  107. where c.CONF_KEY like 'acl_user_%_%_cache_update'
  108. and c.CONF_VAL > '{$nowMinus3Months}'
  109. ");
  110. $sesUsers = array_reduce( $sesUsers, function ($ret, $record) {
  111. // $format = "acl_user_{ID_USERS}_{ID_PROCES}_cache_update";
  112. list($idUser, $idProces) = explode('_', substr($record['CONF_KEY'], strlen('acl_user_'), -1 * strlen('_cache_update')));
  113. if (!array_key_exists($idUser, $ret)) {
  114. $ret[$idUser] = [
  115. 'idUser' => $idUser,
  116. 'lastUpdateAclCache' => $record['CONF_VAL'],
  117. 'log' => []
  118. ];
  119. } else {
  120. $ret[$idUser]['lastUpdateAclCache'] = ($record['CONF_VAL'] > $ret[$idUser]['lastUpdateAclCache'])
  121. ? $record['CONF_VAL']
  122. : $ret[$idUser]['lastUpdateAclCache'];
  123. }
  124. $ret[$idUser]['log'][] = [ 'ID_PROCES' => $idProces, 'data' => $record['CONF_VAL'] ];
  125. return $ret;
  126. }, [] );
  127. $sesUsers = array_map(function ($sesGroup) {
  128. return [
  129. 'idUser' => $sesGroup['idUser'],
  130. 'user' => DB::getPDO()->fetchValue("select u.ADM_ACCOUNT from ADMIN_USERS u where u.ID = {$sesGroup['idUser']}"),
  131. 'lastUpdateAclCache' => $sesGroup['lastUpdateAclCache'],
  132. 'log' => UI::h('span', [ 'style' => "color:#bbb" ], implode('<br>', array_map(function ($log) {
  133. return ($log['ID_PROCES'] > 0)
  134. ? "Filtr procesu {$log['ID_PROCES']} uruchomiony {$log['data']}"
  135. : "Filtr ogólny uruchomiony {$log['data']}";
  136. }, $sesGroup['log']))),
  137. ];
  138. }, $sesUsers);
  139. usort($sesUsers, function ($ua, $ub) {
  140. $a = $ua['lastUpdateAclCache'];
  141. $b = $ub['lastUpdateAclCache'];
  142. return ($a === $b)
  143. ? 0
  144. : ( ($a < $b)
  145. ? 1
  146. : -1
  147. ) ;
  148. });
  149. if ('0' == User::get('ADM_ADMIN_LEVEL')) {
  150. $sesUsers = array_map(function ($row) {
  151. $row['#'] = UI::h('div', [ 'style' => "display:inline-block", 'class' => "activateUserDebugBtn" . ( DBG::hasUserDebug($row['idUser'], User::getID()) ? ' active' : '' ) ], [
  152. UI::h('label', ['style' => "padding-right:6px; font-weight:normal"], "Debug"),
  153. UI::hButtonAjax("Włącz", "activateUserDebug", [
  154. 'class' => "btn btn-xs btn-default activateUserDebugBtn-activate",
  155. 'href' => $this->getLink('startUserDebugAjax'),
  156. 'data' => [ 'idUser' => $row['idUser'], 'do' => 'activate' ]
  157. ]),
  158. UI::hButtonAjax("Wyłącz", "activateUserDebug", [
  159. 'class' => "btn btn-xs btn-default activateUserDebugBtn-deactivate",
  160. 'href' => $this->getLink('startUserDebugAjax'),
  161. 'data' => [ 'idUser' => $row['idUser'], 'do' => 'deactivate' ]
  162. ]),
  163. ]);
  164. return $row;
  165. }, $sesUsers);
  166. }
  167. UI::table([
  168. 'caption' => UI::h('b', ['style' => "color:#000"], "Ostatnie logowania do aplikacji") . " <small>(update acl cache)</small>", // TODO: Aktualnie zalogowani użytkownicy
  169. 'rows' => $sesUsers
  170. ]);
  171. echo UI::h('style', ['type' => "text/css"], "
  172. .activateUserDebugBtn .activateUserDebugBtn-activate { display:inline }
  173. .activateUserDebugBtn .activateUserDebugBtn-deactivate { display:none }
  174. .activateUserDebugBtn.active .activateUserDebugBtn-activate { display:none }
  175. .activateUserDebugBtn.active .activateUserDebugBtn-deactivate { display:inline }
  176. ");
  177. echo UI::h('script', ['src'=>"static/vendor.js?_v=b636cab1", 'type'=>"text/javascript"]);
  178. echo UI::h('script', [], "
  179. (function (global) {
  180. if (!global.p5VendorJs.React) throw 'Missing p5VendorJs.React'
  181. if (!global.p5VendorJs.ReactDOM) throw 'Missing p5VendorJs.ReactDOM'
  182. if (!global.p5VendorJs.ToggleButton) throw 'Missing p5VendorJs.ToggleButton'
  183. if (!global.fetch) throw 'Missing global.fetch'
  184. var React = global.p5VendorJs.React
  185. var ReactDOM = global.p5VendorJs.ReactDOM
  186. var h = React.createElement
  187. function convertToReactToggle(n, activateBtn, deactivateBtn, isActive) {
  188. var toggleReact = document.createElement('div')
  189. toggleReact.style.display = 'inline-block'
  190. n.parentNode.insertBefore(toggleReact, n.nextSibling)
  191. deactivateBtn.style.display = 'none'
  192. activateBtn.style.display = 'none'
  193. function reactToggleBtnOnToggle(value) {
  194. if (value) {
  195. deactivateBtn.onclick()
  196. } else {
  197. activateBtn.onclick()
  198. }
  199. render_reactToggleBtn(!value)
  200. }
  201. function render_reactToggleBtn(state) {
  202. ReactDOM.render(
  203. h(global.p5VendorJs.ToggleButton, {
  204. value: state,
  205. onToggle: reactToggleBtnOnToggle
  206. })
  207. , toggleReact
  208. )
  209. }
  210. render_reactToggleBtn(isActive)
  211. }
  212. var toggles = document.querySelectorAll('.activateUserDebugBtn')
  213. for (var i = 0; i < toggles.length; i++) {
  214. var btns = toggles[i].querySelectorAll('a')
  215. if (2 !== btns.length) contiune;
  216. var activateBtn = btns[0]
  217. var deactivateBtn = btns[1]
  218. convertToReactToggle(toggles[i], activateBtn, deactivateBtn, toggles[i].classList.contains('active'))
  219. }
  220. })(window)
  221. ");
  222. UI::hButtonAjaxOnResponse("activateUserDebug", /* payload, n */ "
  223. p5UI__notifyAjaxCallback(payload)
  224. // console.log('activateUserDebug :: payload', payload)
  225. if ('success' !== payload.type) return false
  226. if (payload.body && 'active' in payload.body) {
  227. if (payload.body.active) {
  228. n.parentNode.classList.add('active')
  229. } else {
  230. n.parentNode.classList.remove('active')
  231. }
  232. }
  233. ");
  234. }
  235. public function startUserDebugAjaxAction() {
  236. Response::sendTryCatchJson(array($this, 'startUserDebugAjax'), $_POST);
  237. }
  238. public function startUserDebugAjax($args) {
  239. if ('0' !== (string)User::get('ADM_ADMIN_LEVEL')) throw new Exception("Access Denied");
  240. $idUser = V::get('idUser', 0, $args);
  241. if ($idUser <= 0) throw new Exception("Missing idUser");
  242. $do = V::get('do', 0, $args);
  243. if (!in_array($do, ['activate', 'deactivate'])) throw new Exception("Missing do");
  244. $hasDebug = DBG::hasUserDebug($idUser, User::getID());
  245. switch ($do) {
  246. case 'activate': DBG::startUserDebug($idUser, User::getID()); break;
  247. case 'deactivate': DBG::stopUserDebug($idUser, User::getID()); break;
  248. }
  249. return [
  250. 'type' => 'success',
  251. '__args' => $args,
  252. '__$hasDebug' => $hasDebug,
  253. 'body' => [
  254. 'active' => DBG::hasUserDebug($idUser, User::getID())
  255. ]
  256. ];
  257. }
  258. }