Commit 6efd9e52 authored by Guy Thouret's avatar Guy Thouret
Browse files

Refactor Boost Code - #1149

parent ab9311d8
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -171,16 +171,17 @@ class Controller


    /**
    /**
     * Gets the list of publically available commands and filters out the system ones.
     * Gets the list of publically available commands and filters out the system ones.
     * @param array $additionalExcludes Additional methods to exclude from command list (optional)
     */
     */
    public function getCommands()
    public function getCommands(array $additionalExcludes = [])
    {
    {
        $excludedMethods = ['__construct', 'help', 'out', 'setArgs', 'setApp', 'getApp', 'getExecCommand', 'getOpt', 'getOpts', 'getAllOpts', 'getCommands', 'displayCommandHelp'];
        $excludedMethods = ['__construct', 'help', 'out', 'setArgs', 'setApp', 'getApp', 'getExecCommand', 'getOpt', 'getOpts', 'getAllOpts', 'getCommands', 'displayCommandHelp', 'exec', 'gatekeeper'];
        $commands = [];
        $commands = [];
        foreach ((new ReflectionClass($this))->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
        foreach ((new ReflectionClass($this))->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
            $commands[] = $method->getName();
            $commands[] = $method->getName();
        }
        }


        return array_diff($commands, $excludedMethods);
        return array_diff($commands, $excludedMethods, $additionalExcludes);
    }
    }


    public function displayCommandHelp()
    public function displayCommandHelp()
+0 −94
Original line number Original line Diff line number Diff line
<?php
/**
 * Minds Admin: Boost Analytics
 *
 * @version 1
 * @author Emi Balbuena
 *
 */
namespace Minds\Controllers\api\v1\admin\boosts;

use Minds\Controllers\api\v1\newsfeed\preview;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Helpers;
use Minds\Entities;
use Minds\Interfaces;
use Minds\Api\Factory;

class analytics implements Interfaces\Api, Interfaces\ApiAdminPam
{
    /**
     * GET
     */
    public function get($pages)
    {
        $response = [];

        $type = isset($pages[0]) ? $pages[0] : 'newsfeed';

        /** @var Core\Boost\Network\Review $review */
        $review = Di::_()->get('Boost\Network\Review');
        $review->setType($type);
        /** @var Core\Boost\Network\Metrics $metrics */
        $metrics = Di::_()->get('Boost\Network\Metrics');
        $metrics->setType($type);
        $cache = Di::_()->get('Cache');

        $cacheKey = "admin:boosts:analytics:{$type}";

        if ($cached = $cache->get($cacheKey)) {
            return Factory::response($cached);
        }

        $reviewQueue = $review->getReviewQueueCount();

        $backlog = $metrics->getBacklogCount();
        $priorityBacklog = $metrics->getPriorityBacklogCount();

        $impressions = $metrics->getBacklogImpressionsSum();

        $avgApprovalTime = $metrics->getAvgApprovalTime();
        $avgImpressions = round($impressions / ($backlog ?: 1));

        $timestamp = time();

        $response = compact(
            'reviewQueue',
            'backlog',
            'priorityBacklog',
            'impressions',
            'avgApprovalTime',
            'avgImpressions',
            'timestamp'
        );

        $cache->set($cacheKey, $response, 15 * 60 /* 15min cache */);

        return Factory::response($response);
    }

    /**
     * POST
     */
    public function post($pages)
    {
        return Factory::response([]);
    }

    /**
     * PUT
     */
    public function put($pages)
    {
        return Factory::response([]);
    }

    /**
     * DELETE
     */
    public function delete($pages)
    {
        return Factory::response([]);
    }
}
+21 −72
Original line number Original line Diff line number Diff line
<?php
<?php
/**
 * Minds Boost Api endpoint
 *
 * @version 1
 * @author Mark Harding
 *
 */


namespace Minds\Controllers\api\v1\boost;
namespace Minds\Controllers\api\v1\boost;


use Minds\Api\Factory;
use Minds\Api\Factory;
use Minds\Core;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Core\Di\Di;
use Minds\Entities;
use Minds\Entities\Entity;
use Minds\Helpers\Counters;
use Minds\Helpers\Counters;
use Minds\Interfaces;
use Minds\Interfaces;
use Minds\Core\Boost;


class fetch implements Interfaces\Api
class fetch implements Interfaces\Api
{
{
@@ -28,14 +22,16 @@ class fetch implements Interfaces\Api
        $user = Core\Session::getLoggedinUser();
        $user = Core\Session::getLoggedinUser();


        if (!$user) {
        if (!$user) {
            return Factory::response([
            Factory::response([
                'status' => 'error',
                'status' => 'error',
                'message' => 'You must be loggedin to view boosts',
                'message' => 'You must be loggedin to view boosts',
            ]);
            ]);
            return;
        }
        }


        if ($user->disabled_boost && $user->isPlus()) {
        if ($user->disabled_boost && $user->isPlus()) {
            return Factory::response([]);
            Factory::response([]);
            return;
        }
        }


        $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 2;
        $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 2;
@@ -45,35 +41,33 @@ class fetch implements Interfaces\Api


        // options specific to newly created users (<=1 hour) and iOS users
        // options specific to newly created users (<=1 hour) and iOS users
        if (time() - $user->getTimeCreated() <= 3600) {
        if (time() - $user->getTimeCreated() <= 3600) {
            $rating = 1; // they can only see safe content
            $rating = Boost\Network\Boost::RATING_SAFE;
            $quality = 75;
            $quality = 75;
        }
        }


        if ($platform === 'ios') {
        if ($platform === 'ios') {
            $rating = 1; // they can only see safe content
            $rating = Boost\Network\Boost::RATING_SAFE;
            $quality = 90;
            $quality = 90;
        }
        }


        /** @var Core\Boost\Network\Iterator $iterator */
        /** @var  $iterator */
        $iterator = Core\Di\Di::_()->get('Boost\Network\Iterator');
        $iterator = new Core\Boost\Network\Iterator();
        $iterator->setLimit($limit)
        $iterator->setLimit($limit)
            ->setRating($rating)
            ->setRating($rating)
            ->setQuality($quality)
            ->setQuality($quality)
            ->setOffset($_GET['offset'])
            ->setOffset($_GET['offset'])
            ->setType($pages[0])
            ->setType($pages[0])
            ->setPriority(true);
            ->setUserGuid($user->getGUID());


        if (isset($_GET['rating']) && $pages[0] == 'newsfeed') {
        if (isset($_GET['rating']) && $pages[0] == Boost\Network\Boost::TYPE_NEWSFEED) {
            $cacher = Core\Data\cache\factory::build('Redis');
            $cacher = Core\Data\cache\factory::build('Redis');
            $offset =  $cacher->get(Core\Session::getLoggedinUser()->guid . ':boost-offset:newsfeed');
            $offset =  $cacher->get(Core\Session::getLoggedinUser()->guid . ':boost-offset:newsfeed');
            $iterator->setOffset($offset);
            $iterator->setOffset($offset);
        }
        }


        switch ($pages[0]) {
        switch ($pages[0]) {
            case 'content':
            case Boost\Network\Boost::TYPE_CONTENT:
                //$iterator->setOffset('');
                /** @var $entity Entity */
                $iterator->setIncrement(true);

                foreach ($iterator as $guid => $entity) {
                foreach ($iterator as $guid => $entity) {
                    $response['boosts'][] = array_merge($entity->export(), [
                    $response['boosts'][] = array_merge($entity->export(), [
                        'boosted_guid' => (string) $guid,
                        'boosted_guid' => (string) $guid,
@@ -87,7 +81,7 @@ class fetch implements Interfaces\Api
                if (!$response['boosts']) {
                if (!$response['boosts']) {
                    $result = Di::_()->get('Trending\Repository')->getList([
                    $result = Di::_()->get('Trending\Repository')->getList([
                        'type' => 'images',
                        'type' => 'images',
                        'rating' => isset($rating) ? (int) $rating : 1,
                        'rating' => isset($rating) ? (int) $rating : Boost\Network\Boost::RATING_SAFE,
                        'limit' => $limit,
                        'limit' => $limit,
                    ]);
                    ]);


@@ -97,7 +91,7 @@ class fetch implements Interfaces\Api
                    }
                    }
                }
                }
                break;
                break;
            case 'newsfeed':
            case Boost\Network\Boost::TYPE_NEWSFEED:
                foreach ($iterator as $guid => $entity) {
                foreach ($iterator as $guid => $entity) {
                    $response['boosts'][] = array_merge($entity->export(), [
                    $response['boosts'][] = array_merge($entity->export(), [
                        'boosted' => true,
                        'boosted' => true,
@@ -109,70 +103,25 @@ class fetch implements Interfaces\Api
                if (isset($_GET['rating']) && $pages[0] == 'newsfeed') {
                if (isset($_GET['rating']) && $pages[0] == 'newsfeed') {
                    $cacher->set(Core\Session::getLoggedinUser()->guid . ':boost-offset:newsfeed', $iterator->getOffset(), (3600 / 2));
                    $cacher->set(Core\Session::getLoggedinUser()->guid . ':boost-offset:newsfeed', $iterator->getOffset(), (3600 / 2));
                }
                }
                if (!$iterator->list && false) {
                break;
                    $cacher = Core\Data\cache\factory::build('apcu');
                    $offset = (int) $cacher->get(Core\Session::getLoggedinUser()->guid . ":newsfeed-fallover-boost-offset") ?: 0;
                    
                    $posts = $this->getSuggestedPosts([
                        'offset' => $offset,
                        'limit' => $limit,
                        'rating' => $rating,
                    ]);

                    foreach ($posts as $entity) {
                        $entity->boosted = true;
                        $response['boosts'][] = array_merge($entity->export(), [ 'boosted' => true ]);
                    }
                    if (!$response['boosts'] || count($response['boosts']) < 5) {
                        $cacher->destroy(Core\Session::getLoggedinUser()->guid . ":newsfeed-fallover-boost-offset");
                    } else {
                        $cacher->set(Core\Session::getLoggedinUser()->guid . ":newsfeed-fallover-boost-offset", ((int) $offset) + count($posts));
                    }
                }
        }
        }


        return Factory::response($response);
        Factory::response($response);
    }
    }


    /**
     */
    public function post($pages)
    public function post($pages)
    {
    {
        /* Not Implemented */
    }
    }


    /**
     * @param array $pages
     */
    public function put($pages)
    public function put($pages)
    {
    {
        $expire = Core\Di\Di::_()->get('Boost\Network\Expire');
        /* Not Implemented */
        $metrics = Core\Di\Di::_()->get('Boost\Network\Metrics');

        $boost = Core\Boost\Factory::build($pages[0])->getBoostEntity($pages[1]);
        if (!$boost) {
            return Factory::response([
                'status' => 'error',
                'message' => 'Boost not found'
            ]);
        }

        $count = $metrics->incrementViews($boost);

        if ($count > $boost->getImpressions()) {
            $expire->setBoost($boost);
            $expire->expire();
    }
    }


        Counters::increment($boost->getEntity()->guid, "impression");
        Counters::increment($boost->getEntity()->owner_guid, "impression");

        return Factory::response([]);
    }

    /**
     */
    public function delete($pages)
    public function delete($pages)
    {
    {
        /* Not Implemented */
    }
    }


    private function getSuggestedPosts($opts = [])
    private function getSuggestedPosts($opts = [])
+3 −9
Original line number Original line Diff line number Diff line
@@ -181,24 +181,18 @@ class newsfeed implements Interfaces\Api


                /** @var Core\Boost\Network\Iterator $iterator */
                /** @var Core\Boost\Network\Iterator $iterator */
                $iterator = Core\Di\Di::_()->get('Boost\Network\Iterator');
                $iterator = Core\Di\Di::_()->get('Boost\Network\Iterator');
                $iterator->setPriority(!get_input('offset', ''))
                $iterator->setType('newsfeed')
                    ->setType('newsfeed')
                    ->setLimit($limit)
                    ->setLimit($limit)
                    ->setOffset($offset)
                    ->setOffset($offset)
                    //->setRating(0)
                    ->setQuality(0)
                    ->setQuality(0)
                    ->setIncrement(false);
                    ->setUserGuid(Core\Session::getLoggedinUserGuid())
                    ->setRating((int) Core\Session::getLoggedinUser()->getBoostRating());




                foreach ($iterator as $guid => $boost) {
                foreach ($iterator as $guid => $boost) {
                    $boost->boosted = true;
                    $boost->boosted = true;
                    $boost->boosted_guid = (string) $guid;
                    $boost->boosted_guid = (string) $guid;
                    array_unshift($activity, $boost);
                    array_unshift($activity, $boost);
                    //if (get_input('offset')) {
                    //bug: sometimes views weren't being calculated on scroll down
                    //Counters::increment($boost->guid, "impression");
                    //Counters::increment($boost->owner_guid, "impression");
                    //}
                }
                }
                $cacher->set(Core\Session::getLoggedinUser()->guid . ':boost-offset:newsfeed', $iterator->getOffset(), (3600 / 2));
                $cacher->set(Core\Session::getLoggedinUser()->guid . ':boost-offset:newsfeed', $iterator->getOffset(), (3600 / 2));
            } catch (\Exception $e) {
            } catch (\Exception $e) {
+8 −9
Original line number Original line Diff line number Diff line
@@ -23,9 +23,8 @@ class views implements Interfaces\Api


        switch ($pages[0]) {
        switch ($pages[0]) {
            case 'boost':
            case 'boost':
                $expire = Di::_()->get('Boost\Network\Expire');
                $metrics = new Core\Boost\Network\Metrics();
                $metrics = Di::_()->get('Boost\Network\Metrics');
                $manager = new Core\Boost\Network\Manager();
                $manager = Di::_()->get('Boost\Network\Manager');


                $urn = "urn:boost:newsfeed:{$pages[1]}";
                $urn = "urn:boost:newsfeed:{$pages[1]}";


@@ -40,8 +39,7 @@ class views implements Interfaces\Api
                $count = $metrics->incrementViews($boost);
                $count = $metrics->incrementViews($boost);


                if ($count > $boost->getImpressions()) {
                if ($count > $boost->getImpressions()) {
                    $expire->setBoost($boost);
                    $manager->expire($boost);
                    $expire->expire();
                }
                }


                Counters::increment($boost->getEntity()->guid, "impression");
                Counters::increment($boost->getEntity()->guid, "impression");
@@ -71,10 +69,11 @@ class views implements Interfaces\Api
                $entity = Entities\Factory::build($pages[1]);
                $entity = Entities\Factory::build($pages[1]);


                if (!$entity) {
                if (!$entity) {
                    return Factory::response([
                    Factory::response([
                        'status' => 'error',
                        'status' => 'error',
                        'message' => 'Could not the entity'
                        'message' => 'Could not the entity'
                    ]);
                    ]);
                    return;
                }
                }


                if ($entity->type === 'activity') {
                if ($entity->type === 'activity') {
@@ -123,16 +122,16 @@ class views implements Interfaces\Api
                break;
                break;
        }
        }


        return Factory::response([]);
        Factory::response([]);
    }
    }


    public function put($pages)
    public function put($pages)
    {
    {
        return Factory::response([]);
        Factory::response([]);
    }
    }


    public function delete($pages)
    public function delete($pages)
    {
    {
        return Factory::response([]);
        Factory::response([]);
    }
    }
}
}
Loading