Skip to content

Commit

Permalink
show_progress: add finished=true/false to archive_progress json, fixes
Browse files Browse the repository at this point in the history
…#6570

also:
- remove empty values from final json
- add test
  • Loading branch information
ThomasWaldmann committed May 8, 2022
1 parent bcac974 commit 9dff959
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/borg/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ def show_progress(self, item=None, final=False, stream=None, dt=None):
if dt is None or now - self.last_progress > dt:
self.last_progress = now
if self.output_json:
data = self.as_dict()
if not final:
data = self.as_dict()
data['path'] = remove_surrogates(item.path if item else '')
else:
data = {}
data.update({
'time': time.time(),
'type': 'archive_progress',
'path': remove_surrogates(item.path if item else ''),
'finished': final,
})
msg = json.dumps(data)
end = '\n'
Expand Down
29 changes: 29 additions & 0 deletions src/borg/testsuite/archive.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from collections import OrderedDict
from datetime import datetime, timezone
from io import StringIO
Expand Down Expand Up @@ -61,6 +62,34 @@ def test_stats_format(stats):
assert repr(stats) == f'<Statistics object at {id(stats):#x} (20, 10, 10)>'


def test_stats_progress_json(stats):
stats.output_json = True

out = StringIO()
stats.show_progress(item=Item(path='foo'), stream=out)
result = json.loads(out.getvalue())
assert result['type'] == 'archive_progress'
assert isinstance(result['time'], float)
assert result['finished'] is False
assert result['path'] == 'foo'
assert result['original_size'] == 20
assert result['compressed_size'] == 10
assert result['deduplicated_size'] == 10
assert result['nfiles'] == 0 # this counter gets updated elsewhere

out = StringIO()
stats.show_progress(stream=out, final=True)
result = json.loads(out.getvalue())
assert result['type'] == 'archive_progress'
assert isinstance(result['time'], float)
assert result['finished'] is True # see #6570
assert 'path' not in result
assert 'original_size' not in result
assert 'compressed_size' not in result
assert 'deduplicated_size' not in result
assert 'nfiles' not in result


class MockCache:

class MockRepo:
Expand Down

0 comments on commit 9dff959

Please sign in to comment.