def get(self, request): return render(request, "dashboard/index.html", { 'usersCount': len(Transaction.objects.distinct('user')), 'transactionsCount': Transaction.objects.count(), 'alertsCount': Transaction.objects(alert=True).count(), 'latestTransactions': Transaction.objects.order_by('-id').limit(20), 'now': datetime.now() })
def consumerTask(data): user_id = int(data['id']) date = datetime.strptime(data['date'], '%d%b%Y') amount = float( data['amount']) if data['type'] == u'D' else -float(data['amount']) # Maintain a total balance, balance_amount = Transaction.objects(user=user_id).sum('amount') # average transaction amount, avg_txn_amount = cal_avg_txn_amount(user_id) # standard deviation of transaction amount standard_deviation = calculate_standard_deviation(user_id) # Average monthly balance. avg_monthly_balance = cal_avg_monthly_bal_amount(user_id) alert = abs(amount) > 2 * standard_deviation # Maintain transaction log for each and every user. Transaction(user=user_id, date=date, amount=amount, alert=alert).save() print(user_id, balance_amount, avg_txn_amount, standard_deviation, avg_monthly_balance, alert)
def calculate_standard_deviation(user_id): x = [i.amount for i in Transaction.objects(user=user_id).only('amount')] try: return statistics.stdev(x) except: return float('inf')
def get(self, request): return render(request, "dashboard/alerts.html", { 'transactions': Transaction.objects(alert=True).order_by('-id') })
def userTransactions(self, request, id): return render(request, "dashboard/transactions.html", { 'transactions': Transaction.objects(user=id) })