<s> from __future__ import absolute_import , division , unicode_literals <EOL> from flask import Response , request <EOL> from changes . api . base import APIView <EOL> from changes . models import LogSource , LogChunk <EOL> LOG_BATCH_SIZE = <NUM_LIT> <EOL> class JobLogDetailsAPIView ( APIView ) : <EOL> def get ( self , job_id , source_id ) : <EOL> """<STR_LIT>""" <EOL> source = LogSource . query . get ( source_id ) <EOL> if source is None or source . job_id != job_id : <EOL> return '<STR_LIT>' , <NUM_LIT> <EOL> offset = int ( request . args . get ( '<STR_LIT>' , - <NUM_LIT:1> ) ) <EOL> limit = int ( request . args . get ( '<STR_LIT>' , - <NUM_LIT:1> ) ) <EOL> raw = request . args . get ( '<STR_LIT>' ) <EOL> if raw and limit == - <NUM_LIT:1> : <EOL> limit = <NUM_LIT:0> <EOL> elif limit == - <NUM_LIT:1> : <EOL> limit = LOG_BATCH_SIZE <EOL> queryset = LogChunk . query . filter ( <EOL> LogChunk . source_id == source . id , <EOL> ) . order_by ( LogChunk . offset . desc ( ) ) <EOL> if offset == - <NUM_LIT:1> : <EOL> tail = queryset . limit ( <NUM_LIT:1> ) . first ( ) <EOL> if tail is None : <EOL> logchunks = [ ] <EOL> else : <EOL> if limit : <EOL> queryset = queryset . filter ( <EOL> ( LogChunk . offset + LogChunk . size ) >= max ( tail . offset + tail . size - limit , <NUM_LIT:0> ) , <EOL> ) <EOL> logchunks = list ( queryset ) <EOL> else : <EOL> queryset = queryset . filter ( <EOL> LogChunk . offset > offset , <EOL> ) <EOL> if limit : <EOL> queryset = queryset . filter ( <EOL> LogChunk . offset <= offset + limit , <EOL> ) <EOL> logchunks = list ( queryset ) <EOL> logchunks . sort ( key = lambda x : x . date_created ) <EOL> if logchunks : <EOL> next_offset = logchunks [ - <NUM_LIT:1> ] . offset + logchunks [ - <NUM_LIT:1> ] . size + <NUM_LIT:1> <EOL> else : <EOL> next_offset = offset <EOL> if raw : <EOL> return Response ( '<STR_LIT>' . join ( l . text for l in logchunks ) , mimetype = '<STR_LIT>' ) <EOL> context = self . serialize ( { <EOL> '<STR_LIT:source>' : source , <EOL> '<STR_LIT>' : logchunks , <EOL> '<STR_LIT>' : next_offset , <EOL> } ) <EOL> context [ '<STR_LIT:source>' ] [ '<STR_LIT>' ] = self . serialize ( source . step ) <EOL> if source . step : <EOL> context [ '<STR_LIT:source>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . serialize ( source . step . phase ) , <EOL> return self . respond ( context , serialize = False ) </s>
