aboutsummaryrefslogtreecommitdiff
path: root/gn3/api/correlation.py
blob: 4e3e07edfb67668480c026f9d55db96133ecf510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Endpoints for computing correlation"""
import pickle
import time
from flask import Blueprint
from flask import jsonify
from flask import request
from flask import g
from flask import after_this_request
from default_settings import SQL_URI

# import pymysql

from sqlalchemy import create_engine
from gn3.correlation.correlation_computations import compute_correlation


correlation = Blueprint("correlation", __name__)



# xtodo implement neat db setup
@correlation.before_request
def connect_db():
    """add connection to db method"""
    print("@app.before_request connect_db")
    db_connection = getattr(g, '_database', None)
    if db_connection is None:
        print("Get new database connector")
        g.db = g._database = create_engine(SQL_URI, encoding="latin1")

    g.initial_time = time.time()


@correlation.after_request
def after_request_func(response):
    final_time = time.time() -  g.initial_time
    print(f"This request for Correlation took {final_time} Seconds")

    g.initial_time = None

    return response




@correlation.route("/corr_compute", methods=["POST"])
def corr_compute_page():
    """api for doing  correlation"""

    # todo accepts both form and json data

    correlation_input = request.json

    if correlation_input is None:
        return jsonify({"error": str("Bad request")}),400


    
    try:
        corr_results = compute_correlation(correlation_input_data=correlation_input)

        
    except Exception as error:  # pylint: disable=broad-except
        return jsonify({"error": str(error)})

    return {
    "correlation_results":corr_results
    }