1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mokka\Command; |
4
|
|
|
|
5
|
|
|
use Mokka\Config\Logger; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Helper\Table; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class ReportCommand extends Command |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected function configure() |
18
|
|
|
{ |
19
|
|
|
$this |
20
|
|
|
->setName('report') |
21
|
|
|
->setDescription('Generate mokka reports') |
22
|
|
|
->addOption('market', 'm', InputOption::VALUE_OPTIONAL, 'Choose market to run') |
23
|
|
|
->addOption('symbol', 's', InputOption::VALUE_OPTIONAL, 'Specific symbol for the report') |
24
|
|
|
->addOption('date', 'd', InputOption::VALUE_OPTIONAL, 'Choose the date of repor. Format: YYYY-MM-DD') |
25
|
|
|
->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'File format for the reports. Default is console output. Available formats; CSV, XML, JSON', "console") |
26
|
|
|
; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param InputInterface $input |
31
|
|
|
* @param OutputInterface $output |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
|
|
$fs = new Filesystem(); |
38
|
|
|
if (!is_dir(__DIR__.'/../../reports')) $fs->mkdir(__DIR__.'/../../reports'); |
39
|
|
|
|
40
|
|
|
$reportDate = $input->getOption("date") ? $input->getOption("date") : (new \DateTime())->format('Y-m-d'); |
41
|
|
|
|
42
|
|
|
//set logs (txt db) |
43
|
|
|
$logger = new Logger(__DIR__.'/../../logs/', $reportDate); |
44
|
|
|
|
45
|
|
|
$report = $logger->read(); |
46
|
|
|
|
47
|
|
|
if ($input->getOption("symbol")) { |
48
|
|
|
$report->where("symbol", "=", $input->getOption("symbol")); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($input->getOption("market")) { |
52
|
|
|
$report->where("market", "=", $input->getOption("market")); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$reportData = $report->get(); |
56
|
|
|
|
57
|
|
|
$formatData = []; |
58
|
|
|
|
59
|
|
|
array_map(function($row) use (&$formatData){ |
60
|
|
|
$row['lastUpdate'] = date('Y-m-d H:i:s', $row['lastUpdate']); |
61
|
|
|
$formatData[] = $row; |
62
|
|
|
}, (array) $reportData->getIterator()); |
63
|
|
|
|
64
|
|
|
$format = $input->getOption("format"); |
65
|
|
|
$method = "output".ucfirst($format); |
66
|
|
|
|
67
|
|
|
if (method_exists($this, $method)) { |
68
|
|
|
$this->$method($input, $output, $formatData); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} catch (\Exception $exception) { |
72
|
|
|
$output->writeln("<error>{$exception->getMessage()}</error>"); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param InputInterface $input |
79
|
|
|
* @param OutputInterface $output |
80
|
|
|
* @param $reportData |
81
|
|
|
*/ |
82
|
|
|
protected function outputConsole(InputInterface $input, OutputInterface $output, $reportData) |
83
|
|
|
{ |
84
|
|
|
$table = new Table($output); |
85
|
|
|
$table->setHeaders(array('Action Price', 'Previous Price', 'Action', 'Symbol', 'Market', 'Update Date')); |
86
|
|
|
|
87
|
|
|
$table->setRows($reportData); |
88
|
|
|
|
89
|
|
|
$table->render(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param InputInterface $input |
94
|
|
|
* @param OutputInterface $output |
95
|
|
|
* @param $reportData |
96
|
|
|
*/ |
97
|
|
|
protected function outputJson(InputInterface $input, OutputInterface $output, $reportData) { |
98
|
|
|
$fs = new Filesystem(); |
99
|
|
|
|
100
|
|
|
$fileName = "report_".time().".json"; |
101
|
|
|
$filePath = __DIR__."/../../reports/".$fileName; |
102
|
|
|
|
103
|
|
|
$fs->dumpFile($filePath, json_encode($reportData)); |
104
|
|
|
|
105
|
|
|
$realPath = realpath(dirname($filePath))."/".$fileName; |
106
|
|
|
$output->writeln("<question>Report Generated!</question>"); |
107
|
|
|
$output->writeln("<question>{$realPath}</question>"); |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |