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

time_to_close module #152

Merged
merged 1 commit into from
Jan 5, 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
12 changes: 4 additions & 8 deletions server/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ async def timetoclose(request):
ttc_worker = time_to_close(app.config['Settings'])
data = []

# column_names = ttc_worker.ttc_view_columns()
# all_rows = loads(ttc_worker.ttc_view_table(onlyClosed=True))
# all_dates = loads(ttc_worker.ttc_view_dates(serviced=False))
time_diff = loads(ttc_worker.ttc_average_time(serviced=True))

# data.append(column_names)
# data.append(all_rows)
# data.append(all_dates)
time_diff = loads(ttc_worker.ttc_time_diff(serviced=True, allRequests=False, requestType="'Bulky Items'"))
summary = loads(ttc_worker.ttc_summary())

data.append(time_diff)
data.append(summary)
return json(data)


Expand Down
92 changes: 60 additions & 32 deletions server/src/services/time_to_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pandas as pd
from datetime import datetime as dt
import numpy as np
import json


class time_to_close(object):
Expand All @@ -16,19 +17,7 @@ def __init__(self, config=None, requestTypes=None, tableName="ingest_staging_tab
self.data = None
pass

def ttc_view_columns(self):
"""
Returns all the columns' names
"""
engine = db.create_engine(self.dbString)

df = pd.read_sql_query("SELECT * FROM %s" % self.table, con=engine)

self.data = df

return df

def ttc_view_table(self, onlyClosed=False):
def ttc_view_data(self, onlyClosed=False):
"""
Returns all entries
Returns only those with Status as 'Closed' if onlyClosed is set to True
Expand All @@ -48,6 +37,8 @@ def ttc_view_table(self, onlyClosed=False):
else:
df = pd.read_sql_query("SELECT * FROM %s" % self.table, con=engine)

# df = pd.read_sql_query("SELECT * FROM %s" % self.table, con=engine)

return df.to_json(orient='index')

def ttc_view_dates(self, serviced=False):
Expand All @@ -70,47 +61,84 @@ def ttc_view_dates(self, serviced=False):

return df.to_json(orient='index')

def ttc_time_diff(self, serviced=False, all=False):
def ttc_to_days(self, dt):
num_days = pd.Timedelta.total_seconds(dt)/(24.*3600)
if num_days <= .000001:
return 0

in_days = pd.Timedelta.total_seconds(dt)/(24.*3600)
return in_days

def ttc_days_to_string(self, day):
return str(day) + " Days"

def ttc_time_diff(self, serviced=False, allRequests=True, requestType=""):
"""
Returns the average time in days or hours for a specific request type to be completed
Returns the amount of time for a request to close in days
If serviced is set to True, only requests that have been serviced are included
"""

engine = db.create_engine(self.dbString)

if serviced:
df = pd.read_sql_query(
"SELECT createddate, closeddate, servicedate FROM %s" % self.table, con=engine)
if not allRequests:
query = "SELECT createddate, closeddate, servicedate FROM %s WHERE requesttype=%s" % (
self.table, requestType)
print(query)
df = pd.read_sql_query(
query, con=engine)
else:
df = pd.read_sql_query(
"SELECT createddate, closeddate, servicedate FROM %s" % self.table, con=engine)
df = df[df['servicedate'].notnull()]
df['servicedate'] = pd.to_datetime(df['servicedate'])
diff_df = pd.DataFrame(
df['servicedate'] - df['createddate'], columns=['time_to_service'])

else:
df = pd.read_sql_query(
"SELECT createddate, closeddate FROM %s" % self.table, con=engine)
if not allRequests:
df = pd.read_sql_query(
"SELECT createddate, closeddate FROM %s WHERE requesttype=%s" % (self.table, requestType), con=engine)
else:
df = pd.read_sql_query(
"SELECT createddate, closeddate FROM %s" % self.table, con=engine)
diff_df = pd.DataFrame({'time_to_close': []})

df['createddate'] = pd.to_datetime(df['createddate'])
df['closeddate'] = pd.to_datetime(df['closeddate'])
diff_df['time_to_close'] = df['closeddate'] - df['createddate']
diff_df = diff_df[diff_df['time_to_close'].notnull()]

for column in diff_df:
diff_df[column] = diff_df[column].apply(self.ttc_to_days)

diff_df_str = diff_df.copy()

for column in diff_df_str:
diff_df_str[column] = diff_df_str[column].apply(
self.ttc_days_to_string)

self.data = diff_df
return diff_df_str.to_json(orient='index')

def ttc_summary(self):
data = self.data

summary_obj = {}

for column in data:
summary = data[column].describe()
summary_obj.update({column: summary.to_json()})

def dt_to_days(dt):
num_days = pd.Timedelta.total_seconds(dt)/(24.*3600)
if num_days <= .000001:
return 0
return pd.Timedelta.total_seconds(dt)/(24.*3600)
return json.dumps(summary_obj)

diff_df['time_to_close'] = diff_df.time_to_close.apply(dt_to_days)
diff_df['time_to_service'] = diff_df.time_to_service.apply(dt_to_days)

### Todo: Convert unix time to strings displaying days
### Todo: Return averages and min/max
### Todo: Implement function for considering request type
# Todo: Implement functionality for only open status data

return diff_df.to_json(orient='index')

if __name__ == "__main__":
ttc = time_to_close()
config = ConfigParser()
config.read("../setting.cfg")
ttc.config = config
ttc.dbString = config['Database']['DB_CONNECTION_STRING']
ttc.ttc_view_table()
ttc.ttc_view_data()