From 8c4553f9f22af0d0513df166ffac086f3386596b Mon Sep 17 00:00:00 2001 From: Riccardo Di Dato Date: Sun, 11 Sep 2022 18:16:13 +0200 Subject: [PATCH] #4 - WIP: Untested stats --- app/os/php.template.ini | 2 + app/private/bin/convertStats.php | 22 ++ app/private/lib/AnalyticsHelper.php | 78 ++-- app/private/lib/PipelineHelper.php | 341 ++++++------------ .../dao/models/stats/PageViewStatsModel.php | 4 +- app/private/lib/gui/GUIHandler.php | 28 +- app/private/lib/gui/GUIHandlerMobile.php | 7 +- app/public/admin/statCustom.php | 7 +- app/public/admin/statSito.php | 14 +- app/public/index.php | 7 +- 10 files changed, 225 insertions(+), 285 deletions(-) diff --git a/app/os/php.template.ini b/app/os/php.template.ini index bb0b306..7a8860b 100644 --- a/app/os/php.template.ini +++ b/app/os/php.template.ini @@ -1,3 +1,5 @@ +log_errors = On +error_log = /dev/stderr display_errors = Off display_startup_errors = On diff --git a/app/private/bin/convertStats.php b/app/private/bin/convertStats.php index 740e5c7..b2bca81 100755 --- a/app/private/bin/convertStats.php +++ b/app/private/bin/convertStats.php @@ -18,6 +18,9 @@ function getModels() { StatisticModel::class, [], [ + 'sort' => [ + 'date' => 1 + ], 'skip' => $iteration * $size, 'limit' => $size ] @@ -26,11 +29,25 @@ function getModels() { return $ret; } +$lastCheckedMonth = ""; +$lastCheckedDay = ""; + +$userMonth = []; +$userDay = []; $models = getModels(); while (sizeof($models)>0) { foreach ($models as $model) { $ts = floor( strval($model->date) / 1000) ; + if (date("m", $ts) != $lastCheckedMonth) { + $lastCheckedMonth = date("m", $ts); + $userMonth = []; + } + if (date("d", $ts) != $lastCheckedDay) { + $lastCheckedDay = date("d", $ts); + $userDay = []; + } + if ($model->statType == StatisticModel::STAT_TYPE_NOTIZIA) { AnalyticsHelper::notiziaImpression( $model->content, @@ -45,6 +62,8 @@ while (sizeof($models)>0) { date("d", $ts), date("m", $ts), date("Y", $ts), + !array_key_exists($model->user->id, $userDay), + !array_key_exists($model->user->id, $userMonth), $model->user->isNew ); } @@ -64,6 +83,9 @@ while (sizeof($models)>0) { date("Y", $ts) ); } + + $userDay[ $model->user->id ] = 1; + $userMonth[ $model->user->id ] = 1; } echo "Completed iteration $iteration", PHP_EOL; diff --git a/app/private/lib/AnalyticsHelper.php b/app/private/lib/AnalyticsHelper.php index 2254565..4bd0120 100644 --- a/app/private/lib/AnalyticsHelper.php +++ b/app/private/lib/AnalyticsHelper.php @@ -5,11 +5,41 @@ Creation Date: 27/gen/2016 */ class AnalyticsHelper { - private static $COOKIE_NAME = "FDN_STAT_NUM"; - private static $COOKIE_TIME = "+1 year"; - + private const COOKIE_UNIQUE_NAME = 'FDN_STAT_NUM'; + + private const COOKIE_TODAY_NAME = 'FDN_TODAY_VISIT'; - public static function pageImpression($content, $d, $m, $y, $isUnique) { + private const COOKIE_MONTH_NAME = 'FDN_MONTH_VISIT'; + + private static function isNewAbsolute(): bool { + return !isset($_COOKIE[self::COOKIE_UNIQUE_NAME]); + } + private static function isNewToday(): bool { + return !isset($_COOKIE[self::COOKIE_TODAY_NAME]); + } + private static function isNewThisMonth(): bool { + return !isset($_COOKIE[self::COOKIE_MONTH_NAME]); + } + + private static function updateCookies(){ + setcookie( + self::COOKIE_UNIQUE_NAME, + uniqid("",true), + mktime(0, 0, 0, date('n'), date('j'), date('Y') + 1 ) + ); + setcookie( + self::COOKIE_TODAY_NAME, + time(), + mktime(0, 0, 0, date('n'), date('j') + 1) + ); + setcookie( + self::COOKIE_MONTH_NAME, + time(), + mktime(0, 0, 0, date('n') + 1, 1) + ); + } + + public static function pageImpression($content, $d, $m, $y, $isNewToday, $isNewMonth, $isNewAbsolute) { $dao = GlobalVariables::get("dao"); if ($dao instanceof GenericDao) { $upd = [ @@ -17,8 +47,14 @@ class AnalyticsHelper { 'views' => 1 ] ]; - if ($isUnique) { - $upd['$inc']['unique'] = 1; + if ($isNewAbsolute || $isNewMonth || $isNewToday) { + $upd['$inc']['dayNewVisitors'] = 1; + } + if ($isNewAbsolute || $isNewMonth) { + $upd['$inc']['monthNewVisitors'] = 1; + } + if ($isNewAbsolute) { + $upd['$inc']['absNewVisitors'] = 1; } $dao->rawUpsert( PageViewStatsModel::class, @@ -41,9 +77,11 @@ class AnalyticsHelper { intval(date('d')), intval(date('m')), intval(date('Y')), - !isset($_COOKIE[self::$COOKIE_NAME]) + self::isNewToday(), + self::isNewThisMonth(), + self::isNewAbsolute() ); - self::prepareStatCookie(); + self::updateCookies(); } /** @@ -61,12 +99,7 @@ class AnalyticsHelper { GUIHandler::addOnLoadJsEvent($function); } - - private static function prepareStatCookie(){ - setcookie(self::$COOKIE_NAME, uniqid("",true), strtotime(self::$COOKIE_TIME) ); - } - - + public static function notiziaImpression($content, $d, $m, $y) { $dao = GlobalVariables::get("dao"); if ($dao instanceof GenericDao) { @@ -103,12 +136,7 @@ class AnalyticsHelper { * @param string $id */ public static function logNotiziaImpression($id){ - if (!isset($_COOKIE[self::$COOKIE_NAME])){ - self::prepareStatCookie(); - } - $ip = $_SERVER['REMOTE_ADDR']; - $notizia = $id; $source = null; if (array_key_exists("HTTP_REFERER",$_SERVER)){ $source = $_SERVER['HTTP_REFERER']; @@ -182,10 +210,6 @@ class AnalyticsHelper { * @param string $id */ public static function logBannerImpression($id){ - if (!isset($_COOKIE[self::$COOKIE_NAME])){ - self::prepareStatCookie(); - } - $ip = $_SERVER['REMOTE_ADDR']; $source = null; if (array_key_exists("HTTP_REFERER",$_SERVER)){ @@ -235,9 +259,6 @@ class AnalyticsHelper { * @param string $id */ public static function getBannerClickLink($banner){ - if (!isset($_COOKIE[self::$COOKIE_NAME])){ - self::prepareStatCookie(); - } return "bannerClick('".$banner->id."','".$banner->link."');"; } @@ -245,7 +266,7 @@ class AnalyticsHelper { * Restitusce il numero di impressions per una notizia * @param string $id */ - public static function countBannerClicks($id, $start = null, $end = null){ + public static function countBannerClicks($id, $start = null, $end = null) { $ret = GlobalVariables::get("dao")->aggregate( BannerStatsModel::class, PipelineHelper::getBannerClicks($id, $start, $end) @@ -260,7 +281,8 @@ class AnalyticsHelper { * @param int $position * @param string $replaced id del banner rimpiazzato */ - public static function getBannerToShow($position, $replaced = null, $category = BannerModel::GENERAL_CATEGORY){ + public static function getBannerToShow($position, $replaced = null, $category = BannerModel::GENERAL_CATEGORY) { + // ?TODO: Ragionaci // Rimuovi il replaced dalla lista $shown = array(); $rval = null; diff --git a/app/private/lib/PipelineHelper.php b/app/private/lib/PipelineHelper.php index bc0c8ae..73e3841 100644 --- a/app/private/lib/PipelineHelper.php +++ b/app/private/lib/PipelineHelper.php @@ -66,202 +66,107 @@ class PipelineHelper { $to ); } - + + + + + + + + public static function getDailyVisualizationPipeline($startDate){ - $rval = array( - array( - '$match'=>array( - 'statType'=>StatisticModel::STAT_TYPE_PAGE, - 'date'=>array( -// '$gte'=>new MongoDate($startDate) - '$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000) - ) - ) - ), - array( - '$group'=>array( - "_id"=>array( - 'date'=>array( - "day"=>array('$dayOfMonth'=>'$date'), - "month"=>array('$month'=>'$date'), - "year"=>array('$year'=>'$date') - ), - 'user'=>'$user.id', - ), - "visualizzazioni"=>array( - '$sum'=>1 - ), - "isNew"=>array( - '$sum'=>array( - '$cond'=>array( - array('$eq'=>array('$user.isNew',true)), - 1, - 0 - ) - ) - ) - ) - ), - array( - '$group'=>array( - "_id"=>array( - 'date'=>'$_id.date' - ), - "visualizzazioni"=>array( - '$sum'=>'$visualizzazioni' - ), - "unici"=>array( - '$sum'=>'$isNew' - ), - "visitatori"=>array( - '$sum'=>1 - ) - ) - ), - array( - '$sort'=>array( - "_id.date.year"=>1, - "_id.date.month"=>1, - "_id.date.day"=>1 - ) - ) - ); - return $rval; + return [ + [ + '$match' => [ + 'time' => [ + '$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000) + ] + ] + ], + [ + '$addFields' => [ + "visualizzazioni" => '$views', + "unici" => '$absNewVisitors', + "visitatori" => '$dayNewVisitors' + ] + ], + [ + '$sort' => [ + "y"=>1, + "m"=>1, + "d"=>1 + ] + ] + ]; } - public static function getDailyVisualizationPipelineByStartAndEnd($startDate,$endDate){ - $rval = array( - array( - '$project'=>array( - 'tdate'=>array( - '$add'=>array( - '$date',60*60*1000 - ) - ), - "statType"=>'$statType', - 'user'=>'$user', - ) - ), - array( - '$match'=>array( - 'statType'=>StatisticModel::STAT_TYPE_PAGE, - 'tdate'=>array( -// '$gte'=>new MongoDate($startDate), -// '$lte'=>new MongoDate($endDate) - '$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000), - '$lte'=>new MongoDB\BSON\UTCDateTime($endDate * 1000) - ) - ) - ), - array( - '$group'=>array( - "_id"=>array( - 'date'=>array( - "day"=>array('$dayOfMonth'=>'$tdate'), - "month"=>array('$month'=>'$tdate'), - "year"=>array('$year'=>'$tdate') - ), - 'user'=>'$user.id', - ), - "visualizzazioni"=>array( - '$sum'=>1 - ), - "isNew"=>array( - '$sum'=>array( - '$cond'=>array( - array('$eq'=>array('$user.isNew',true)), - 1, - 0 - ) - ) - ) - ) - ), - array( - '$group'=>array( - "_id"=>array( - 'date'=>'$_id.date' - ), - "visualizzazioni"=>array( - '$sum'=>'$visualizzazioni' - ), - "unici"=>array( - '$sum'=>'$isNew' - ), - "visitatori"=>array( - '$sum'=>1 - ) - ) - ), - array( - '$sort'=>array( - "_id.date.year"=>1, - "_id.date.month"=>1, - "_id.date.day"=>1 - ) - ) - ); - return $rval; + public static function getDailyVisualizationPipelineByStartAndEnd($startDate,$endDate) { + return [ + [ + '$match' => [ + 'time' => [ + '$gte' => new MongoDB\BSON\UTCDateTime($startDate * 1000), + '$lte' => new MongoDB\BSON\UTCDateTime($endDate * 1000) + ] + ] + ], + [ + '$addFields' => [ + "visualizzazioni" => '$views', + "unici" => '$absNewVisitors', + "visitatori" => '$dayNewVisitors' + ] + ], + [ + '$sort' => [ + "y"=>1, + "m"=>1, + "d"=>1 + ] + ] + ]; } public static function getMonthlyVisualizationPipeline($startDate){ - $rval = array( - array( - '$match'=>array( - 'statType'=>StatisticModel::STAT_TYPE_PAGE, - 'date'=>array( -// '$gte'=>new MongoDate($startDate) - '$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000) - ) - ) - ), - array( - '$group'=>array( - "_id"=>array( - 'date'=>array( - "month"=>array('$month'=>'$date'), - "year"=>array('$year'=>'$date') - ), - 'user'=>'$user.id', - ), - "visualizzazioni"=>array( - '$sum'=>1 - ), - "isNew"=>array( - '$sum'=>array( - '$cond'=>array( - array('$eq'=>array('$user.isNew',true)), - 1, - 0 - ) - ) - ) - ) - ), - array( - '$group'=>array( - "_id"=>array( - 'date'=>'$_id.date' - ), - "visualizzazioni"=>array( - '$sum'=>'$visualizzazioni' - ), - "unici"=>array( - '$sum'=>'$isNew' - ), - "visitatori"=>array( - '$sum'=>1 - ) - ) - ), - array( - '$sort'=>array( - "_id.date.year"=>1, - "_id.date.month"=>1 - ) - ) - ); - return $rval; + return [ + [ + '$match' => [ + 'time' => [ + '$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000) + ] + ] + ], + [ + '$group' => [ + "_id" => [ + 'y' => '$y', + 'm' => '$m' + ], + "visualizzazioni" => [ + '$sum' => '$views' + ], + "unici" => [ + '$sum' => '$absNewVisitors' + ], + "visitatori" => [ + '$sum' => '$absNewVisitors' + ], + ] + ], + [ + '$addFields' => [ + "visualizzazioni" => '$views', + "unici" => '$absNewVisitors', + "visitatori" => '$monthNewVisitors' + ] + ], + [ + '$sort' => [ + "_id.y"=>1, + "_id.m"=>1 + ] + ] + ]; } @@ -269,9 +174,10 @@ class PipelineHelper { * Il risultato di questa pipeline restituisce un oggetto con _id.banner contenente l'id del banner e visualizzazioni contenente il numero * di visualizzazioni per oggi ordinati in modo crescente per visualizzazioni * @param unknown $position - * @return multitype:multitype:multitype:number multitype:multitype:unknown number multitype:MongoDate multitype:multitype:multitype:number multitype:multitype:string + * @return mixed */ - public static function getTodaysBannerVisualizationPipeline($position){ + public static function getTodaysBannerVisualizationPipeline($position) { + // ?TODO: QUesto metodo potrebbe essere obsoleto $rval = array( array( '$match'=>array( @@ -304,39 +210,26 @@ class PipelineHelper { * Il risultato di questa pipeline restituisce un oggetto con _id.banner contenente l'id del banner e visualizzazioni contenente il numero * di visualizzazioni per oggi ordinati in modo crescente per visualizzazioni * @param unknown $position - * @return multitype:multitype:multitype:number multitype:multitype:unknown number multitype:MongoDate multitype:multitype:multitype:number multitype:multitype:string + * @return mixed */ - public static function getMostViewedOfLastDays($startDate,$limit = 5){ - $rval = array( - array( - '$match'=>array( - 'statType'=>StatisticModel::STAT_TYPE_NOTIZIA, - 'date'=>array( -// '$gte'=>new MongoDate($startDate) - '$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000) - ) - ) - ), - array( - '$group'=>array( - "_id"=>array( - 'notizia'=>'$content.notizia', - ), - "visualizzazioni"=>array( - '$sum'=>1 - ) - ) - ), - array( - '$sort'=>array( - "visualizzazioni"=>-1 - ) - ), - array( - '$limit'=>$limit - ) - ); - return $rval; + public static function getMostViewedOfLastDays($startDate, $limit = 5) { + return [ + [ + '$match' => [ + 'time' => [ + '$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000) + ] + ] + ], + [ + '$sort' => [ + "views"=> -1 + ] + ], + [ + '$limit' => $limit + ] + ]; } } diff --git a/app/private/lib/dao/models/stats/PageViewStatsModel.php b/app/private/lib/dao/models/stats/PageViewStatsModel.php index 60e7eb2..f012cb2 100644 --- a/app/private/lib/dao/models/stats/PageViewStatsModel.php +++ b/app/private/lib/dao/models/stats/PageViewStatsModel.php @@ -25,7 +25,9 @@ class PageViewStatsModel extends Model { 'd' => intval($day), 'time' => new MongoDB\BSON\UTCDateTime( $time * 1000 ), 'views' => 0, - 'unique' => 0 + 'absNewVisitors' => 0, + 'dayNewVisitors' => 0, + 'monthNewVisitors' => 0 ] ); } diff --git a/app/private/lib/gui/GUIHandler.php b/app/private/lib/gui/GUIHandler.php index be0094a..4c57ceb 100644 --- a/app/private/lib/gui/GUIHandler.php +++ b/app/private/lib/gui/GUIHandler.php @@ -210,15 +210,12 @@ class GUIHandler {
- -
-
@@ -375,26 +372,13 @@ class GUIHandler { } - public static function generateRightBannerBlock($category = BannerModel::GENERAL_CATEGORY){ -// $stats = GlobalVariables::get("dao")->aggregate("StatisticModel",PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days"),5)); -// $idArray = array_map(function ($ele){ return new MongoId($ele["_id"]["notizia"]);}, $stats); - -// $lastsNews = GlobalVariables::get("dao")->query("NotiziaModel", -// array("status"=>NotiziaModel::STATUS_ACCEPTED,"_id"=>array('$in'=>$idArray)), -// array("sort"=>array("about.data"=>-1)) -// ); + public static function generateRightBannerBlock($category = BannerModel::GENERAL_CATEGORY) { -// if (sizeof($lastsNews)<5){ -// $missing = 5-sizeof($lastsNews); -// $add = GlobalVariables::get("dao")->query("NotiziaModel", -// array("status"=>NotiziaModel::STATUS_ACCEPTED,"_id"=>array('$nin'=>$idArray)), -// array("sort"=>array("about.data"=>-1),"limit"=>$missing) -// ); -// $lastsNews = array_merge($lastsNews,$add); -// } - - $stats = GlobalVariables::get("dao")->aggregate("StatisticModel",PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days CET"),5)); - $idArray = array_map(function ($ele){ return new MongoDB\BSON\ObjectID($ele["_id"]["notizia"]);}, $stats); + $stats = GlobalVariables::get("dao")->aggregate( + ArticleStatsModel::class, + PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days CET"),5) + ); + $idArray = array_map(function ($ele){ return new MongoDB\BSON\ObjectID($ele["notizia"]);}, $stats); // $lastsNews = GlobalVariables::get("dao")->query("NotiziaModel", // array("status"=>NotiziaModel::STATUS_ACCEPTED,"_id"=>array('$in'=>$idArray),"about.data"=>array('$lte'=>new MongoDate(),'$gte'=>new MongoDate(strtotime('-2 months')))), diff --git a/app/private/lib/gui/GUIHandlerMobile.php b/app/private/lib/gui/GUIHandlerMobile.php index d46b523..5c98f12 100644 --- a/app/private/lib/gui/GUIHandlerMobile.php +++ b/app/private/lib/gui/GUIHandlerMobile.php @@ -401,8 +401,11 @@ class GUIHandlerMobile { public static function generateRightBannerBlock($category = BannerModel::GENERAL_CATEGORY){ - $stats = GlobalVariables::get("dao")->aggregate("StatisticModel",PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days"),5)); - $idArray = array_map(function ($ele){ return new MongoDB\BSON\ObjectID($ele["_id"]["notizia"]);}, $stats); + $stats = GlobalVariables::get("dao")->aggregate( + ArticleStatsModel::class, + PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days"),5) + ); + $idArray = array_map(function ($ele){ return new MongoDB\BSON\ObjectID($ele["notizia"]);}, $stats); $lastsNews = GlobalVariables::get("dao")->query("NotiziaModel", array("status"=>NotiziaModel::STATUS_ACCEPTED,"_id"=>array('$in'=>$idArray)), diff --git a/app/public/admin/statCustom.php b/app/public/admin/statCustom.php index 0ebebed..cda60e8 100644 --- a/app/public/admin/statCustom.php +++ b/app/public/admin/statCustom.php @@ -54,10 +54,13 @@ if (!is_null($action) && strcmp($action, "show")==0){ aggregate("StatisticModel",PipelineHelper::getDailyVisualizationPipelineByStartAndEnd($start,$end)); + $result = GlobalVariables::get("dao")->aggregate( + PageViewStatsModel::class, + PipelineHelper::getDailyVisualizationPipelineByStartAndEnd($start,$end) + ); for ($i=0; $iaggregate("StatisticModel",PipelineHelper::getDailyVisualizationPipeline(strtotime("-1 month"))); +$result = GlobalVariables::get("dao")->aggregate( + PageViewStatsModel::class, + PipelineHelper::getDailyVisualizationPipeline(strtotime("-1 month")) +); for ($i=0; $i
@@ -92,10 +95,13 @@ $(function () { }); aggregate("StatisticModel",PipelineHelper::getMonthlyVisualizationPipeline(strtotime("-1 year"))); + $result = GlobalVariables::get("dao")->aggregate( + PageViewStatsModel::class, + PipelineHelper::getMonthlyVisualizationPipeline(strtotime("-1 year")) + ); for ($i=0; $i $('#containerMonthly').highcharts({ diff --git a/app/public/index.php b/app/public/index.php index 1622349..42b652c 100644 --- a/app/public/index.php +++ b/app/public/index.php @@ -85,8 +85,11 @@ try { $ristorantiNews = array_map(function($ele){$ele->testo = strip_tags($ele->testo);$ele->sottotitolo = strip_tags($ele->sottotitolo); return $ele; }, $ristorantiNews); } - $stats = GlobalVariables::get("dao")->aggregate("StatisticModel",PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days"),4)); - $idArray = array_map(function ($ele){ return new MongoDB\BSON\ObjectID( $ele["_id"]["notizia"] ); }, $stats); + $stats = GlobalVariables::get("dao")->aggregate( + ArticleStatsModel::class, + PipelineHelper::getMostViewedOfLastDays(strtotime("-7 days"),4) + ); + $idArray = array_map(function ($ele){ return new MongoDB\BSON\ObjectID( $ele["notizia"] ); }, $stats); $lastsNews = GlobalVariables::get("dao")->query("NotiziaModel", array("status"=>NotiziaModel::STATUS_ACCEPTED,"_id"=>array('$in'=>$idArray),"about.lastEdit"=>array('$lte'=>new MongoDB\BSON\UTCDateTime( time() * 1000 ),'$gte'=>new MongoDB\BSON\UTCDateTime(strtotime('-2 months') * 1000 ))),