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 timetoclose endpoint #422

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions server/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,22 @@ async def index(request):
return json('You hit the index')


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

# dates = loads(ttc_worker.ttc_view_dates())
summary = ttc_worker.ttc_summary(allData=True,
service=True,
allRequests=False,
requestType="'Bulky Items'",
viewDates=True)
postArgs = request.json
start = postArgs.get('startDate', None)
end = postArgs.get('endDate', None)
ncs = postArgs.get('ncList', [])
requests = postArgs.get('requestTypes', [])

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


@app.route('/requestfrequency')
Expand Down
99 changes: 98 additions & 1 deletion server/src/services/time_to_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sqlalchemy as db
import pandas as pd
import json
from .dataService import DataService
import numpy as np


class time_to_close(object):
Expand All @@ -18,8 +20,102 @@ def __init__(self,
else self.config['Database']['DB_CONNECTION_STRING']
self.table = tableName
self.data = None
self.dataAccess = DataService(config, tableName)
pass

def ttc(self, startDate=None, endDate=None, ncList=[], requestTypes=[]):
"""
For each requestType, returns the statistics necessary to generate
a boxplot of the number of days it took to close the requests.

Example response:
{
lastPulled: Timestamp,
data: {
'Bulky Items': {
'min': float,
'q1': float,
'median': float,
'q3': float,
'max': float,
'whiskerMin': float,
'whiskerMax': float,
'outliers': [float],
'count': int
}
...
}
}
"""

def get_boxplot_stats(arr, C=1.5):
"""
Takes a one-dimensional numpy array of floats and generates boxplot
statistics for the data. The basic algorithm is standard.
See https://en.wikipedia.org/wiki/Box_plot

The max length of the whiskers is the constant C, multiplied by the
interquartile range. This is a common method, although there
are others. The default value of C=1.5 is typical when this
method is used.
See matplotlib.org/3.1.3/api/_as_gen/matplotlib.pyplot.boxplot.html
"""

# calculate first and third quantiles
q1 = np.quantile(arr, 0.25)
q3 = np.quantile(arr, 0.75)

# calculate whiskers
iqr = q3 - q1
whiskerMin = arr[arr >= q1 - C * iqr].min()
whiskerMax = arr[arr <= q3 + C * iqr].max()

# calculate outliers
minOutliers = arr[arr < whiskerMin]
maxOutliers = arr[arr > whiskerMax]
outliers = list(np.concatenate((minOutliers, maxOutliers)))

return {
'min': np.min(arr),
'q1': q1,
'median': np.median(arr),
'q3': q3,
'max': np.max(arr),
'whiskerMin': whiskerMin,
'whiskerMax': whiskerMax,
'outliers': outliers,
'count': len(arr)
}

# grab the necessary data from the db
fields = ['requesttype', 'createddate', 'closeddate']
filters = self.dataAccess.standardFilters(
startDate, endDate, ncList, requestTypes)
data = self.dataAccess.query(fields, filters)

# read into a dataframe, drop the nulls, and halt if no rows exist
df = pd.DataFrame(data['data']).dropna()
if len(df) == 0:
data['data'] = {}
return data

# generate a new dataframe that contains the number of days it
# takes to close each request, plus the type of request
df['closeddate'] = pd.to_datetime(df['closeddate'])
df['createddate'] = pd.to_datetime(df['createddate'])
df['time-to-close'] = df['closeddate'] - df['createddate']
df['hours-to-close'] = df['time-to-close'].astype('timedelta64[h]')
df['days-to-close'] = (df['hours-to-close'] / 24).round(2)
dtc_df = df[['requesttype', 'days-to-close']]

# group the requests by type and get box plot stats for each type
data['data'] = dtc_df \
.groupby(by='requesttype') \
.apply(lambda df: get_boxplot_stats(df['days-to-close'].values)) \
.to_dict()

return data

def ttc_view_dates(self, service=False):
"""
Returns all rows under the CreatedDate and
Expand Down Expand Up @@ -131,7 +227,8 @@ def ttc_summary(self,
"""
Returns summary data of the amount of time it takes for a
request to close as a dataframe.
If service is set to True, returns summary data of time_to_service as well
If service is set to True, returns summary data of time_to_service
as well
If allData is set to True, returns the data of every entry as well
If allRequests are set to False, queries data of
the value of requestType only
Expand Down
8 changes: 7 additions & 1 deletion server/test/test_time_to_close.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from src.services.time_to_close import time_to_close

TESTCONFIG = {
"Database": {
"DB_CONNECTION_STRING": "postgresql://testingString/postgresql"
}
}


def test_serviceExists():
# Arrange
testString = 'result'
print(testString)

# Act
ttc_worker = time_to_close()
ttc_worker = time_to_close(TESTCONFIG)
print(ttc_worker)

# Assert
Expand Down