File indexing completed on 2026-04-09 07:58:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 import os
0012 from traceback import format_exc
0013
0014 from flask import Blueprint, send_from_directory
0015
0016 from idds.common import exceptions
0017 from idds.common.constants import HTTP_STATUS_CODE
0018 from idds.common.utils import tar_zip_files, get_rest_cacher_dir
0019 from idds.core import (transforms as core_transforms)
0020 from idds.rest.v1.controller import IDDSController
0021
0022
0023 class Logs(IDDSController):
0024 """ get(download) logs. """
0025
0026 def get(self, workload_id, request_id):
0027 """ Get(download) logs.
0028
0029 :param workload_id: the workload id.
0030 :param request_id: The id of the request.
0031
0032 HTTP Success:
0033 200 OK
0034 HTTP Error:
0035 404 Not Found
0036 500 InternalError
0037 :returns: logs.
0038 """
0039
0040 try:
0041 if workload_id == 'null':
0042 workload_id = None
0043 if request_id == 'null':
0044 request_id = None
0045
0046 if workload_id is None and request_id is None:
0047 error = "One of workload_id and request_id should not be None or empty"
0048 return self.generate_http_response(HTTP_STATUS_CODE.InternalError, exc_cls=exceptions.CoreException.__name__, exc_msg=error)
0049 except Exception as error:
0050 print(error)
0051 print(format_exc())
0052 return self.generate_http_response(HTTP_STATUS_CODE.InternalError, exc_cls=exceptions.CoreException.__name__, exc_msg=error)
0053
0054 try:
0055 transforms = core_transforms.get_transforms(request_id=request_id, workload_id=workload_id)
0056 workdirs = []
0057 for transform in transforms:
0058 work = transform['transform_metadata']['work']
0059 workdir = work.get_workdir()
0060 if workdir:
0061 workdirs.append(workdir)
0062 if not workdirs:
0063 error = "No log files founded."
0064 return self.generate_http_response(HTTP_STATUS_CODE.InternalError, exc_cls=exceptions.CoreException.__name__, exc_msg=error)
0065
0066 cache_dir = get_rest_cacher_dir()
0067 if request_id and workload_id:
0068 output_filename = "request_%s.workload_%s.logs.tar.gz" % (request_id, workload_id)
0069 elif request_id:
0070 output_filename = "request_%s.logs.tar.gz" % (request_id)
0071 elif workload_id:
0072 output_filename = "workload_%s.logs.tar.gz" % (workload_id)
0073 else:
0074 output_filename = "%s.logs.tar.gz" % (os.path.basename(cache_dir))
0075 tar_zip_files(cache_dir, output_filename, workdirs)
0076 return send_from_directory(cache_dir, output_filename, as_attachment=True, mimetype='application/x-tgz')
0077 except exceptions.NoObject as error:
0078 return self.generate_http_response(HTTP_STATUS_CODE.NotFound, exc_cls=error.__class__.__name__, exc_msg=error)
0079 except exceptions.IDDSException as error:
0080 return self.generate_http_response(HTTP_STATUS_CODE.InternalError, exc_cls=error.__class__.__name__, exc_msg=error)
0081 except Exception as error:
0082 print(error)
0083 print(format_exc())
0084 return self.generate_http_response(HTTP_STATUS_CODE.InternalError, exc_cls=exceptions.CoreException.__name__, exc_msg=error)
0085
0086 def post_test(self):
0087 import pprint
0088 pprint.pprint(self.get_request())
0089 pprint.pprint(self.get_request().endpoint)
0090 pprint.pprint(self.get_request().url_rule)
0091
0092
0093 """----------------------
0094 Web service url maps
0095 ----------------------"""
0096
0097
0098 def get_blueprint():
0099 bp = Blueprint('logs', __name__)
0100
0101 logs_view = Logs.as_view('logs')
0102 bp.add_url_rule('/logs/<workload_id>/<request_id>', view_func=logs_view, methods=['get', ])
0103
0104 return bp