Commit 866a01b0 authored by Emiliano Balbuena's avatar Emiliano Balbuena

(refactor): Single campaigns should be fetch from Cassandra

1 merge request!235WIP: Boost Campaigns (&24)
Pipeline #73581229 passed with stages
in 9 minutes
......@@ -10,7 +10,7 @@ use Exception;
class Dispatcher
{
const IMPRESSIONS_SYNC_THRESHOLD = 1;
const IMPRESSIONS_SYNC_THRESHOLD = 5;
/** @var Manager */
protected $manager;
......
......@@ -102,7 +102,15 @@ class Manager
*/
public function getList(array $opts = [])
{
return $this->elasticRepository->getList($opts)->map(function (Campaign $campaign) {
$opts = array_merge([
'useElastic' => true
], $opts);
$response = $opts['useElastic'] ?
$this->elasticRepository->getList($opts) :
$this->repository->getList($opts);
return $response->map(function (Campaign $campaign) {
try {
$campaign
->setPayments(
......@@ -141,8 +149,12 @@ class Manager
* @return Campaign|null
* @throws Exception
*/
public function get($urn)
public function get($urn, array $opts = [])
{
$opts = array_merge([
'useElastic' => false
], $opts);
$urn = new Urn($urn);
$guid = $urn->getNss();
......@@ -151,7 +163,8 @@ class Manager
}
$campaigns = $this->getList([
'guid' => $guid
'guid' => $guid,
'useElastic' => $opts['useElastic'],
])->toArray();
if (!$campaigns) {
......
......@@ -6,12 +6,15 @@
namespace Minds\Core\Boost\Campaigns;
use Cassandra\Varint;
use Cassandra\Bigint;
use Cassandra\Rows;
use Exception;
use Minds\Common\Repository\Response;
use Minds\Core\Data\Cassandra\Client as CassandraClient;
use Minds\Core\Data\Cassandra\Prepared\Custom;
use Minds\Core\Di\Di;
use Minds\Helpers\Number;
use Minds\Helpers\Text;
use NotImplementedException;
class Repository
......@@ -32,11 +35,87 @@ class Repository
/**
* @param array $opts
* @throws NotImplementedException
* @return Response
* @throws Exception
*/
public function getList(array $opts = [])
{
throw new NotImplementedException();
$opts = array_merge([
'limit' => 12,
'offset' => null,
'guid' => null,
], $opts);
if (!$opts['guid']) {
throw new \Exception('Cassandra getList only supports GUID constraint');
}
$cql = "SELECT * FROM boost_campaigns WHERE guid = ?";
$values = [
new Bigint($opts['guid'])
];
$cqlOpts = [];
if ($opts['limit']) {
$cqlOpts['page_size'] = (int) $opts['limit'];
}
if ($opts['offset']) {
$cqlOpts['paging_state_token'] = base64_decode($opts['offset']);
}
$prepared = new Custom();
$prepared->query($cql, $values);
$prepared->setOpts($cqlOpts);
$response = new Response();
try {
/** @var Rows $rows */
$rows = $this->db->request($prepared);
if ($rows) {
foreach ($rows as $row) {
$campaign = new Campaign();
$campaign
->setUrn("urn:campaign:{$row['guid']->toInt()}")
->setOwnerGuid($row['owner_guid']->toInt())
->setType($row['type']);
$data = json_decode($row['json_data'] ?: '{}', true);
$campaign
->setName($data['name'])
->setEntityUrns(Text::buildArray($data['entity_urns']))
->setHashtags(Text::buildArray($data['hashtags']))
->setNsfw(Number::buildIntArray($data['nsfw']))
->setStart((int) $data['start'])
->setEnd((int) $data['end'])
->setBudget((string) $data['budget'])
->setBudgetType($data['budget_type'])
->setChecksum($data['checksum'])
->setImpressions((int) $data['impressions'])
->setImpressionsMet($data['impressions_met'])
->setRating($data['rating'])
->setQuality($data['quality'])
->setCreatedTimestamp(((int) $data['timestamp_timestamp']) ?: null)
->setReviewedTimestamp(((int) $data['reviewed_timestamp']) ?: null)
->setRejectedTimestamp(((int) $data['rejected_timestamp']) ?: null)
->setRevokedTimestamp(((int) $data['revoked_timestamp']) ?: null)
->setCompletedTimestamp(((int) $data['completed_timestamp']) ?: null);
$response[] = $campaign;
}
$response->setPagingToken(base64_encode($rows->pagingStateToken()));
$response->setLastPage($rows->isLastPage());
}
} catch (Exception $e) {
$response->setException($e);
}
return $response;
}
/**
......@@ -49,8 +128,8 @@ class Repository
$cql = "INSERT INTO boost_campaigns (type, guid, owner_guid, json_data, delivery_status) VALUES (?, ?, ?, ?, ?)";
$values = [
$campaign->getType(),
new Varint($campaign->getGuid()),
new Varint($campaign->getOwnerGuid()),
new Bigint($campaign->getGuid()),
new Bigint($campaign->getOwnerGuid()),
json_encode([
'urn' => $campaign->getUrn(),
'owner_guid' => (string) $campaign->getOwnerGuid(),
......
......@@ -1447,20 +1447,13 @@ CREATE TABLE minds.email_campaign_logs (
) WITH CLUSTERING ORDER BY (time_sent desc);
CREATE TABLE minds.boost_campaigns (
guid bigint,
owner_guid bigint,
type text,
guid varint,
owner_guid varint,
json_data text,
delivery_status text,
PRIMARY KEY (type, guid)
) WITH CLUSTERING ORDER BY (guid ASC);
CREATE MATERIALIZED VIEW minds.boost_campaigns_by_owner AS
SELECT *
FROM minds.boost_campaigns
WHERE type IS NOT null AND owner_guid IS NOT null AND guid IS NOT null
PRIMARY KEY (type, owner_guid, guid)
WITH CLUSTERING ORDER BY (owner_guid ASC, guid DESC);
PRIMARY KEY (guid)
);
CREATE TABLE minds.boost_campaigns_payments (
owner_guid bigint,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment