123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
-
- namespace SCF\Core;
-
- use SCF\Core\Database;
-
- class StatsGenerator {
-
- /**
- * @var \SCF\Core\Database
- */
- private $_db;
-
- /**
- * @var array
- */
- private $userStatValues = [];
-
- /**
- * @var array
- */
- private $downloadStatValue = [];
-
- /**
- * @var array
- */
- private $dateLabels = [];
-
- /**
- * @var array
- */
- private $userDateRows = [];
-
- /**
- * @var array
- */
- private $downloadDateRows = [];
-
- /**
- * StatsGenerator constructor.
- * @param \SCF\Core\Database $db
- */
- public function __construct(Database $db)
- {
- $this->_db = $db;
-
- $this->userReadDatabaseToArray();
- $this->downloadReadDatabaseToArray();
-
- }
-
- /**
- * @return array
- */
- private function userReadDatabaseToArray()
- {
- $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");
- $visitorsPerDay = $this->_db->fetchArray();
-
- foreach ($visitorsPerDay as $key => $value) {
- $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']);
- $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']);
- $year = $value['visit_year'];
- $this->userStatValues[$day.'.'.$month.'.'.$year] = (int) $value['visit_count'];
- }
-
-
- return $this->userStatValues;
- }
-
- /**
- * @return array
- */
- private function downloadReadDatabaseToArray()
- {
- $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");
- $visitorsPerDay = $this->_db->fetchArray();
-
- foreach ($visitorsPerDay as $key => $value) {
- $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']);
- $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']);
- $year = $value['visit_year'];
- $this->downloadStatValue[$day.'.'.$month.'.'.$year] = (int) $value['visit_count'];
- }
-
-
- return $this->downloadStatValue;
- }
-
- /**
- * @param string $day
- * @param string $month
- * @param string $year
- * @param string $range
- * @return array
- */
- private function generateLabels($day = "", $month="", $year="", $range="")
- {
- $genDates = [];
- $dateValue = $day.'.'.$month.'.'.$year;
-
- $genStart = new \DateTime($dateValue);
- $interval = new \DateInterval('P1D');
- if(is_numeric($range)) {
- $range = ($range > 100 ? 100 : $range);
- $genStart = $genStart->modify("-" . $range . " days");
- $genEnd = clone $genStart;
- $genEnd = $genEnd->modify("+" .($range+1)." days");
- } else {
- $genEnd = clone $genStart;
- $genEnd->add(new \DateInterval("P1M"));
- }
-
- $dateRange = new \DatePeriod($genStart,$interval,$genEnd);
-
- foreach ($dateRange as $date) {
- $genDates[] = $date->format("d.m.");
- }
-
- return $genDates;
-
- }
-
- /**
- * @param string $day
- * @param string $month
- * @param string $year
- * @param string $range
- * @return array
- */
- private function generateRows($type="", $day = "", $month="", $year="", $range="")
- {
- if($type == "user") {
- $rowArray = $this->userStatValues;
- } elseif ($type="dls") {
- $rowArray = $this->downloadStatValue;
- }
- $genRows = [];
- $dateValue = $day.'.'.$month.'.'.$year;
-
- $genStart = new \DateTime($dateValue);
- $interval = new \DateInterval('P1D');
- if(is_numeric($range)) {
- $range = ($range > 100 ? 100 : $range);
- $genStart = $genStart->modify("-" . $range . " days");
- $genEnd = clone $genStart;
- $genEnd = $genEnd->modify("+" . ($range * 2) . " days");
- } else {
- $genEnd = clone $genStart;
- $genEnd->add(new \DateInterval("P1M"));
- }
-
- $dateRange = new \DatePeriod($genStart,$interval,$genEnd);
-
- $genEnd = clone $genStart;
- $genEnd->add(new \DateInterval("P1M"));
-
- foreach ($dateRange as $date) {
- $realDate = $date->format("d.m.Y");
- if(array_key_exists($realDate, $rowArray))
- {
- $genRows[] = $rowArray[$realDate];
- } else {
- $genRows[] = 0;
- }
- //var_dump($rowArray);
- }
-
- return $genRows;
- }
-
- /**
- * @param string $day
- * @param string $month
- * @param string $year
- * @param string $range
- */
- public function setDateLabels($day = "", $month="", $year="", $range="")
- {
-
- $day = (!empty($day) ? $day : date("d", time()));
- $month = (!empty($month) ? $month : date("m", time()));
- $year = (!empty($year) ? $year : date("Y", time()));
- $range =(!empty($range) ? $range : 10);
- $this->dateLabels = $this->generateLabels($day,$month,$year,$range);
- }
-
- /**
- * @return array
- */
- public function getDateLabels()
- {
- return json_encode($this->dateLabels);
- }
-
- /**
- * @param string $type
- * @param string $day
- * @param string $month
- * @param string $year
- * @param string $range
- */
- public function setUserDateRows($type="", $day = "", $month="", $year="", $range="")
- {
-
- $day = (!empty($day) ? $day : date("d", time()));
- $month = (!empty($month) ? $month : date("m", time()));
- $year = (!empty($year) ? $year : date("Y", time()));
- $range =(!empty($range) ? $range : 10);
- $this->userDateRows = $this->generateRows($type, $day,$month,$year,$range);
- }
-
- /**
- * @param string $type
- * @param string $day
- * @param string $month
- * @param string $year
- * @param string $range
- */
- public function setDownloadDateRows($type="", $day = "", $month="", $year="", $range="")
- {
-
- $day = (!empty($day) ? $day : date("d", time()));
- $month = (!empty($month) ? $month : date("m", time()));
- $year = (!empty($year) ? $year : date("Y", time()));
- $range =(!empty($range) ? $range : 10);
- $this->downloadDateRows = $this->generateRows($type, $day,$month,$year,$range);
- }
- /**
- * @return array
- */
- public function getUserDateRows()
- {
- return $this->userDateRows;
- }
-
- /**
- * @return array
- */
- public function getDownloadDateRows()
- {
- return $this->downloadDateRows;
- }
- }
|