1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-28 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Commands; |
11
|
|
|
|
12
|
|
|
use Chapi\Entity\Chronos\JobEntity; |
13
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
14
|
|
|
use Symfony\Component\Console\Helper\Table; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
|
17
|
|
|
class ListCommand extends AbstractCommand |
18
|
|
|
{ |
19
|
|
|
const DEFAULT_VALUE_JOB_NAME = 'all'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Configures the current command. |
23
|
|
|
*/ |
24
|
3 |
|
protected function configure() |
25
|
|
|
{ |
26
|
3 |
|
$this->setName('list') |
27
|
3 |
|
->setDescription('Display your jobs and filter they by status') |
28
|
3 |
|
->addOption('onlyFailed', 'f', InputOption::VALUE_NONE, 'Display only failed jobs') |
29
|
3 |
|
->addOption('onlyDisabled', 'd', InputOption::VALUE_NONE, 'Display only disabled jobs') |
30
|
|
|
; |
31
|
3 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return int |
35
|
|
|
* @throws \LogicException |
36
|
|
|
*/ |
37
|
3 |
|
protected function process() |
38
|
|
|
{ |
39
|
|
|
/** @var JobRepositoryInterface $_oJobRepositoryChronos */ |
40
|
3 |
|
$_oJobRepositoryChronos = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS); |
41
|
|
|
|
42
|
3 |
|
$_bOnlyFailed = (bool) $this->oInput->getOption('onlyFailed'); |
43
|
3 |
|
$_bOnlyDisabled = (bool) $this->oInput->getOption('onlyDisabled'); |
44
|
|
|
|
45
|
3 |
|
$_oTable = new Table($this->oOutput); |
46
|
3 |
|
$_oTable->setHeaders(array( |
47
|
3 |
|
'Job', |
48
|
|
|
'Info' |
49
|
3 |
|
)); |
50
|
|
|
|
51
|
|
|
/** @var JobEntity $_oJobEntity */ |
52
|
3 |
|
foreach ($_oJobRepositoryChronos->getJobs() as $_oJobEntity) |
53
|
|
|
{ |
54
|
3 |
|
if ($this->hasJobToPrint($_oJobEntity, $_bOnlyFailed, $_bOnlyDisabled)) |
55
|
3 |
|
{ |
56
|
3 |
|
$this->printJobTableRow($_oTable, $_oJobEntity); |
57
|
3 |
|
} |
58
|
3 |
|
} |
59
|
|
|
|
60
|
3 |
|
$_oTable->render(); |
61
|
|
|
|
62
|
3 |
|
return 0; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param JobEntity $oJobEntity |
67
|
|
|
* @param bool $bOnlyFailed |
68
|
|
|
* @param bool $bOnlyDisabled |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
3 |
|
private function hasJobToPrint(JobEntity $oJobEntity, $bOnlyFailed, $bOnlyDisabled) |
72
|
|
|
{ |
73
|
3 |
|
$_bPrintAllJobs = (false === $bOnlyFailed && false === $bOnlyDisabled); |
74
|
|
|
if ($_bPrintAllJobs) |
75
|
3 |
|
{ |
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
$_bHasToPrint = false; |
80
|
|
|
|
81
|
2 |
|
if (true === $bOnlyFailed && $oJobEntity->errorsSinceLastSuccess > 0) |
82
|
2 |
|
{ |
83
|
1 |
|
$_bHasToPrint = true; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
2 |
|
if (true === $bOnlyDisabled && true === $oJobEntity->disabled) |
87
|
2 |
|
{ |
88
|
1 |
|
$_bHasToPrint = true; |
89
|
1 |
|
} |
90
|
|
|
|
91
|
2 |
|
return $_bHasToPrint; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Table $oTable |
96
|
|
|
* @param JobEntity $oJobEntity |
97
|
|
|
*/ |
98
|
3 |
|
private function printJobTableRow(Table $oTable, JobEntity $oJobEntity) |
99
|
|
|
{ |
100
|
3 |
|
$oTable->addRow([ |
101
|
3 |
|
sprintf( |
102
|
3 |
|
$this->getOutputFormat($oJobEntity), |
103
|
3 |
|
$oJobEntity->name |
104
|
3 |
|
), |
105
|
|
|
|
106
|
3 |
|
sprintf( |
107
|
3 |
|
$this->getOutputFormat($oJobEntity), |
108
|
3 |
|
$this->getOutputLabel($oJobEntity) |
109
|
3 |
|
), |
110
|
3 |
|
]); |
111
|
3 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param JobEntity $oJobEntity |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
3 |
|
private function getOutputLabel(JobEntity $oJobEntity) |
118
|
|
|
{ |
119
|
3 |
|
$_aJobInfoText = []; |
120
|
|
|
|
121
|
3 |
|
if ($oJobEntity->disabled) |
122
|
3 |
|
{ |
123
|
1 |
|
$_aJobInfoText[] = 'disabled'; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
3 |
|
if ($oJobEntity->errorCount > 0) |
127
|
3 |
|
{ |
128
|
1 |
|
$_fErrorRate = ($oJobEntity->successCount > 0) |
129
|
1 |
|
? 100 / $oJobEntity->successCount * $oJobEntity->errorCount |
130
|
1 |
|
: 100; |
131
|
|
|
|
132
|
1 |
|
$_aJobInfoText[] = 'errors rate: ' . round($_fErrorRate, 2) . '%'; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
3 |
|
if ($oJobEntity->errorsSinceLastSuccess > 0) |
136
|
3 |
|
{ |
137
|
1 |
|
$_aJobInfoText[] = 'errors since last success:' . $oJobEntity->errorsSinceLastSuccess; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
3 |
|
return (!empty($_aJobInfoText)) |
141
|
3 |
|
? implode(' | ', $_aJobInfoText) |
142
|
3 |
|
: 'ok'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param JobEntity $oJobEntity |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
3 |
|
private function getOutputFormat(JobEntity $oJobEntity) |
150
|
|
|
{ |
151
|
3 |
|
if ($oJobEntity->errorsSinceLastSuccess > 0) |
152
|
3 |
|
{ |
153
|
1 |
|
return '<fg=red>%s</>'; |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
if ($oJobEntity->errorCount > 0 || true === $oJobEntity->disabled) |
157
|
2 |
|
{ |
158
|
1 |
|
return '<comment>%s</comment>'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// else |
162
|
1 |
|
return '<info>%s</info>'; |
163
|
|
|
} |
164
|
|
|
} |