From 3568558571873b9f53067ec841b2a16c7fbcb117 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Wed, 17 Mar 2021 16:46:12 -0400 Subject: [PATCH] A couple of bug fixes --- awx/main/analytics/collectors.py | 7 +++++-- awx/main/analytics/core.py | 16 ++++++++-------- awx/main/management/commands/gather_analytics.py | 3 +-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/awx/main/analytics/collectors.py b/awx/main/analytics/collectors.py index 2ca217c8d7..247613639d 100644 --- a/awx/main/analytics/collectors.py +++ b/awx/main/analytics/collectors.py @@ -274,7 +274,10 @@ class FileSplitter(io.StringIO): self.files = self.files[:-1] # If we only have one file, remove the suffix if len(self.files) == 1: - os.rename(self.files[0], self.files[0].replace('_split0', '')) + filename = self.files.pop() + new_filename = filename.replace('_split0', '') + os.rename(filename, new_filename) + self.files.append(new_filename) return self.files def write(self, s): @@ -302,7 +305,7 @@ def events_slicing(key, since, until): last_entries = Setting.objects.filter(key='AUTOMATION_ANALYTICS_LAST_ENTRIES').first() last_entries = json.loads((last_entries.value if last_entries is not None else '') or '{}') previous_pk = last_entries.get(key) or pk_values['pk__min'] or 0 - final_pk = pk_values['pk__max'] + final_pk = pk_values['pk__max'] or 0 for start in range(previous_pk, final_pk + 1, step): yield (start, min(start + step, final_pk)) diff --git a/awx/main/analytics/core.py b/awx/main/analytics/core.py index 4077730408..2572321e2b 100644 --- a/awx/main/analytics/core.py +++ b/awx/main/analytics/core.py @@ -87,24 +87,24 @@ def package(target, data, timestamp): for name, (item, version) in data.items(): try: if isinstance(item, str): - info = f.gettarinfo(item, arcname=f'./{name}') - f.addfile(info) + f.add(item, arcname=f'./{name}') else: - fileobj = io.BytesIO(json.dumps(item).encode('utf-8')) + buf = json.dumps(item).encode('utf-8') info = tarfile.TarInfo(f'./{name}') - info.size = len(fileobj.getvalue()) + info.size = len(buf) info.mtime = timestamp.timestamp() - f.addfile(info, fileobj=fileobj) + f.addfile(info, fileobj=io.BytesIO(buf)) manifest[name] = version except Exception: logger.exception(f"Could not generate metric {name}") + return None try: - fileobj = io.BytesIO(json.dumps(manifest).encode('utf-8')) + buf = json.dumps(manifest).encode('utf-8') info = tarfile.TarInfo('./manifest.json') - info.size = len(fileobj.getvalue()) + info.size = len(buf) info.mtime = timestamp.timestamp() - f.addfile(info, fileobj=fileobj) + f.addfile(info, fileobj=io.BytesIO(buf)) except Exception: logger.exception("Could not generate manifest.json") return None diff --git a/awx/main/management/commands/gather_analytics.py b/awx/main/management/commands/gather_analytics.py index cb5506103e..60d1524fc7 100644 --- a/awx/main/management/commands/gather_analytics.py +++ b/awx/main/management/commands/gather_analytics.py @@ -48,8 +48,7 @@ class Command(BaseCommand): if opt_ship and opt_dry_run: self.logger.error('Both --ship and --dry-run cannot be processed at the same time.') return - tgzfiles = analytics.gather(collection_type='manual' if opt_ship else 'dry-run', - since=since, until=until) + tgzfiles = analytics.gather(collection_type='manual' if opt_ship else 'dry-run', since=since, until=until) if tgzfiles: for tgz in tgzfiles: self.logger.info(tgz)