api.onusigcompressor.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * This class is responsible for compressing ONU signal history logs by filtering and trimming the data.
  4. * It processes signal history files to retain only the first and last entries of each day,
  5. * while keeping the current month's logs unchanged.
  6. **/
  7. class ONUSigCompressor {
  8. /**
  9. * contains OLTattractor ONU signal history path
  10. *
  11. * @var string
  12. */
  13. protected $onuSigPath = '';
  14. /**
  15. * Contains list of all available ONU signal history files as fileName=>filePath
  16. *
  17. * @var array
  18. */
  19. protected $allOnuSigFiles = array();
  20. /**
  21. * Contains current month in YYYY-MM- format
  22. *
  23. * @var string
  24. */
  25. protected $curMonth = '';
  26. /**
  27. * Total skipped records count
  28. *
  29. * @var int
  30. */
  31. protected $recordsSkipped = 0;
  32. /**
  33. * Some other predefined stuff
  34. */
  35. const PID = 'ONUSIGCOMPRESSOR';
  36. public function __construct() {
  37. $this->setOptions();
  38. $this->loadOnuSigDataList();
  39. }
  40. /**
  41. * Sets some options for the ONU signal compressor.
  42. *
  43. * @return void
  44. */
  45. protected function setOptions() {
  46. $this->onuSigPath = OLTAttractor::ONUSIG_PATH;
  47. $this->curMonth = curmonth() . '-';
  48. }
  49. /**
  50. * Loads the list of ONU signal data files into the `allOnuSigFiles` property.
  51. *
  52. * @return void
  53. */
  54. protected function loadOnuSigDataList() {
  55. $tmp = rcms_scandir($this->onuSigPath);
  56. if (!empty($tmp)) {
  57. foreach ($tmp as $io => $each) {
  58. if (strlen($each) == 32) { // hash-named data
  59. $this->allOnuSigFiles[$each] = $this->onuSigPath . $each;
  60. }
  61. }
  62. }
  63. }
  64. /**
  65. * Filters signal history data from obsolete data
  66. *
  67. * @param array $lines An array of log lines to be filtered.
  68. *
  69. * @return array
  70. */
  71. protected function filterDailyLogs($lines) {
  72. $logsByDate = array();
  73. $currentMonthLogs = array();
  74. $filteredLogs = array();
  75. foreach ($lines as $line) {
  76. if (ispos($line, $this->curMonth)) {
  77. $currentMonthLogs[] = $line;
  78. } else {
  79. list($timestamp, $value) = explode(',', $line);
  80. $date = substr($timestamp, 0, 10); // YYYY-MM-DD date
  81. if (!isset($logsByDate[$date])) {
  82. $logsByDate[$date] = ["first" => $line, "last" => $line];
  83. } else {
  84. $logsByDate[$date]["last"] = $line;
  85. }
  86. }
  87. }
  88. foreach ($logsByDate as $entries) {
  89. $filteredLogs[] = $entries["first"];
  90. if ($entries["first"] !== $entries["last"]) {
  91. $filteredLogs[] = $entries["last"];
  92. }
  93. }
  94. //appending current month logs unchanged
  95. $filteredLogs = array_merge($filteredLogs, $currentMonthLogs);
  96. return ($filteredLogs);
  97. }
  98. /**
  99. * Trims signal data from the specified file by filtering out unnecessary logs.
  100. *
  101. * This method reads the content of the specified file, filters the logs based on the current month,
  102. * and overwrites the file with the filtered content if any records were removed.
  103. *
  104. * @param string $filePath The path to the file containing the signal data to be trimmed.
  105. *
  106. * @return void
  107. */
  108. protected function trimSignalData($filePath) {
  109. if (file_exists($filePath)) {
  110. $fileContent = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  111. if (!empty($fileContent)) {
  112. $newFileContent = $this->filterDailyLogs($fileContent);
  113. $recsBefore = sizeof($fileContent);
  114. $recsAfter = sizeof($newFileContent);
  115. if ($recsAfter < $recsBefore) {
  116. //compression successful, overwriting source
  117. $this->recordsSkipped += $recsBefore - $recsAfter;
  118. file_put_contents($filePath, implode(PHP_EOL, $newFileContent) . PHP_EOL);
  119. }
  120. }
  121. }
  122. }
  123. /**
  124. * Runs the ONUSIG compressor process.
  125. *
  126. * @return void
  127. */
  128. public function run() {
  129. set_time_limit(0);
  130. $process = new StarDust(self::PID);
  131. if ($process->notRunning()) {
  132. $process->start();
  133. log_register('ONUSIGCOMPRESSOR STARTED');
  134. if (!empty($this->allOnuSigFiles)) {
  135. foreach ($this->allOnuSigFiles as $io => $eachSigHistory) {
  136. $this->trimSignalData($eachSigHistory);
  137. }
  138. }
  139. log_register('ONUSIGCOMPRESSOR `' . $this->recordsSkipped . '` RECORDS CLEANED IN `' . sizeof($this->allOnuSigFiles) . '` FILES');
  140. log_register('ONUSIGCOMPRESSOR FINISHED');
  141. $process->stop();
  142. } else {
  143. log_register('ONUSIGCOMPRESSOR ALREADY RUNNING SKIPPED');
  144. }
  145. }
  146. }