-
-
Notifications
You must be signed in to change notification settings - Fork 64
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 Frequency Module #432
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fd7b45b
framework for precache file
rgao 5573b9b
Merge remote-tracking branch 'upstream/dev' into dev
rgao 1cc35a8
implemented method to query recently added entries for precaching req…
rgao 2190233
Merge remote-tracking branch 'upstream/dev' into dev
rgao 25102d8
pre-compiled recent datasets of created dates for each request type /…
rgao 417c67a
Merge remote-tracking branch 'upstream/dev' into dev
rgao 11476df
updated query string for precaching
rgao 2e5de41
updated from upstream
rgao c0fb61d
updated frequency module, fixed typo on sqlingest
rgao 37c6818
fixed merge conflict in app.py
rgao 881c240
fixed merge issues in app.py and fixed [most of] the issues in json r…
rgao cbde943
fixed all linting errors, specified POST method for frequency module …
rgao aa5218d
Merge branch 'dev' into dev
sellnat77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,186 @@ | ||
from configparser import ConfigParser | ||
import sqlalchemy as db | ||
from .dataService import DataService | ||
import pandas as pd | ||
import json | ||
|
||
|
||
class frequency(object): | ||
class FrequencyService(object): | ||
def __init__(self, config=None, tableName="ingest_staging_table"): | ||
self.config = config | ||
self.dbString = None if not self.config \ | ||
else self.config['Database']['DB_CONNECTION_STRING'] | ||
self.dataAccess = DataService(config, tableName) | ||
|
||
self.table = tableName | ||
self.data = None | ||
pass | ||
async def get_frequency(self, | ||
startDate=None, | ||
endDate=None, | ||
ncList=[], | ||
requestTypes=[], | ||
window='month'): | ||
|
||
def freq_view_all(self, serviced=False, aggregate=True): | ||
""" | ||
Returns the request type and associated dates for all data | ||
Sorted by request type, followed by created date, | ||
service date (if applicable), and then closed date | ||
{ | ||
"lastPulled": "NOW", | ||
"data": [{ | ||
"bucketStartDates": ["2015-01-01", "2015-01-04", | ||
"2015-01-07", "2015-01-10", | ||
"2015-01-13", "2015-01-16"], | ||
"requestTypes": [{ | ||
"type": "Homeless Encampment", | ||
"numRequests": [200, 250, 12, 143, 200, 250] | ||
}, { | ||
"type": "Bulky Items", | ||
"numRequests": [2, 25, 682, 333, 444, 666] | ||
}] | ||
}] | ||
} | ||
""" | ||
# Todo: implement condition for serviced date | ||
engine = db.create_engine(self.dbString) | ||
|
||
if serviced: | ||
query = "SELECT \ | ||
requesttype,\ | ||
createddate,\ | ||
closeddate,\ | ||
servicedate\ | ||
FROM %s" % self.table | ||
else: | ||
query = "SELECT \ | ||
requesttype,\ | ||
createddate,\ | ||
closeddate\ | ||
FROM %s" % self.table | ||
|
||
df = pd.read_sql_query(query, con=engine) | ||
|
||
if serviced: | ||
df['servicedate'] = pd.to_datetime(df['servicedate']) | ||
|
||
df['closeddate'] = pd.to_datetime(df['closeddate']) | ||
df = df.sort_values(by=['requesttype', 'createddate', 'closeddate']) | ||
|
||
return df.to_json(orient="records") | ||
filters = self.dataAccess.standardFilters( | ||
startDate, endDate, ncList, requestTypes) | ||
|
||
def freq_aggregate(self, df): | ||
request_counts = df['requesttype'].value_counts() | ||
fields = ['createddate', 'requesttype'] | ||
|
||
return request_counts.to_json() | ||
|
||
def freq_view_data(self, | ||
service=False, | ||
aggregate=True, | ||
councils=[], | ||
startdate="", | ||
enddate=""): | ||
""" | ||
Returns the request type, neighborhood council, created and | ||
closed dates for all data sorted by request type, followed by | ||
neighborhood council #, then created date, and then closed date | ||
Returns serviced date as well if service is set to True | ||
Returns data for all councils if councils=[], otherwise returns data | ||
for only the array of neighborhood council #s | ||
Returns summary data as well if aggregate is set to True | ||
Returns only entries created between startdate and enddate if values | ||
are set for those parameters | ||
Format of startdate and enddate should be a string in | ||
the form 2019-12-01 23:02:05 | ||
""" | ||
engine = db.create_engine(self.dbString) | ||
|
||
if service: | ||
df = pd.read_sql_query("SELECT\ | ||
requesttype,\ | ||
createddate,\ | ||
closeddate,\ | ||
servicedate,\ | ||
nc,\ | ||
ncname\ | ||
FROM %s" % self.table, con=engine) | ||
df['servicedate'] = pd.to_datetime(df['servicedate']) | ||
filteredData = self.dataAccess.query(fields, filters) | ||
df = pd.DataFrame(data=filteredData['data']) | ||
|
||
if window == 'month': | ||
numBins = 10 | ||
else: | ||
df = pd.read_sql_query("SELECT\ | ||
requesttype,\ | ||
createddate,\ | ||
closeddate,\ | ||
nc,\ | ||
ncname\ | ||
FROM %s" % self.table, con=engine) | ||
|
||
df['closeddate'] = pd.to_datetime(df['closeddate']) | ||
|
||
if councils != []: | ||
df = df[df.nc.isin(councils)] | ||
|
||
if startdate != "": | ||
start = pd.to_datetime(startdate) | ||
df = df[(df['createddate'] >= start)] | ||
|
||
if enddate != "": | ||
end = pd.to_datetime(enddate) | ||
df = df[df['createddate'] <= end] | ||
|
||
df = df.sort_values(by=['requesttype', | ||
'nc', | ||
'createddate', | ||
'closeddate']) | ||
df_json = json.loads(df.to_json(orient="records")) | ||
|
||
if aggregate: | ||
summary = self.freq_aggregate(df) | ||
json_data = [] | ||
json_data.append(json.loads(summary)) | ||
json_data.append(df_json) | ||
return json_data | ||
|
||
return df_json | ||
|
||
# Todo: filter by NC at the sql request stage instead of afterwards | ||
|
||
|
||
if __name__ == "__main__": | ||
freq = frequency() | ||
config = ConfigParser() | ||
config.read("../setting.cfg") | ||
freq.config = config | ||
freq.dbString = config['Database']['DB_CONNECTION_STRING'] | ||
freq.freq_view_data(service=True, aggregate=True) | ||
numBins = 12 | ||
|
||
df['buckets'] = pd.qcut(df['createddate'], q=numBins, precision=0) | ||
bucketStartDates = [str(df['buckets'].unique()[i].left) | ||
for i in range(numBins)] | ||
|
||
return [{ | ||
'bucketStartDates': bucketStartDates, | ||
'requestTypes': [{ | ||
'type': request, | ||
'numRequests': df['buckets'][df['requesttype'] == request] | ||
.value_counts(sort=False).values.tolist() | ||
} for request in requestTypes] | ||
}] | ||
|
||
# Following is deprecated, saving for reference | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove all this commented stuff |
||
|
||
# class frequency(object): | ||
# def __init__(self, config=None, tableName="ingest_staging_table"): | ||
# self.config = config | ||
# self.dbString = None if not self.config \ | ||
# else self.config['Database']['DB_CONNECTION_STRING'] | ||
|
||
# self.table = tableName | ||
# self.data = None | ||
# pass | ||
|
||
# Following code are deprecated, saving in the meantime for reference | ||
|
||
# def freq_view_all(self, serviced=False, aggregate=True): | ||
# """ | ||
# Returns the request type and associated dates for all data | ||
# Sorted by request type, followed by created date, | ||
# service date (if applicable), and then closed date | ||
# """ | ||
# engine = db.create_engine(self.dbString) | ||
|
||
# if serviced: | ||
# query = "SELECT \ | ||
# requesttype,\ | ||
# createddate,\ | ||
# closeddate,\ | ||
# servicedate\ | ||
# FROM %s" % self.table | ||
# else: | ||
# query = "SELECT \ | ||
# requesttype,\ | ||
# createddate,\ | ||
# closeddate\ | ||
# FROM %s" % self.table | ||
|
||
# df = pd.read_sql_query(query, con=engine) | ||
|
||
# if serviced: | ||
# df['servicedate'] = pd.to_datetime(df['servicedate']) | ||
|
||
# df['closeddate'] = pd.to_datetime(df['closeddate']) | ||
# df = df.sort_values(by=['requesttype', 'createddate', 'closeddate']) | ||
|
||
# return df.to_json(orient="records") | ||
|
||
# def freq_aggregate(self, df): | ||
# request_counts = df['requesttype'].value_counts() | ||
|
||
# return request_counts.to_json() | ||
|
||
# def freq_view_data(self, | ||
# service=False, | ||
# aggregate=True, | ||
# councils=[], | ||
# startdate="", | ||
# enddate=""): | ||
# """ | ||
# Returns the request type, neighborhood council, created and | ||
# closed dates for all data sorted by request type, followed by | ||
# neighborhood council #, then created date, and then closed date | ||
# Returns serviced date as well if service is set to True | ||
# Returns data for all councils if councils=[], otherwise returns data | ||
# for only the array of neighborhood council #s | ||
# Returns summary data as well if aggregate is set to True | ||
# Returns only entries created between startdate and enddate if values | ||
# are set for those parameters | ||
# Format of startdate and enddate should be a string in | ||
# the form 2019-12-01 23:02:05 | ||
# """ | ||
# engine = db.create_engine(self.dbString) | ||
|
||
# if service: | ||
# df = pd.read_sql_query("SELECT\ | ||
# requesttype,\ | ||
# createddate,\ | ||
# closeddate,\ | ||
# servicedate,\ | ||
# nc,\ | ||
# ncname\ | ||
# FROM %s" % self.table, con=engine) | ||
# df['servicedate'] = pd.to_datetime(df['servicedate']) | ||
|
||
# else: | ||
# df = pd.read_sql_query("SELECT\ | ||
# requesttype,\ | ||
# createddate,\ | ||
# closeddate,\ | ||
# nc,\ | ||
# ncname\ | ||
# FROM %s" % self.table, con=engine) | ||
|
||
# df['closeddate'] = pd.to_datetime(df['closeddate']) | ||
|
||
# if councils != []: | ||
# df = df[df.nc.isin(councils)] | ||
|
||
# if startdate != "": | ||
# start = pd.to_datetime(startdate) | ||
# df = df[(df['createddate'] >= start)] | ||
|
||
# if enddate != "": | ||
# end = pd.to_datetime(enddate) | ||
# df = df[df['createddate'] <= end] | ||
|
||
# df = df.sort_values(by=['requesttype', | ||
# 'nc', | ||
# 'createddate', | ||
# 'closeddate']) | ||
# df_json = json.loads(df.to_json(orient="records")) | ||
|
||
# if aggregate: | ||
# summary = self.freq_aggregate(df) | ||
# json_data = [] | ||
# json_data.append(json.loads(summary)) | ||
# json_data.append(df_json) | ||
# return json_data | ||
|
||
# return df_json | ||
|
||
# if __name__ == "__main__": | ||
# freq = frequency() | ||
# config = ConfigParser() | ||
# config.read("../setting.cfg") | ||
# freq.config = config | ||
# freq.dbString = config['Database']['DB_CONNECTION_STRING'] | ||
# freq.freq_view_data(service=True, aggregate=True) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now this is using hardcoded values, let throw in the logic to pull parameters from the request object