Skip to content

Commit

Permalink
Feat: Run metrics are now downloaded as zip (#816)
Browse files Browse the repository at this point in the history
Needs to be tested in LIVE environment to compare the behavior happening in DEV.
  • Loading branch information
crazyplayy authored Sep 11, 2023
1 parent fa2ea62 commit 03f2399
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
20 changes: 16 additions & 4 deletions web/api/maestro_api/controllers/run_metric.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import time
from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED
from io import BytesIO
from flask import request, jsonify, send_file

from maestro_api.db.models.run import Run
Expand Down Expand Up @@ -67,16 +70,25 @@ def download(self, run_id, data, user):
]

filename = f"metrics_{run.id}.csv"
content_type = "text/csv"

binary_file = CsvBytesIO.create_from_dict(headers, jmeter_metrics)

zip_buffer = BytesIO()
zip_filename = f"run_metrics_{run.id}.zip"

with ZipFile(zip_buffer, "w", compression=ZIP_DEFLATED) as zip_file:
csv_info = ZipInfo(filename)
csv_info.date_time = time.localtime(time.time())[:6]
zip_file.writestr(csv_info, binary_file.getvalue())

zip_buffer.seek(0)

return (
send_file(
binary_file,
zip_buffer,
as_attachment=True,
download_name=filename,
mimetype=content_type,
download_name=zip_filename,
mimetype="application/zip",
),
200,
)
Expand Down
12 changes: 11 additions & 1 deletion web/api/tests/routes/test_run_metric.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io
import zipfile
import json
from datetime import datetime, timedelta

Expand Down Expand Up @@ -409,4 +411,12 @@ def test_run_metrics_download(client):
)

assert 200 == response.status_code
assert file_content == response.data.decode("utf-8")
assert "application/zip" in response.content_type

zip_data = io.BytesIO(response.data)
with zipfile.ZipFile(zip_data, "r") as zip_file:
assert len(zip_file.namelist()) == 1
csv_file_name = zip_file.namelist()[0]

csv_content = zip_file.read(csv_file_name).decode("utf-8")
assert csv_content == file_content

0 comments on commit 03f2399

Please sign in to comment.