1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-09-09 |
7
|
|
|
* |
8
|
|
|
* @link: https://github.com/msiebeneicher/chapi/issues/24 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Chapi\Commands; |
13
|
|
|
|
14
|
|
|
use Chapi\Component\DatePeriod\DatePeriodFactoryInterface; |
15
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
16
|
|
|
use Chapi\Service\Chronos\JobStatsServiceInterface; |
17
|
|
|
use Chapi\Service\JobDependencies\JobDependencyServiceInterface; |
18
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
19
|
|
|
use Symfony\Component\Console\Helper\Table; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
|
22
|
|
|
class SchedulingViewCommand extends AbstractCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var JobStatsServiceInterface $oJobStatsService |
26
|
|
|
*/ |
27
|
|
|
private $oJobStatsService; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var JobDependencyServiceInterface $oJobDependencyService |
31
|
|
|
*/ |
32
|
|
|
private $oJobDependencyService; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var JobRepositoryInterface $oJobRepositoryChronos |
36
|
|
|
*/ |
37
|
|
|
private $oJobRepositoryChronos; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var DatePeriodFactoryInterface $oDatePeriodFactory |
41
|
|
|
*/ |
42
|
|
|
private $oDatePeriodFactory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Configures the current command. |
46
|
|
|
*/ |
47
|
3 |
|
protected function configure() |
48
|
|
|
{ |
49
|
3 |
|
$this->setName('scheduling') |
50
|
3 |
|
->setDescription('Display upcoming jobs in a specified timeframe.') |
51
|
3 |
|
->addOption('starttime', 's', InputOption::VALUE_OPTIONAL, 'Start time to display the jobs', null) |
52
|
3 |
|
->addOption('endtime', 'e', InputOption::VALUE_OPTIONAL, 'End time to display the jobs', null) |
53
|
|
|
; |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
3 |
|
protected function process() |
60
|
|
|
{ |
61
|
|
|
// init necessary services |
62
|
3 |
|
$this->oJobStatsService = $this->getContainer()->get(JobStatsServiceInterface::DIC_NAME); |
|
|
|
|
63
|
3 |
|
$this->oJobDependencyService = $this->getContainer()->get(JobDependencyServiceInterface::DIC_NAME); |
64
|
3 |
|
$this->oJobRepositoryChronos = $this->getContainer()->get(JobRepositoryInterface::DIC_NAME_CHRONOS); |
65
|
3 |
|
$this->oDatePeriodFactory = $this->getContainer()->get(DatePeriodFactoryInterface::DIC_NAME); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// init timeframe by user input |
68
|
3 |
|
$_iCurrentTime = time() + 60; // default 1min in the future |
69
|
|
|
|
70
|
3 |
|
$_sStartTime = $this->oInput->getOption('starttime'); |
71
|
3 |
|
$_sEndTime = $this->oInput->getOption('endtime'); |
|
|
|
|
72
|
|
|
|
73
|
3 |
|
$_iStartTime = ($_sStartTime == null) ? $_iCurrentTime : strtotime($_sStartTime); |
74
|
3 |
|
if ($_sStartTime != null && $_iStartTime < $_iCurrentTime) |
75
|
|
|
{ |
76
|
1 |
|
$_iStartTime = strtotime($_sStartTime . ' + 24 hours'); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
$_iEndTime = ($_sEndTime == null) ? $_iStartTime + 7200 : strtotime($_sEndTime); |
80
|
|
|
|
81
|
|
|
// print table for timeframe |
82
|
3 |
|
$this->printTimeLineTable($_iStartTime, $_iEndTime); |
83
|
|
|
|
84
|
3 |
|
return 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int $iStartTime |
89
|
|
|
* @param int $iEndTime |
90
|
|
|
*/ |
91
|
3 |
|
private function printTimeLineTable($iStartTime, $iEndTime) |
92
|
|
|
{ |
93
|
3 |
|
$_oDatePeriod = $this->createDatePeriod(null, $iStartTime, null, $iEndTime, 'PT1M'); |
94
|
|
|
|
95
|
3 |
|
$_oTable = new Table($this->oOutput); |
96
|
3 |
|
$_oTable->setHeaders(array( |
97
|
3 |
|
'Job', |
98
|
3 |
|
'Timeline for ' . date('Y-m-d H:i', $iStartTime) . ' till ' . date('Y-m-d H:i', $iEndTime) |
99
|
|
|
)); |
100
|
|
|
|
101
|
3 |
|
$_aJobs = $this->getJobsWhichShouldStartInPeriod($iStartTime, $iEndTime); |
102
|
|
|
|
103
|
3 |
|
$_iRowCount = 0; |
104
|
|
|
|
105
|
|
|
/** @var ChronosJobEntity $_oJobEntity */ |
106
|
3 |
|
foreach ($_aJobs as $_oJobEntity) |
107
|
|
|
{ |
108
|
3 |
|
if (!empty($_oJobEntity->schedule)) |
109
|
|
|
{ |
110
|
3 |
|
$_bPrintTime = (0 == ($_iRowCount % 5)) ? true : false; |
111
|
3 |
|
$_oJobStats = $this->oJobStatsService->getJobStats($_oJobEntity->name); |
|
|
|
|
112
|
|
|
|
113
|
3 |
|
$_oJobDatePeriod = $this->createDatePeriodForJob($_oJobEntity, $iEndTime); |
114
|
3 |
|
$_fJobRunTime = $_oJobStats->histogram->mean / 1000; |
|
|
|
|
115
|
|
|
|
116
|
3 |
|
$_oTable->addRow( |
117
|
|
|
array( |
118
|
3 |
|
$_oJobEntity->name, |
119
|
3 |
|
$this->getTimelineStr( |
120
|
|
|
$_oDatePeriod, |
121
|
|
|
$_oJobDatePeriod, |
122
|
|
|
$_fJobRunTime, |
123
|
|
|
$_bPrintTime |
124
|
|
|
) |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
|
128
|
3 |
|
++$_iRowCount; |
129
|
|
|
|
130
|
|
|
// print child jobs |
131
|
3 |
|
$this->printChildJobs($_oTable, $_oDatePeriod, $_oJobDatePeriod, $_oJobEntity->name, $_fJobRunTime, $_iRowCount, 0); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
$_oTable->render(); |
136
|
3 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Table $oTable |
140
|
|
|
* @param \DatePeriod $oDisplayPeriod |
141
|
|
|
* @param \DatePeriod $oJobDatePeriod |
142
|
|
|
* @param string $sParentJobName |
143
|
|
|
* @param float $fParentJobRunTime |
144
|
|
|
* @param int $iRowCount |
145
|
|
|
* @param int $iCurrentChildLevel |
146
|
|
|
*/ |
147
|
3 |
|
private function printChildJobs( |
148
|
|
|
Table $oTable, |
149
|
|
|
\DatePeriod $oDisplayPeriod, |
150
|
|
|
\DatePeriod $oJobDatePeriod, |
151
|
|
|
$sParentJobName, |
152
|
|
|
$fParentJobRunTime, |
153
|
|
|
&$iRowCount, |
154
|
|
|
$iCurrentChildLevel = 0 |
155
|
|
|
) |
156
|
|
|
{ |
157
|
3 |
|
$_aChildJobs = $this->oJobDependencyService->getChildJobs($sParentJobName, JobDependencyServiceInterface::REPOSITORY_LOCAL); |
158
|
|
|
|
159
|
3 |
|
foreach ($_aChildJobs as $_sChildJobName) |
160
|
|
|
{ |
161
|
1 |
|
$_bPrintTime = (0 == ($iRowCount % 5)) ? true : false; |
|
|
|
|
162
|
1 |
|
$_oChildJobStats = $this->oJobStatsService->getJobStats($_sChildJobName); |
163
|
|
|
|
164
|
1 |
|
$oTable->addRow( |
165
|
|
|
array( |
166
|
1 |
|
str_repeat(' ', $iCurrentChildLevel) . '|_ ' . $_sChildJobName, |
167
|
1 |
|
$this->getTimelineStr( |
168
|
|
|
$oDisplayPeriod, |
169
|
|
|
$oJobDatePeriod, |
170
|
1 |
|
$_oChildJobStats->histogram->mean / 1000, |
171
|
|
|
$_bPrintTime, |
172
|
|
|
round($fParentJobRunTime) |
173
|
|
|
) |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
|
177
|
1 |
|
++$iRowCount; |
178
|
|
|
|
179
|
|
|
// next level |
180
|
1 |
|
$this->printChildJobs( |
181
|
|
|
$oTable, |
182
|
|
|
$oDisplayPeriod, |
183
|
|
|
$oJobDatePeriod, |
184
|
|
|
$_sChildJobName, |
185
|
1 |
|
$_oChildJobStats->histogram->mean / 1000 + $fParentJobRunTime, |
186
|
|
|
$iRowCount, |
187
|
1 |
|
++$iCurrentChildLevel |
188
|
|
|
); |
189
|
|
|
} |
190
|
3 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param \DatePeriod $oDatePeriod |
194
|
|
|
* @param \DatePeriod $oJobDatePeriod |
195
|
|
|
* @param float $fRunSeconds |
196
|
|
|
* @param boolean $bPrintTime |
197
|
|
|
* @param int $iJobStartTimeDelay |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
3 |
|
private function getTimelineStr( |
201
|
|
|
\DatePeriod $oDatePeriod, |
202
|
|
|
\DatePeriod $oJobDatePeriod, |
203
|
|
|
$fRunSeconds = 0.0, |
204
|
|
|
$bPrintTime = true, |
205
|
|
|
$iJobStartTimeDelay = 0 |
206
|
|
|
) |
207
|
|
|
{ |
208
|
3 |
|
$_sTimeline = ''; |
|
|
|
|
209
|
3 |
|
$_aStartTimes = $this->getJobStartTimesInPeriod($oJobDatePeriod, $iJobStartTimeDelay); |
210
|
|
|
|
211
|
3 |
|
$_bJobStarted = false; |
212
|
3 |
|
$_sSpacer = '-'; |
|
|
|
|
213
|
|
|
|
214
|
3 |
|
$_iRunMinutes = ($fRunSeconds > 0) ? round($fRunSeconds / 60) : 0; |
|
|
|
|
215
|
3 |
|
$_iPrintedRunMinutes = 0; |
216
|
|
|
|
217
|
3 |
|
$_bHasToCloseFinalTag = false; |
218
|
|
|
|
219
|
|
|
/** @var \DateTime $_oTime */ |
220
|
3 |
|
foreach ($oDatePeriod as $_oTime) |
221
|
|
|
{ |
222
|
3 |
|
$_bPrintJobEnd = false; |
|
|
|
|
223
|
3 |
|
$_bPrintJobStart = false; |
224
|
|
|
|
225
|
3 |
|
if (isset($_aStartTimes[$_oTime->format('YmdHi')])) |
226
|
|
|
{ |
227
|
3 |
|
$_bJobStarted = true; |
|
|
|
|
228
|
3 |
|
$_bPrintJobStart = true; |
229
|
3 |
|
$_sSpacer = '='; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
3 |
|
if ($_bJobStarted) |
233
|
|
|
{ |
234
|
3 |
|
++$_iPrintedRunMinutes; |
235
|
|
|
|
236
|
3 |
|
if ($_iRunMinutes <= $_iPrintedRunMinutes) |
237
|
|
|
{ |
238
|
3 |
|
$_bJobStarted = false; |
|
|
|
|
239
|
3 |
|
$_bPrintJobEnd = true; |
|
|
|
|
240
|
3 |
|
$_sSpacer = '-'; |
|
|
|
|
241
|
3 |
|
$_iPrintedRunMinutes = 0; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
3 |
|
$_iMod = ((int) $_oTime->format('i')) % 15; |
246
|
|
|
|
247
|
3 |
|
if ($_iMod == 0) |
248
|
|
|
{ |
249
|
3 |
|
if ($bPrintTime) |
250
|
|
|
{ |
251
|
3 |
|
$_sTimeline .= $this->parseTimeLineStrMark($_bPrintJobStart, $_bPrintJobEnd, $_bHasToCloseFinalTag, $_oTime->format('H>i'), $_oTime->format('H:i')); |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
else |
255
|
|
|
{ |
256
|
3 |
|
$_sTimeline .= $this->parseTimeLineStrMark($_bPrintJobStart, $_bPrintJobEnd, $_bHasToCloseFinalTag, str_repeat('>', 5), str_repeat($_sSpacer, 5)); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
else |
260
|
|
|
{ |
261
|
3 |
|
$_sTimeline .= $this->parseTimeLineStrMark($_bPrintJobStart, $_bPrintJobEnd, $_bHasToCloseFinalTag, '>', $_sSpacer); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// add final tag to the end if you runs longer than current timeframe |
266
|
3 |
|
if ($_bHasToCloseFinalTag) |
267
|
|
|
{ |
268
|
|
|
$_sTimeline .= '</comment>'; |
269
|
|
|
} |
270
|
|
|
|
271
|
3 |
|
return $_sTimeline; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param bool $bPrintJobStart |
276
|
|
|
* @param bool $bPrintJobEnd |
277
|
|
|
* @param bool $bHasToCloseFinalTag |
278
|
|
|
* @param string $sStartStopMark |
279
|
|
|
* @param string $sSpacer |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
3 |
|
private function parseTimeLineStrMark($bPrintJobStart, $bPrintJobEnd, &$bHasToCloseFinalTag, $sStartStopMark, $sSpacer) |
283
|
|
|
{ |
284
|
3 |
|
if ($bPrintJobStart && $bPrintJobEnd) |
285
|
|
|
{ |
286
|
|
|
$_sTimelineSnippet = sprintf('<comment>%s</comment>', $sStartStopMark); |
287
|
|
|
} |
288
|
3 |
|
elseif ($bPrintJobStart) |
289
|
|
|
{ |
290
|
3 |
|
$_sTimelineSnippet = sprintf('<comment>%s', $sStartStopMark); |
|
|
|
|
291
|
3 |
|
$bHasToCloseFinalTag = true; |
292
|
|
|
} |
293
|
3 |
|
elseif ($bPrintJobEnd) |
294
|
|
|
{ |
295
|
3 |
|
$_sTimelineSnippet = sprintf('%s</comment>', $sStartStopMark); |
|
|
|
|
296
|
3 |
|
$bHasToCloseFinalTag = false; |
297
|
|
|
} |
298
|
|
|
else |
299
|
|
|
{ |
300
|
3 |
|
$_sTimelineSnippet = $sSpacer; |
301
|
|
|
} |
302
|
|
|
|
303
|
3 |
|
return $_sTimelineSnippet; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param \DatePeriod $oJobDatePeriod |
308
|
|
|
* @param int $iJobStartTimeDelay |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
3 |
|
private function getJobStartTimesInPeriod(\DatePeriod $oJobDatePeriod, $iJobStartTimeDelay = 0) |
312
|
|
|
{ |
313
|
3 |
|
$_aStartTimes = []; |
314
|
|
|
|
315
|
|
|
/** @var \DateTime $_oJobTime */ |
316
|
3 |
|
foreach ($oJobDatePeriod as $_oJobTime) |
317
|
|
|
{ |
318
|
3 |
|
if ($iJobStartTimeDelay > 0) |
319
|
|
|
{ |
320
|
1 |
|
$_oJobTime->add(new \DateInterval('PT' . $iJobStartTimeDelay . 'S')); |
321
|
|
|
} |
322
|
|
|
|
323
|
3 |
|
$_aStartTimes[$_oJobTime->format('YmdHi')] = $_oJobTime; |
324
|
|
|
} |
325
|
|
|
|
326
|
3 |
|
return $_aStartTimes; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param ChronosJobEntity $oJobEntity |
331
|
|
|
* @param int $iEndTime |
332
|
|
|
* @return \DatePeriod |
333
|
|
|
*/ |
334
|
3 |
|
private function createDatePeriodForJob(ChronosJobEntity $oJobEntity, $iEndTime) |
335
|
|
|
{ |
336
|
3 |
|
$_oIso8601Entity = $this->oDatePeriodFactory->createIso8601Entity($oJobEntity->schedule); |
337
|
3 |
|
return $this->createDatePeriod($_oIso8601Entity->sStartTime, 0, null, $iEndTime, $_oIso8601Entity->sInterval); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param string $sDateTimeStart |
342
|
|
|
* @param int $iTimestampStart |
343
|
|
|
* @param string $sDateTimeEnd |
344
|
|
|
* @param int $iTimestampEnd |
345
|
|
|
* @param string $sDateInterval |
346
|
|
|
* @return \DatePeriod |
347
|
|
|
*/ |
348
|
3 |
|
private function createDatePeriod($sDateTimeStart = '', $iTimestampStart = 0, $sDateTimeEnd = '', $iTimestampEnd = 0, $sDateInterval = 'PT1M') |
349
|
|
|
{ |
350
|
3 |
|
$_oDateStart = new \DateTime($sDateTimeStart); |
351
|
3 |
|
if ($iTimestampStart > 0) |
352
|
|
|
{ |
353
|
3 |
|
$_oDateStart->setTimestamp($iTimestampStart); |
354
|
|
|
} |
355
|
|
|
|
356
|
3 |
|
$_oDateEnd = new \DateTime($sDateTimeEnd); |
357
|
3 |
|
if ($iTimestampEnd > 0) |
358
|
|
|
{ |
359
|
3 |
|
$_oDateEnd->setTimestamp($iTimestampEnd); |
360
|
|
|
} |
361
|
|
|
|
362
|
3 |
|
$_oDateInterval = new \DateInterval($sDateInterval); |
363
|
|
|
|
364
|
3 |
|
return new \DatePeriod($_oDateStart, $_oDateInterval, $_oDateEnd); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param int $iStartTime |
369
|
|
|
* @param int $iEndTime |
370
|
|
|
* @return ChronosJobEntity[] |
371
|
|
|
*/ |
372
|
3 |
|
private function getJobsWhichShouldStartInPeriod($iStartTime, $iEndTime) |
373
|
|
|
{ |
374
|
3 |
|
$_oJobCollection = $this->oJobRepositoryChronos->getJobs(); |
375
|
|
|
|
376
|
3 |
|
$_aJobs = []; |
377
|
|
|
|
378
|
|
|
/** @var ChronosJobEntity $_oJobEntity */ |
379
|
3 |
|
foreach ($_oJobCollection as $_oJobEntity) |
380
|
|
|
{ |
381
|
|
|
if ( |
382
|
3 |
|
$_oJobEntity->isSchedulingJob() |
383
|
3 |
|
&& false === $_oJobEntity->disabled |
384
|
3 |
|
&& false === strpos($_oJobEntity->schedule, 'R0') |
385
|
|
|
) |
386
|
|
|
{ |
387
|
3 |
|
$_oJobDatePeriod = $this->createDatePeriodForJob($_oJobEntity, $iEndTime); |
388
|
3 |
|
if ($this->isPeriodInTimeFrame($_oJobDatePeriod, $iStartTime, $iEndTime)) |
389
|
|
|
{ |
390
|
3 |
|
$_aJobs[] = $_oJobEntity; |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
3 |
|
return $_aJobs; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @param \DatePeriod $oJobDatePeriod |
400
|
|
|
* @param int $iStartTime |
401
|
|
|
* @param int $iEndTime |
402
|
|
|
* @return bool |
403
|
|
|
*/ |
404
|
3 |
|
private function isPeriodInTimeFrame(\DatePeriod $oJobDatePeriod, $iStartTime, $iEndTime) |
405
|
|
|
{ |
406
|
3 |
|
$_iLastTime = 0; |
407
|
|
|
|
408
|
|
|
/** @var \DateTime $_oJobTime */ |
409
|
3 |
|
foreach ($oJobDatePeriod as $_oJobTime) |
410
|
|
|
{ |
411
|
|
|
// jobs under 1 hours should always be displayed (break after the second loop) |
412
|
3 |
|
if ($_oJobTime->getTimestamp() - $_iLastTime <= 3600) |
413
|
|
|
{ |
414
|
3 |
|
return true; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
// is one starting point in timeframe? |
418
|
3 |
|
if ($_oJobTime->getTimestamp() >= $iStartTime && $_oJobTime->getTimestamp() <= $iEndTime) |
419
|
|
|
{ |
420
|
|
|
return true; |
421
|
|
|
} |
422
|
|
|
|
423
|
3 |
|
$_iLastTime = $_oJobTime->getTimestamp(); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
return false; |
427
|
|
|
} |
428
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.