Skip to content

Commit

Permalink
fix: torrent-stat and torrent-list correct totals when limit
Browse files Browse the repository at this point in the history
Now limit and sort made before count totals.

Closes #21
  • Loading branch information
popstas committed Mar 7, 2016
1 parent 7229c34 commit 6e7f584
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
76 changes: 42 additions & 34 deletions src/Command/StatsGet.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
]
];
}
}
25 changes: 14 additions & 11 deletions src/Helpers/TorrentListUtils.php
Expand Up @@ -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
];
}

Expand All @@ -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)
Expand Down

0 comments on commit 6e7f584

Please sign in to comment.