Commit 13a62c92 authored by Guy Thouret's avatar Guy Thouret

Boost Campaign Preview Endpoint - #1201

1 merge request!417WIP: Boost Campaigns
Pipeline #102045404 running with stages
<?php
namespace Minds\Controllers\api\v2\boost\campaigns;
use Minds\Api\AbstractApi;
use Minds\Common\Urn;
use Minds\Core\Boost\Network\Campaign;
use Minds\Core\Boost\Network\CampaignStats;
class preview extends AbstractApi
{
/**
* Equivalent to HTTP POST method
* @param array $pages
* @return mixed|null
*/
public function post($pages): void
{
$urns = $_POST['entity_urns'] ?? [];
if (empty($urns)) {
$this->sendNotAcceptable('No entity_urns');
return;
}
try {
$urn = new Urn($urns[0]);
$entityGuid = (string)$urn->getNss();
} catch (\Exception $e) {
$this->sendNotAcceptable($e->getMessage());
return;
}
$campaign = (new Campaign())
->setType($_POST['type'] ?? '')
->setEntityGuid($entityGuid)
->setStart((int)($_POST['start'] ?? 0))
->setEnd((int)($_POST['end'] ?? 0))
->setBudget((float)($_POST['budget'] ?? 0))
->setImpressions($_POST['impressions']);
$this->send([
'preview' => (new CampaignStats())->setCampaign($campaign)->getAll()
]);
}
}
<?php
namespace Minds\Core\Boost\Network;
use Minds\Core\Analytics\EntityCentric\BoostViewsDaily;
use Minds\Helpers\Time;
class CampaignStats
{
/** @var Campaign */
protected $campaign;
/** @var BoostViewsDaily */
protected $boostViewsDaily;
public function __construct(BoostViewsDaily $boostViewsDaily = null)
{
$this->boostViewsDaily = $boostViewsDaily ?: new BoostViewsDaily();
}
/**
* @param Campaign $campaign
* @return CampaignStats
*/
public function setCampaign(Campaign $campaign): self
{
$this->campaign = $campaign;
return $this;
}
/**
* @return array
*/
public function getAll(): array
{
/* TODO: Evaluate the campaign targeting parameters against our data */
$campaignDurationDays = ($this->campaign->getEnd() - $this->campaign->getStart()) / Time::ONE_DAY_MS;
$campaignViewsPerDayReq = ($campaignDurationDays > 0) ? $this->campaign->getImpressions() / $campaignDurationDays : 0;
$globalViewsPerDay = $this->boostViewsDaily->getAvg();
return [
'canBeDelivered' => ($campaignViewsPerDayReq < $globalViewsPerDay),
'durationDays' => $campaignDurationDays,
'viewsPerDayRequested' => $campaignViewsPerDayReq,
'globalViewsPerDay' => $globalViewsPerDay
];
}
}
Please register or to comment