You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatsGenerator.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace SCF\Core;
  3. use SCF\Core\Database;
  4. class StatsGenerator {
  5. /**
  6. * @var \SCF\Core\Database
  7. */
  8. private $_db;
  9. /**
  10. * @var array
  11. */
  12. private $userStatValues = [];
  13. /**
  14. * @var array
  15. */
  16. private $downloadStatValue = [];
  17. /**
  18. * @var array
  19. */
  20. private $dateLabels = [];
  21. /**
  22. * @var array
  23. */
  24. private $userDateRows = [];
  25. /**
  26. * @var array
  27. */
  28. private $downloadDateRows = [];
  29. /**
  30. * StatsGenerator constructor.
  31. * @param \SCF\Core\Database $db
  32. */
  33. public function __construct(Database $db)
  34. {
  35. $this->_db = $db;
  36. $this->userReadDatabaseToArray();
  37. $this->downloadReadDatabaseToArray();
  38. }
  39. /**
  40. * @return array
  41. */
  42. private function userReadDatabaseToArray()
  43. {
  44. $this->_db->query("SELECT COUNT(guid) AS visit_count, DAY(FROM_UNIXTIME(`timestamp`)) as visit_day, MONTH(FROM_UNIXTIME(`timestamp`)) as visit_month, YEAR(FROM_UNIXTIME(`timestamp`)) AS visit_year FROM sks_unique_user GROUP BY visit_year DESC, visit_month DESC, visit_day DESC");
  45. $visitorsPerDay = $this->_db->fetchArray();
  46. foreach ($visitorsPerDay as $key => $value) {
  47. $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']);
  48. $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']);
  49. $year = $value['visit_year'];
  50. $this->userStatValues[$day.'.'.$month.'.'.$year] = (int) $value['visit_count'];
  51. }
  52. return $this->userStatValues;
  53. }
  54. /**
  55. * @return array
  56. */
  57. private function downloadReadDatabaseToArray()
  58. {
  59. $this->_db->query("SELECT COUNT(guid) AS visit_count, DAY(FROM_UNIXTIME(`timestamp`)) as visit_day, MONTH(FROM_UNIXTIME(`timestamp`)) as visit_month, YEAR(FROM_UNIXTIME(`timestamp`)) AS visit_year FROM sks_unique_dls GROUP BY visit_year DESC, visit_month DESC, visit_day DESC");
  60. $visitorsPerDay = $this->_db->fetchArray();
  61. foreach ($visitorsPerDay as $key => $value) {
  62. $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']);
  63. $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']);
  64. $year = $value['visit_year'];
  65. $this->downloadStatValue[$day.'.'.$month.'.'.$year] = (int) $value['visit_count'];
  66. }
  67. return $this->downloadStatValue;
  68. }
  69. /**
  70. * @param string $day
  71. * @param string $month
  72. * @param string $year
  73. * @param string $range
  74. * @return array
  75. */
  76. private function generateLabels($day = "", $month="", $year="", $range="")
  77. {
  78. $genDates = [];
  79. $dateValue = $day.'.'.$month.'.'.$year;
  80. $genStart = new \DateTime($dateValue);
  81. $interval = new \DateInterval('P1D');
  82. if(is_numeric($range)) {
  83. $range = ($range > 100 ? 100 : $range);
  84. $genStart = $genStart->modify("-" . $range . " days");
  85. $genEnd = clone $genStart;
  86. $genEnd = $genEnd->modify("+" .($range+1)." days");
  87. } else {
  88. $genEnd = clone $genStart;
  89. $genEnd->add(new \DateInterval("P1M"));
  90. }
  91. $dateRange = new \DatePeriod($genStart,$interval,$genEnd);
  92. foreach ($dateRange as $date) {
  93. $genDates[] = $date->format("d.m.");
  94. }
  95. return $genDates;
  96. }
  97. /**
  98. * @param string $day
  99. * @param string $month
  100. * @param string $year
  101. * @param string $range
  102. * @return array
  103. */
  104. private function generateRows($type="", $day = "", $month="", $year="", $range="")
  105. {
  106. if($type == "user") {
  107. $rowArray = $this->userStatValues;
  108. } elseif ($type="dls") {
  109. $rowArray = $this->downloadStatValue;
  110. }
  111. $genRows = [];
  112. $dateValue = $day.'.'.$month.'.'.$year;
  113. $genStart = new \DateTime($dateValue);
  114. $interval = new \DateInterval('P1D');
  115. if(is_numeric($range)) {
  116. $range = ($range > 100 ? 100 : $range);
  117. $genStart = $genStart->modify("-" . $range . " days");
  118. $genEnd = clone $genStart;
  119. $genEnd = $genEnd->modify("+" . ($range * 2) . " days");
  120. } else {
  121. $genEnd = clone $genStart;
  122. $genEnd->add(new \DateInterval("P1M"));
  123. }
  124. $dateRange = new \DatePeriod($genStart,$interval,$genEnd);
  125. $genEnd = clone $genStart;
  126. $genEnd->add(new \DateInterval("P1M"));
  127. foreach ($dateRange as $date) {
  128. $realDate = $date->format("d.m.Y");
  129. if(array_key_exists($realDate, $rowArray))
  130. {
  131. $genRows[] = $rowArray[$realDate];
  132. } else {
  133. $genRows[] = 0;
  134. }
  135. //var_dump($rowArray);
  136. }
  137. return $genRows;
  138. }
  139. /**
  140. * @param string $day
  141. * @param string $month
  142. * @param string $year
  143. * @param string $range
  144. */
  145. public function setDateLabels($day = "", $month="", $year="", $range="")
  146. {
  147. $day = (!empty($day) ? $day : date("d", time()));
  148. $month = (!empty($month) ? $month : date("m", time()));
  149. $year = (!empty($year) ? $year : date("Y", time()));
  150. $range =(!empty($range) ? $range : 10);
  151. $this->dateLabels = $this->generateLabels($day,$month,$year,$range);
  152. }
  153. /**
  154. * @return array
  155. */
  156. public function getDateLabels()
  157. {
  158. return json_encode($this->dateLabels);
  159. }
  160. /**
  161. * @param string $type
  162. * @param string $day
  163. * @param string $month
  164. * @param string $year
  165. * @param string $range
  166. */
  167. public function setUserDateRows($type="", $day = "", $month="", $year="", $range="")
  168. {
  169. $day = (!empty($day) ? $day : date("d", time()));
  170. $month = (!empty($month) ? $month : date("m", time()));
  171. $year = (!empty($year) ? $year : date("Y", time()));
  172. $range =(!empty($range) ? $range : 10);
  173. $this->userDateRows = $this->generateRows($type, $day,$month,$year,$range);
  174. }
  175. /**
  176. * @param string $type
  177. * @param string $day
  178. * @param string $month
  179. * @param string $year
  180. * @param string $range
  181. */
  182. public function setDownloadDateRows($type="", $day = "", $month="", $year="", $range="")
  183. {
  184. $day = (!empty($day) ? $day : date("d", time()));
  185. $month = (!empty($month) ? $month : date("m", time()));
  186. $year = (!empty($year) ? $year : date("Y", time()));
  187. $range =(!empty($range) ? $range : 10);
  188. $this->downloadDateRows = $this->generateRows($type, $day,$month,$year,$range);
  189. }
  190. /**
  191. * @return array
  192. */
  193. public function getUserDateRows()
  194. {
  195. return $this->userDateRows;
  196. }
  197. /**
  198. * @return array
  199. */
  200. public function getDownloadDateRows()
  201. {
  202. return $this->downloadDateRows;
  203. }
  204. }