#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.


import psycopg2
import json
from timeit import default_timer as timer
import logging

import logan_db_config


# Set up logging
logging.basicConfig(level=logging.DEBUG,
                    format='%(levelname)s: %(asctime)s: %(message)s')


def record_master_viability_lambda_handler(event, context):

    my_payload = run()

    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world",
            "payload": my_payload,
        }),
    }


def run():

    print("Now connecting to database...")

    conn = psycopg2.connect(
        host=logan_db_config.db_hostname,
        database=logan_db_config.db_name,
        user=logan_db_config.db_username,
        password=logan_db_config.db_password)

    with conn.cursor() as cur:

        cur.execute('SET SESSION lock_timeout = 3000;')
        cur.execute('SET SESSION statement_timeout = %d;' % (1000*60))  # 1 minute

        print("Work begins now...")

        start = timer()

        cur.execute("SELECT snapshot_master_viable_commit_age();")
        conn.commit()

        end = timer()

        execution_seconds = end - start
        print("Completed in", execution_seconds, "seconds")

        return {
            "elapsed_time_seconds": execution_seconds,
        }


if __name__ == "__main__":

    payload = run()
    print(payload)
