Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Frequency Module #432

Merged
merged 13 commits into from
Mar 23, 2020
Merged
83 changes: 17 additions & 66 deletions server/src/app.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
import os
from sanic import Sanic
from sanic.response import json
from sanic_cors import CORS
from sanic_gzip import Compress
from configparser import ConfigParser
from threading import Timer
from datetime import datetime
from multiprocessing import cpu_count

from services.time_to_close import time_to_close
from services.frequency import frequency
from services.frequency import FrequencyService
from services.pinService import PinService
from services.requestCountsService import RequestCountsService
from services.requestDetailService import RequestDetailService
from services.ingress_service import ingress_service
from services.sqlIngest import DataHandler

app = Sanic(__name__)
CORS(app)
compress = Compress()


def environment_overrides():
if os.environ.get('DB_CONNECTION_STRING', None):
app.config['Settings']['Database']['DB_CONNECTION_STRING'] =\
os.environ.get('DB_CONNECTION_STRING')
if os.environ.get('PORT', None):
app.config['Settings']['Server']['PORT'] =\
os.environ.get('PORT')
if os.environ.get('TOKEN', None):
app.config['Settings']['Socrata']['TOKEN'] =\
os.environ.get('TOKEN')


def configure_app():
Expand All @@ -39,13 +21,14 @@ def configure_app():
settings_file = os.path.join(os.getcwd(), 'settings.cfg')
config.read(settings_file)
app.config['Settings'] = config
environment_overrides()
if os.environ.get('DB_CONNECTION_STRING', None):
app.config['Settings']['Database']['DB_CONNECTION_STRING'] =\
os.environ.get('DB_CONNECTION_STRING')
app.config["STATIC_DIR"] = os.path.join(os.getcwd(), "static")
os.makedirs(os.path.join(app.config["STATIC_DIR"], "temp"), exist_ok=True)


@app.route('/')
@compress.compress()
async def index(request):
return json('You hit the index')

Expand All @@ -69,36 +52,37 @@ async def timetoclose(request):


@app.route('/requestfrequency')
@compress.compress()
async def requestfrequency(request):
freq_worker = frequency(app.config['Settings'])
async def frequency(request):
freq_worker = FrequencyService(app.config['Settings'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this is using hardcoded values, let throw in the logic to pull parameters from the request object


data = freq_worker.freq_view_data(service=True,
councils=[],
aggregate=True)
start = ('2015-01-01')
end = ('2020-12-31 01:01:01')
ncs = (['SHERMAN OAKS NC'])
requests = (['Bulky Items', 'Other'])

return json(data)
return_data = await freq_worker.get_frequency(startDate=start,
endDate=end,
ncList=ncs,
requestTypes=requests)

return json(return_data)


@app.route('/sample-data')
@compress.compress()
async def sample_route(request):
sample_dataset = {'cool_key': ['value1', 'value2'],
app.config['REDACTED']: app.config['REDACTED']}
return json(sample_dataset)


@app.route('/ingest', methods=["POST"])
@compress.compress()
async def ingest(request):
"""Accept POST requests with a list of years to import.
Query parameter name is 'years', and parameter value is
a comma-separated list of years to import.
Ex. '/ingest?years=2015,2016,2017'
"""
current_year = datetime.now().year
querySize = request.args.get("querySize", None)
limit = request.args.get("limit", None)
ALLOWED_YEARS = [year for year in range(2015, current_year+1)]
if not request.args.get("years"):
return json({"error": "'years' parameter is required."})
Expand All @@ -107,31 +91,26 @@ async def ingest(request):
return json({"error":
f"'years' param values must be one of {ALLOWED_YEARS}"})
loader = DataHandler(app.config['Settings'])
loader.populateFullDatabase(yearRange=years,
querySize=querySize,
limit=limit)
loader.populateFullDatabase(yearRange=years)
return_data = {'response': 'ingest ok'}
return json(return_data)


@app.route('/update')
@compress.compress()
async def update(request):
ingress_worker = ingress_service()
return_data = ingress_worker.update()
return json(return_data)


@app.route('/delete')
@compress.compress()
async def delete(request):
ingress_worker = ingress_service()
return_data = ingress_worker.delete()
return json(return_data)


@app.route('/pins', methods=["POST"])
@compress.compress()
async def pinMap(request):
pin_worker = PinService(app.config['Settings'])
postArgs = request.json
Expand All @@ -147,35 +126,7 @@ async def pinMap(request):
return json(return_data)


@app.route('/requestcounts', methods=["POST"])
@compress.compress()
async def requestCounts(request):
counts_worker = RequestCountsService(app.config['Settings'])
postArgs = request.json
start = postArgs.get('startDate', None)
end = postArgs.get('endDate', None)
ncs = postArgs.get('ncList', [])
requests = postArgs.get('requestTypes', [])
countFields = postArgs.get('countFields', [])

return_data = await counts_worker.get_req_counts(startDate=start,
endDate=end,
ncList=ncs,
requestTypes=requests,
countFields=countFields)
return json(return_data)


@app.route('/servicerequest/<srnumber>', methods=["GET"])
async def requestDetails(request, srnumber):
detail_worker = RequestDetailService(app.config['Settings'])

return_data = await detail_worker.get_request_detail(srnumber)
return json(return_data)


@app.route('/test_multiple_workers')
@compress.compress()
async def test_multiple_workers(request):
Timer(10.0, print, ["Timer Test."]).start()
return json("Done")
Expand Down
Loading