StatusController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Server\Status\StatusController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin\Controllers\Server\Status;
  10. use PhpMyAdmin\ReplicationGui;
  11. use PhpMyAdmin\Util;
  12. use Throwable;
  13. use Twig_Error_Loader;
  14. use Twig_Error_Runtime;
  15. use Twig_Error_Syntax;
  16. /**
  17. * Class StatusController
  18. * @package PhpMyAdmin\Controllers\Server\Status
  19. */
  20. class StatusController extends AbstractController
  21. {
  22. /**
  23. * @param ReplicationGui $replicationGui ReplicationGui instance
  24. *
  25. * @return string
  26. * @throws Throwable
  27. * @throws Twig_Error_Loader
  28. * @throws Twig_Error_Runtime
  29. * @throws Twig_Error_Syntax
  30. */
  31. public function index(ReplicationGui $replicationGui): string
  32. {
  33. global $replication_info;
  34. $traffic = [];
  35. $connections = [];
  36. $replication = '';
  37. if ($this->data->dataLoaded) {
  38. $networkTraffic = implode(
  39. ' ',
  40. Util::formatByteDown(
  41. $this->data->status['Bytes_received'] + $this->data->status['Bytes_sent'],
  42. 3,
  43. 1
  44. )
  45. );
  46. $uptime = Util::timespanFormat($this->data->status['Uptime']);
  47. $startTime = Util::localisedDate($this->getStartTime());
  48. $traffic = $this->getTrafficInfo();
  49. $connections = $this->getConnectionsInfo();
  50. // display replication information
  51. if ($replication_info['master']['status']
  52. || $replication_info['slave']['status']
  53. ) {
  54. $replication = $this->getReplicationInfo($replicationGui);
  55. }
  56. }
  57. return $this->template->render('server/status/status/index', [
  58. 'is_data_loaded' => $this->data->dataLoaded,
  59. 'network_traffic' => $networkTraffic ?? null,
  60. 'uptime' => $uptime ?? null,
  61. 'start_time' => $startTime ?? null,
  62. 'traffic' => $traffic,
  63. 'connections' => $connections,
  64. 'is_master' => $replication_info['master']['status'],
  65. 'is_slave' => $replication_info['slave']['status'],
  66. 'replication' => $replication,
  67. ]);
  68. }
  69. /**
  70. * @return int
  71. */
  72. private function getStartTime(): int
  73. {
  74. return (int) $this->dbi->fetchValue(
  75. 'SELECT UNIX_TIMESTAMP() - ' . $this->data->status['Uptime']
  76. );
  77. }
  78. /**
  79. * @return array
  80. */
  81. private function getTrafficInfo(): array
  82. {
  83. $hourFactor = 3600 / $this->data->status['Uptime'];
  84. return [
  85. [
  86. 'name' => __('Received'),
  87. 'number' => implode(
  88. ' ',
  89. Util::formatByteDown(
  90. $this->data->status['Bytes_received'],
  91. 3,
  92. 1
  93. )
  94. ),
  95. 'per_hour' => implode(
  96. ' ',
  97. Util::formatByteDown(
  98. $this->data->status['Bytes_received'] * $hourFactor,
  99. 3,
  100. 1
  101. )
  102. ),
  103. ],
  104. [
  105. 'name' => __('Sent'),
  106. 'number' => implode(
  107. ' ',
  108. Util::formatByteDown(
  109. $this->data->status['Bytes_sent'],
  110. 3,
  111. 1
  112. )
  113. ),
  114. 'per_hour' => implode(
  115. ' ',
  116. Util::formatByteDown(
  117. $this->data->status['Bytes_sent'] * $hourFactor,
  118. 3,
  119. 1
  120. )
  121. ),
  122. ],
  123. [
  124. 'name' => __('Total'),
  125. 'number' => implode(
  126. ' ',
  127. Util::formatByteDown(
  128. $this->data->status['Bytes_received'] + $this->data->status['Bytes_sent'],
  129. 3,
  130. 1
  131. )
  132. ),
  133. 'per_hour' => implode(
  134. ' ',
  135. Util::formatByteDown(
  136. ($this->data->status['Bytes_received'] + $this->data->status['Bytes_sent']) * $hourFactor,
  137. 3,
  138. 1
  139. )
  140. ),
  141. ],
  142. ];
  143. }
  144. /**
  145. * @return array
  146. */
  147. private function getConnectionsInfo(): array
  148. {
  149. $hourFactor = 3600 / $this->data->status['Uptime'];
  150. $failedAttemptsPercentage = '---';
  151. $abortedPercentage = '---';
  152. if ($this->data->status['Connections'] > 0) {
  153. $failedAttemptsPercentage = Util::formatNumber(
  154. $this->data->status['Aborted_connects'] * 100 / $this->data->status['Connections'],
  155. 0,
  156. 2,
  157. true
  158. ) . '%';
  159. $abortedPercentage = Util::formatNumber(
  160. $this->data->status['Aborted_clients'] * 100 / $this->data->status['Connections'],
  161. 0,
  162. 2,
  163. true
  164. ) . '%';
  165. }
  166. return [
  167. [
  168. 'name' => __('Max. concurrent connections'),
  169. 'number' => Util::formatNumber(
  170. $this->data->status['Max_used_connections'],
  171. 0
  172. ),
  173. 'per_hour' => '---',
  174. 'percentage' => '---',
  175. ],
  176. [
  177. 'name' => __('Failed attempts'),
  178. 'number' => Util::formatNumber(
  179. $this->data->status['Aborted_connects'],
  180. 4,
  181. 1,
  182. true
  183. ),
  184. 'per_hour' => Util::formatNumber(
  185. $this->data->status['Aborted_connects'] * $hourFactor,
  186. 4,
  187. 2,
  188. true
  189. ),
  190. 'percentage' => $failedAttemptsPercentage,
  191. ],
  192. [
  193. 'name' => __('Aborted'),
  194. 'number' => Util::formatNumber(
  195. $this->data->status['Aborted_clients'],
  196. 4,
  197. 1,
  198. true
  199. ),
  200. 'per_hour' => Util::formatNumber(
  201. $this->data->status['Aborted_clients'] * $hourFactor,
  202. 4,
  203. 2,
  204. true
  205. ),
  206. 'percentage' => $abortedPercentage,
  207. ],
  208. [
  209. 'name' => __('Total'),
  210. 'number' => Util::formatNumber(
  211. $this->data->status['Connections'],
  212. 4,
  213. 0
  214. ),
  215. 'per_hour' => Util::formatNumber(
  216. $this->data->status['Connections'] * $hourFactor,
  217. 4,
  218. 2
  219. ),
  220. 'percentage' => Util::formatNumber(100, 0, 2) . '%',
  221. ],
  222. ];
  223. }
  224. /**
  225. * @param ReplicationGui $replicationGui ReplicationGui instance
  226. *
  227. * @return string
  228. */
  229. private function getReplicationInfo(ReplicationGui $replicationGui): string
  230. {
  231. global $replication_info, $replication_types;
  232. $output = '';
  233. foreach ($replication_types as $type) {
  234. if (isset($replication_info[$type]['status'])
  235. && $replication_info[$type]['status']
  236. ) {
  237. $output .= $replicationGui->getHtmlForReplicationStatusTable($type);
  238. }
  239. }
  240. return $output;
  241. }
  242. }