Files
fdn2/app/private/bin/convertStatsQuick.php
T
2022-09-23 01:33:08 +02:00

363 lines
10 KiB
PHP
Executable File

#!/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
);
}
}
}
?>