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

(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
parent 5d68c7c7
Loading
Loading
Loading
Loading
+15 −1
Original line number Original line Diff line number Diff line
@@ -30,7 +30,11 @@ class Analytics extends Cli\Controller implements Interfaces\CliControllerInterf
                $this->out('Prints the counts of a user');
                $this->out('Prints the counts of a user');
                $this->out('--from={timestamp in milliseconds} the day to start count. Default is yesterday');
                $this->out('--from={timestamp in milliseconds} the day to start count. Default is yesterday');
                $this->out('--guid={user guid} REQUIRED the user to aggregate');
                $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:
            default:
                $this->out('Syntax usage: cli analytics <type>');
                $this->out('Syntax usage: cli analytics <type>');
                $this->displayCommandHelp();
                $this->displayCommandHelp();
@@ -158,4 +162,14 @@ class Analytics extends Cli\Controller implements Interfaces\CliControllerInterf
        }
        }
        $this->out('Done');
        $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);
    }
}
}
+114 −0
Original line number Original line Diff line number Diff line
<?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;
    }
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -8,6 +8,7 @@ class Time
    const ONE_HOUR = 3600;
    const ONE_HOUR = 3600;
    const TWO_HOUR = 7200;
    const TWO_HOUR = 7200;
    const ONE_DAY = 86400;
    const ONE_DAY = 86400;
    const ONE_DAY_MS = 86400000;
    const ONE_MIN = 60;
    const ONE_MIN = 60;
    const FIVE_MIN = 300;
    const FIVE_MIN = 300;
    const TEN_MIN = 600;
    const TEN_MIN = 600;