setName('events-report') ->setDescription('Создание случайных событий для каждой компании') ->addOption('company-id', null, InputOption::VALUE_REQUIRED, 'ID компании') ->addOption('month', null, InputOption::VALUE_OPTIONAL, 'Месяц') ->addOption('year', null, InputOption::VALUE_REQUIRED, 'Год') ->addOption('format', null, InputOption::VALUE_OPTIONAL, 'Тип вывода: ' . $formats, self::FORMAT_TABLE); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $eventsRepo = new EventsRepository( $this->getApplication()->getClickhouseClient() ); if (!$input->getOption('company-id')) { throw new \InvalidArgumentException('Не указан идентификатор компании'); } $today = new \DateTime(); $month = $input->getOption('month') ?: (int) $today->format('m'); $year = $input->getOption('year') ?: (int) $today->format('Y'); $result = $eventsRepo->findEventsByCompanyId( new ObjectId($input->getOption('company-id')), $month, $year ); $result = MonthEventsReportTransformer::transform($result); switch ($input->getOption('format')) { case self::FORMAT_TABLE: $this->renderTable($output, $result); break; case self::FORMAT_GRAPH: $this->renderGraph($result); break; default: throw new \InvalidArgumentException('Некорректный формат вывода'); } } /** * Вывод таблицы с событиями * * @param OutputInterface $output * @param array $result */ private function renderTable(OutputInterface $output, array $result): void { $eventTypes = CompanyEvent::keys(); $rows = []; foreach ($result as $date => $counts) { $row = [$date]; foreach ($eventTypes as $eventType) { $row[] = $counts[$eventType] ?? '0'; } $rows[] = $row; } $table = new Table($output); $table ->setHeaders(array_merge(['Дата'], $eventTypes)) ->setRows($rows); $table->render(); } /** * Сохранение графика * * @param array $result */ public function renderGraph(array $result) { $eventTypes = CompanyEvent::keys(); $plot = (new GnuPlot()) ->setXTimeFormat('%Y-%m-%d') ->setXLabel('Дата') ->setYLabel('Количество') ->setWidth(640); foreach ($eventTypes as $eventIndex => $eventType) { $plot->setTitle($eventIndex, $eventType); } foreach ($result as $date => $counts) { foreach ($eventTypes as $eventIndex => $eventType) { $plot->push($date, $counts[$eventType] ?? '0', $eventIndex); } } echo $plot->get(); } }