Commit feb0fb6a authored by Marcelo Rivera's avatar Marcelo Rivera Committed by Mark Harding
Browse files

(fix): fixes to queries

(fix): never include last unit in given timestamp
parent 11bb14cd
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -18,25 +18,25 @@ class analytics implements Interfaces\Api, Interfaces\ApiIgnorePam
            ]);
        }

        $span = 12;
        $span = 13;
        $unit = 'month';

        switch ($_GET['timespan'] ?? null) {
            case 'hourly':
                $span = 24;
                $span = 25;
                $unit = 'hour';
                break;
            case 'daily':
                $span = 7;
                $span = 17;
                $unit = 'day';
                break;
            case 'monthly':
                $span = 12;
                $span = 13;
                $unit = 'month';
                break;
        }

        /** @var Core\Analytics\Metrics\Manager $manager */
        /** @var Core\Analytics\Graphs\Manager $manager */
        $manager = Di::_()->get('Analytics\Graphs\Manager');

        try {
+60 −18
Original line number Diff line number Diff line
@@ -3,11 +3,11 @@
namespace Minds\Core\Analytics\Graphs\Aggregates;

use DateTime;
use Minds\Core\Analytics\Graphs\Manager;
use Minds\Core\Data\cache\abstractCacher;
use Minds\Core\Data\ElasticSearch;
use Minds\Core\Data\ElasticSearch\Client;
use Minds\Core\Di\Di;
use Minds\Core\Analytics\Graphs\Manager;

class ActiveUsers implements AggregateInterface
{
@@ -39,14 +39,36 @@ class ActiveUsers implements AggregateInterface
    {
        $result = [];
        foreach (['hour', 'day', 'month'] as $unit) {
            switch ($unit) {
                case 'hour':
                    $span = 25;
                    break;
                case 'day':
                    $span = 17;
                    break;
                case 'month':
                    $span = 13;
                    break;
            }
            $k = Manager::buildKey([
                'aggregate' => $opts['aggregate'] ?? 'activeusers',
                'key' => null,
                'unit' => $unit,
                'span' => $span
            ]);
            $result[$k] = $this->fetch([
                'unit' => $unit,
                'span' => $span,
            ]);

            $avgKey = Manager::buildKey([
                'aggregate' => $opts['aggregate'] ?? 'activeusers',
                'key' => 'avg',
                'unit' => $unit,
                'span' => $span
            ]);

            $result[$avgKey] = Manager::calculateAverages($result[$k]);
        }
        return $result;
    }
@@ -54,7 +76,7 @@ class ActiveUsers implements AggregateInterface
    public function fetch(array $options = [])
    {
        $options = array_merge([
            'span' => 12,
            'span' => 13,
            'unit' => 'month', // day / month
            'userGuid' => null,
        ], $options);
@@ -64,21 +86,29 @@ class ActiveUsers implements AggregateInterface
        $from = null;
        switch ($options['unit']) {
            case "hour":
                $from = (new DateTime('midnight'))->modify("-{$options['span']} hours");
                $to = (new DateTime('midnight'));
                $to = new DateTime('now');
                $from = (new DateTime())
                    ->setTimestamp($to->getTimestamp())
                    ->modify("-{$options['span']} hours");

                $this->dateFormat = 'y-m-d H:i';

                return $this->getHourlyPageviews($from, $to, $user_guid);
            case "day":
                $from = (new DateTime('midnight'))->modify("-{$options['span']} days");
                $to = (new DateTime('midnight'));
                $to = new DateTime('now');
                $from = (new DateTime('midnight'))
                    ->modify("-{$options['span']} days");

                $this->dateFormat = 'y-m-d';

                return $this->getDailyPageviews($from, $to, $user_guid);
                break;
            case "month":
                $from = (new DateTime('midnight first day of next month'))->modify("-{$options['span']} months");
                $to = new DateTime('midnight first day of next month');
                $from = (new DateTime())
                    ->setTimestamp($to->getTimestamp())
                    ->modify("-{$options['span']} months");

                $this->dateFormat = 'y-m';

                return $this->getMonthlyPageviews($from, $to, $user_guid);
@@ -159,12 +189,14 @@ class ActiveUsers implements AggregateInterface

        $response = [
            [
                'name' => 'HAU (Logged In)',
                'key' => 'loggedInHAU',
                'name' => 'Hourly Active Users',
                'x' => [],
                'y' => []
            ],
            [
                'name' => 'HAU (Unique)',
                'key' => 'uniqueHAU',
                'name' => 'Hourly Unique Visits',
                'x' => [],
                'y' => []
            ]
@@ -254,18 +286,19 @@ class ActiveUsers implements AggregateInterface

        $response = [
            [
                'name' => 'DAU (Logged In)',
                'key' => 'loggedInDAU',
                'name' => 'Daily Active Users',
                'x' => [],
                'y' => []
            ],
            [
                'name' => 'DAU (Unique)',
                'key' => 'uniqueDAU',
                'name' => 'Daily Unique Visits',
                'x' => [],
                'y' => []
            ]
        ];


        foreach ($result['aggregations']['histogram']['buckets'] as $count) {
            $date = date($this->dateFormat, $count['key'] / 1000);

@@ -291,6 +324,13 @@ class ActiveUsers implements AggregateInterface
                    ]
                ]
            ],
            [
                "match_phrase" => [
                    "platform.keyword" => [
                        "query" => "browser"
                    ]
                ]
            ]
        ];

        // filter by user_guid
@@ -359,22 +399,24 @@ class ActiveUsers implements AggregateInterface
 
        $response = [
            [
                'name' => 'MAU',
                'key' => 'loggedInMAU',
                'name' => 'Monthly Active Users',
                'x' => [],
                'y' => []
            ],
            [
                'name' => 'Visitors',
                'key' => 'uniqueMAU',
                'name' => 'Monthly Active Visits',
                'x' => [],
                'y' => []
            ],
            [
                'name' => 'Avg. DAU',
                'name' => 'Avg. Daily Unique Visits',
                'x' => [],
                'y' => []
            ],
            [
                'name' => 'DAU',
                'name' => 'Daily Unique Visits',
                'x' => [],
                'y' => []
            ]
+23 −36
Original line number Diff line number Diff line
@@ -3,11 +3,11 @@
namespace Minds\Core\Analytics\Graphs\Aggregates;

use DateTime;
use Minds\Core\Analytics\Graphs\Manager;
use Minds\Core\Data\cache\abstractCacher;
use Minds\Core\Data\ElasticSearch;
use Minds\Core\Data\ElasticSearch\Client;
use Minds\Core\Di\Di;
use Minds\Core\Analytics\Graphs\Manager;

class AvgPageviews implements AggregateInterface
{
@@ -34,23 +34,27 @@ class AvgPageviews implements AggregateInterface
    {
        $result = [];
        foreach ([
            'mau_unique',
            'mau_loggedin',
            'dau_loggedin',
            'dau_unique',
                     'total_pageviews',
            'hau_unique',
            'hau_loggedin',
                 ] as $key) {
            foreach ([ 'day', 'month' ] as $unit) {
            foreach ([/*'day',*/ 'month'] as $unit) {
                switch ($unit) {
                    case 'day':
                        $span = 17;
                        break;
                    case 'month':
                        $span = 13;
                        break;
                }
                $k = Manager::buildKey([
                    'aggregate' => $opts['aggregate'] ?? 'avgpageviews',
                    'key' => $key,
                    'unit' => $unit,
                    'span' => $span,
                ]);
                $result[$k] = $this->fetch([
                    'key' => $key,
                    'unit' => $unit
                    'unit' => $unit,
                    'span' => $span,
                ]);
            }
        }
@@ -60,7 +64,7 @@ class AvgPageviews implements AggregateInterface
    public function fetch(array $options = [])
    {
        $options = array_merge([
            'span' => 12,
            'span' => 13,
            'unit' => 'month', // day / month
            'key' => null,
        ], $options);
@@ -74,14 +78,15 @@ class AvgPageviews implements AggregateInterface
        $from = null;
        switch ($options['unit']) {
            case "day":
                $from = (new DateTime('midnight'))->modify("-{$options['span']} days");
                $to = (new DateTime('midnight'));
                $interval = '1d';
                $to = new DateTime('now');
                $from = (new DateTime('midnight'))
                    ->modify("-{$options['span']} days");
                break;
            case "month":
                $from = (new DateTime('midnight first day of next month'))->modify("-{$options['span']} months");
                $to = new DateTime('midnight first day of next month');
                $interval = '1M';
                $from = (new DateTime())
                    ->setTimestamp($to->getTimestamp())
                    ->modify("-{$options['span']} months");
                break;
            default:
                throw new \Exception("{$options['unit']} is not an accepted unit");
@@ -89,27 +94,9 @@ class AvgPageviews implements AggregateInterface

        $response = null;
        switch ($key) {
            case 'mau_unique':
                $response = $this->getMauUnique($from, $to, $interval);
                break;
            case 'mau_loggedin':
                $response = $this->getMauLoggedIn($from, $to, $interval);
                break;
            case 'dau_loggedin':
                $response = $this->getDauLoggedIn($from, $to);
                break;
            case 'dau_unique':
                $response = $this->getDauUnique($from, $to);
                break;
            case 'total_pageviews':
                $response = $this->getTotalPageviews($from, $to);
                break;
            case 'hau_unique':
                $response = $this->getHauUnique($from, $to);
                break;
            case 'hau_loggedin':
                $response = $this->getHauLoggedIn($from, $to);
                break;
        }

        return $response;
+42 −10
Original line number Diff line number Diff line
@@ -7,11 +7,11 @@
namespace Minds\Core\Analytics\Graphs\Aggregates;

use DateTime;
use Minds\Core\Analytics\Graphs\Manager;
use Minds\Core\Data\cache\abstractCacher;
use Minds\Core\Data\ElasticSearch\Client;
use Minds\Core\Data\ElasticSearch\Prepared\Search;
use Minds\Core\Di\Di;
use Minds\Core\Analytics\Graphs\Manager;

class Comments implements AggregateInterface
{
@@ -43,14 +43,36 @@ class Comments implements AggregateInterface
    {
        $result = [];
        foreach (['hour', 'day', 'month'] as $unit) {
            switch ($unit) {
                case 'hour':
                    $span = 25;
                    break;
                case 'day':
                    $span = 17;
                    break;
                case 'month':
                    $span = 13;
                    break;
            }
            $k = Manager::buildKey([
                'aggregate' => $opts['aggregate'] ?? 'comments',
                'key' => null,
                'unit' => $unit,
                'span' => $span,
            ]);
            $result[$k] = $this->fetch([
                'unit' => $unit,
                'span' => $span,
            ]);

            $avgKey = Manager::buildKey([
                'aggregate' => $opts['aggregate'] ?? 'comments',
                'key' => 'avg',
                'unit' => $unit,
                'span' => $span,
            ]);

            $result[$avgKey] = Manager::calculateAverages($result[$k]);
        }
        return $result;
    }
@@ -58,7 +80,7 @@ class Comments implements AggregateInterface
    public function fetch(array $options = [])
    {
        $options = array_merge([
            'span' => 12,
            'span' => 13,
            'unit' => 'month', // day / month
            'userGuid' => null,
        ], $options);
@@ -68,20 +90,28 @@ class Comments implements AggregateInterface
        $from = null;
        switch ($options['unit']) {
            case "hour":
                $from = (new DateTime('midnight'))->modify("-{$options['span']} hours");
                $to = (new DateTime('midnight'));
                $to = new DateTime('now');
                $from = (new DateTime())
                    ->setTimestamp($to->getTimestamp())
                    ->modify("-{$options['span']} hours");

                $interval = '1h';
                $this->dateFormat = 'y-m-d H:i';
                break;
            case "day":
                $from = (new DateTime('midnight'))->modify("-{$options['span']} days");
                $to = (new DateTime('midnight'));
                $to = new DateTime('now');
                $from = (new DateTime('midnight'))
                    ->modify("-{$options['span']} days");

                $interval = '1d';
                $this->dateFormat = 'y-m-d';
                break;
            case "month":
                $from = (new DateTime('midnight first day of next month'))->modify("-{$options['span']} months");
                $to = new DateTime('midnight first day of next month');
                $from = (new DateTime())
                    ->setTimestamp($to->getTimestamp())
                    ->modify("-{$options['span']} months");

                $interval = '1M';
                $this->dateFormat = 'y-m';
                break;
@@ -165,6 +195,7 @@ class Comments implements AggregateInterface

        $response = [
            [
                'key' => 'comments',
                'name' => 'Comments',
                'x' => [],
                'y' => []
@@ -172,7 +203,8 @@ class Comments implements AggregateInterface
        ];
        if (!$userGuid) {
            $response[] = [
                'name' => 'Number of Commenting Users',
                'key' => 'commentingUsers',
                'name' => 'Commenting Users',
                'x' => [],
                'y' => []
            ];
+9 −4
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ use Minds\Core\Data\ElasticSearch\Client;
use Minds\Core\Data\ElasticSearch\Prepared\Count;
use Minds\Core\Data\ElasticSearch\Prepared\Search;
use Minds\Core\Di\Di;
use Minds\Core\Analytics\Graphs\Manager;

class Interactions implements AggregateInterface
{
@@ -55,14 +54,20 @@ class Interactions implements AggregateInterface
        $from = null;
        switch ($options['unit']) {
            case "day":
                $from = (new DateTime('midnight'))->modify("-{$options['span']} days");
                $to = (new DateTime('midnight'));
                $to = new DateTime('now');
                $from = (new DateTime())
                    ->setTimestamp($to->getTimestamp())
                    ->modify("-{$options['span']} hours");

                $interval = '1d';
                $this->dateFormat = 'y-m-d';
                break;
            case "month":
                $from = (new DateTime('midnight first day of next month'))->modify("-{$options['span']} months");
                $to = new DateTime('midnight first day of next month');
                $from = (new DateTime())
                    ->setTimestamp($to->getTimestamp())
                    ->modify("-{$options['span']} months");

                $interval = '1M';
                $this->dateFormat = 'y-m';
                break;
Loading