#9 Still in testing
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
* File : convertStats.php
|
||||
*******************************************/
|
||||
|
||||
die();
|
||||
require_once(dirname(__FILE__)."/../load.php");
|
||||
|
||||
$iteration = 0;
|
||||
|
||||
Executable
+362
@@ -0,0 +1,362 @@
|
||||
#!/usr/local/bin/php
|
||||
<?php
|
||||
/******************************************
|
||||
* Author : Riccardo Di Dato
|
||||
* Created On : Tue Aug 30 2022
|
||||
* File : convertStats.php
|
||||
*******************************************/
|
||||
|
||||
require_once(dirname(__FILE__)."/../load.php");
|
||||
|
||||
|
||||
function getDailyBannerImpressionsPipeline($startDate, $endDate) {
|
||||
return [
|
||||
[
|
||||
'$project' => [
|
||||
'tdate'=> [
|
||||
'$add' => [ '$date' , 60*60*1000 ]
|
||||
],
|
||||
"statType" => '$statType',
|
||||
'user' => '$user',
|
||||
'banner' => '$content.banner',
|
||||
]
|
||||
],
|
||||
[
|
||||
'$match' => [
|
||||
'statType' => StatisticModel::STAT_TYPE_BANNER_IMPRESSION,
|
||||
'tdate' => [
|
||||
'$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000),
|
||||
'$lte'=>new MongoDB\BSON\UTCDateTime($endDate * 1000)
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'date' => [
|
||||
'day' => [ '$dayOfMonth' => '$tdate' ],
|
||||
"month" => [ '$month' => '$tdate' ],
|
||||
"year" => [ '$year'=>'$tdate' ]
|
||||
],
|
||||
'banner' => '$banner',
|
||||
],
|
||||
"visualizzazioni" => [ '$sum' => 1 ]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
function getDailyBannerClicksPipeline($startDate, $endDate) {
|
||||
return [
|
||||
[
|
||||
'$project' => [
|
||||
'tdate'=> [
|
||||
'$add' => [ '$date' , 60*60*1000 ]
|
||||
],
|
||||
"statType" => '$statType',
|
||||
'user' => '$user',
|
||||
'banner' => '$content.banner',
|
||||
]
|
||||
],
|
||||
[
|
||||
'$match' => [
|
||||
'statType' => StatisticModel::STAT_TYPE_BANNER_CLICK,
|
||||
'tdate' => [
|
||||
'$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000),
|
||||
'$lte'=>new MongoDB\BSON\UTCDateTime($endDate * 1000)
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'date' => [
|
||||
'day' => [ '$dayOfMonth' => '$tdate' ],
|
||||
"month" => [ '$month' => '$tdate' ],
|
||||
"year" => [ '$year'=>'$tdate' ]
|
||||
],
|
||||
'banner' => '$banner',
|
||||
],
|
||||
"visualizzazioni" => [ '$sum' => 1 ]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
function getDailyNotiziaImpressionsPipeline($startDate, $endDate) {
|
||||
return [
|
||||
[
|
||||
'$project' => [
|
||||
'tdate'=> [
|
||||
'$add' => [ '$date' , 60*60*1000 ]
|
||||
],
|
||||
"statType" => '$statType',
|
||||
'user' => '$user',
|
||||
"content" => '$content'
|
||||
]
|
||||
],
|
||||
[
|
||||
'$match' => [
|
||||
'statType' => StatisticModel::STAT_TYPE_NOTIZIA,
|
||||
'tdate' => [
|
||||
'$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000),
|
||||
'$lte'=>new MongoDB\BSON\UTCDateTime($endDate * 1000)
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'date' => [
|
||||
'day' => [ '$dayOfMonth' => '$tdate' ],
|
||||
"month" => [ '$month' => '$tdate' ],
|
||||
"year" => [ '$year'=>'$tdate' ]
|
||||
],
|
||||
'notizia' => '$content.notizia',
|
||||
],
|
||||
"visualizzazioni" => [ '$sum' => 1 ]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
function getYearlyNotiziaImpressionsPipeline($startDate, $endDate) {
|
||||
return [
|
||||
[
|
||||
'$project' => [
|
||||
'tdate'=> [
|
||||
'$add' => [ '$date' , 60*60*1000 ]
|
||||
],
|
||||
"statType" => '$statType',
|
||||
'user' => '$user',
|
||||
"content" => '$content'
|
||||
]
|
||||
],
|
||||
[
|
||||
'$match' => [
|
||||
'statType' => StatisticModel::STAT_TYPE_NOTIZIA,
|
||||
'tdate' => [
|
||||
'$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000),
|
||||
'$lte'=>new MongoDB\BSON\UTCDateTime($endDate * 1000)
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'notizia' => '$content.notizia',
|
||||
],
|
||||
"visualizzazioni" => [ '$sum' => 1 ]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
function getDailyPageImpressionsPipeline($startDate, $endDate) {
|
||||
return [
|
||||
[
|
||||
'$project' => [
|
||||
'tdate'=> [
|
||||
'$add' => [ '$date' , 60*60*1000 ]
|
||||
],
|
||||
"statType" => '$statType',
|
||||
'user' => '$user',
|
||||
'content' => '$content',
|
||||
]
|
||||
],
|
||||
[
|
||||
'$match' => [
|
||||
'statType' => StatisticModel::STAT_TYPE_PAGE,
|
||||
'tdate' => [
|
||||
'$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000),
|
||||
'$lte'=>new MongoDB\BSON\UTCDateTime($endDate * 1000)
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'date' => [
|
||||
'day' => [ '$dayOfMonth' => '$tdate' ],
|
||||
"month" => [ '$month' => '$tdate' ],
|
||||
"year" => [ '$year'=>'$tdate' ]
|
||||
],
|
||||
'user' => '$user.id',
|
||||
'content' => '$content',
|
||||
],
|
||||
"visualizzazioni" => [ '$sum' => 1 ]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'date' => '$_id.date',
|
||||
'content' => '$_id.content',
|
||||
],
|
||||
"visualizzazioni" => [ '$sum' => '$visualizzazioni' ],
|
||||
'isNewToday' => [ '$sum' => 1 ],
|
||||
'isAbsoluteNew' => [ '$sum' => 1 ]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
function getMonthlyPageImpressionsPipeline($startDate, $endDate) {
|
||||
return [
|
||||
[
|
||||
'$match' => [
|
||||
'statType' => StatisticModel::STAT_TYPE_PAGE,
|
||||
'tdate' => [
|
||||
'$gte'=>new MongoDB\BSON\UTCDateTime($startDate * 1000),
|
||||
'$lte'=>new MongoDB\BSON\UTCDateTime($endDate * 1000)
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'date' => [
|
||||
"month" => [ '$month' => '$tdate' ],
|
||||
"year" => [ '$year'=>'$tdate' ]
|
||||
],
|
||||
'user' => '$user.id',
|
||||
'content' => '$content',
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
'$group' => [
|
||||
'_id' => [
|
||||
'date' => '$_id.date',
|
||||
'content' => '$_id.content',
|
||||
],
|
||||
"isMonthlyNew" => [ '$sum' => 1 ]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
for ($y = 2016; $y <= intval(date("Y")); $y++) {
|
||||
echo "Working on year : $y", PHP_EOL;
|
||||
|
||||
$from = strtotime($y."-01-01");
|
||||
$to = strtotime($y."-12-31");
|
||||
|
||||
$dao = GlobalVariables::get("dao");
|
||||
if ($dao instanceof GenericDao) {
|
||||
$pipe = getDailyPageImpressionsPipeline( $from, $to );
|
||||
|
||||
echo " - Working on Daily Page Impressions", PHP_EOL;
|
||||
$data = $dao->aggregate(
|
||||
StatisticModel::class,
|
||||
$pipe,
|
||||
['cursor' => true]
|
||||
);
|
||||
|
||||
foreach ($data as $elem) {
|
||||
AnalyticsHelper::multiPageImpression(
|
||||
$elem->_id->content,
|
||||
$elem->_id->date->day,
|
||||
$elem->_id->date->month,
|
||||
$elem->_id->date->year,
|
||||
$elem->visualizzazioni,
|
||||
$elem->isNewToday,
|
||||
0,
|
||||
$elem->isAbsoluteNew
|
||||
);
|
||||
}
|
||||
|
||||
$pipe = getMonthlyPageImpressionsPipeline( $from, $to );
|
||||
|
||||
echo " - Working on Monthly Page Impressions", PHP_EOL;
|
||||
$data = $dao->aggregate(
|
||||
StatisticModel::class,
|
||||
$pipe,
|
||||
['cursor' => true]
|
||||
);
|
||||
|
||||
foreach ($data as $elem) {
|
||||
AnalyticsHelper::multiPageImpression(
|
||||
$elem->_id->content,
|
||||
1,
|
||||
$elem->_id->date->month,
|
||||
$elem->_id->date->year,
|
||||
0,
|
||||
0,
|
||||
$elem->isMonthlyNew,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if ($y == 2022) {
|
||||
$pipe = getDailyNotiziaImpressionsPipeline( $from, $to );
|
||||
}
|
||||
else {
|
||||
$pipe = getYearlyNotiziaImpressionsPipeline($from, $to);
|
||||
}
|
||||
|
||||
echo " - Working on Notizia Impressions", PHP_EOL;
|
||||
$data = $dao->aggregate(
|
||||
StatisticModel::class,
|
||||
$pipe,
|
||||
['cursor' => true]
|
||||
);
|
||||
|
||||
foreach ($data as $elem) {
|
||||
$day = $y == 2022 ? $elem->_id->date->day : 1;
|
||||
$month = $y == 2022 ? $elem->_id->date->month : 1;
|
||||
AnalyticsHelper::multiNotiziaImpression(
|
||||
$elem->_id->notizia,
|
||||
$day,
|
||||
$month,
|
||||
$y,
|
||||
$elem->visualizzazioni
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$pipe = getDailyBannerImpressionsPipeline( $from, $to );
|
||||
|
||||
echo " - Working on Banner Impressions", PHP_EOL;
|
||||
$data = $dao->aggregate(
|
||||
StatisticModel::class,
|
||||
$pipe,
|
||||
['cursor' => true]
|
||||
);
|
||||
|
||||
foreach ($data as $elem) {
|
||||
AnalyticsHelper::multiBannerImpression(
|
||||
$elem->_id->banner,
|
||||
$elem->_id->date->day,
|
||||
$elem->_id->date->month,
|
||||
$elem->_id->date->year,
|
||||
$elem->visualizzazioni
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$pipe = getDailyBannerClicksPipeline( $from, $to );
|
||||
|
||||
echo " - Working on Banner Clicks", PHP_EOL;
|
||||
$data = $dao->aggregate(
|
||||
StatisticModel::class,
|
||||
$pipe,
|
||||
['cursor' => true]
|
||||
);
|
||||
|
||||
foreach ($data as $elem) {
|
||||
AnalyticsHelper::multiBannerClick(
|
||||
$elem->_id->banner,
|
||||
$elem->_id->date->day,
|
||||
$elem->_id->date->month,
|
||||
$elem->_id->date->year,
|
||||
$elem->visualizzazioni
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
@@ -39,22 +39,22 @@ class AnalyticsHelper {
|
||||
);
|
||||
}
|
||||
|
||||
public static function pageImpression($content, $d, $m, $y, $isNewToday, $isNewMonth, $isNewAbsolute) {
|
||||
public static function multiPageImpression($content, $d, $m, $y, $incViews = 1, $incToday = 0, $incMonth = 0, $incAbsolute = 0) {
|
||||
$dao = GlobalVariables::get("dao");
|
||||
if ($dao instanceof GenericDao) {
|
||||
$upd = [
|
||||
'$inc' => [
|
||||
'views' => 1
|
||||
'views' => $incViews
|
||||
]
|
||||
];
|
||||
if ($isNewAbsolute || $isNewMonth || $isNewToday) {
|
||||
$upd['$inc']['dayNewVisitors'] = 1;
|
||||
if ($incToday > 0) {
|
||||
$upd['$inc']['dayNewVisitors'] = $incToday;
|
||||
}
|
||||
if ($isNewAbsolute || $isNewMonth) {
|
||||
$upd['$inc']['monthNewVisitors'] = 1;
|
||||
if ($incMonth > 0) {
|
||||
$upd['$inc']['monthNewVisitors'] = $incMonth;
|
||||
}
|
||||
if ($isNewAbsolute) {
|
||||
$upd['$inc']['absNewVisitors'] = 1;
|
||||
if ($incAbsolute > 0) {
|
||||
$upd['$inc']['absNewVisitors'] = $incAbsolute;
|
||||
}
|
||||
$dao->rawUpsert(
|
||||
PageViewStatsModel::class,
|
||||
@@ -64,6 +64,19 @@ class AnalyticsHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public static function pageImpression($content, $d, $m, $y, $isNewToday, $isNewMonth, $isNewAbsolute) {
|
||||
self::multiPageImpression(
|
||||
$content,
|
||||
$d,
|
||||
$m,
|
||||
$y,
|
||||
1,
|
||||
$isNewAbsolute || $isNewMonth || $isNewToday ? 1 : 0,
|
||||
$isNewAbsolute || $isNewMonth ? 1 : 0,
|
||||
$isNewAbsolute ? 1 : 0
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* La funzione si occupa del salvataggio vero e proprio della impression della pagina.
|
||||
* UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/pageImpression.php). Nelle pagine base utilizzare invece la funzione di log
|
||||
@@ -99,8 +112,7 @@ class AnalyticsHelper {
|
||||
GUIHandler::addOnLoadJsEvent($function);
|
||||
}
|
||||
|
||||
|
||||
public static function notiziaImpression($content, $d, $m, $y) {
|
||||
public static function multiNotiziaImpression($content, $d, $m, $y, $incViews) {
|
||||
$dao = GlobalVariables::get("dao");
|
||||
if ($dao instanceof GenericDao) {
|
||||
$dao->rawUpsert(
|
||||
@@ -108,13 +120,17 @@ class AnalyticsHelper {
|
||||
ArticleStatsModel::getFilter($content, $d, $m, $y),
|
||||
[
|
||||
'$inc' => [
|
||||
'views' => 1
|
||||
'views' => $incViews
|
||||
]
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function notiziaImpression($content, $d, $m, $y) {
|
||||
self::multiNotiziaImpression($content, $d, $m, $y, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* La funzione si occupa del salvataggio vero e proprio della impression della notizia.
|
||||
* UTILIZZARE QUESTA FUNZIONE SOLO NELLA PAGINA DI BACKEND (stats/notiziaImpression.php). Nelle pagine base utilizzare invece la funzione di log
|
||||
@@ -160,7 +176,7 @@ class AnalyticsHelper {
|
||||
}
|
||||
|
||||
|
||||
public static function bannerImpression($bannerId, $d, $m, $y) {
|
||||
public static function multiBannerImpression($bannerId, $d, $m, $y, $views) {
|
||||
$dao = GlobalVariables::get("dao");
|
||||
if ($dao instanceof GenericDao) {
|
||||
$dao->rawUpsert(
|
||||
@@ -168,25 +184,32 @@ class AnalyticsHelper {
|
||||
BannerStatsModel::getFilter($bannerId, $d, $m, $y),
|
||||
[
|
||||
'$inc' => [
|
||||
'views' => 1
|
||||
'views' => $views
|
||||
]
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
public static function bannerImpression($bannerId, $d, $m, $y) {
|
||||
self::multiBannerImpression($bannerId, $d, $m, $y, 1);
|
||||
}
|
||||
|
||||
public static function multiBannerClick($bannerId, $d, $m, $y, $clicks) {
|
||||
$dao = GlobalVariables::get("dao");
|
||||
if ($dao instanceof GenericDao) {
|
||||
$dao->rawUpsert(
|
||||
BannerStatsModel::class,
|
||||
BannerStatsModel::getFilter($bannerId, $d, $m, $y),
|
||||
[
|
||||
'$inc' => [
|
||||
'clicks' => $clicks
|
||||
]
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
public static function bannerClick($bannerId, $d, $m, $y) {
|
||||
$dao = GlobalVariables::get("dao");
|
||||
if ($dao instanceof GenericDao) {
|
||||
$dao->rawUpsert(
|
||||
BannerStatsModel::class,
|
||||
BannerStatsModel::getFilter($bannerId, $d, $m, $y),
|
||||
[
|
||||
'$inc' => [
|
||||
'clicks' => 1
|
||||
]
|
||||
]
|
||||
);
|
||||
}
|
||||
self::multiBannerClick($bannerId, $d, $m, $y, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -275,72 +298,6 @@ class AnalyticsHelper {
|
||||
return sizeof($ret) == 0 ? 0 : $ret[0]['tot'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Restituisce il banner da mostrare tenendo in considerazione quelli già visualizzati e le visualizzazioni totali
|
||||
* @param int $position
|
||||
* @param string $replaced id del banner rimpiazzato
|
||||
*/
|
||||
public static function getBannerToShow($position, $replaced = null, $category = BannerModel::GENERAL_CATEGORY) {
|
||||
// ?TODO: Ragionaci
|
||||
// Rimuovi il replaced dalla lista
|
||||
$shown = array();
|
||||
$rval = null;
|
||||
if (ASDSessionHandler::sessionValueExists("bannerActive")){
|
||||
$shown = ASDSessionHandler::getSessionValue("bannerActive");
|
||||
|
||||
if (!is_null($replaced) && in_array($replaced, $shown)){
|
||||
$key = array_search($replaced, $shown);
|
||||
unset($shown[$key]);
|
||||
ASDSessionHandler::setSessionValue("bannerActive",$shown);
|
||||
}
|
||||
}
|
||||
|
||||
// idlist contiene gli id dei banner in ordine di visualizzazione crescente (esclusi quelli non visualizzati)
|
||||
$pipeline = PipelineHelper::getTodaysBannerVisualizationPipeline($position);
|
||||
$stats = GlobalVariables::get("dao")->aggregate("StatisticModel",$pipeline);
|
||||
// var_dump($stats);
|
||||
$idList = array();
|
||||
if (sizeof($stats)>0){
|
||||
foreach ($stats as $stat){
|
||||
$idList[] = $stat["_id"]["banner"];
|
||||
}
|
||||
}
|
||||
|
||||
// $baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDate(strtotime("tomorrow"))),"categories"=>$category);
|
||||
$baseSearchArray = array("position"=>$position,"end"=>array('$gte'=>new MongoDB\BSON\UTCDateTime( strtotime("tomorrow CET") * 1000 ) ),"categories"=>$category);
|
||||
// Prendi il primo dei "mai mostrati oggi" tra quelli non presenti nella pagina
|
||||
$todayPlusShown = array_merge($idList,$shown);
|
||||
if (sizeof($todayPlusShown)>0){
|
||||
$tmp = array_map(function ($id) {return new MongoDB\BSON\ObjectID($id);}, $todayPlusShown);
|
||||
$tmp = array('_id'=>array('$nin'=>$tmp));
|
||||
$rval = GlobalVariables::get("dao")->getFirst("BannerModel", array_merge($baseSearchArray, $tmp) );
|
||||
}
|
||||
else {
|
||||
$rval = GlobalVariables::get("dao")->getFirst("BannerModel", $baseSearchArray);
|
||||
}
|
||||
|
||||
if (is_null($rval)){
|
||||
$todayLessShown = array_values(array_diff($idList, $shown));
|
||||
if (sizeof($todayLessShown)>0){
|
||||
$i = 0;
|
||||
while (is_null($rval) && $i<sizeof($todayLessShown)){
|
||||
$condition = array_merge($baseSearchArray, array("id"=>$todayLessShown[$i]) );
|
||||
$rval = GlobalVariables::get("dao")->getFirst("BannerModel", $condition );
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!is_null($rval)){
|
||||
$shown[] = $rval->id;
|
||||
ASDSessionHandler::setSessionValue("bannerActive",$shown);
|
||||
}
|
||||
return $rval;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -258,6 +258,10 @@ class GenericDao{
|
||||
] );
|
||||
$cursor = $this->client->executeCommand( $this->dbName, $command );
|
||||
|
||||
if (array_key_exists('cursor', $options) && $options['cursor'] == true) {
|
||||
return $cursor;
|
||||
}
|
||||
else {
|
||||
$rval = [];
|
||||
foreach ($cursor as $ele){ // inganniamo il cursore, in realtà il risultato è uno
|
||||
//TODO CHECK
|
||||
@@ -267,6 +271,8 @@ class GenericDao{
|
||||
return $rval;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,10 +49,6 @@ services:
|
||||
§PROJECT_BUILDNAME§-network:
|
||||
|
||||
networks:
|
||||
nginx-network:
|
||||
external:
|
||||
name:
|
||||
nginx-network
|
||||
db-network:
|
||||
§PROJECT_BUILDNAME§-network:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user