-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from hackforla/265_BE_Pinmap
Fixes #265 Added pinService and started data layer abstraction
- Loading branch information
Showing
4 changed files
with
120 additions
and
18 deletions.
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sqlalchemy as db | ||
import pandas as pd | ||
|
||
|
||
class DataService(object): | ||
def includeMeta(func): | ||
def inner1(*args, **kwargs): | ||
dataResponse = func(*args, **kwargs) | ||
|
||
withMeta = {'lastPulled': 'NOW', 'data': dataResponse} | ||
print(withMeta) | ||
return withMeta | ||
|
||
return inner1 | ||
|
||
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 | ||
self.engine = db.create_engine(self.dbString) | ||
|
||
@includeMeta | ||
def query(self, queryItems=[], queryfilters=[], limit=None): | ||
items = ', '.join(queryItems) | ||
query = 'SELECT {} FROM {}'.format(items, self.table) | ||
if queryfilters: | ||
filters = ' AND '.join(queryfilters) | ||
query += ' WHERE {}'.format(filters) | ||
if limit: | ||
query += ' LIMIT {}'.format(limit) | ||
|
||
df = pd.read_sql_query(query, con=self.engine) | ||
return df.to_dict(orient='records') | ||
|
||
def storedProc(self): | ||
pass |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from .dataService import DataService | ||
|
||
|
||
class PinService(object): | ||
def __init__(self, config=None, tableName="ingest_staging_table"): | ||
self.dataAccess = DataService(config, tableName) | ||
|
||
async def get_base_pins(self, | ||
startDate='', | ||
endDate='', | ||
ncList=[], | ||
requestTypes=[]): | ||
""" | ||
Returns the base pin data given times, ncs, and request filters | ||
{ | ||
'LastPulled': 'Timestamp', | ||
'data': [ | ||
{ | ||
'ncname':'String', | ||
'requesttype':'String', | ||
'srnumber':'String', | ||
'latitude': 'String', | ||
'longitude': 'String', | ||
'address': 'String', | ||
'createddate': 'Timestamp' | ||
} | ||
] | ||
} | ||
""" | ||
|
||
items = ['ncname', | ||
'requesttype', | ||
'srnumber', | ||
'latitude', | ||
'longitude', | ||
'address', | ||
'createddate'] | ||
|
||
ncs = '\'' + '\', \''.join(ncList) + '\'' | ||
requests = '\'' + '\', \''.join(requestTypes) + '\'' | ||
|
||
filters = ['createddate > \'{}\''.format(startDate), | ||
'createddate < \'{}\''.format(endDate), | ||
'ncname IN ({})'.format(ncs), | ||
'requesttype IN ({})'.format(requests)] | ||
result = self.dataAccess.query(items, filters) | ||
|
||
return result |
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