Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added logging

  • Loading branch information...
commit 00356387d03fe4449849371237983b14f0119e8f 1 parent d5fc781
@alimjiwa alimjiwa authored
Showing with 67 additions and 27 deletions.
  1. +28 −27 sps/transactions/commands.py
  2. +39 −0 sps/transactions/xml.py
View
55 sps/transactions/commands.py
@@ -7,12 +7,8 @@
from sps.transactions import xml
from sps.config import config
from datetime import datetime
-import eventlet
from os import path
-from logging import getLogger
-log = getLogger(__name__)
-
class CommandError(Exception):
"""
An exception that holds both a message for the user and the original cause
@@ -44,9 +40,6 @@ class NoBuyTransactionError(CommandError):
class NoSellTransactionError(CommandError):
user_message = 'No SELL transaction is pending'
-class NoTriggerError(CommandError):
- user_message = 'No trigger is pending'
-
class ExpiredBuyTransactionError(CommandError):
user_message = 'BUY transaction has expired'
@@ -114,9 +107,12 @@ def run(self, username, amount):
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
- amount = Money.from_string(amount)
- user.account_balance += amount
+ xml.log_error('ADD','username not found')
+ money_amount = Money.from_string(amount)
+ user.account_balance += money_amount
session.commit()
+ # create log
+ xml.log_event('ADD', username, amount=amount)
return xml.ResultResponse('success')
@@ -129,11 +125,18 @@ def run(self, username, stock_symbol):
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
+ xml.log_error('QUOTE','username not found')
if len(stock_symbol) > 4:
raise InvalidInputError('stock symbol too long: %d' % \
len(stock_symbol))
+ xml.log_error('QUOTE','stock symbol too long')
+
quote_client = get_quote_client()
quote = quote_client.get_quote(stock_symbol, username)
+
+ #create log
+ xml.log_event('QUOTE', username, stock_symbol)
+
return str(quote)
@@ -147,19 +150,20 @@ def run(self, username, stock_symbol, amount):
user = session.query(User).filter_by(username=username).first()
if not user:
raise UserNotFoundError(username)
-
+ xml.log_error('BUY','username not found')
# Check for existing uncommitted transaction
if session.query(Transaction).filter_by(
user=user, operation='BUY', committed=False).count() > 0:
raise BuyTransactionActiveError()
-
+ xml.log_error('BUY','Outstanding Buy Exists')
+
# Getting stock quote
quote_client = get_quote_client()
quote = quote_client.get_quote(stock_symbol, username)
# Work out quantity of stock to buy, fail if not enough for one stock
amount = Money.from_string(amount)
- quantity = amount_to_quantity(quote, amount)
+ quantity = self.quantity(quote, amount)
if quantity == 0:
raise InsufficientFundsError()
@@ -172,8 +176,20 @@ def run(self, username, stock_symbol, amount):
session.add(transaction)
session.commit()
+ xml.log_transaction('BUY', transaction)
return xml.QuoteResponse(quantity=quantity, price=price)
+ def quantity(self, price, amount):
+ q = 0
+ while(True):
+ amount = amount - price
+ if (amount.dollars < 0) or (amount.cents < 0):
+ break
+ else:
+ q += 1
+
+ return q
+
class COMMIT_BUYCommand(CommandHandler):
"""
@@ -428,7 +444,6 @@ def run(self, username, stock_symbol):
return xml.ResultResponse('trigger cancelled')
-
class SET_BUY_TRIGGERCommand(CommandHandler):
"""
Sets the trigger point base on the current stock price when any SET_BUY
@@ -708,20 +723,6 @@ def dumplog_admin(self, filename):
return xml.ResultResponse('Wrote transactions to "%s"' % full_path)
-def amount_to_quantity(price, amount):
- """ Given the price of a stock and an maximum dollar value to buy, returns
- the quantity of stock that can be bought. """
- q = 0
- while(True):
- amount = amount - price
- if (amount.dollars < 0) or (amount.cents < 0):
- break
- else:
- q += 1
-
- return q
-
-
CommandHandler.register_command('ADD', ADDCommand)
CommandHandler.register_command('QUOTE', QUOTECommand)
CommandHandler.register_command('BUY', BUYCommand)
View
39 sps/transactions/xml.py
@@ -1,5 +1,6 @@
from lxml import etree
from lxml import objectify
+import logging
ElementMaker = objectify.ElementMaker(annotate=False)
Quote = ElementMaker.quote
@@ -12,6 +13,11 @@
Trigger = ElementMaker.trigger
AccountBalance = ElementMaker.account_balance
ReserveAccount = ElementMaker.reserve_account
+Log = ElementMaker.log
+Event = ElementMaker.event
+
+#configure logging format for transaction logs
+TransactionLog = logging.getLogger("TransactionLogs")
class QuoteResponse():
def __init__(self, quantity, price):
@@ -100,4 +106,37 @@ def trigger_element(t):
operation=t.operation,
)
+def log_transaction(transaction_type, db_transaction, status_message=None):
+ if status_message == None:
+ xml = Log(Transaction(transaction_element(db_transaction), transaction_type=transaction_type, time=str(datetime.now()) ))
+ else:
+
+ xml = Log(Transaction(transaction_element(db_transaction),transaction_type=transaction_type, status_message=status_message, time=str(datetime.now()) ))
+
+ # write a transaction log message to STDOUT that has the string of the XML tags
+ TransactionLog.info(etree.tostring(xml))
+
+def log_trigger(trigger_type, db_trigger, status_message = None):
+
+ if status_message == None:
+ xml = Log(Trigger(trigger_element(db_trigger), trigger_type=trigger_type, time=str(datetime.now()) ))
+ else:
+ xml = Log(Trigger(trigger_element(db_trigger), trigger_type=trigger_type, status_message=status_message, time=str(datetime.now()) ))
+
+ # write a transaction log message to STDOUT that has the string of the XML tags
+ TransactionLog.info(etree.tostring(xml))
+
+def log_event(event_type, username, stock_symbol=None, amount=None, status_message=None):
+
+ xml = Log(Event(event_type=event_type, username=username, stock_symbol=stock_symbol, amount=amount, status_message=status_message, time=str(datetime.now()) ))
+
+ # write a transaction log message to STDOUT that has the string of the XML tags
+ TransactionLog.info(etree.tostring(xml))
+
+def log_error(command, message):
+
+ xml = Log(Event(command=command, error_message=message, time=str(datetime.now()) ))
+
+ # write a transaction log message to STDOUT that has the string of the XML tags
+ TransactionLog.error(etree.tostring(xml))
Please sign in to comment.
Something went wrong with that request. Please try again.