From 6e7f584b99c6c9db5f225c24d0bafe36ec40cea5 Mon Sep 17 00:00:00 2001 From: Stanislav Popov Date: Tue, 8 Mar 2016 01:23:58 +0500 Subject: [PATCH] fix: torrent-stat and torrent-list correct totals when limit Now limit and sort made before count totals. Closes #21 --- src/Command/StatsGet.php | 76 ++++++++++++++++++-------------- src/Helpers/TorrentListUtils.php | 25 ++++++----- 2 files changed, 56 insertions(+), 45 deletions(-) diff --git a/src/Command/StatsGet.php b/src/Command/StatsGet.php index d34fd70..22e89c6 100644 --- a/src/Command/StatsGet.php +++ b/src/Command/StatsGet.php @@ -118,47 +118,21 @@ protected function execute(InputInterface $input, OutputInterface $output) return $torrent; }, $torrentList); - - $torrentList = TorrentListUtils::filterTorrents($torrentList, [ - 'profit' => ['type' => 'numeric', 'value' => $input->getOption('profit')] - ]); - - - $rows = []; - - foreach ($torrentList as $torrent) { - $rows[] = [ - $torrent[Torrent\Get::NAME], - $torrent[Torrent\Get::ID], - $torrent['age'], - TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]), - TorrentUtils::getSizeInGb($torrent['uploaded']), - $torrent['per_day'], - $torrent['profit'] - ]; - } } catch (\Exception $e) { $logger->critical($e->getMessage()); return 1; } - TableUtils::printTable([ - 'headers' => ['Name', 'Id', 'Age, days', 'Size, GB', 'Uploaded, GB', 'Per day, GB', 'Profit, %'], - 'rows' => $rows, - 'totals' => [ - '', - '', - '', - // TODO: it wrong if sort and limit applied, see https://github.com/popstas/transmission-cli/issues/21 - TorrentUtils::getSizeInGb(TorrentListUtils::sumArrayField($torrentList, Torrent\Get::TOTAL_SIZE)), - TorrentListUtils::sumArrayField($rows, 4), - TorrentListUtils::sumArrayField($rows, 5), - TorrentListUtils::sumArrayField($rows, 6), - ] - ], $output, $input->getOption('sort'), $limit); + $torrentList = TorrentListUtils::filterTorrents($torrentList, [ + 'profit' => ['type' => 'numeric', 'value' => $input->getOption('profit')] + ]); + + $tableData = $this->buildTableData($torrentList, $input->getOption('sort'), $limit); + + TableUtils::printTable($tableData, $output); if ($input->getOption('rm')) { - return $this->removeTorrents($input, $output, $rows); + return $this->removeTorrents($input, $output, $tableData['rows']); } return 0; @@ -187,4 +161,38 @@ private function removeTorrents(InputInterface $input, OutputInterface $output, $removeInput = new ArrayInput($arguments); return $command->run($removeInput, $output); } + + private function buildTableData(array $torrentList, $sort, $limit) + { + $rows = []; + + foreach ($torrentList as $torrent) { + $rows[] = [ + $torrent[Torrent\Get::NAME], + $torrent[Torrent\Get::ID], + $torrent['age'], + TorrentUtils::getSizeInGb($torrent[Torrent\Get::TOTAL_SIZE]), + TorrentUtils::getSizeInGb($torrent['uploaded']), + $torrent['per_day'], + $torrent['profit'] + ]; + } + + $rows = TableUtils::sortRowsByColumnNumber($rows, $sort); + $rows = TableUtils::limitRows($rows, $limit); + + return [ + 'headers' => ['Name', 'Id', 'Age, days', 'Size, GB', 'Uploaded, GB', 'Per day, GB', 'Profit, %'], + 'rows' => $rows, + 'totals' => [ + '', + '', + '', + TorrentListUtils::sumArrayField($rows, 3), + TorrentListUtils::sumArrayField($rows, 4), + TorrentListUtils::sumArrayField($rows, 5), + TorrentListUtils::sumArrayField($rows, 6), + ] + ]; + } } diff --git a/src/Helpers/TorrentListUtils.php b/src/Helpers/TorrentListUtils.php index 4837301..90bdf95 100644 --- a/src/Helpers/TorrentListUtils.php +++ b/src/Helpers/TorrentListUtils.php @@ -70,19 +70,9 @@ public static function buildTableData(array $torrentList) ]; } - $totals = [ - 'Total', - '', - '', - TorrentUtils::getSizeInGb(self::sumArrayField($torrentList, Torrent\Get::TOTAL_SIZE)), - TorrentUtils::getSizeInGb(self::sumArrayField($torrentList, Torrent\Get::UPLOAD_EVER)), - '' - ]; - return [ 'headers' => $headers, 'rows' => $rows, - 'totals' => $totals ]; } @@ -93,7 +83,20 @@ public static function printTorrentsTable( $limit = 0 ) { $data = self::buildTableData($torrentList); - TableUtils::printTable($data, $output, $sortColumnNumber, $limit); + + $data['rows'] = TableUtils::sortRowsByColumnNumber($data['rows'], $sortColumnNumber); + $data['rows'] = TableUtils::limitRows($data['rows'], $limit); + + $data['totals'] = [ + 'Total', + '', + '', + self::sumArrayField($data['rows'], 3), + self::sumArrayField($data['rows'], 4), + '' + ]; + + TableUtils::printTable($data, $output); } public static function getObsoleteTorrents(array $torrentList)