Created
June 3, 2017 14:06
-
-
Save smitthakkar96/ea55d0b864e18ab381658d0e5c3c14cd to your computer and use it in GitHub Desktop.
Revisions
-
smitthakkar96 created this gist
Jun 3, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ def get_portfolio_notes_by_month(portfolioId): """ Group Portfolio notes by month of purchase Input: Trade History by month """ pipeline = [ { '$match': { 'portfolioId': portfolioId, 'issueDate': {'$exists': True, '$ne': None} } }, { '$unwind': '$dailyPerformance' }, { '$group': { '_id': { 'month': {"$month": '$dailyPerformance.dateFetched'}, 'date': {"$dayOfMonth": '$dailyPerformance.dateFetched'}, 'year': {'$year': '$dailyPerformance.dateFetched'} }, 'totalNoteAmount': {'$sum': '$noteAmount'}, 'totalPrincipalPending': {'$sum': '$dailyPerformance.principalPending'}, 'totalInterestReceived': {'$sum': '$dailyPerformance.interestReceived'}, 'totalAccuredInterest': {'$sum': '$dailyPerformance.accruedInterest'}, 'totalPaymentsReceived': {'$sum': '$dailyPerformance.paymentsReceived'}, 'totalPrincipalReceived': {'$sum': '$dailyPerformance.principalReceived'}, 'totalPrincipalPendingWithStatusLate': {'$sum': { "$cond": { "if": { "$eq": ["$dailyPerformance.loanStatus", 'Late (31-120 days)'] }, "then": "$performanceMetrics.cumulativeRecivedAmt", "else": 0 } }}, 'totalPrincipalPendingWithStatusChargedOff': {'$sum': { "$cond": { "if": { "$eq": ["$dailyPerformance.loanStatus", 'Charged Off'] }, "then": "$performanceMetrics.cumulativeRecivedAmt", "else": 0 } }} } } ] # portfolio_notes_by_month = [] # for trade_history in trade_history_by_month: # loan_ids = [int(loan_id) for loan_id in trade_history['loan_ids']] # portfolio_notes_by_month.append({"month": trade_history['_id'], # "notes": list(db.portfolioNotes.find({"_id": {"$in": loan_ids}}))}) # return portfolio_notes_by_month notes = db.portfolioNotes.aggregate(pipeline, allowDiskUse=True) return list(notes)