Working example of report command.

master
Lio Novelli 2021-04-05 16:23:06 +02:00
parent c833741939
commit 270d58124e
9 changed files with 176 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/app/vendor /app/vendor
/resources/data

View File

@ -18,6 +18,7 @@
- hourly wage - hourly wage
- folder for reports - folder for reports
** Components ** Components
- report service - provides a csv of a report - report service - provides a csv of a report
- local csv file read (provided via argument) - local csv file read (provided via argument)

View File

@ -21,7 +21,7 @@
"repositories": [ "repositories": [
{ {
"type": "vcs", "type": "vcs",
"url": "https://git.nixnet.xyz/l3/rprt" "url": "https://git.nixnet.services/l3n/RprtCli.git"
} }
], ],
"autoload": { "autoload": {

View File

@ -4,8 +4,13 @@ use function DI\create;
use function DI\get; use function DI\get;
use RprtCli\Commands\RprtCommand; use RprtCli\Commands\RprtCommand;
use RprtCli\Utils\CsvReport\CsvReport;
use RprtCli\Utils\CsvReport\CsvReportInterface;
return [ return [
RprtCommand::class => DI\create( \GuzzleHttp\ClientInterface::class => DI\get(\GuzzleHttp\Client::class),
CsvReportInterface::class => DI\get(CsvReport::class),
RprtCommand::class => DI\create()->constructor(
DI\get(CsvReportInterface::class)
) )
]; ];

View File

@ -4,17 +4,22 @@
namespace RprtCli\Commands; namespace RprtCli\Commands;
use RprtCli\Utils\CsvReport\CsvReportInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator; use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputOption;
class RprtCommand extends Command { class RprtCommand extends Command {
protected $csv;
//public function __construct() { public function __construct(CsvReportInterface $csv) {
// } $this->csv = $csv;
parent::__construct();
}
/** /**
* Get configuration. * Get configuration.
@ -22,14 +27,43 @@ class RprtCommand extends Command {
protected function configure(): void { protected function configure(): void {
$this->setName('rprt'); $this->setName('rprt');
$this->setDescription('Generate monthly report'); $this->setDescription('Generate monthly report');
// @TODO $this->addUsage('');
$this->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'Specify the input csv file to generate report from.');
} }
protected function execute(InputInterface $input, OutputInterface $output): int { protected function execute(InputInterface $input, OutputInterface $output): int {
if ($file = $input->getOption('file')) {
$data = $this->csv->getReportData($file);
$table = $this->generateTable($output, $data);
$table->render();
return Command::SUCCESS;
}
$this->dummyOutput($input, $output); $this->dummyOutput($input, $output);
return Command::SUCCESS; return Command::SUCCESS;
} }
protected function generateTable($output, $data) {
// @TODO - get configuration
$table = new Table($output);
$table->setHeaders(['Project', 'Hours']);
$rows = [];
$together = 0;
foreach ($data as $project => $hours) {
$row = [$project, $hours/60];
$together += $hours / 60;
$rows[] = $row;
}
$rows[] = new TableSeparator();
$rows[] = ['Zusamen', $together];
$table->setRows($rows);
return $table;
}
/**
* Dummy output for testing.
*/
protected function dummyOutput(InputInterface $input, OutputInterface $output): void { protected function dummyOutput(InputInterface $input, OutputInterface $output): void {
$output->writeln('I will output a nice table.'); $output->writeln('I will output a nice table.');
$table = New Table($output); $table = New Table($output);

View File

@ -0,0 +1,42 @@
<?php
// src/Utils/Configuration/ConfigurationInterface.php
namespace RprtCli\Utils\Configuration;
interface PonsapiConfigInterface
{
/**
* Checks for config file.
*
* @return string|bool
* Full path to config file or FALSE if it doesn't exist.
*/
// function findConfig($file);
/**
* Get and read the configuration from file.
*/
function getConfig();
/**
* Get a specific configuration for key.
*
* @param string $key
* Config key.
* @param mixed $default
* Default value if config for key is not yet specified.
*
* @return mixed
* Data.
*/
public function get($key, $default = null);
/**
* Checks if key exists in the configuration file.
*
* @param string $key
* Key to check for.
*/
public function exists($key);
}

View File

@ -0,0 +1,66 @@
<?php
namespace RprtCli\Utils\CsvReport;
use RprtCli\Utils\CsvReport\CsvReportInterface;
class CsvReport implements CsvReportInterface {
public function getReportData(string $file_path): array {
$output = [];
// @TODO replace with config service.
$config = $this->dummyConfig();
foreach (array_keys($config['projects']) as $key) {
$output[$key] = 0;
}
if ($file = fopen($file_path, 'r')) {
while (($line = fgetcsv($file)) !== FALSE) {
$parsed = $this->parseCsvFile($line);
// $key = reset(array_keys($parsed));
$key = array_key_first($parsed);
if (isset($output[$key])) {
$output[$key] += (int) reset($parsed);
}
}
}
return $output;
}
protected function parseCsvFile(array $raw_data): array {
$config = $this->dummyConfig();
// var_dump($raw_data);
foreach ($config['projects'] as $key => $project) {
if (preg_match('/'.$project['pattern'].'/', $raw_data[1])) {
return [$key => $raw_data[4]];
}
}
return [];
}
protected function dummyConfig(): array {
$config = [
'projects' => [
'LDP' => [
'name' => 'lupus.digital',
'pattern' => 'LDP-[0-9]+',
'price' => 26,
// optional specify columns
],
'WV' => [
'name' => 'Wirtschaftsverlag',
'pattern' => 'WV-[0-9]+',
'price' => 26,
// optional specify columns
],
'Other' => [
'name' => 'Other projects',
'pattern' => '(?!.\bLDP\b)(?!.\bWV\b)',
'price' => 26,
// optional specify columns
],
],
];
return $config;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace RprtCli\Utils\CsvReport;
/**
* Handles creating report data from csv file downloaded from youtrack service.
*/
interface CsvReportInterface {
/**
* Gets project configuration and parses the data.
*
* Calculate number of hours per project.
*/
// protected function parseCsvFile(array $data): array;
/**
* Returns array of hours per configured projects.
*/
public function getReportData(string $file_path): array;
}