human
Solve the following python programming problem given the following code:
@check_job_permission
def job_single_logs(request, job, offset=LOG_OFFSET_BYTES):
assistant
#Here is the Python code you requested.

    """Try to smartly detect the most useful task attempt (e.g. Oozie launcher, failed task) and get its MR logs.
    """
    def cmp_exec_time(task1, task2):
        return cmp(task1.execStartTimeMs, task2.execStartTimeMs)
    task = None
    failed_tasks = job.filter_tasks(task_states=('failed',))
    failed_tasks.sort(cmp_exec_time)
    if failed_tasks:
        task = failed_tasks[0]
        if ((not task.taskAttemptIds) and (len(failed_tasks) > 1)):
            task = failed_tasks[1]
    else:
        task_states = ['running', 'succeeded']
        if job.is_mr2:
            task_states.append('scheduled')
        recent_tasks = job.filter_tasks(task_states=task_states, task_types=('map', 'reduce'))
        recent_tasks.sort(cmp_exec_time, reverse=True)
        if recent_tasks:
            task = recent_tasks[0]
    if ((task is None) or (not task.taskAttemptIds)):
        raise PopupException((_('No    tasks    found    for    job    %(id)s.') % {'id': job.jobId}))
    params = {'job': job.jobId, 'taskid': task.taskId, 'attemptid': task.taskAttemptIds[(-1)], 'offset': offset}
    if (request.GET.get('format') == 'link'):
        return JsonResponse(params)
    else:
        return single_task_attempt_logs(request, **params)