Issues (125)

lib/Command/ProgressCommand.php (1 issue)

1
<?php
2
/**
3
 * @copyright Copyright (c) 2020, Matias De lellis <[email protected]>
4
 *
5
 * @author Matias De lellis <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OCA\FaceRecognition\Command;
24
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Helper\Table;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
use OCP\IDateTimeFormatter;
32
33
use OCA\FaceRecognition\Db\ImageMapper;
34
use OCA\FaceRecognition\Db\FaceMapper;
35
use OCA\FaceRecognition\Db\PersonMapper;
36
37
use OCA\FaceRecognition\Service\SettingsService;
38
39
class ProgressCommand extends Command {
40
41
	/** @var IDateTimeFormatter */
42
	protected $dateTimeFormatter;
43
44
	/** @var ImageMapper */
45
	protected $imageMapper;
46
47
	/** @var FaceMapper */
48
	protected $faceMapper;
49
50
	/** @var PersonMapper */
51
	protected $personMapper;
52
53
	/** @var SettingsService */
54
	private $settingsService;
55
56
	/**
57
	 * @param IDateTimeFormatter $dateTimeFormatter
58
	 * @param ImageMapper $imageMapper
59
	 * @param FaceMapper $faceMapper
60
	 * @param PersonMapper $personMapper
61
	 * @param SettingsService $settingsService
62
	 */
63
	public function __construct(IDateTimeFormatter $dateTimeFormatter,
64
	                            ImageMapper        $imageMapper,
65
	                            FaceMapper         $faceMapper,
66
	                            PersonMapper       $personMapper,
67
	                            SettingsService    $settingsService)
68
	{
69
		parent::__construct();
70
71
		$this->dateTimeFormatter = $dateTimeFormatter;
72
		$this->imageMapper       = $imageMapper;
73
		$this->faceMapper        = $faceMapper;
74
		$this->personMapper      = $personMapper;
75
		$this->settingsService   = $settingsService;
76
	}
77
78
	/**
79
	 * @return void
80
	 */
81
	protected function configure() {
82
		$this
83
			->setName('face:progress')
84
			->setDescription('Get the progress of the analysis and an estimated time')
85
			->addOption(
86
				'json',
87
				'j',
88
				InputOption::VALUE_NONE,
89
				'Print in a json format, useful to analyze it with another tool.',
90
				null
91
			);
92
	}
93
94
	/**
95
	 * @param InputInterface $input
96
	 * @param OutputInterface $output
97
	 * @return int
98
	 */
99
	protected function execute(InputInterface $input, OutputInterface $output) {
100
		$modelId = $this->settingsService->getCurrentFaceModel();
101
102
		$totalImages = $this->imageMapper->countImages($modelId);
103
		$processedImages = $this->imageMapper->countProcessedImages($modelId);
104
105
		$remainingImages = $totalImages - $processedImages;
106
107
		$avgProcessingTime = $this->imageMapper->avgProcessingDuration($modelId);
108
		$estimatedSeconds = (int) ($remainingImages * $avgProcessingTime/1000);
109
110
		if ($input->getOption('json')) {
111
			$this->printJsonProgress($output, $totalImages, $remainingImages, $estimatedSeconds);
112
		} else {
113
			$this->printTabledProgress($output, $totalImages, $remainingImages, $estimatedSeconds);
114
		}
115
		return 0;
116
	}
117
118
	private function printTabledProgress(OutputInterface $output, $totalImages, $remainingImages, $estimatedSeconds): void {
119
		if ($estimatedSeconds) {
120
			$estimatedTime = $this->dateTimeFormatter->formatTimeSpan((time() + $estimatedSeconds));
121
		} else {
122
			$estimatedTime = '-';
123
		}
124
125
		$table = new Table($output);
126
		$table
127
			->setHeaders(['Images', 'Remaining', 'ETA'])
128
			->setRows([[strval($totalImages), strval($remainingImages), $estimatedTime]]);
129
		$table->render();
130
	}
131
132
	private function printJsonProgress(OutputInterface $output, $totalImages, $remainingImages, $estimatedSeconds): void {
133
		$stats[] = array(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$stats was never initialized. Although not strictly required by PHP, it is generally a good practice to add $stats = array(); before regardless.
Loading history...
134
			'images'    => $totalImages,
135
			'remaining' => $remainingImages,
136
			'eta'       => $estimatedSeconds
137
		);
138
		$output->writeln(json_encode($stats));
139
	}
140
}
141