...
 
Commits (5)
<?php
namespace Minds\Controllers\api\v2\analytics;
use Minds\Api\Factory;
......@@ -19,6 +18,11 @@ class views implements Interfaces\Api
return Factory::response([]);
}
/**
* @param array $pages
* @return void
* @throws \Exception
*/
public function post($pages)
{
$viewsManager = new Core\Analytics\Views\Manager();
......@@ -60,34 +64,35 @@ class views implements Interfaces\Api
);
}
} catch (\Exception $e) {
return Factory::response([
Factory::response([
'status' => 'error',
'message' => $e->getMessage(),
]);
return;
}
return Factory::response([]);
Factory::response([]);
return;
}
$urn = (string) $urn;
$expire = Di::_()->get('Boost\Network\Expire');
$metrics = Di::_()->get('Boost\Network\Metrics');
$manager = Di::_()->get('Boost\Network\Manager');
$metrics = new Boost\Network\Metrics();
$manager = new Boost\Network\Manager();
$boost = $manager->get($urn, [ 'hydrate' => true ]);
if (!$boost) {
return Factory::response([
Factory::response([
'status' => 'error',
'message' => 'Could not find boost'
]);
return;
}
$count = $metrics->incrementViews($boost);
if ($count > $boost->getImpressions()) {
$expire->setBoost($boost);
$expire->expire();
$manager->expire($boost);
}
Counters::increment($boost->getEntity()->guid, "impression");
......@@ -106,21 +111,23 @@ class views implements Interfaces\Api
error_log($e);
}
return Factory::response([
Factory::response([
'status' => 'success',
'impressions' => $boost->getImpressions(),
'impressions_met' => $count,
]);
return;
break;
case 'activity':
case 'entity':
$entity = Entities\Factory::build($pages[1]);
if (!$entity) {
return Factory::response([
Factory::response([
'status' => 'error',
'message' => 'Could not the entity'
]);
return;
}
if ($entity->type === 'activity') {
......@@ -169,16 +176,16 @@ class views implements Interfaces\Api
break;
}
return Factory::response([]);
Factory::response([]);
}
public function put($pages)
{
return Factory::response([]);
Factory::response([]);
}
public function delete($pages)
{
return Factory::response([]);
Factory::response([]);
}
}
......@@ -2,18 +2,17 @@
namespace Minds\Core\Boost;
use Minds\Core\Data;
use Minds\Core\Data\Client;
use Minds\Core\Di\Provider;
use Minds\Core\Di;
/**
* Boost Providers
*/
class BoostProvider extends Provider
class BoostProvider extends Di\Provider
{
/**
* Registers providers onto DI
* @return void
* @throws Di\ImmutableException
*/
public function register()
{
......@@ -21,52 +20,50 @@ class BoostProvider extends Provider
return new Repository();
}, ['useFactory' => true]);
$this->di->bind('Boost\Network', function ($di) {
return new Network([], Client::build('MongoDB'), new Data\Call('entities_by_time'));
}, ['useFactory' => true]);
$this->di->bind('Boost\Network\Manager', function ($di) {
return new Network\Manager;
return new Network\Manager();
}, ['useFactory' => false]);
$this->di->bind('Boost\Network\Iterator', function ($di) {
return new Network\Iterator();
}, ['useFactory' => false]);
$this->di->bind('Boost\Network\Metrics', function ($di) {
return new Network\Metrics(Client::build('MongoDB'));
return new Network\Metrics();
}, ['useFactory' => false]);
$this->di->bind('Boost\Network\Review', function ($di) {
return new Network\Review();
}, ['useFactory' => false]);
$this->di->bind('Boost\Newsfeed', function ($di) {
return new Newsfeed([], Client::build('MongoDB'), new Data\Call('entities_by_time'));
}, ['useFactory' => true]);
$this->di->bind('Boost\Content', function ($di) {
return new Content([], Client::build('MongoDB'), new Data\Call('entities_by_time'));
}, ['useFactory' => true]);
$this->di->bind('Boost\Peer', function ($di) {
return new Peer();
}, ['useFactory' => true]);
$this->di->bind('Boost\Peer\Metrics', function ($di) {
return new Peer\Metrics(Client::build('MongoDB'));
}, ['useFactory' => false]);
$this->di->bind('Boost\Peer\Review', function ($di) {
return new Peer\Review();
}, ['useFactory' => false]);
$this->di->bind('Boost\Payment', function ($di) {
return new Payment();
}, ['useFactory' => true]);
$this->di->bind(Campaigns\Dispatcher::getDiAlias(), function ($di) {
return new Campaigns\Dispatcher();
}, ['useFactory' => true]);
$this->di->bind(Campaigns\Metrics::getDiAlias(), function ($di) {
return new Campaigns\Metrics();
}, ['useFactory' => true]);
$this->di->bind(Campaigns\Manager::getDiAlias(), function ($di) {
return new Campaigns\Manager();
}, ['useFactory' => true]);
$this->di->bind(Campaigns\Repository::getDiAlias(), function ($di) {
return new Campaigns\Repository();
}, ['useFactory' => true]);
$this->di->bind(Campaigns\Stats::getDiAlias(), function ($di) {
return new Campaigns\Stats();
}, ['useFactory' => true]);
......
<?php
/**
* Description
*
* @author emi
*/
namespace Minds\Core\Boost;
use Minds\Entities\Entity;
......
......@@ -133,9 +133,6 @@ class Boost
/** @var string $checksum */
private $checksum;
/** @var string $mongoId */
private $mongoId;
/**
* Return the state
*/
......
......@@ -2,22 +2,13 @@
namespace Minds\Core\Boost\Network;
use Minds\Core\Boost\Repository;
use Minds\Core\Data;
use Minds\Core\Di\Di;
use Minds\Entities\Boost\Network;
use Minds\Helpers;
class Metrics
{
protected $mongo;
protected $type;
public function __construct(Data\Interfaces\ClientInterface $mongo = null)
{
$this->mongo = $mongo ?: Data\Client::build('MongoDB');
}
/**
* @param string $type
* @return $this
......@@ -33,96 +24,11 @@ class Metrics
* @param Network $boost
* @return int updated boost impressions count
*/
public function incrementViews($boost)
public function incrementViews($boost): int
{
//increment impression counter
Helpers\Counters::increment((string) $boost->getGuid(), "boost_impressions", 1);
//get the current impressions count for this boost
Helpers\Counters::increment(0, "boost_impressions", 1);
$count = Helpers\Counters::get((string) $boost->getGuid(), "boost_impressions", false);
if ($boost->getMongoId()) {
$count += Helpers\Counters::get((string) $boost->getMongoId(), "boost_impressions", false);
}
return $count;
}
public function getBacklogCount($userGuid = null)
{
$query = [
'state' => 'approved',
'type' => $this->type,
];
if ($userGuid) {
$match['owner_guid'] = $userGuid;
}
return (int) $this->mongo->count('boost', $query);
}
public function getPriorityBacklogCount()
{
return (int) $this->mongo->count('boost', [
'state' => 'approved',
'type' => $this->type,
'priority' => [
'$exists' => true,
'$gt' => 0
],
]);
}
public function getBacklogImpressionsSum()
{
$result = $this->mongo->aggregate('boost', [
[
'$match' => [
'state' => 'approved',
'type' => $this->type
]
],
[
'$group' => [
'_id' => null,
'total' => ['$sum' => '$impressions']
]
]
]);
return reset($result)->total ?: 0;
}
public function getAvgApprovalTime()
{
$result = $this->mongo->aggregate('boost', [
[
'$match' => [
'state' => 'approved',
'type' => $this->type,
'createdAt' => ['$ne' => null],
'approvedAt' => ['$ne' => null]
]
],
[
'$project' => [
'diff' => [
'$subtract' => ['$approvedAt', '$createdAt']
]
]
],
[
'$group' => [
'_id' => null,
'count' => ['$sum' => 1],
'diffSum' => ['$sum' => '$diff']
]
]
]);
$totals = reset($result);
return ($totals->diffSum ?: 0) / ($totals->count ?: 1);
return Helpers\Counters::get((string) $boost->getGuid(), "boost_impressions", false);
}
}
<?php
namespace Minds\Core\Boost\Peer;
use Minds\Core\Data;
class Metrics
{
protected $mongo;
public function __construct(Data\Interfaces\ClientInterface $mongo = null)
{
$this->mongo = $mongo ?: Data\Client::build('MongoDB');
}
}
......@@ -3,23 +3,15 @@
namespace Minds\Core\Boost\Peer;
use Minds\Core;
use Minds\Core\Data;
use Minds\Entities;
use Minds\Helpers\MagicAttributes;
use Minds\Interfaces\BoostReviewInterface;
class Review implements BoostReviewInterface
{
/** @var Entities\Boost\Peer $boost */
protected $boost;
protected $mongo;
protected $type;
public function __construct(Data\Interfaces\ClientInterface $mongo = null)
{
$this->mongo = $mongo ?: Data\Client::build('MongoDB');
}
/**
* @param string $type
* @return $this
......
......@@ -209,43 +209,6 @@ class Repository
return $boost;
}
/**
* @param string $type
* @param string $mongo_id
* @return Entities\Boost\BoostEntityInterface|false
*/
public function getEntityById($type, $mongo_id)
{
if (!$type || !$mongo_id) {
return false;
}
$template = "SELECT * FROM boosts_by_mongo_id WHERE type = ? AND mongo_id = ? LIMIT ?";
$values = [
(string) $type,
(string) $mongo_id,
1
];
$query = new Prepared\Custom();
$query->query($template, $values);
$boost = false;
try {
$result = $this->db->request($query);
if (isset($result[0]) && $result[0]) {
$boost = (new Entities\Boost\Factory())->build($result[0]['type']);
$boost->loadFromArray($result[0]['data']);
}
} catch (\Exception $e) {
// TODO: Log or warning
}
return $boost;
}
/**
* Insert or update a boost
* @param string $type
......@@ -271,7 +234,7 @@ class Repository
throw new \Exception('State is required');
}
$template = "INSERT INTO boosts (type, guid, owner_guid, destination_guid, mongo_id, state, data) VALUES (?, ?, ?, ?, ?, ?, ?)";
$template = "INSERT INTO boosts (type, guid, owner_guid, destination_guid, state, data) VALUES (?, ?, ?, ?, ?, ?)";
$destination = null;
......@@ -284,7 +247,6 @@ class Repository
new Cassandra\Varint($data['guid']),
new Cassandra\Varint($data['owner']['guid']),
$destination,
(string) $data['_id'],
(string) $data['state'],
json_encode($data)
];
......