def marketsInsert(event, context): #validation if 'body' in event: body=json.loads(event['body']) if 'name' in body: name=body['name'] else: return {'statusCode': 400, 'message': 'Name not provided'} if 'marketopen' in body: try: marketopen=datetime.strptime(body['marketopen'], '%Y-%m-%d') except ValueError: return {'statusCode': 400, 'message': 'Invalid market open date format'} if 'auctionclose' in body: if len(body['auctionclose']) > 0: try: auctionclose=datetime.strptime(body['auctionclose'], '%Y-%m-%d') except ValueError: return {'statusCode': 400, 'message': 'Invalid auction close date format'} else: auctionclose=None else: return {'statusCode': 400, 'message': 'No JSON body provided'} #save dbsession=SessionFactory() try: newMarket=Market(name=name, marketopen=marketopen, auctionclose = auctionclose) dbsession.add(newMarket) dbsession.commit() dbsession.close() return {'statusCode': 200} except: dbsession.rollback() dbsession.close() return {'statusCode': 400, 'message': 'SQL insert error'}
def save_commodities(data, session): authorised = data['header']['softwareName'] in AUTHORISED_SOFTWARES if not authorised: return ts = int( dateutil.parser.isoparse(data['message']['timestamp']).timestamp()) valid, loop_ts = get_loop_timestamp(ts) if not valid: return system = data['message']['systemName'] station = data['message']['stationName'] market = data['message']['marketId'] for commodity in data['message']['commodities']: name = commodity['name'].lower() if name not in commodities_mapping: continue commodity_id = commodities_mapping[name] price = commodity['sellPrice'] mean = commodity['meanPrice'] if price <= mean: continue demand = commodity['demand'] entry = session.query(CommodityMaxPrice).filter_by( commodity_id=commodity_id, timestamp=loop_ts, market_id=market).first() if entry is None: market_entry = session.query(Market).filter_by(id=market).first() if market_entry is None: new_market = Market(id=market, system=system, station=station) session.add(new_market) new_commodity = CommodityMaxPrice(commodity_id=commodity_id, sell_price=price, sell_demand=demand, timestamp=loop_ts, updated=ts, market_id=market) session.add(new_commodity) logging.info("New entry: %s", new_commodity) else: if entry.market is None: session.add(Market(id=market, system=system, station=station)) if price > entry.sell_price: entry.sell_price = price if price == entry.sell_price and demand > entry.sell_demand: entry.sell_demand = demand if price == entry.sell_price or demand == entry.sell_demand: entry.updated = ts logging.info("Update entry: %s", entry) session.merge(entry)
from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/raviteja/betbright/betBright.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db = SQLAlchemy(app) # See important note below from model import Market from model import Selection from model import Event from model import Message from model import Sport #Simple data insertions for available tables s1 = Selection(name="Poland",odds=3.01) s2 = Selection(name="Irland",odds=7.01); m1 = Market(name="Winner",selections=[s1,s2]) ev1 = Event("Poland vs Irland","http://example.com/api/match/2",2,1) db.session.add(m1) db.session.add(s1) db.session.add(s2) db.session.add(ev1) db.session.commit()