Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
corradio committed Nov 20, 2016
1 parent 0705b5c commit 7b6e427
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
21 changes: 15 additions & 6 deletions feeder/feeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@
# Set up requests
session = requests.session()

REQUIRED_PRODUCTION_FIELDS = [
'coal'
]
def validate_production(obj, country_code):
if not 'datetime' in obj:
raise Exception('datetime was not returned for %s' % country_code)
if obj.get('countryCode', None) != country_code:
raise Exception("Country codes %s and %s don't match" % (obj.get('countryCode', None), country_code))
if arrow.get(obj['datetime']) > arrow.now():
raise Exception("Data from %s can't be in the future" % country_code)
for key in REQUIRED_PRODUCTION_FIELDS:
if not key in obj.get('production', {}):
raise Exception("Production key %s is required for %s" % (key, country_code))

def db_upsert(col, obj, database_key):
try:
createdAt = arrow.now().datetime
Expand Down Expand Up @@ -209,12 +223,7 @@ def fetch_productions():
with statsd.StatsdTimer('fetch_one_production'):
obj = parser(country_code, session)
if not obj: continue
if not 'datetime' in obj:
raise Exception('datetime was not returned for %s' % country_code)
if obj.get('countryCode', None) != country_code:
raise Exception("Country codes %s and %s don't match" % (obj.get('countryCode', None), country_code))
if arrow.get(obj['datetime']) > arrow.now():
raise Exception("Data from %s can't be in the future" % country_code)
validate_production(obj, country_code)
# Data quality check
for k, v in obj['production'].iteritems():
if v is None: continue
Expand Down
7 changes: 7 additions & 0 deletions feeder/migrate_db.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pymongo
from feeder import validate_production

def migrate(db):
print 'Starting data migration..'
Expand Down Expand Up @@ -28,4 +29,10 @@ def migrate(db):
except pymongo.errors.DuplicateKeyError: pass
# Delete in old collection
col_old.remove({'_id': row['_id']})
# ** Validate production data
for row in col_production.find():
try: validate_production(row, row.get('countryCode', None))
except:
print 'Warning: row %s did not pass validation' % row['_id']
print row
print 'Migration done.'

0 comments on commit 7b6e427

Please sign in to comment.