...
 
......@@ -2,29 +2,41 @@
namespace Minds\Controllers\Cli;
use DateTime;
use Elasticsearch\ClientBuilder;
use Minds\Cli;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Entities;
use Minds\Helpers\Flags;
use Minds\Interfaces;
use Minds\Core\Rewards\Contributions\UsersIterator;
class Contributions extends Cli\Controller implements Interfaces\CliControllerInterface
{
private $start;
private $elasticsearch;
public function help($command = null)
{
$this->out('Syntax usage: cli trending <type>');
switch ($command) {
case 'sync':
$this->out('Get contributions for all users');
$this->out('--from={timestamp} the day to start from. Default is yesterday at midnight');
$this->out('--incremental={true|false} Provide estimates during current day');
$this->out('--action={active|subscribe|jury-duty} Type of action');
$this->out('--dry-run={true|false} true prevents saving the data');
break;
case 'syncCheckins':
$this->out('--from={timestamp} the day to start from. Default is yesterday at midnight');
$this->out('--incremental={true|false} Provide estimates during current day');
break;
case 'test':
$this->out('Test contributions for a user');
$this->out('--from={timestamp} the day to start from. Default is 7 days ago');
$this->out('--guid={guid} the guid of the user to get contributions for');
default:
$this->out('Syntax usage: cli contributions <command>');
$this->displayCommandHelp();
}
}
public function exec()
{
return $this->help();
}
public function sync()
......@@ -33,6 +45,7 @@ class Contributions extends Cli\Controller implements Interfaces\CliControllerIn
ini_set('display_errors', 1);
$from = $this->getOpt('from');
$dryRun = $this->getOpt('dry-run') === 'true';
if (!$from && $this->getOpt('incremental')) {
$from = strtotime('midnight') * 1000; //run throughout the day, provides estimates
......@@ -65,8 +78,8 @@ class Contributions extends Cli\Controller implements Interfaces\CliControllerIn
$manager = new Core\Rewards\Contributions\Manager();
$manager->setFrom($from)
->setUser($user);
//$manager->setDryRun(true);
->setUser($user)
->setDryRun($dryRun);
$results = $manager->sync();
foreach ($results as $result) {
......
......@@ -260,6 +260,7 @@ CREATE TABLE minds.contributions (
metric text,
amount varint,
score varint,
score_decimal decimal,
PRIMARY KEY (user_guid, timestamp, metric)
) WITH CLUSTERING ORDER BY (timestamp DESC, metric ASC)
AND bloom_filter_fp_chance = 0.01
......@@ -278,9 +279,9 @@ CREATE TABLE minds.contributions (
AND speculative_retry = '99PERCENTILE';
CREATE MATERIALIZED VIEW minds.contributions_by_timestamp AS
SELECT timestamp, user_guid, metric, amount, score
SELECT timestamp, user_guid, metric, amount, score, score_decimal
FROM minds.contributions
WHERE user_guid IS NOT NULL AND timestamp IS NOT NULL AND metric IS NOT NULL AND amount IS NOT NULL AND score IS NOT NULL
WHERE user_guid IS NOT NULL AND timestamp IS NOT NULL AND metric IS NOT NULL AND amount IS NOT NULL
PRIMARY KEY (timestamp, user_guid, metric)
WITH CLUSTERING ORDER BY (user_guid ASC, metric ASC)
AND bloom_filter_fp_chance = 0.01
......
......@@ -3,7 +3,6 @@ namespace Minds\Core\Rewards\Contributions;
class Contribution
{
protected $metric;
protected $timestamp;
protected $amount = 0;
......
......@@ -102,7 +102,7 @@ class Manager
$this->repository->add($contributions);
return $contributions;
return $contributions;
}
/**
......@@ -131,7 +131,7 @@ class Manager
/**
* Gather the entire site contribution score
*/
public function getSiteContributionScore()
public function getSiteContributionScore(): float
{
if (isset($this->site_contribution_score_cache[$this->from])) {
return $this->site_contribution_score_cache[$this->from];
......@@ -144,9 +144,9 @@ class Manager
/**
* Gather the contribution score for the user
* @return int
* @return float
*/
public function getUserContributionScore(): int
public function getUserContributionScore(): float
{
return $this->sums
->setTimestamp($this->from)
......@@ -160,13 +160,6 @@ class Manager
*/
public function getRewardsAmount(): string
{
//$share = BigNumber::_($this->getUserContributionScore(), 18)->div($this->getSiteContribtionScore());
//$pool = BigNumber::toPlain('100000000', 18)->div(15)->div(365);
//$velocity = 10;
//$pool = $pool->div($velocity);
$tokensPerScore = BigNumber::_(pi())->mul(10 ** 18)->div(200);
$tokens = BigNumber::_($this->getUserContributionScore())->mul($tokensPerScore);
return (string) $tokens;
......
......@@ -4,6 +4,7 @@ namespace Minds\Core\Rewards\Contributions;
use Cassandra;
use Cassandra\Varint;
use Cassandra\Timestamp;
use Cassandra\Decimal;
use Minds\Core\Data\Cassandra\Client;
use Minds\Core\Data\Cassandra\Prepared\Custom;
use Minds\Core\Di\Di;
......@@ -31,7 +32,8 @@ class Repository
user_guid,
metric,
amount,
score
score,
score_decimal
)
VALUES (?,?,?,?,?)";
foreach ($contributions as $contribution) {
......@@ -42,7 +44,8 @@ class Repository
new Varint($contribution->getUser()->guid),
$contribution->getMetric(),
new Varint($contribution->getAmount()),
new Varint($contribution->getScore())
null,
new Decimal($contribution->getScore())
]
];
}
......@@ -114,7 +117,7 @@ class Repository
->setMetric((string) $row['metric'])
->setTimestamp($row['timestamp']->time() * 1000)
->setAmount((string) BigNumber::_($row['amount']))
->setScore((int) $row['score']);
->setScore((float) $row['score_decimal'] ?? $row['score']);
$contributions[] = $contribution;
}
......
......@@ -76,14 +76,14 @@ class Sums
$query = new Custom();
if ($this->user) {
$query->query("SELECT SUM(score) as score from contributions WHERE user_guid = ?
$query->query("SELECT SUM(score_decimal) as score from contributions WHERE user_guid = ?
AND timestamp = ?",
[
new Varint((int) $this->user->guid),
new Timestamp($this->timestamp / 1000)
]);
} else {
$query->query("SELECT SUM(score) as score from contributions_by_timestamp WHERE timestamp = ?",
$query->query("SELECT SUM(score_decimal) as score from contributions_by_timestamp WHERE timestamp = ?",
[
new Timestamp($this->timestamp / 1000)
]);
......@@ -95,7 +95,6 @@ class Sums
error_log($e->getMessage());
}
return (int) $rows[0]['score'];
return (float) $rows[0]['score'];
}
}
......@@ -73,7 +73,7 @@ class ManagerSpec extends ObjectBehavior
->setFrom($from)
->setTo($to);
$this->sync()->getAmount()->shouldBe(20);
$this->sync()->getAmount()->shouldBe("20");
$this->sync()->getContract()->shouldBe('offchain:reward');
$this->sync()->getTimestamp()->shouldBe(strtotime('-1 second', $to / 1000));
}
......