...
 
Commits (2)
......@@ -99,7 +99,9 @@ class campaigns implements Interfaces\Api
->setName(trim($_POST['name'] ?? ''))
->setStart((int) ($_POST['start'] ?? 0))
->setEnd((int) ($_POST['end'] ?? 0))
->setBudget((float) ($_POST['budget'] ?? 0));
->setBudget((float) ($_POST['budget'] ?? 0))
->setDailyCap((int) ($_POST['daily_cap'] ?? 0))
->setImpressions($_POST['impressions'] ?? 0);
/** @var Manager $manager */
$manager = Di::_()->get('Boost\Network\Manager');
......
......@@ -77,8 +77,6 @@ class Record
*/
public function recordBoost(): bool
{
/** @var Core\Boost\Network\Expire $expire */
$expire = Di::_()->get('Boost\Network\Expire');
/** @var Core\Boost\Network\Metrics $metrics */
$metrics = Di::_()->get('Boost\Network\Metrics');
/** @var Core\Boost\Network\Manager $manager */
......@@ -92,13 +90,34 @@ class Record
return false;
}
$count = $metrics->incrementViews($boost);
$impressionsTotal = $metrics->incrementTotalViews($boost);
$impressionsRequested = $boost->getImpressions();
$impressionsDaily = $metrics->incrementDailyViews($boost);
if ($count > $boost->getImpressions()) {
$expire->setBoost($boost);
$expire->expire();
$this->boostData = [
'impressions' => $impressionsRequested,
'impressions_met' => $impressionsTotal,
'impressions_daily' => $impressionsDaily
];
if ($boost->getBoostType() === Core\Boost\Network\Boost::BOOST_TYPE_CAMPAIGN) {
$impressionsDailyCap = $boost->getDailyCap();
if ($impressionsDaily >= $impressionsDailyCap) {
// TODO: Pause campaign with status notification when daily cap reached
error_log("boost|pause|daily:{$impressionsDaily}|cap:{$impressionsDailyCap}");
}
} else {
if ($impressionsTotal >= $impressionsRequested) {
$manager->expire($boost);
}
}
$this->boostData = [
'impressions' => $impressionsRequested,
'impressions_met' => $impressionsTotal,
'impressions_daily' => $impressionsDaily
];
Counters::increment($boost->getEntity()->guid, "impression");
Counters::increment($boost->getEntity()->owner_guid, "impression");
......@@ -113,11 +132,6 @@ class Record
error_log($e);
}
$this->boostData = [
'impressions' => $boost->getImpressions(),
'impressions_met' => $count
];
return true;
}
......
......@@ -40,5 +40,9 @@ class BoostProvider extends Di\Provider
$this->di->bind('Boost\Payment', function ($di) {
return new Payment();
}, ['useFactory' => true]);
$this->di->bind('Boost\Network\Metrics', function ($di) {
return new Network\Metrics();
}, ['useFactory' => false]);
}
}
......@@ -94,7 +94,7 @@ class Boost
protected $bidType;
/** @var int $impressions */
protected $impressions;
protected $impressions = 0;
/** @var int $impressionsMet */
protected $impressionsMet;
......@@ -207,6 +207,7 @@ class Boost
'type' => $this->type,
'rejection_reason' => $this->rejectedReason,
'boost_type' => $this->boostType,
'impressions_met' => $this->impressionsMet
];
}
......
......@@ -68,6 +68,8 @@ use Minds\Traits\MagicAttributes;
* @method Campaign setDailyCap(int $dailyCap)
* @method int getPaused()
* @method Campaign setPaused(int $paused)
* @method int getTodayImpressions()
* @method Campaign setTodayImpressions($todayImpressions)
*/
class Campaign extends Boost implements \JsonSerializable
{
......@@ -89,6 +91,8 @@ class Campaign extends Boost implements \JsonSerializable
protected $dailyCap;
/** @var bool $paused */
protected $paused;
/** @var int $todayImpressions */
protected $todayImpressions = 0;
public function export($fields = []): array
{
......@@ -101,7 +105,8 @@ class Campaign extends Boost implements \JsonSerializable
'daily_cap' => $this->dailyCap,
'delivery_status' => $this->getDeliveryStatus(),
'cpm' => $this->cpm(),
'urn' => $this->getUrn()
'urn' => $this->getUrn(),
'today_impressions' => $this->todayImpressions
];
return array_merge($boostExport, $campaignExport);
......
......@@ -80,6 +80,7 @@ class CassandraRepository
$boost->setEnd($data['end']);
$boost->setBudget($data['budget']);
$boost->setPaused($data['paused']);
$boost->setDailyCap($data['daily_cap'] ?? 0);
} else {
$boost = new Boost();
}
......
......@@ -231,6 +231,7 @@ class ElasticRepository
$boost->setEnd($doc['_source']['@end']);
$boost->setBudget($doc['_source']['budget']);
$boost->setPaused($doc['_source']['paused']);
$boost->setDailyCap($doc['_source']['daily_cap'] ?? 0);
} else {
$boost = new Boost();
}
......@@ -309,6 +310,7 @@ class ElasticRepository
$body['doc']['@start'] = $boost->getStart();
$body['doc']['@end'] = $boost->getEnd();
$body['doc']['paused'] = $boost->getPaused();
$body['doc']['daily_cap'] = $boost->getDailyCap();
}
if ($boost->getBidType() === 'tokens') {
......
......@@ -285,7 +285,20 @@ class Manager
'owner_guid' => $this->actor->getGUID(),
'boost_type' => Boost::BOOST_TYPE_CAMPAIGN
], $opts);
return $this->elasticRepository->getList($opts);
$response = $this->elasticRepository->getList($opts);
/** @var Metrics $metrics */
$metrics = Di::_()->get('Boost\Network\Metrics');
/** @var Campaign $campaign */
foreach ($response as $campaign) {
$todayImpressions = $metrics->getDailyViews($campaign);
$totalImpressions = $metrics->getTotalViews($campaign);
$campaign->setTodayImpressions($todayImpressions);
$campaign->setImpressionsMet($totalImpressions);
}
return $response;
}
public function setActor(User $user): self
......
......@@ -7,15 +7,66 @@ use Minds\Helpers;
class Metrics
{
/**
* Increments impressions to a given boost
* @param Boost $boost
* @return int updated boost impressions count
* Increments boost total views
* @param Boost|Campaign $boost
* @return int Updated boost total views
*/
public function incrementViews($boost): int
public function incrementTotalViews($boost): int
{
Helpers\Counters::increment((string) $boost->getGuid(), "boost_impressions", 1);
Helpers\Counters::increment(0, "boost_impressions", 1);
Helpers\Counters::increment((string) $boost->getGuid(), $this->getTotalKey(), 1);
Helpers\Counters::increment(0, $this->getTotalKey(), 1);
return Helpers\Counters::get((string) $boost->getGuid(), "boost_impressions", false);
return $this->getTotalViews($boost);
}
/**
* Increment boost daily views
* @param Boost|Campaign $boost
* @return int Updated boost daily views
*/
public function incrementDailyViews($boost): int
{
Helpers\Counters::increment((string) $boost->getGuid(), $this->getDailyKey(), 1);
Helpers\Counters::increment(0, $this->getDailyKey(), 1);
return $this->getDailyViews($boost);
}
/**
* Get the boost total views value
* @param Boost|Campaign $boost
* @return int Total boost views
*/
public function getTotalViews($boost): int
{
return Helpers\Counters::get((string) $boost->getGuid(), $this->getTotalKey(), false);
}
/**
* Get the boost daily views value
* @param Boost|Campaign $boost
* @return int Daily boost views
*/
public function getDailyViews($boost): int
{
return Helpers\Counters::get((string) $boost->getGuid(), $this->getDailyKey(), false);
}
/**
* Returns key for boost impressions metric
* @return string
*/
public function getTotalKey(): string
{
return 'boost_impressions';
}
/**
* Returns key for boost daily impressions metric
* @return string
*/
public function getDailyKey(): string
{
return 'boost_impressions_' . date('dmy');
}
}