Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save smitthakkar96/ea55d0b864e18ab381658d0e5c3c14cd to your computer and use it in GitHub Desktop.

Select an option

Save smitthakkar96/ea55d0b864e18ab381658d0e5c3c14cd to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment