def note_page_scheduler(self, days_old): ''' return a list of (loanID,orderID,noteID) tuples for note pages that are out of date or whose note orders have recently changed. ''' NO = NoteOrders() NO.grab_data(0, 999999) np_tups = [] for note_order in NO.get_data(): if self.out_of_date(note_order, days_old) or self.order_changed(note_order): try: np_tups.append((int(note_order['loanGUID']), int(note_order['orderId']), int(note_order['noteId']))) except KeyError: continue return np_tups
class NoteOrdersUpdater(object): def __init__(self): self.notes = get_db('lc_db').notes self.NO = NoteOrders() def update(self): print 'Pulling new data from foliofn...' self.NO.grab_data(0, 999999) print 'Updating DB...' for note in self.NO.get_data(): self.update_note(note) print 'Done.' def update_note(self, note): ''' Insert new note data into the 'notes' collection. 'note' is a JSON style document captured from foliofn 'note_doc' is created in this method, it is the document that is part of the db notes collection If no note exists in the db, create a new note document. If a note exists, update the time series data ''' note_doc = self.notes.find_one({'noteID':int(note['noteId'])}) if note_doc is None: self.notes.insert(self.create_note_doc(note)) else: self.update_field(note, note_doc, 'asking_price') self.update_field(note, note_doc, 'ytm') self.update_field(note, note_doc, 'markup_discount') self.notes.update({'noteID':note['noteId']}, {'$set': {'outstanding_principal':float(note['outstanding_principal']), 'accrued_interest':float(note['accrued_interest']) } }, safe=True ) def update_field(self, note, note_doc, field): ''' Update a field in a note document which is an array of subdocuments if the value of the last entry is different from the current measurement ''' subdoc = note_doc.get(field,None) try: if subdoc is not None and subdoc[-1][field] == float(note[field]): return except ValueError: return self.notes.update({'noteID':int(note['noteId'])}, {"$push":{field:{field:float(note[field]), 'time':datetime.datetime.utcnow() } } }, safe=True ) def create_note_doc(self, note): ''' Create a new note document to add to notes collection 'note' is a JSON style document captured from foliofn ''' try: note_doc = { 'orderID':int(note['orderId']), 'noteID':int(note['noteId']), 'loanID':int(note['loanGUID']), 'asking_price':NoteOrdersUpdater.create_subdoc('asking_price',note['asking_price']), 'markup_discount':NoteOrdersUpdater.create_subdoc('markup_discount',note['markup_discount']), 'ytm':NoteOrdersUpdater.create_subdoc('ytm', note['ytm']), 'trading_status':True, 'outstanding_principal':float(note['outstanding_principal']), 'accrued_interest':float(note['accrued_interest']), } except: raise Exception('unable to create new note document') return note_doc @staticmethod def create_subdoc(field, val): if val != 'null': val = float(val) return [{field:val, 'time':datetime.datetime.utcnow()}]