Commit 6bff170d authored by Guy Thouret's avatar Guy Thouret

(feat) Implement Daily Cap for Boost Campaigns - #1169

2 merge requests!432WIP: Max views per day for Boost Campaign,!417WIP: Boost Campaigns
Pipeline #104984845 failed with stages
in 12 minutes and 37 seconds
......@@ -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');
......
......@@ -132,8 +132,6 @@ class Record
error_log($e);
}
return true;
}
......
......@@ -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
......
......@@ -16,7 +16,7 @@ class Metrics
Helpers\Counters::increment((string) $boost->getGuid(), $this->getTotalKey(), 1);
Helpers\Counters::increment(0, $this->getTotalKey(), 1);
return Helpers\Counters::get((string) $boost->getGuid(), $this->getTotalKey(), false);
return $this->getTotalViews($boost);
}
/**
......@@ -29,14 +29,42 @@ class Metrics
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');
......
Please register or to comment