def caption_latest()

in source/lambdafunctions/lambdaedge/lambda_function.py [0:0]


def caption_latest(pipe_id, lang_id):
    start_processtime_dynamo = time.time()
    if DEBUG:
        print("DEBUG caption_latest(pipe_id, lang_id): {}, {}".format(pipe_id, lang_id))
    messages_table = DYNAMO_RESOURCE.Table(DYNAMO_TABLE)
    try:
        response = messages_table.query(
            KeyConditionExpression=Key('id_lang').eq(lang_id),
            ScanIndexForward=False,
            Limit=5,
            IndexName=DYNAMO_INDEX,
            ProjectionExpression='timestamp_created, transcript_resultid, transcript_transcript, transcript_endtime, transcript_starttime',
            FilterExpression=Attr("id_pipe").eq(pipe_id)
            )
    except ClientError as e:
        print("ERROR DynamoDB: %s" % e)
        put_count_metrics('error_lambda', 1, pipe_id, lang_id)
        return False
    if len(response["Items"]) < 1:
        print("ERROR DynamoDB: item list is empty")
        return False
    put_duration_metrics('processtime_dynamo', start_processtime_dynamo, time.time(), pipe_id, lang_id)

    caption_string = False
    caption_latency = False
    if response["Items"][0]:
        caption_string = response["Items"][0]['transcript_transcript']
        caption_latency = float(time.time()) - float(response["Items"][0]['timestamp_created'])
        if float(response["Items"][0]['transcript_endtime']) - float(response["Items"][0]['transcript_starttime']) < float(CAPTION_BUFFER):
            # We need to show the old caption till the buffer time is over
            if DEBUG:
                print("DEBUG newest caption is smaller than CAPTION_BUFFER")
            caption_string = ""
            i = 0
            for item in response["Items"][1:]:
                i = i + 1
                caption_string = item['transcript_transcript'] + "\n" + caption_string
                caption_latency = float(time.time()) - float(item['timestamp_created'])
                if float(item['transcript_endtime']) - float(item['transcript_starttime']) > float(CAPTION_BUFFER):
                    break
        if DEBUG: 
            print("DEBUG caption_string: {}".format(caption_string))
            print("DEBUG caption_latency: {}".format(caption_latency))
        if caption_latency:
            put_known_duration_metrics('latency_caption', caption_latency, pipe_id, lang_id)
    else:
        print("ERROR no current_caption!")
        put_count_metrics('error_lambda', 1, pipe_id, lang_id)
        return False

    return caption_string