Commit 3a4091b7 by Ashley Penney

Tweaks to enable datadog error events as well as some pep8 tidyup vim was shouting at me about.

parent 333f2e51
...@@ -7,19 +7,25 @@ from collections import OrderedDict ...@@ -7,19 +7,25 @@ from collections import OrderedDict
from datetime import datetime from datetime import datetime
from os.path import isdir from os.path import isdir
from optparse import make_option from optparse import make_option
from dogapi import dog_http_api, dog_stats_api
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from student.models import TestCenterUser, TestCenterRegistration from student.models import TestCenterUser, TestCenterRegistration
class Command(BaseCommand): class Command(BaseCommand):
dog_http_api.api_key = settings.DATADOG_API
args = '<input zip file>' args = '<input zip file>'
help = """ help = """
Import Pearson confirmation files and update TestCenterUser and TestCenterRegistration tables Import Pearson confirmation files and update TestCenterUser
with status. and TestCenterRegistration tables with status.
""" """
def datadog_error(string):
dog_http_api.event("Pearson Import", string, alert_type='error')
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
if len(args) < 1: if len(args) < 1:
print Command.help print Command.help
...@@ -27,8 +33,8 @@ class Command(BaseCommand): ...@@ -27,8 +33,8 @@ class Command(BaseCommand):
source_zip = args[0] source_zip = args[0]
if not is_zipfile(source_zip): if not is_zipfile(source_zip):
raise CommandError("Input file is not a zipfile: \"{}\"".format(source_zip)) raise CommandError("Input file is not a zipfile: \"{}\"".format(source_zip))
# loop through all files in zip, and process them based on filename prefix: # loop through all files in zip, and process them based on filename prefix:
with ZipFile(source_zip, 'r') as zipfile: with ZipFile(source_zip, 'r') as zipfile:
for fileinfo in zipfile.infolist(): for fileinfo in zipfile.infolist():
...@@ -39,70 +45,69 @@ class Command(BaseCommand): ...@@ -39,70 +45,69 @@ class Command(BaseCommand):
self.process_vcdc(zipentry) self.process_vcdc(zipentry)
else: else:
raise CommandError("Unrecognized confirmation file type \"{}\" in confirmation zip file \"{}\"".format(fileinfo.filename, zipfile)) raise CommandError("Unrecognized confirmation file type \"{}\" in confirmation zip file \"{}\"".format(fileinfo.filename, zipfile))
def process_eac(self, eacfile): def process_eac(self, eacfile):
print "processing eac" print "processing eac"
reader = csv.DictReader(eacfile, delimiter="\t") reader = csv.DictReader(eacfile, delimiter="\t")
for row in reader: for row in reader:
client_authorization_id = row['ClientAuthorizationID'] client_authorization_id = row['ClientAuthorizationID']
if not client_authorization_id: if not client_authorization_id:
if row['Status'] == 'Error': if row['Status'] == 'Error':
print "Error in EAD file processing ({}): {}".format(row['Date'], row['Message']) Command.datadog_error("Error in EAD file processing ({}): {}".format(row['Date'], row['Message']))
else: else:
print "Encountered bad record: {}".format(row) Command.datadog_error("Encountered bad record: {}".format(row))
else: else:
try: try:
registration = TestCenterRegistration.objects.get(client_authorization_id=client_authorization_id) registration = TestCenterRegistration.objects.get(client_authorization_id=client_authorization_id)
print "Found authorization record for user {}".format(registration.testcenter_user.user.username) Command.datadog_error("Found authorization record for user {}".format(registration.testcenter_user.user.username))
# now update the record: # now update the record:
registration.upload_status = row['Status'] registration.upload_status = row['Status']
registration.upload_error_message = row['Message'] registration.upload_error_message = row['Message']
try: try:
registration.processed_at = strftime('%Y-%m-%d %H:%M:%S', strptime(row['Date'], '%Y/%m/%d %H:%M:%S')) registration.processed_at = strftime('%Y-%m-%d %H:%M:%S', strptime(row['Date'], '%Y/%m/%d %H:%M:%S'))
except ValueError as ve: except ValueError as ve:
print "Bad Date value found for {}: message {}".format(client_authorization_id, ve) Command.datadog_error("Bad Date value found for {}: message {}".format(client_authorization_id, ve))
# store the authorization Id if one is provided. (For debugging) # store the authorization Id if one is provided. (For debugging)
if row['AuthorizationID']: if row['AuthorizationID']:
try: try:
registration.authorization_id = int(row['AuthorizationID']) registration.authorization_id = int(row['AuthorizationID'])
except ValueError as ve: except ValueError as ve:
print "Bad AuthorizationID value found for {}: message {}".format(client_authorization_id, ve) Command.datadog_error("Bad AuthorizationID value found for {}: message {}".format(client_authorization_id, ve))
registration.confirmed_at = datetime.utcnow() registration.confirmed_at = datetime.utcnow()
registration.save() registration.save()
except TestCenterRegistration.DoesNotExist: except TestCenterRegistration.DoesNotExist:
print " Failed to find record for client_auth_id {}".format(client_authorization_id) Command.datadog_error("Failed to find record for client_auth_id {}".format(client_authorization_id))
def process_vcdc(self, vcdcfile):
def process_vcdc(self, vcdcfile):
print "processing vcdc" print "processing vcdc"
reader = csv.DictReader(vcdcfile, delimiter="\t") reader = csv.DictReader(vcdcfile, delimiter="\t")
for row in reader: for row in reader:
client_candidate_id = row['ClientCandidateID'] client_candidate_id = row['ClientCandidateID']
if not client_candidate_id: if not client_candidate_id:
if row['Status'] == 'Error': if row['Status'] == 'Error':
print "Error in CDD file processing ({}): {}".format(row['Date'], row['Message']) Command.datadog_error("Error in CDD file processing ({}): {}".format(row['Date'], row['Message']))
else: else:
print "Encountered bad record: {}".format(row) Command.datadog_error("Encountered bad record: {}".format(row))
else: else:
try: try:
tcuser = TestCenterUser.objects.get(client_candidate_id=client_candidate_id) tcuser = TestCenterUser.objects.get(client_candidate_id=client_candidate_id)
print "Found demographics record for user {}".format(tcuser.user.username) Command.datadog_error("Found demographics record for user {}".format(tcuser.user.username))
# now update the record: # now update the record:
tcuser.upload_status = row['Status'] tcuser.upload_status = row['Status']
tcuser.upload_error_message = row['Message'] tcuser.upload_error_message = row['Message']
try: try:
tcuser.processed_at = strftime('%Y-%m-%d %H:%M:%S', strptime(row['Date'], '%Y/%m/%d %H:%M:%S')) tcuser.processed_at = strftime('%Y-%m-%d %H:%M:%S', strptime(row['Date'], '%Y/%m/%d %H:%M:%S'))
except ValueError as ve: except ValueError as ve:
print "Bad Date value found for {}: message {}".format(client_candidate_id, ve) Command.datadog_error("Bad Date value found for {}: message {}".format(client_candidate_id, ve))
# store the candidate Id if one is provided. (For debugging) # store the candidate Id if one is provided. (For debugging)
if row['CandidateID']: if row['CandidateID']:
try: try:
tcuser.candidate_id = int(row['CandidateID']) tcuser.candidate_id = int(row['CandidateID'])
except ValueError as ve: except ValueError as ve:
print "Bad CandidateID value found for {}: message {}".format(client_candidate_id, ve) Command.datadog_error("Bad CandidateID value found for {}: message {}".format(client_candidate_id, ve))
tcuser.confirmed_at = datetime.utcnow() tcuser.confirmed_at = datetime.utcnow()
tcuser.save() tcuser.save()
except TestCenterUser.DoesNotExist: except TestCenterUser.DoesNotExist:
print " Failed to find record for client_candidate_id {}".format(client_candidate_id) Command.datadog_error(" Failed to find record for client_candidate_id {}".format(client_candidate_id))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment