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

Removed redundant config loading #284

Merged
merged 2 commits into from
Feb 24, 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
6 changes: 3 additions & 3 deletions server/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ async def ingest(request):
if not all(year in ALLOWED_YEARS for year in years):
return json({"error":
f"'years' param values must be one of {ALLOWED_YEARS}"})
loader = DataHandler()
loader.loadConfig(configFilePath='./settings.cfg')
loader = DataHandler(app.config['Settings'])
loader.populateFullDatabase(yearRange=years)
return_data = {'response': 'ingest ok'}
return json(return_data)
Expand Down Expand Up @@ -125,7 +124,8 @@ async def test_multiple_workers(request):

if __name__ == '__main__':
configure_app()
worker_count = max(cpu_count()//2, 1)
app.run(host=app.config['Settings']['Server']['HOST'],
port=int(app.config['Settings']['Server']['PORT']),
workers=cpu_count()//2,
workers=worker_count,
debug=app.config['Settings']['Server']['DEBUG'])
29 changes: 10 additions & 19 deletions server/src/services/sqlIngest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import pandas as pd
import sqlalchemy as db
from sodapy import Socrata
from .databaseOrm import tableFields, insertFields, readFields # Contains db specs and field definitions
from configparser import ConfigParser
if __name__ == '__main__':
# Contains db specs and field definitions
from databaseOrm import tableFields, insertFields, readFields
else:
from .databaseOrm import tableFields, insertFields, readFields


class DataHandler:
Expand All @@ -14,29 +18,15 @@ def __init__(self, config=None, configFilePath=None, separator=','):
self.config = config
self.dbString = None if not self.config \
else self.config['Database']['DB_CONNECTION_STRING']
self.token = None if config['Socrata']['TOKEN'] == 'None' \
else config['Socrata']['TOKEN']
self.filePath = None
self.configFilePath = configFilePath
self.separator = separator
self.fields = tableFields
self.insertParams = insertFields
self.readParams = readFields
self.dialect = None

def loadConfig(self, configFilePath):
'''Load and parse config data'''
if self.config:
print('Config already exists at %s. Nothing to load.' %
self.configFilePath)
return
print('Loading config file %s' % configFilePath)
self.configFilePath = configFilePath
config = ConfigParser()
config.read(configFilePath)
self.config = config
self.dbString = config['Database']['DB_CONNECTION_STRING']
self.dialect = self.dbString.split(':')[0]
self.token = None if config['Socrata']['TOKEN'] == 'None' \
else config['Socrata']['TOKEN']

def loadData(self, fileName="2018_mini"):
'''Load dataset into pandas object'''
Expand Down Expand Up @@ -242,8 +232,9 @@ def fix_nan_vals(resultDict):

if __name__ == "__main__":
'''Class DataHandler workflow from initial load to SQL population'''
loader = DataHandler()
loader.loadConfig(configFilePath='../settings.cfg')
config = ConfigParser()
config.read('../settings.cfg')
loader = DataHandler(config)
loader.fetchSocrataFull()
ryanmswan marked this conversation as resolved.
Show resolved Hide resolved
loader.cleanData()
loader.ingestData('ingest_staging_table')