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

Release update #425

Merged
merged 15 commits into from
Mar 14, 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
1 change: 1 addition & 0 deletions server/src/services/pinService.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def get_base_pins(self,
"""

items = ['srnumber',
'requesttype',
'latitude',
'longitude']

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
2 changes: 1 addition & 1 deletion src/components/PinMap/PinMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class PinMap extends Component {
}

const mapStateToProps = state => ({
data: state.data.data,
data: state.data.pins,
});

PinMap.propTypes = {
Expand Down
97 changes: 20 additions & 77 deletions src/components/Visualizations/Contact311.jsx
Original file line number Diff line number Diff line change
@@ -1,95 +1,38 @@
import React from 'react';
import PropTypes from 'proptypes';
import { connect } from 'react-redux';
import Chart from './Chart';

const Contact311 = () => {
// // DATA ////

const randomInt = () => {
const min = 10;
const max = 100;
return Math.round(Math.random() * (max - min) + min);
};

const dummyData = [{
label: 'Mobile App',
color: '#1D66F2',
value: randomInt(),
}, {
label: 'Call',
color: '#D8E5FF',
value: randomInt(),
}, {
label: 'Email',
color: '#708ABD',
value: randomInt(),
}, {
label: 'Driver Self Report',
color: '#C4C6C9',
value: randomInt(),
}, {
label: 'Self Service',
color: '#0C2A64',
value: randomInt(),
}, {
label: 'Other',
color: '#6A98F1',
value: randomInt(),
}];

const total = dummyData.reduce((p, c) => p + c.value, 0);

const chartData = {
labels: dummyData.map(el => el.label),
datasets: [{
data: dummyData.map(el => el.value),
backgroundColor: dummyData.map(el => el.color),
datalabels: {
labels: {
index: {
align: 'end',
anchor: 'end',
formatter: (value, ctx) => {
const { label } = dummyData[ctx.dataIndex];
const percentage = (100 * (value / total)).toFixed(1);
return `${label}\n${percentage}%`;
},
offset: 4,
},
},
},
}],
};

// // OPTIONS ////

const chartOptions = {
aspectRatio: 1.0,
animation: false,
layout: {
padding: 65,
},
};
import { REQUEST_SOURCES } from '@components/common/CONSTANTS';
import PieChart from './PieChart';

const Contact311 = ({
sourceCounts,
}) => {
const sectors = Object.keys(sourceCounts).map(key => ({
label: key,
value: sourceCounts[key],
color: REQUEST_SOURCES.find(s => s.type === key)?.color,
}));

return (
<Chart
<PieChart
title="How People Contact 311"
type="pie"
data={chartData}
options={chartOptions}
datalabels
sectors={sectors}
className="contact-311"
addLabels
/>
);
};

const mapStateToProps = state => ({
requestTypes: state.data.requestTypes,
sourceCounts: state.data.counts.source,
});

export default connect(mapStateToProps)(Contact311);

Contact311.propTypes = {
requestTypes: PropTypes.shape({}).isRequired,
sourceCounts: PropTypes.shape({}),
};

Contact311.defaultProps = {
sourceCounts: {},
};
8 changes: 4 additions & 4 deletions src/components/Visualizations/Criteria.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ const Criteria = ({
};

const mapStateToProps = state => ({
startDate: state.data.startDate,
endDate: state.data.endDate,
councils: state.data.councils,
startDate: state.filters.startDate,
endDate: state.filters.endDate,
councils: state.filters.councils,
});

export default connect(mapStateToProps)(Criteria);

Criteria.propTypes = {
startDate: PropTypes.string,
endDate: PropTypes.string,
councils: PropTypes.arrayOf(PropTypes.shape({})).isRequired,
councils: PropTypes.arrayOf(PropTypes.string).isRequired,
};

Criteria.defaultProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Visualizations/Frequency.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const Frequency = ({
};

const mapStateToProps = state => ({
requestTypes: state.data.requestTypes,
requestTypes: state.filters.requestTypes,
});

export default connect(mapStateToProps)(Frequency);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Visualizations/Legend.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Legend = ({
};

const mapStateToProps = state => ({
requestTypes: state.data.requestTypes,
requestTypes: state.filters.requestTypes,
});

export default connect(mapStateToProps)(Legend);
Expand Down
9 changes: 7 additions & 2 deletions src/components/Visualizations/NumberOfRequests.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'proptypes';

function addCommas(num) {
Expand All @@ -18,12 +19,16 @@ const NumberOfRequests = ({
</div>
);

export default NumberOfRequests;
const mapStateToProps = state => ({
numRequests: state.data.pins.length,
});

export default connect(mapStateToProps)(NumberOfRequests);

NumberOfRequests.propTypes = {
numRequests: PropTypes.number,
};

NumberOfRequests.defaultProps = {
numRequests: 1285203, // until we get data
numRequests: 0,
};
Loading