Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 373fa26

Browse files
committed
Correctly aggregate totals for >10k items
Previously, we would write out totals for every page of listings, like $ swift list sync --prefix=09-21 --total -l 80000000000 80000000000 80000000000 58096000000 Now, roll those all into a single total: $ swift list sync --prefix=09-21 --total -l 298096000000 Change-Id: Icc265636815220e33e8c9eec0a3ab80e9f899038
1 parent 5129b33 commit 373fa26

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

swiftclient/shell.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,7 @@ def st_download(parser, args, output_manager, return_parser=False):
531531

532532
def st_list(parser, args, output_manager, return_parser=False):
533533

534-
def _print_stats(options, stats, human):
535-
total_count = total_bytes = 0
534+
def _print_stats(options, stats, human, totals):
536535
container = stats.get("container", None)
537536
for item in stats["listing"]:
538537
item_name = item.get('name')
@@ -543,7 +542,7 @@ def _print_stats(options, stats, human):
543542
item_bytes = item.get('bytes')
544543
byte_str = prt_bytes(item_bytes, human)
545544
count = item.get('count')
546-
total_count += count
545+
totals['count'] += count
547546
try:
548547
meta = item.get('meta')
549548
utc = gmtime(float(meta.get('x-timestamp')))
@@ -578,17 +577,7 @@ def _print_stats(options, stats, human):
578577
output_manager.print_msg(
579578
"%s %10s %8s %24s %s",
580579
byte_str, date, xtime, content_type, item_name)
581-
total_bytes += item_bytes
582-
583-
# report totals
584-
if options['long'] or human:
585-
if not container:
586-
output_manager.print_msg(
587-
"%12s %s", prt_bytes(total_count, True),
588-
prt_bytes(total_bytes, human))
589-
else:
590-
output_manager.print_msg(
591-
prt_bytes(total_bytes, human))
580+
totals['bytes'] += item_bytes
592581

593582
parser.add_argument(
594583
'-l', '--long', dest='long', action='store_true', default=False,
@@ -642,6 +631,7 @@ def _print_stats(options, stats, human):
642631
try:
643632
if not args:
644633
stats_parts_gen = swift.list()
634+
container = None
645635
else:
646636
container = args[0]
647637
args = args[1:]
@@ -667,12 +657,24 @@ def listing(stats_parts_gen=stats_parts_gen):
667657
sort_keys=True, indent=2)
668658
output_manager.print_msg('')
669659
return
660+
661+
totals = {'count': 0, 'bytes': 0}
670662
for stats in stats_parts_gen:
671663
if stats["success"]:
672-
_print_stats(options, stats, human)
664+
_print_stats(options, stats, human, totals)
673665
else:
674666
raise stats["error"]
675667

668+
# report totals
669+
if options['long'] or human:
670+
if container is None:
671+
output_manager.print_msg(
672+
"%12s %s", prt_bytes(totals['count'], True),
673+
prt_bytes(totals['bytes'], human))
674+
else:
675+
output_manager.print_msg(
676+
prt_bytes(totals['bytes'], human))
677+
676678
except SwiftError as e:
677679
output_manager.error(e.value)
678680

test/unit/test_shell.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,12 @@ def test_list_container(self, connection):
547547

548548
self.assertEqual(output.out, 'object_a\n')
549549

550-
# Test container listing with --long
550+
# Test container listing with --long and multiple pages
551551
connection.return_value.get_container.side_effect = [
552-
[None, [{'name': 'object_a', 'bytes': 0,
552+
[None, [{'name': 'object_a', 'bytes': 3,
553+
'content_type': 'type/content',
554+
'last_modified': '123T456'}]],
555+
[None, [{'name': 'object_b', 'bytes': 5,
553556
'content_type': 'type/content',
554557
'last_modified': '123T456'}]],
555558
[None, []],
@@ -567,9 +570,11 @@ def test_list_container(self, connection):
567570
connection.return_value.get_container.assert_has_calls(calls)
568571

569572
self.assertEqual(output.out,
570-
' 0 123 456'
573+
' 3 123 456'
571574
' type/content object_a\n'
572-
' 0\n')
575+
' 5 123 456'
576+
' type/content object_b\n'
577+
' 8\n')
573578

574579
@mock.patch('swiftclient.service.Connection')
575580
def test_list_container_with_headers(self, connection):

0 commit comments

Comments
 (0)