forked from marius-reed/fastapihackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (49 loc) · 1.97 KB
/
main.py
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
69
70
71
72
73
import psycopg2
from fastapi import FastAPI, status
import os
import urllib
import json
host_server = os.environ.get('HOST_SERVER', 'pssql-fastapihackatahon01.postgres.database.azure.com')
db_name = os.environ.get('DB_NAME', 'postgres')
db_username = os.environ.get('DB_USERNAME', 'pssqlfastapihackathonadmin')
db_password = os.environ.get('DB_PASSWORD', '')
ssl_mode = os.environ.get('SSL_MODE','require')
app = FastAPI()
@app.get("/v1/Customer/ChurnScore/{customer_id}")
async def get_customer_churn_score(customer_id:int):
conn = psycopg2.connect(
host=host_server,
database=db_name,
user=db_username,
password=db_password)
cur = conn.cursor()
query = "Select * from fastapihackathon.CustomerScore where model_name = 'churn_30d' and customer_id = %s and not has_churned "
records = cur.execute(query,(customer_id,))
records = cur.fetchall()
record = [dict((cur.description[i][0],value) for i, value in enumerate(row)) for row in records]
return record
@app.post("/v1/Customer/HasChurned/{customer_id}")
async def post_has_churned(customer_id:int):
conn = psycopg2.connect(
host=host_server,
database=db_name,
user=db_username,
password=db_password)
cur = conn.cursor()
query = "UPDATE fastapihackathon.CustomerScore SET has_churned = True where customer_id = %s"
cur.execute(query,(customer_id,))
conn.commit()
return []
@app.get("/v1/Customer/HighestPrediction/{customer_id}")
async def get_customer_highest_prediction(customer_id:int):
conn = psycopg2.connect(
host=host_server,
database=db_name,
user=db_username,
password=db_password)
cur = conn.cursor()
query = "Select * from fastapihackathon.CustomerScore where customer_id = %s ORDER BY score DESC LIMIT 1"
records = cur.execute(query,(customer_id,))
records = cur.fetchall()
record = [dict((cur.description[i][0],value) for i, value in enumerate(row)) for row in records]
return record