-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
167 lines (137 loc) · 7.71 KB
/
views.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from concurrent.futures.thread import _worker
from flask import Blueprint, render_template, request, redirect, url_for
from bs4 import BeautifulSoup as bs
from h11 import Data
from Visualizations.perCaseType.statusLineGraph import outputStatusPerTypeDictAndGraph
from helpers.getCases import getAllRanges, getStatusDataPerTypeDict, getScannerPercentage
from helpers.dbOperations import getTodayApprovedCases, scrapeSingle, createRangeLogTable, addToDistributionTable, createRangeQueryableTable, returnAllRanges
from helpers.conversions import getRangeId, getStatusCode, getRangeText, scrapeAll, parseUserRequest
from helpers.checks import checkType, rangeExist
from helpers.dbConnect import DatabaseConnect
from Visualizations.caseTypePie import outputPlot
from workers import checkAndFillRange
from rq import Queue, Retry
from redis import Redis
import json
from constants import CASE_TYPES
from bokeh.embed import components
from datetime import date, datetime
from helpers.htmlHelpers import getWorkerBannerText
from customWorker import conn
# conn = Redis()
#
views = Blueprint(__name__, "views")
@views.route("/")
def home():
banner=getWorkerBannerText()
todayApprovedDict=getTodayApprovedCases()
todayString = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return render_template("home.html", banner=banner, todayApprovedDict=todayApprovedDict, caseTypes = CASE_TYPES, today=todayString)
@views.route("/about")
def about():
return render_template("about.html")
@views.route("/contact")
def contact():
return render_template("contact.html")
@views.route("/displayRanges")
def displayRanges():
#if deployed
#webCache = Redis.from_url(redis_url)
redisResult = conn.get("rangeToTextPercentageDict")
if redisResult:
rangeToTextPercentageDict = json.loads(redisResult)
else:
rangeList = getAllRanges()
rangeToTextPercentageDict = {}
for range in rangeList:
rangeToTextPercentageDict[range]=(getRangeText(range), getScannerPercentage(range))
conn.set("rangeToTextPercentageDict", json.dumps(rangeToTextPercentageDict), ex=300)
nowTime=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return render_template("range.html", rangeToTextPercentageDict=rangeToTextPercentageDict, nowTime = nowTime)
@views.route('/handle_data', methods=['POST'])
def handle_data():
form=parseUserRequest(request)
if form["case_number"]=="" or form["petition_date"]=="" or form["petition_type"]=="" or form["home_country"]=="" or form["state"]=="":
return redirect(request.referrer)
case_number = form["case_number"]
result = scrapeSingle(case_number)
if result==None:
return render_template("invalid.html")
petition_type=checkType(form["petition_type"], result['content'])
#store inquirer's information
if petition_type!="Other":
status_code=getStatusCode(result['title'])
with DatabaseConnect("UserInfo") as (cnx, cursor):
query="Select count(*) from UserInfo where CaseNumber=%s"
cursor.execute(query, (case_number,))
tup = cursor.fetchone()
if tup[0]==0:
query="INSERT INTO UserInfo (CaseNumber, CaseSubType, State, HomeCountry, PetitionDate, StatusCode) values(%s, %s, %s, %s, %s, %s)"
cursor.execute(query,(case_number, petition_type, form["state"], form["home_country"], form["petition_date"], status_code))
rangeId=getRangeId(case_number)
if not rangeExist(rangeId):
userPromptedWorkerQueue = Queue('default', connection=conn)
createRangeJob = userPromptedWorkerQueue.enqueue('helpers.dbOperations.createRangeQueryableTable', rangeId)
populateRangeJob=userPromptedWorkerQueue.enqueue('helpers.dbOperations.populateRangeTable', rangeId, retry=Retry(max=10, interval=10),
depends_on=createRangeJob, job_timeout='24h')
initScrapeJob = userPromptedWorkerQueue.enqueue('workers.batchScrape', rangeId, retry=Retry(max=10, interval=10), depends_on=populateRangeJob)
addToDistributionTableJob=userPromptedWorkerQueue.enqueue('helpers.dbOperations.addToDistributionTable', rangeId, retry=Retry(max=10, interval=10), depends_on=initScrapeJob)
createRangeLogTableJob=userPromptedWorkerQueue.enqueue('helpers.dbOperations.createRangeLogTable', rangeId, retry=Retry(max=10, interval=10), depends_on=initScrapeJob)
return render_template("checkBacklater.html")
else:
return redirect(url_for('views.caseData', rangeId = rangeId))
@views.route("/invalid")
def invalid():
return render_template("invalid.html")
@views.route('/caseData/<rangeId>', methods=['GET'])
def caseData(rangeId):
script = conn.get("script").decode('ASCII') if conn.get("script")!=None else None
divDist = conn.get("divDist").decode('ASCII') if conn.get("divDist")!=None else None
divTable = conn.get("divTable").decode('ASCII') if conn.get("divDist")!=None else None
dataByTypeDict = json.loads(conn.get("dataByTypeDict")) if conn.get("dataByTypeDict")!=None else None
statusGraphDict=json.loads(conn.get("statusGraphDict")) if conn.get("statusGraphDict")!=None else None
if script == None or divDist == None or divTable ==None or dataByTypeDict ==None or statusGraphDict ==None:
distGraph, dataTable = outputPlot(rangeId)
statusGraphDict=outputStatusPerTypeDictAndGraph(rangeId)
#{'I-485':[date, 135, 543, 654,....], 'I-765':[date, 453, 21, 54, ...]}
#{'I-485': , 'I-765': 834, ...}
dataByTypeDict = getStatusDataPerTypeDict(rangeId)
if statusGraphDict==None:
return render_template("checkBacklater.html")
else:
script, divTups = components((distGraph, dataTable, *statusGraphDict.values()))
divDist = divTups[0]
print(divDist)
divTable = divTups[1]
caseTypeDivs = divTups[2:]
DivDicts = {}
i=0
for key in statusGraphDict.keys():
statusGraphDict[key]=caseTypeDivs[i]
i+=1
conn.set("script", script, ex=300)
conn.set("divDist", divDist, ex=300)
conn.set("divTable", divTable, ex=300)
conn.set("dataByTypeDict", json.dumps(dataByTypeDict), ex=300)
conn.set("statusGraphDict", json.dumps(statusGraphDict), ex=300)
return render_template("caseData.html", rangeText=getRangeText(rangeId),
script = script, divDist = divDist, divTable = divTable, dataByTypeDict=dataByTypeDict, statusGraphDict=statusGraphDict)
@views.route('/scrapeAll', methods=['GET'])
def scrapeAdmin():
rangesList = returnAllRanges()
for range in rangesList:
init = Queue('default', connection=conn)
dailyScrapeJob = init.enqueue('workers.batchScrape', range, retry=Retry(max=10, interval=10),job_timeout='24h', job_id=getRangeText(range))
createRangeLogTableJob = init.enqueue('helpers.dbOperations.createRangeLogTable', range, retry=Retry(max=10, interval=10), depends_on=dailyScrapeJob)
checkAndFillRangeLogJob = init.enqueue('workers.checkAndFillRange', range, retry=Retry(max=10, interval=10), depends_on=createRangeLogTableJob)
return render_template("checkBacklater.html")
@views.route('/createRangeAll', methods=['GET'])
def populateRangeLog():
rangesList = returnAllRanges()
for range in rangesList:
init = Queue('default', connection=conn)
createRangeLogTable(range)
checkAndFillRange(range)
# createRangeLogTableJob = init.enqueue('helpers.dbOperations.createRangeLogTable', range, retry=Retry(max=10, interval=10))
# checkAndFillRangeLogJob = init.enqueue('workers.checkAndFillRange', range, retry=Retry(max=10, interval=10), depends_on=createRangeLogTableJob)
return render_template("checkBacklater.html")