def result_download(result_id, label):
    if result_id is not None:
        results = [get_result(result_id)]
    else:
        results = get_all_results(label=label)

    if results is None or len(results) == 0:
        return json.dumps({
            'status': 'error',
            'code': 1
        }), 404, {
            'ContentType': 'application/json'
        }

    out_path = f'public/results_{time.time()}.zip'

    zipf = zipfile.ZipFile(out_path, 'w', zipfile.ZIP_DEFLATED)
    for r in results:
        zipf.writestr(f'result_{r.id}.json', r.toJson())
    if len(results) > 1:
        zipf.writestr('results.json', Result.toListJson(results))
    zipf.close()

    file = io.FileIO(out_path, 'r')
    memory_file = io.BytesIO()
    memory_file.write(file.readall())
    memory_file.seek(0)
    file.close()

    os.remove(out_path)

    return send_file(memory_file,
                     attachment_filename='results.zip',
                     as_attachment=True)
def results(result_id, label):
    if result_id is not None:
        result = get_result(result_id)
        if result is None:
            result = json.dumps(result)
        else:
            result = result.toJson()
    else:
        result_list = get_all_results(label=label)

        if label is None:
            result_list = list(set([x.config[0]['label']
                                    for x in result_list]))

        result = Result.toListJson(result_list)

    return result, 200, {'ContentType': 'application/json'}