Skip to content

Commit

Permalink
Merge pull request #606 from hackforla/605-BACK-EndpointConsolidation
Browse files Browse the repository at this point in the history
endpoint consolidation
  • Loading branch information
jmensch1 authored May 15, 2020
2 parents fd0277a + 37ace7f commit 033c14c
Show file tree
Hide file tree
Showing 13 changed files with 425 additions and 701 deletions.
155 changes: 29 additions & 126 deletions server/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
from datetime import datetime
from multiprocessing import cpu_count

from services.timeToCloseService import TimeToCloseService
from services.frequencyService import FrequencyService
from services.pinService import PinService
from services.pinClusterService import PinClusterService
from services.heatmapService import HeatmapService
from services.requestCountsService import RequestCountsService
from services.requestDetailService import RequestDetailService
from services.visualizationsService import VisualizationsService
from services.comparisonService import ComparisonService
from services.sqlIngest import DataHandler
from services.feedbackService import FeedbackService
from services.dataService import DataService
Expand Down Expand Up @@ -78,82 +76,6 @@ async def index(request):
return json('You hit the index')


@app.route('/timetoclose', methods=["POST"])
@compress.compress()
async def timetoclose(request):
ttc_worker = TimeToCloseService(app.config['Settings'])

postArgs = request.json
start = postArgs.get('startDate', None)
end = postArgs.get('endDate', None)
ncs = postArgs.get('ncList', [])
requests = postArgs.get('requestTypes', [])

data = await ttc_worker.get_ttc(startDate=start,
endDate=end,
ncList=ncs,
requestTypes=requests)
return json(data)


@app.route('/timetoclose-comparison', methods=["POST"])
@compress.compress()
async def timetoclose_comparison(request):
ttc_worker = TimeToCloseService(app.config['Settings'])

postArgs = request.json
startDate = postArgs.get('startDate', None)
endDate = postArgs.get('endDate', None)
requestTypes = postArgs.get('requestTypes', [])
set1 = postArgs.get('set1', None)
set2 = postArgs.get('set2', None)

data = await ttc_worker.get_ttc_comparison(startDate=startDate,
endDate=endDate,
requestTypes=requestTypes,
set1=set1,
set2=set2)
return json(data)


@app.route('/requestfrequency', methods=["POST"])
@compress.compress()
async def requestfrequency(request):
freq_worker = FrequencyService(app.config['Settings'])

postArgs = request.json
startDate = postArgs.get('startDate', None)
endDate = postArgs.get('endDate', None)
ncList = postArgs.get('ncList', [])
requestTypes = postArgs.get('requestTypes', [])

data = await freq_worker.get_frequency(startDate=startDate,
endDate=endDate,
ncList=ncList,
requestTypes=requestTypes)
return json(data)


@app.route('/requestfrequency-comparison', methods=["POST"])
@compress.compress()
async def requestfrequency_comparison(request):
worker = FrequencyService(app.config['Settings'])

postArgs = request.json
startDate = postArgs.get('startDate', None)
endDate = postArgs.get('endDate', None)
requestTypes = postArgs.get('requestTypes', [])
set1 = postArgs.get('set1', None)
set2 = postArgs.get('set2', None)

data = await worker.get_frequency_comparison(startDate=startDate,
endDate=endDate,
requestTypes=requestTypes,
set1=set1,
set2=set2)
return json(data)


@app.route('/ingest', methods=["GET"])
@compress.compress()
async def ingest(request):
Expand Down Expand Up @@ -211,23 +133,6 @@ async def ingest(request):
return json(data)


@app.route('/pins', methods=["POST"])
@compress.compress()
async def pinMap(request):
pin_worker = PinService(app.config['Settings'])
postArgs = request.json
start = postArgs.get('startDate', '2015-01-01')
end = postArgs.get('endDate', '2015-12-31 01:01:01')
ncs = postArgs.get('ncList', ['SHERMAN OAKS NC'])
requests = postArgs.get('requestTypes', ['Bulky Items'])

return_data = await pin_worker.get_base_pins(startDate=start,
endDate=end,
ncList=ncs,
requestTypes=requests)
return json(return_data)


@app.route('/pin-clusters', methods=["POST"])
@compress.compress()
async def pinClusters(request):
Expand Down Expand Up @@ -265,53 +170,51 @@ async def heatmap(request):
return json(heatmap)


@app.route('/requestcounts', methods=["POST"])
@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('/visualizations', methods=["POST"])
@compress.compress()
async def requestCounts(request):
counts_worker = RequestCountsService(app.config['Settings'])
async def visualizations(request):
worker = VisualizationsService()

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)
data = await worker.visualizations(startDate=start,
endDate=end,
requestTypes=requests,
ncList=ncs)
return json(data)


@app.route('/requestcounts-comparison', methods=["POST"])
@app.route('/comparison/<type>', methods=["POST"])
@compress.compress()
async def requestCountsComparison(request):
worker = RequestCountsService(app.config['Settings'])
async def comparison(request, type):
worker = ComparisonService()

postArgs = request.json
startDate = postArgs.get('startDate', None)
endDate = postArgs.get('endDate', None)
requestTypes = postArgs.get('requestTypes', [])
set1 = postArgs.get('set1', None)
set2 = postArgs.get('set2', None)
countFields = postArgs.get('countFields', [])

data = await worker.get_req_counts_comparison(startDate=startDate,
endDate=endDate,
requestTypes=requestTypes,
set1=set1,
set2=set2,
countFields=countFields)
return json(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)
data = await worker.comparison(type=type,
startDate=startDate,
endDate=endDate,
requestTypes=requestTypes,
set1=set1,
set2=set2)
return json(data)


@app.route('/feedback', methods=["POST"])
Expand Down
163 changes: 163 additions & 0 deletions server/src/services/comparisonService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
from .dataService import DataService
from utils.stats import box_plots, date_bins, date_histograms, counts


class ComparisonService(object):
def __init__(self, config=None):
self.dataAccess = DataService()

def frequency_comparison(self,
startDate=None,
endDate=None,
requestTypes=[],
set1={'district': None, 'list': []},
set2={'district': None, 'list': []}):

def get_data(district, items, bins, start, end):
common = {
'startDate': start,
'endDate': end,
'requestTypes': requestTypes
}

if district == 'nc':
common['ncList'] = items
groupField = 'nc'
elif district == 'cc':
common['cdList'] = items
groupField = 'cd'

fields = [groupField, 'createddate']
filters = self.dataAccess.comparisonFilters(**common)
df = self.dataAccess.query(fields, filters, table='vis')

return date_histograms(
df,
dateField='createddate',
bins=bins,
groupField=groupField,
groupFieldItems=items)

bins, start, end = date_bins(startDate, endDate)
set1data = get_data(set1['district'], set1['list'], bins, start, end)
set2data = get_data(set2['district'], set2['list'], bins, start, end)

return {
'bins': list(bins.astype(str)),
'set1': {
'district': set1['district'],
'counts': set1data
},
'set2': {
'district': set2['district'],
'counts': set2data
}
}

def ttc_comparison(self,
startDate=None,
endDate=None,
requestTypes=[],
set1={'district': None, 'list': []},
set2={'district': None, 'list': []}):

def get_data(district, items):
common = {
'startDate': startDate,
'endDate': endDate,
'requestTypes': requestTypes
}

if district == 'nc':
common['ncList'] = items
groupField = 'nc'
elif district == 'cc':
common['cdList'] = items
groupField = 'cd'

fields = [groupField, '_daystoclose']
filters = self.dataAccess.comparisonFilters(**common)
df = self.dataAccess.query(fields, filters, table='vis')

return box_plots(
df,
plotField='_daystoclose',
groupField=groupField,
groupFieldItems=items)

set1data = get_data(set1['district'], set1['list'])
set2data = get_data(set2['district'], set2['list'])

return {
'set1': {
'district': set1['district'],
'data': set1data
},
'set2': {
'district': set2['district'],
'data': set2data
}
}

def counts_comparison(self,
startDate=None,
endDate=None,
requestTypes=[],
set1={'district': None, 'list': []},
set2={'district': None, 'list': []}):

def get_data(district, items):
common = {
'startDate': startDate,
'endDate': endDate,
'requestTypes': requestTypes
}

if district == 'nc':
common['ncList'] = items
elif district == 'cc':
common['cdList'] = items

fields = ['requestsource']
filters = self.dataAccess.comparisonFilters(**common)
df = self.dataAccess.query(fields, filters, table='vis')

return counts(df, 'requestsource')

set1data = get_data(set1['district'], set1['list'])
set2data = get_data(set2['district'], set2['list'])

return {
'set1': {
'district': set1['district'],
'source': set1data
},
'set2': {
'district': set2['district'],
'source': set2data
}
}

async def comparison(self,
type=None,
startDate=None,
endDate=None,
requestTypes=[],
set1={'district': None, 'list': []},
set2={'district': None, 'list': []}):

args = {
'startDate': startDate,
'endDate': endDate,
'requestTypes': requestTypes,
'set1': set1,
'set2': set2}

if type == 'frequency':
return self.frequency_comparison(**args)
elif type == 'timetoclose':
return self.ttc_comparison(**args)
elif type == 'counts':
return self.counts_comparison(**args)
else:
return {'Error': 'Unrecognized comparison type'}
Loading

0 comments on commit 033c14c

Please sign in to comment.