1 | <?php |
||
19 | class ListCommand extends AbstractCommand |
||
20 | { |
||
21 | const DEFAULT_VALUE_JOB_NAME = 'all'; |
||
22 | |||
23 | /** |
||
24 | * Configures the current command. |
||
25 | */ |
||
26 | 3 | protected function configure() |
|
27 | { |
||
28 | 3 | $this->setName('list') |
|
29 | 3 | ->setDescription('Display your jobs and filter them by status') |
|
30 | 3 | ->addOption('onlyFailed', 'f', InputOption::VALUE_NONE, 'Display only failed jobs') |
|
31 | 3 | ->addOption('onlyDisabled', 'd', InputOption::VALUE_NONE, 'Display only disabled jobs') |
|
32 | ; |
||
33 | 3 | } |
|
34 | |||
35 | /** |
||
36 | * @return int |
||
37 | * @throws \LogicException |
||
38 | */ |
||
39 | 3 | protected function process() |
|
40 | { |
||
41 | /** @var JobRepositoryInterface $_oJobRepositoryChronos */ |
||
42 | 3 | $_oJobRepositoryChronos = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS); |
|
43 | /** @var JobRepositoryInterface $_oJobRepositoryMarathon */ |
||
44 | 3 | $_oJobRepositoryMarathon = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_MARATHON); |
|
45 | |||
46 | 3 | $_bOnlyFailed = (bool) $this->oInput->getOption('onlyFailed'); |
|
47 | 3 | $_bOnlyDisabled = (bool) $this->oInput->getOption('onlyDisabled'); |
|
48 | |||
49 | 3 | $_oTable = new Table($this->oOutput); |
|
50 | 3 | $_oTable->setHeaders(array( |
|
51 | 3 | 'Job', |
|
52 | 'Info', |
||
53 | 'Type' |
||
54 | )); |
||
55 | |||
56 | 3 | $_aAllEntities = array_merge( |
|
57 | 3 | $_oJobRepositoryChronos->getJobs()->getArrayCopy(), |
|
58 | 3 | $_oJobRepositoryMarathon->getJobs()->getArrayCopy() |
|
59 | ); |
||
60 | |||
61 | /** @var ChronosJobEntity $_oJobEntity */ |
||
62 | 3 | foreach ($_aAllEntities as $_oJobEntity) |
|
63 | { |
||
64 | 3 | if ($this->hasJobToPrint($_oJobEntity, $_bOnlyFailed, $_bOnlyDisabled)) |
|
65 | { |
||
66 | 3 | $this->printJobTableRow($_oTable, $_oJobEntity); |
|
67 | } |
||
68 | } |
||
69 | |||
70 | 3 | $_oTable->render(); |
|
71 | |||
72 | 3 | return 0; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param JobEntityInterface $oJobEntity |
||
77 | * @param bool $bOnlyFailed |
||
78 | * @param bool $bOnlyDisabled |
||
79 | * @return bool |
||
80 | */ |
||
81 | 3 | private function hasJobToPrint(JobEntityInterface $oJobEntity, $bOnlyFailed, $bOnlyDisabled) |
|
82 | { |
||
83 | 3 | if ($oJobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE) |
|
84 | { |
||
85 | 3 | return true; |
|
86 | } |
||
87 | |||
88 | 3 | if (!$oJobEntity instanceof ChronosJobEntity) |
|
89 | { |
||
90 | throw new \RuntimeException('Entity not of type ChronosJobEntity'); |
||
91 | } |
||
92 | |||
93 | 3 | $_bPrintAllJobs = (false === $bOnlyFailed && false === $bOnlyDisabled); |
|
94 | 3 | if ($_bPrintAllJobs) |
|
95 | { |
||
96 | 1 | return true; |
|
97 | } |
||
98 | |||
99 | 2 | $_bHasToPrint = false; |
|
100 | |||
101 | 2 | if (true === $bOnlyFailed && $oJobEntity->errorsSinceLastSuccess > 0) |
|
102 | { |
||
103 | 1 | $_bHasToPrint = true; |
|
104 | } |
||
105 | |||
106 | 2 | if (true === $bOnlyDisabled && true === $oJobEntity->disabled) |
|
107 | { |
||
108 | 1 | $_bHasToPrint = true; |
|
109 | } |
||
110 | |||
111 | 2 | return $_bHasToPrint; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param Table $oTable |
||
116 | * @param JobEntityInterface $oJobEntity |
||
117 | */ |
||
118 | 3 | private function printJobTableRow(Table $oTable, JobEntityInterface $oJobEntity) |
|
119 | { |
||
120 | 3 | $oTable->addRow([ |
|
121 | 3 | sprintf( |
|
122 | 3 | $this->getOutputFormat($oJobEntity), |
|
123 | 3 | $oJobEntity->getKey() |
|
124 | ), |
||
125 | |||
126 | 3 | sprintf( |
|
127 | 3 | $this->getOutputFormat($oJobEntity), |
|
128 | 3 | $this->getOutputLabel($oJobEntity) |
|
129 | ), |
||
130 | 3 | sprintf( |
|
131 | 3 | $this->getOutputFormat($oJobEntity), |
|
132 | 3 | $oJobEntity->getEntityType() |
|
133 | ) |
||
134 | ]); |
||
135 | 3 | } |
|
136 | |||
137 | /** |
||
138 | * @param JobEntityInterface $oJobEntity |
||
139 | * @return string |
||
140 | */ |
||
141 | 3 | private function getOutputLabel(JobEntityInterface $oJobEntity) |
|
142 | { |
||
143 | |||
144 | 3 | if ($oJobEntity->getEntityType() == JobEntityInterface::MARATHON_TYPE) |
|
145 | { |
||
146 | 3 | return 'ok'; |
|
147 | } |
||
148 | |||
149 | 3 | if (!$oJobEntity instanceof ChronosJobEntity) |
|
150 | { |
||
151 | throw new \RuntimeException('Entity not of type ChronosJobEntity'); |
||
152 | } |
||
153 | |||
154 | 3 | $_aJobInfoText = []; |
|
155 | |||
156 | 3 | if ($oJobEntity->disabled) |
|
157 | { |
||
158 | 1 | $_aJobInfoText[] = 'disabled'; |
|
159 | } |
||
160 | |||
161 | 3 | if ($oJobEntity->errorCount > 0) |
|
162 | { |
||
163 | 1 | $_fErrorRate = ($oJobEntity->successCount > 0) |
|
164 | 1 | ? 100 / $oJobEntity->successCount * $oJobEntity->errorCount |
|
165 | 1 | : 100; |
|
166 | |||
167 | 1 | $_aJobInfoText[] = 'errors rate: ' . round($_fErrorRate, 2) . '%'; |
|
168 | } |
||
169 | |||
170 | 3 | if ($oJobEntity->errorsSinceLastSuccess > 0) |
|
171 | { |
||
172 | 1 | $_aJobInfoText[] = 'errors since last success:' . $oJobEntity->errorsSinceLastSuccess; |
|
173 | } |
||
174 | |||
175 | 3 | return (!empty($_aJobInfoText)) |
|
176 | 2 | ? implode(' | ', $_aJobInfoText) |
|
177 | 3 | : 'ok'; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param JobEntityInterface $oJobEntity |
||
182 | * @return string |
||
183 | */ |
||
184 | 3 | private function getOutputFormat(JobEntityInterface $oJobEntity) |
|
209 | } |