Commit 033e249f authored by Guy Thouret's avatar Guy Thouret

(feat) BoostViewsData to return total and daily boost views over a time range...

(feat) BoostViewsData to return total and daily boost views over a time range and an Analytics CLI command to test with - #929
1 merge request!235WIP: Boost Campaigns (&24)
Pipeline #87995015 passed with stages
in 12 minutes and 29 seconds
......@@ -30,7 +30,11 @@ class Analytics extends Cli\Controller implements Interfaces\CliControllerInterf
$this->out('Prints the counts of a user');
$this->out('--from={timestamp in milliseconds} the day to start count. Default is yesterday');
$this->out('--guid={user guid} REQUIRED the user to aggregate');
// no break
break;
case 'boostViews':
$this->out('Return total boost views for period with daily breakdown');
$this->out('--from={timestamp} the start day for view range. Default is 10 days ago');
$this->out('--to={timestamp} the end day for view range. Default is yesterday');
default:
$this->out('Syntax usage: cli analytics <type>');
$this->displayCommandHelp();
......@@ -158,4 +162,14 @@ class Analytics extends Cli\Controller implements Interfaces\CliControllerInterf
}
$this->out('Done');
}
public function boostViews()
{
$from = $this->getOpt('from') ?: strtotime('-10 days');
$to = $this->getOpt('to') ?: strtotime('-1 day');
$boostViews = new Core\Analytics\EntityCentric\BoostViewsData();
$data = $boostViews->getDataSetForDateRange($from, $to);
print_r($data);
}
}
<?php
namespace Minds\Core\Analytics\EntityCentric;
use Minds\Core\Data\ElasticSearch\Client;
use Minds\Core\Data\ElasticSearch\Prepared\Search;
use Minds\Core\Di\Di;
use Minds\Core\Time;
class BoostViewsData
{
/**
* @var Client
*/
protected $es;
public function __construct(Client $esClient = null)
{
$this->es = $esClient ?: Di::_()->get('Database\ElasticSearch');
}
/**
* {
* "query": {
* "range" : {
* "views::boosted" : {
* "gt" : 0
* }
* }
* },
* "size": 0,
* "aggs" : {
* "boost_views" : { "sum" : { "field" : "views::boosted" } },
* "boost_views_daily" : {
* "date_histogram": {
* "field": "@timestamp",
* "interval": "1d"
* },
* "aggs" : {
* "boost_views" : { "sum" : { "field" : "views::boosted" } }
* }
* }
* }
* }
*/
public function getDataSetForDateRange(int $start, int $end): array
{
$startDayMs = Time::toInterval($start, Time::ONE_DAY) * 1000;
$endDayMs = Time::toInterval($end, Time::ONE_DAY) * 1000;
$must = [
'range' => [
'@timestamp' => [
'gte' => $startDayMs,
'lte' => $endDayMs,
]
]
];
$query = [
'index' => 'minds-entitycentric-*',
'size' => 0,
'body' => [
'query' => [
'bool' => [
'must' => $must,
],
],
'aggs' => [
'boost_views_total' => [
'sum' => [
'field' => 'views::boosted',
],
],
'boost_views_daily' => [
'date_histogram' => [
'field' => '@timestamp',
'interval' => '1d'
],
'aggs' => [
'boost_views' => [
'sum' => [
'field' => 'views::boosted'
]
]
]
]
]
]
];
$prepared = new Search();
$prepared->query($query);
$response = $this->es->request($prepared);
$dataset = [
'total' => 0,
'daily' => []
];
if (isset($response['aggregations']['boost_views_total'])) {
$dataset['total'] = $response['aggregations']['boost_views_total']['value'];
}
if (isset($response['aggregations']['boost_views_daily']['buckets'])) {
foreach ($response['aggregations']['boost_views_daily']['buckets'] as $bucket) {
$dataset['daily'][$bucket['key']] = $bucket['boost_views']['value'];
}
}
return $dataset;
}
}
......@@ -8,6 +8,7 @@ class Time
const ONE_HOUR = 3600;
const TWO_HOUR = 7200;
const ONE_DAY = 86400;
const ONE_DAY_MS = 86400000;
const ONE_MIN = 60;
const FIVE_MIN = 300;
const TEN_MIN = 600;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment