Skip to content

Commit

Permalink
Add predict command for dbverifier
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Apr 6, 2018
1 parent c354a2f commit 05fa085
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Goteo/Application/Lang.php
Expand Up @@ -63,9 +63,9 @@ static public function addSqlTranslation($lang) {
* @param string $lang Language ID (es, en, fr, etc.)
* @param [type] $yaml_file [description]
*/
static public function addYamlTranslation($lang, $yaml_file) {
static public function addYamlTranslation($lang, $yaml_file, $group = null) {
static::factory($lang);
$group = strtok(basename($yaml_file), '.');
$group = $group ?: strtok(basename($yaml_file), '.');
if(isset(static::$all_groups[$group]))
static::$all_groups[$group][] = $yaml_file;
else
Expand Down
35 changes: 32 additions & 3 deletions src/Goteo/Console/Command/DBVerifierCommand.php
Expand Up @@ -83,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$update = $input->getOption('update');
$scope = $input->getArgument('scope');
if(!in_array($scope, ['all', 'feed', 'mailing', 'token', 'blocked', 'toolkit'])) {
if(!in_array($scope, ['all', 'feed', 'mailing', 'token', 'blocked', 'toolkit', 'predict'])) {
throw new \Exception('Scope is not valid!');
}

Expand Down Expand Up @@ -244,9 +244,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$nerrors++;
}
$html .= "<h3>bin/console toolkit $key</h3>\n";
$html .= '<p>'.nl2br($converter->convert($out->fetch())).'</p>';
$res = $out->fetch();
$html .= '<p>'.nl2br($converter->convert($res)).'</p>';
if($output->isVerbose()) {
$output->writeln(strip_tags($html));
$output->writeln($res);
}
}

Expand All @@ -263,6 +264,34 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if(in_array($scope, ['all', 'predict'])) {
$html = "<h2>PROJECT FAILURE PREDICTIONS:</h2>";
$converter = new AnsiToHtmlConverter(new SolarizedLightTheme());

$command = $this->getApplication()->find('endround');
$out = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
$args = new ArrayInput(['command' => 'endround', '--skip-invests' => true, '--predict' => 1]);
$command->setOutput($out);
$command->setInput($args);
// print_r($command->output);die;
if($command->run($args, $command->output) !== 0) {
$output->writeln("<error>Errors found!</error>");
$res = $out->fetch();
$html .= '<p>'.nl2br($converter->convert($res)).'</p>';
if($output->isVerbose()) {
$output->writeln($res);
}
$index = 1;
$mailer = Mail::createFromHtml(Config::getMail('fail'), '', "DATABASE INCONSISTENCY in [" .Config::get('url.main')."]", $html);
$errors = [];
if(!$mailer->send($errors)) {
throw new \RuntimeException('Error sending email: ' . implode("\n", $errors));
}
} else {
$output->writeln("<info>Everything ok</info>");
}
}

if($index == 0) {
$output->writeln("<info>No cleaning needed</info>");
}
Expand Down
34 changes: 31 additions & 3 deletions src/Goteo/Console/Command/RoundCommand.php
Expand Up @@ -31,7 +31,8 @@ protected function configure() {
->setDefinition(array(
new InputOption('update', 'u', InputOption::VALUE_NONE, 'Actually does the job. If not specified, nothing is done, readonly process.'),
new InputOption('project', 'p', InputOption::VALUE_OPTIONAL, 'Only processes the specified Project ID'),
new InputOption('skip-invests', 's', InputOption::VALUE_NONE, 'Do not processes Invests returns'),
new InputOption('skip-invests', 's', InputOption::VALUE_NONE, 'Do not processes Invests returns'),
new InputOption('predict', 't', InputOption::VALUE_REQUIRED, 'Try to predict the endround in the number of days specified'),
))
->setHelp(<<<EOT
This script proccesses active projects reaching ending rounds.
Expand All @@ -54,6 +55,8 @@ protected function configure() {
does not execute refunds on the related invests
<info>./console endround --project demo-project --skip-invests --update</info>
Says if a project will change status 1 day before real end
<info>./console endround --predict 1 --update</info>
EOT
)
Expand All @@ -64,7 +67,8 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output) {
$update = $input->getOption('update');
$project_id = $input->getOption('project');
$skip_invests = $input->getOption('skip-invests');
$skip_invests = $input->getOption('skip-invests');
$predict = $input->getOption('predict');

if ($project_id) {
$output->writeln("<info>Processing Project [$project_id]:</info>");
Expand All @@ -76,6 +80,27 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$projects = Project::getList(['status' => Project::STATUS_IN_CAMPAIGN], null, 0, 10000);
}

if($predict) {
if($update) {
throw new \RuntimeException("Option --predict cannot be executed with --update");
}
foreach($projects as $p) {
$published = new \Datetime($p->published);
$published->modify("-$predict day");
$new = $published->format("Y-m-d");
if($new != $p->published) {
$output->write("<fg=red>Mocking project</> <info>{$p->id}</info> from published at <comment>{$p->published}</comment> to <fg=red>{$new}</>. From days active <comment>{$p->days_active}</comment> to ");
$p->published = $new;
$p->setDays();
}
$output->writeln("<fg=red>{$p->days_active}</>");

}
// die;
}

// print_r($projects->days_active);die(date("Y-m-d H:i\n"));

if (!$projects) {
$this->info("No projects found");
return;
Expand All @@ -84,6 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$symbol = Currency::getDefault('id');
$processed = 0;
$action_done = false;
$returnd_code = 0;
foreach ($projects as $project) {
if ((int) $project->status !== Project::STATUS_IN_CAMPAIGN) {
$this->debug("Skipping status [{$project->status}] from PROJECT: {$project->id}. Only projects IN CAMPAIGN will be processed");
Expand All @@ -109,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

if ($skip_invests) {
$this->warning("Skipping Invests refunds as required", [$project]);
$this->warning("Skipping Invests refunds as required\n--");
} else {
// Execute "console refund" command for returning invests
// refund Invests for this project
Expand Down Expand Up @@ -161,6 +187,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
// $this->error('Subcommand failed', $arguments);
// }
}
$return_code = 2;
} else {
if ($project->one_round) {
if(empty($project->success)) {
Expand Down Expand Up @@ -213,5 +240,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->warning('Dummy execution. No write operations done');
$output->writeln('<comment>No write operations done. Please execute the command with the --update modifier to perform write operations</comment>');
}
return $return_code;
}
}
2 changes: 1 addition & 1 deletion var/php/php-fpm.conf
Expand Up @@ -542,7 +542,7 @@ php_flag[short_open_tag] = off
php_flag[display_errors] = on
;php_admin_value[error_log] = php.log
;php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 128M
php_admin_value[memory_limit] = 256M
;php_value[session.save_path] = var/php/sessions
php_flag[allow_url_fopen] = 1
php_value[file_uploads] = on
Expand Down

0 comments on commit 05fa085

Please sign in to comment.