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

calculating _daystoclose during ingest; nc code fixes #557

Merged
merged 3 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion server/src/services/databaseOrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class Ingest(Base, Mixin):

# dates
createddate = Column(DateTime, index=True)
closeddate = Column(DateTime)
_daystoclose = Column(Float(1))
updateddate = Column(DateTime)
servicedate = Column(DateTime)
closeddate = Column(DateTime)

# about
requesttype = Column(String, index=True)
Expand Down
46 changes: 44 additions & 2 deletions server/src/services/sqlIngest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def dropDuplicates(table, report):

report.append({
'description': 'dropped duplicate rows by srnumber',
'rows': rows.rowcount
'rowsAffected': rows.rowcount
})

def switchPrimaryKey(table, report):
Expand All @@ -97,7 +97,7 @@ def switchPrimaryKey(table, report):

report.append({
'description': 'switched primary key column to srnumber',
'rows': 'N/A'
'rowsAffected': 'N/A'
})

def removeInvalidClosedDates(table, report):
Expand All @@ -112,13 +112,55 @@ def removeInvalidClosedDates(table, report):
'rowsAffected': result.rowcount
})

def setDaysToClose(table, report):
result = exec_sql(f"""
UPDATE {table}
SET _daystoclose = EXTRACT (
EPOCH FROM
(closeddate::timestamp - createddate::timestamp) /
(60 * 60 * 24)
);
""")

report.append({
'description': 'set _daystoclose column',
'rowsAffected': result.rowcount
})

def fixNorthWestwood(table, report):
result = exec_sql(f"""
UPDATE {table}
SET nc = 127
WHERE nc = 0 AND ncname = 'NORTH WESTWOOD NC'
""")

report.append({
'description': 'fix nc code for North Westwood NC',
'rowsAffected': result.rowcount
})

def fixHistoricCulturalNorth(table, report):
result = exec_sql(f"""
UPDATE {table}
SET nc = 128
WHERE nc = 0 AND ncname = 'HISTORIC CULTURAL NORTH NC'
""")

report.append({
'description': 'fix nc code for Historic Cultural North NC',
'rowsAffected': result.rowcount
})

log('\nCleaning ingest table.')
table = Ingest.__tablename__
report = []

dropDuplicates(table, report)
switchPrimaryKey(table, report)
removeInvalidClosedDates(table, report)
setDaysToClose(table, report)
fixNorthWestwood(table, report)
Copy link
Member

Choose a reason for hiding this comment

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

@ryanmswan This looked fine to me but i just want a sanity check before this gets merged pleassseee 🙏

fixHistoricCulturalNorth(table, report)

return report

Expand Down
15 changes: 3 additions & 12 deletions server/src/services/timeToCloseService.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,16 @@ def get_boxplot_stats(arr, C=1.5):
}

# grab the necessary data from the db
fields = [groupField, 'createddate', 'closeddate']
fields = [groupField, '_daystoclose']
data = self.dataAccess.query(fields, filters)

# read into a dataframe and drop the nulls
df = pd.DataFrame(data, columns=fields).dropna()

# 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[[groupField, 'days-to-close']]
dtc_df = pd.DataFrame(data, columns=fields).dropna()

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

# if no rows exist for a particular item in the groupField,
Expand Down