def lambda_handler()

in assets/functions/redshift/consumption/app.py [0:0]


def lambda_handler(event, context):
    dbname = os.environ['Db_schema']
    parameter = event["pathParameters"]

    if ("meter_id" not in parameter) or ("year" not in parameter) or ("requested_aggregation" not in parameter):
        return {
            'statusCode': 500,
            'body': "error: meter_id, year and aggregation type needs to be provided."
        }

    meter_id = parameter["meter_id"]
    year = parameter["year"]
    requested_aggregation = parameter["requested_aggregation"]

    redshift_cred = load_redshift_cred()

    connection = psycopg2.connect(user=redshift_cred["username"],
                                  password=redshift_cred["password"],
                                  host=redshift_cred["host"],
                                  port=redshift_cred["port"],
                                  database=dbname)

    cursor = connection.cursor()

    sql = get_sql_statement(requested_aggregation)

    cursor.execute(sql, (meter_id, year,))
    daily_reads = cursor.fetchall()

    return {
        'statusCode': 200,
        'body': json.dumps(daily_reads, cls=JSONDateTimeEncoder)
    }