Commit 486982c5 authored by Guy Thouret's avatar Guy Thouret

(chore) Boost repository only use ms time, update schema where s is used instead of ms

(chore) Change schema to stop using time_created and last_updated data field in Cassandra
Related to #1111 #1110
1 merge request!235WIP: Boost Campaigns (&24)
Pipeline #96760965 passed with stages
in 6 minutes and 50 seconds
......@@ -9,6 +9,7 @@ use Minds\Common\Repository\Response;
use Minds\Core\Di\Di;
use Minds\Core\Data\Cassandra\Prepared;
use Cassandra;
use Minds\Core\Time;
class Repository
{
......@@ -65,27 +66,13 @@ class Repository
$boost = new Boost();
$data = json_decode($row['data'], true);
if (!isset($data['schema']) && $data['schema'] != '04-2019') {
$data['entity_guid'] = $data['entity']['guid'];
$data['owner_guid'] = $data['owner']['guid'];
$data['@created'] = $data['time_created'] * 1000;
$data['@reviewed'] = $data['state'] === 'accepted' ? ($data['last_updated'] * 1000) : null;
$data['@revoked'] = $data['state'] === 'revoked' ? ($data['last_updated'] * 1000) : null;
$data['@rejected'] = $data['state'] === 'rejected' ? ($data['last_updated'] * 1000) : null;
$data['@completed'] = $data['state'] === 'completed' ? ($data['last_updated'] * 1000) : null;
}
$data = $this->updateTimestampsToMsValues($data);
if ($data['@created'] < 1055503139000) {
$data['@created'] = $data['@created'] * 1000;
}
if ($data['is_campaign'] ?? false) {
// Skip campaigns
continue;
if (!isset($data['schema']) && $data['schema'] != '04-2019') {
$data = $this->updateOldSchema($data);
}
$boost->setGuid((string) $row['guid'])
->setMongoId($data['_id'])
->setEntityGuid($data['entity_guid'])
->setOwnerGuid($data['owner_guid'])
->setType($row['type'])
......@@ -116,6 +103,41 @@ class Repository
return $response;
}
protected function updateTimestampsToMsValues(array $data): array
{
if (isset($data['last_updated'])) {
if ($data['last_updated'] < Time::HISTORIC_MS_VALUE) {
$data['last_updated'] = Time::sToMs($data['last_updated']);
}
}
if (isset($data['time_created'])) {
if ($data['time_created'] < Time::HISTORIC_MS_VALUE) {
$data['time_created'] = Time::sToMs($data['time_created']);
}
}
if ($data['@created'] < Time::HISTORIC_MS_VALUE) {
$data['@created'] = Time::sToMs($data['@created']);
}
return $data;
}
protected function updateOldSchema(array $data): array
{
$data['entity_guid'] = $data['entity']['guid'];
$data['owner_guid'] = $data['owner']['guid'];
$data['@created'] = $data['time_created'];
$data['@reviewed'] = $data['state'] === Boost::STATE_APPROVED ? $data['last_updated'] : null;
$data['@revoked'] = $data['state'] === Boost::STATE_REVOKED ? $data['last_updated'] : null;
$data['@rejected'] = $data['state'] === Boost::STATE_REJECTED ? $data['last_updated'] : null;
$data['@completed'] = $data['state'] === Boost::STATE_COMPLETED ? $data['last_updated'] : null;
$data['schema'] = '04-2019';
return $data;
}
/**
* Return a single boost via urn
* @param string $urn
......@@ -150,26 +172,22 @@ class Repository
}
$template = "INSERT INTO boosts
(type, guid, owner_guid, destination_guid, mongo_id, state, data)
(type, guid, owner_guid, state, data)
VALUES
(?, ?, ?, ?, ?, ?, ?)
(?, ?, ?, ?, ?)
";
$data = [
'guid' => $boost->getGuid(),
'schema' => '04-2019',
'_id' => $boost->getMongoId(), //TODO: remove once on production
'entity_guid' => $boost->getEntityGuid(),
'entity' => $boost->getEntity() ? $boost->getEntity()->export() : null, //TODO: remove once on production
'bid' => $boost->getBid(),
'impressions' => $boost->getImpressions(),
//'bidType' => $boost->getBidType(),
'bidType' => in_array($boost->getBidType(), [ 'onchain', 'offchain' ], true) ? 'tokens' : $boost->getBidType(), //TODO: remove once on production
'owner_guid' => $boost->getOwnerGuid(),
'owner' => $boost->getOwner() ? $boost->getOwner()->export() : null, //TODO: remove once on production
'@created' => $boost->getCreatedTimestamp(),
'time_created' => $boost->getCreatedTimestamp(), //TODO: remove once on production
'last_updated' => time(), //TODO: remove once on production
'@reviewed' => $boost->getReviewedTimestamp(),
'@rejected' => $boost->getRejectedTimestamp(),
'@revoked' => $boost->getRevokedTimestamp(),
......@@ -190,8 +208,6 @@ class Repository
(string) $boost->getType(),
new Cassandra\Varint($boost->getGuid()),
new Cassandra\Varint($boost->getOwnerGuid()),
null,
(string) $boost->getMongoId(),
(string) $boost->getState(),
json_encode($data)
];
......
......@@ -4,6 +4,8 @@ namespace Minds\Core;
class Time
{
const HISTORIC_MS_VALUE = 1000000000000;
const HALF_HOUR = 1800;
const ONE_HOUR = 3600;
const TWO_HOUR = 7200;
......@@ -52,4 +54,14 @@ class Time
return $intervals;
}
/**
* Descriptive method explicitly converts s value to ms value
* @param int $s
* @return int
*/
public static function sToMs(int $s) : int
{
return $s * 1000;
}
}
Please register or to comment