forked from EqualStreetNames/module-process
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendarCommand.php
More file actions
134 lines (118 loc) · 4.46 KB
/
CalendarCommand.php
File metadata and controls
134 lines (118 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace App\Command\Tool;
use App\Exception\FileException;
use Cron\CronExpression;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Check update process calendar.
*
* @package App\Command\Tool
*/
class CalendarCommand extends Command
{
/** {@inheritdoc} */
protected static $defaultName = 'tool:calendar';
/**
* {@inheritdoc}
*
* @return void
*
* @throws InvalidArgumentException
*/
protected function configure(): void
{
$this->setDescription('Check update process calendar.');
}
/**
* {@inheritdoc}
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
$data = [];
$glob = glob('../cities/*/*');
if ($glob !== false) {
foreach ($glob as $directory) {
$path = sprintf('%s/.github/workflows/update-data.yml', $directory);
if (!file_exists($path) || !is_readable($path)) {
throw new FileException(sprintf('File "%s" doesn\'t exist or is not readable.', $path));
}
$yaml = Yaml::parseFile($path);
/** @var CronExpression[] */
$calendar = [];
foreach ($yaml['on']['schedule'] as $schedule) {
$crons = array_values(array_filter($schedule, function ($key): bool {
return $key === 'cron';
}, ARRAY_FILTER_USE_KEY));
$cronExpressions = array_map(function ($c): CronExpression {
return new CronExpression($c);
}, $crons);
$calendar = array_merge($calendar, $cronExpressions);
}
foreach ($calendar as $cron) {
$parts = $cron->getParts();
$day = $parts[4];
if ($day === '0') {
$day = '7';
}
$time = sprintf('%02s:%02s', $parts[1], $parts[0]);
$duplicates = array_filter(
$data,
fn (array $row): bool => $row[4]->getNextRunDate()->format('c') === $cron->getNextRunDate()->format('c')
// fn (array $row): bool => ($row[1] === $day && $row[3] === $time) || ($row[1] === '*' && $row[3] === $time) || ($day === '*' && $row[3] === $time)
);
$warning = count($duplicates) > 0 ? '⚠ Duplicate' : null;
$data[] = [
substr($directory, 10),
$day,
$parts[2],
$time,
$cron,
// $cron->getNextRunDate()->format('d M Y H:i'),
$warning
];
}
}
}
$nextRun = array_map(
fn (array $row): string => $row[4]->getNextRunDate()->format('c'),
$data
);
array_multisort(
$nextRun,
SORT_ASC,
$data
);
$display = array_map(
fn (array $row): array => [
$row[0],
$row[1],
$row[2],
$row[3],
$row[4]->getExpression(),
$row[4]->getNextRunDate()->format('d M Y H:i'),
$row[5],
],
$data
);
$table = new Table($output);
$table->setHeaders(['City', 'Day of week', 'Day of month', 'Time', 'Cron', 'Next run', '']);
$table->setRows($display);
$table->render();
return Command::SUCCESS;
} catch (Exception $error) {
$output->writeln(sprintf('<error>%s</error>', $error->getMessage()));
return Command::FAILURE;
}
}
}