'''
Created on Jun 18, 2012

@author: jacob.bennett
'''

from tables import *
from numpy import *
import Queue
import datetime
import threading

dicts = Queue.Queue()
tableExists = {}
openFi = None
group = None
currentDate = None
notset = True
lock = threading.Lock()

class Tick(IsDescription):
    timestamp = UInt64Col()
    side = UInt64Col()
    level = UInt64Col()
    price = UInt64Col()
    quant = UInt64Col()
    orders = UInt16Col()
    
#def updateBook(instrument, timestamp, side, level, price, quant, orders, monthyear, day):
#    global group
#    if day != currentDayOfMonth:
#        currentDayOfMonth = day
#        group = file.createGroup("/", "day" + str(day), 'Day of Month')
#    try:
#        table = file.getNode("/day" + str(day), "tick" + str(instrument))
#    except (NoSuchNodeError):
#        table = file.createTable(group, "tick" + str(instrument), Tick, str(instrument) + "Tick")
#        #table.cols.timestamp.createIndex()
#    table.row['timestamp'] = timestamp
#    table.row['side'] = side
#    table.row['level'] = level
#    table.row['price'] = price
#    table.row['quant'] = quant
#    table.row['orders'] = orders
#    table.row.append()
#    table.flush()
#    file.flush()
#    return

def acceptDict():
    while True:
        while not dicts.empty():
            lock.acquire()
            dictInst = dicts.get()
            for instrument, dataArray in dictInst.iteritems():
                if instrument in tableExists:
                    tableD = openFi.getNode("/t" + str(instrument), "direct")
                else:
                    tableExists[instrument] = 1
                    group = openFi.createGroup("/", "t" + str(instrument), str(instrument))
                    tableD = openFi.createTable(group, "direct", Tick, "direct")
                for i in dataArray:
                    tableD.row['timestamp'] = i[0]
                    tableD.row['side'] = i[1]
                    tableD.row['level'] = i[2]
                    tableD.row['price'] = i[3]
                    tableD.row['quant'] = i[4]
                    tableD.row['orders'] = i[5]
                    tableD.row.append()
                tableD.flush()
                openFi.flush()
            lock.release() 
        
def addDictToQueue(dictInst):
    dicts.put(dictInst)
    return

def changeFile():
    global currentDate
    global notset
    global openFi
    global tableExists
    while True:
        if notset:
            lock.acquire()
            currentDate = datetime.datetime.utcnow()
            openFi = openFile("Book" + str(currentDate.year) + str(currentDate.month) + str(currentDate.day), mode = "w", title = str(currentDate.day))
            tableExists = {}
            notset = False
            lock.release()
        elif currentDate.day != datetime.datetime.utcnow().day:
            lock.acquire()
            currentDate = datetime.datetime.utcnow()
            openFi.close()
            openFi = openFile("Book" + str(currentDate.year) + str(currentDate.month) + str(currentDate.day), mode = "w", title = str(currentDate.day))
            tableExists = {}
            lock.release()