PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/bitrix/modules/bitrixcloud/classes/general/monitoring_result.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 394 lines | 285 code | 3 blank | 106 comment | 11 complexity | 7a0ce0076f45e3877a6c0033d3b8cee8 MD5 | raw file
  1. <?php
  2. interface CBitrixCloudMonitoring_Access extends Iterator, ArrayAccess
  3. {
  4. // new stuff
  5. }
  6. class CBitrixCloudMonitoringTest
  7. {
  8. private $name = "";
  9. private $status = "";
  10. private $time = 0;
  11. private $uptime = "";
  12. private $result = "";
  13. /**
  14. *
  15. * @param string $name
  16. * @param string $status
  17. * @param int $time UTC timestamp
  18. * @param string $result
  19. * @return void
  20. *
  21. */
  22. public function __construct($name, $status, $time, $uptime, $result)
  23. {
  24. $this->name = $name;
  25. $this->status = $status;
  26. $this->time = $time;
  27. $this->uptime = $uptime;
  28. $this->result = $result;
  29. }
  30. /**
  31. *
  32. * @return string
  33. *
  34. */
  35. public function getName()
  36. {
  37. return $this->name;
  38. }
  39. /**
  40. *
  41. * @return string
  42. *
  43. */
  44. public function getStatus()
  45. {
  46. return $this->status;
  47. }
  48. /**
  49. *
  50. * @return string
  51. *
  52. */
  53. public function getResult()
  54. {
  55. return $this->result;
  56. }
  57. /**
  58. *
  59. * @return string
  60. *
  61. */
  62. public function getUptime()
  63. {
  64. return $this->uptime;
  65. }
  66. /**
  67. *
  68. * @return string
  69. *
  70. */
  71. public function getTime()
  72. {
  73. return $this->time;
  74. }
  75. /**
  76. *
  77. * @param CDataXMLNode $node
  78. * @return CBitrixCloudMonitoringTest
  79. *
  80. */
  81. public static function fromXMLNode(CDataXMLNode $node)
  82. {
  83. return new CBitrixCloudMonitoringTest(
  84. $node->getAttribute("id"),
  85. $node->getAttribute("status") == 2? CBitrixCloudMonitoringResult::RED_LAMP: CBitrixCloudMonitoringResult::GREEN_LAMP,
  86. strtotime($node->getAttribute("time")),
  87. $node->getAttribute("uptime"),
  88. $node->textContent()
  89. );
  90. }
  91. }
  92. class CBitrixCloudMonitoringDomainResult implements CBitrixCloudMonitoring_Access
  93. {
  94. /** @var string $name */
  95. private $name = "";
  96. /** @var array[int]CBitrixCloudMonitoringTest $tests */
  97. private $tests = /*.(array[int]CBitrixCloudMonitoringTest.*/ array();
  98. /**
  99. *
  100. * @return string
  101. *
  102. */
  103. public function getName()
  104. {
  105. return $this->name;
  106. }
  107. /**
  108. *
  109. * @return string
  110. *
  111. */
  112. public function getStatus()
  113. {
  114. foreach ($this->tests as $testName => $testResult)
  115. {
  116. if ($testResult->getStatus() === CBitrixCloudMonitoringResult::RED_LAMP)
  117. return CBitrixCloudMonitoringResult::RED_LAMP;
  118. }
  119. return CBitrixCloudMonitoringResult::GREEN_LAMP;
  120. }
  121. /**
  122. *
  123. * @param string $name
  124. * @param array[int]CBitrixCloudMonitoringTest $tests
  125. * @return void
  126. *
  127. */
  128. public function __construct($name, array $tests)
  129. {
  130. $this->name = $name;
  131. $this->setTests($tests);
  132. }
  133. /**
  134. *
  135. * @param string $testName
  136. * @return CBitrixCloudMonitoringTest
  137. *
  138. */
  139. public function getTestByName($testName)
  140. {
  141. return $this->tests[$testName];
  142. }
  143. /**
  144. *
  145. * @return array[int]CBitrixCloudMonitoringTest
  146. *
  147. */
  148. public function getTests()
  149. {
  150. return $this->tests;
  151. }
  152. /**
  153. *
  154. * @param array[int]CBitrixCloudMonitoringTest $tests
  155. * @return CBitrixCloudMonitoringDomainResult
  156. *
  157. */
  158. public function setTests(array $tests)
  159. {
  160. foreach ($tests as $test)
  161. {
  162. if (
  163. is_object($test)
  164. && $test instanceof CBitrixCloudMonitoringTest
  165. )
  166. {
  167. $this->tests[$test->getName()] = $test;
  168. }
  169. }
  170. return $this;
  171. }
  172. public function saveToOptions(CBitrixCloudOption $option)
  173. {
  174. $tests = array();
  175. foreach ($this->tests as $testName => $testResult)
  176. {
  177. $tests[$testName] = serialize(array(
  178. "status" => $testResult->getStatus(),
  179. "time" => $testResult->getTime(),
  180. "uptime" => $testResult->getUptime(),
  181. "result" => $testResult->getResult(),
  182. ));
  183. }
  184. $option->setArrayValue($tests);
  185. }
  186. public static function loadFromOptions($name, CBitrixCloudOption $option)
  187. {
  188. $tests = array();
  189. foreach($option->getArrayValue() as $testName => $testResult)
  190. {
  191. $testResult = unserialize($testResult);
  192. if (is_array($testResult))
  193. {
  194. $test = new CBitrixCloudMonitoringTest(
  195. $testName,
  196. $testResult["status"],
  197. $testResult["time"],
  198. $testResult["uptime"],
  199. $testResult["result"]
  200. );
  201. $tests[$test->getName()] = $test;
  202. }
  203. }
  204. return new CBitrixCloudMonitoringDomainResult($name, $tests);
  205. }
  206. /**
  207. *
  208. * @param CDataXMLNode $node
  209. * @return CBitrixCloudMonitoringDomainResult
  210. *
  211. */
  212. public static function fromXMLNode(CDataXMLNode $node)
  213. {
  214. $name = $node->getAttribute("name");
  215. $tests = array();
  216. foreach ($node->children() as $nodeTest)
  217. {
  218. $tests[] = CBitrixCloudMonitoringTest::fromXMLNode($nodeTest);
  219. }
  220. return new CBitrixCloudMonitoringDomainResult($name, $tests);
  221. }
  222. public function rewind()
  223. {
  224. reset($this->tests);
  225. }
  226. public function current()
  227. {
  228. return current($this->tests);
  229. }
  230. public function key()
  231. {
  232. return key($this->tests);
  233. }
  234. public function next()
  235. {
  236. next($this->tests);
  237. }
  238. public function valid()
  239. {
  240. return key($this->tests) !== null;
  241. }
  242. public function offsetSet($offset, $value) {
  243. if (is_null($offset)) {
  244. $this->tests[] = $value;
  245. } else {
  246. $this->tests[$offset] = $value;
  247. }
  248. }
  249. public function offsetExists($offset) {
  250. return isset($this->tests[$offset]);
  251. }
  252. public function offsetUnset($offset) {
  253. unset($this->tests[$offset]);
  254. }
  255. public function offsetGet($offset) {
  256. return isset($this->tests[$offset]) ? $this->tests[$offset] : null;
  257. }
  258. }
  259. class CBitrixCloudMonitoringResult implements CBitrixCloudMonitoring_Access
  260. {
  261. const GREEN_LAMP = 'green';
  262. const RED_LAMP = 'red';
  263. private $domains = /*.(array[string]CBitrixCloudMonitoringDomainResult).*/ array();
  264. /**
  265. *
  266. * @param CBitrixCloudMonitoringDomainResult $domainResult
  267. * @return CBitrixCloudMonitoringResult
  268. *
  269. */
  270. public function addDomainResult(CBitrixCloudMonitoringDomainResult $domainResult)
  271. {
  272. $this->domains[$domainResult->getName()] = $domainResult;
  273. return $this;
  274. }
  275. /**
  276. *
  277. * @param string $domainName
  278. * @return CBitrixCloudMonitoringDomainResult
  279. *
  280. */
  281. public function getResultByDomainName($domainName)
  282. {
  283. return $this->domains[$domainName];
  284. }
  285. /**
  286. *
  287. * @return string
  288. *
  289. */
  290. public function getStatus()
  291. {
  292. foreach ($this->domains as $domainName => $domainResult)
  293. {
  294. if ($domainResult->getStatus() === CBitrixCloudMonitoringResult::RED_LAMP)
  295. return CBitrixCloudMonitoringResult::RED_LAMP;
  296. }
  297. return CBitrixCloudMonitoringResult::GREEN_LAMP;
  298. }
  299. public static function isExpired()
  300. {
  301. $time = CBitrixCloudOption::getOption("monitoring_expire_time")->getIntegerValue();
  302. return ($time < time());
  303. }
  304. public static function getExpirationTime()
  305. {
  306. return CBitrixCloudOption::getOption("monitoring_expire_time")->getIntegerValue();
  307. }
  308. public static function setExpirationTime($time)
  309. {
  310. $time = intval($time);
  311. CBitrixCloudOption::getOption("monitoring_expire_time")->setStringValue($time);
  312. return $time;
  313. }
  314. public function loadFromOptions()
  315. {
  316. $domains = new CBitrixCloudMonitoringResult;
  317. foreach(CBitrixCloudOption::getOption("monitoring_result")->getArrayValue() as $i => $domainName)
  318. {
  319. $domains->addDomainResult(CBitrixCloudMonitoringDomainResult::loadFromOptions(
  320. $domainName,
  321. CBitrixCloudOption::getOption("monitoring_result_$i")
  322. ));
  323. }
  324. return $domains;
  325. }
  326. public function saveToOptions()
  327. {
  328. $domainNames = array_keys($this->domains);
  329. CBitrixCloudOption::getOption("monitoring_result")->setArrayValue($domainNames);
  330. foreach ($domainNames as $i => $domainName)
  331. {
  332. $this->domains[$domainName]->saveToOptions(
  333. CBitrixCloudOption::getOption("monitoring_result_$i")
  334. );
  335. }
  336. }
  337. /**
  338. *
  339. * @param CDataXMLNode $node
  340. * @return CBitrixCloudMonitoringResult
  341. *
  342. */
  343. public static function fromXMLNode(CDataXMLNode $node)
  344. {
  345. $domains = new CBitrixCloudMonitoringResult;
  346. if (is_array($node->children()))
  347. {
  348. foreach ($node->children() as $sub_node)
  349. {
  350. $domains->addDomainResult(CBitrixCloudMonitoringDomainResult::fromXMLNode($sub_node));
  351. }
  352. }
  353. return $domains;
  354. }
  355. public function rewind()
  356. {
  357. reset($this->domains);
  358. }
  359. public function current()
  360. {
  361. return current($this->domains);
  362. }
  363. public function key()
  364. {
  365. return key($this->domains);
  366. }
  367. public function next()
  368. {
  369. next($this->domains);
  370. }
  371. public function valid()
  372. {
  373. return key($this->domains) !== null;
  374. }
  375. public function offsetSet($offset, $value) {
  376. if (is_null($offset)) {
  377. $this->domains[] = $value;
  378. } else {
  379. $this->domains[$offset] = $value;
  380. }
  381. }
  382. public function offsetExists($offset) {
  383. return isset($this->domains[$offset]);
  384. }
  385. public function offsetUnset($offset) {
  386. unset($this->domains[$offset]);
  387. }
  388. public function offsetGet($offset) {
  389. return isset($this->domains[$offset]) ? $this->domains[$offset] : null;
  390. }
  391. }