def __init__(self): # self.invoicer = Invoicer() # self.pubsub = RedisPubSub("mama") self.threadQ = Queue.Queue() self.webIntf = WebIntf(self) self.solver = DilatorSolver(self.threadQ) self.results = [] self.toNodeQ = [] self.prevNodeQ = [None] * 1000 self.dbChanged = True pub.subscribe(self.setCurrentPage, "currentPage") self.range = 0 print "Server Listening..." while 1: if self.dbChanged: self.doLpInterval() self.dbChanged = False time.sleep(30) self.doCrtvInterval()
class DilatorDaemon(object): """ Main server daemon responsible the LP and sending crtvs """ def __init__(self): # self.invoicer = Invoicer() # self.pubsub = RedisPubSub("mama") self.threadQ = Queue.Queue() self.webIntf = WebIntf(self) self.solver = DilatorSolver(self.threadQ) self.results = [] self.toNodeQ = [] self.prevNodeQ = [None] * 1000 self.dbChanged = True pub.subscribe(self.setCurrentPage, "currentPage") self.range = 0 print "Server Listening..." while 1: if self.dbChanged: self.doLpInterval() self.dbChanged = False time.sleep(30) self.doCrtvInterval() def setCurrentPage(self, range): self.range = int(range) # print "Range: "+str(range)+" to "+str(int(range)+15) self.doCrtvInterval() def doCrtvInterval(self): """ Iterates through toNodeQ, formats, and passes to webintf """ # print "Sent Crtvs" self.createSignCrtvQueues() self.toNodeQ = sorted(self.toNodeQ, key=lambda k: k.get("sign").id) tonode = "[" for q in self.toNodeQ: signid = int(q.get("sign").id) if signid >= self.range and signid <= self.range + 15: tonode = tonode + self.formatJsonMsg(q.get("sign"), q.get("currCrtv"), q.get("cmpgn")) + "," tonode = tonode[:-1] + "]" self.webIntf.publish(tonode) def doLpInterval(self): """ DocString """ print "Lp Interval" self.toNodeQ = [] self.lpLock = 1 self.solver.run() self.formatProb(self.threadQ.get()) self.lpLock = 0 def formatProb(self, prob): for v in prob: if v[1] == "NULL": cmpgn = "NULL" else: cmpgn = self.solver.cmpgns.get(id=v[1]) sign = self.solver.signs.get(id=v[0]) self.results.append({"cmpgn": cmpgn, "sign": sign}) def createSignCrtvQueues(self): """ For each Sign/Cmpgn match, assaign a random creative to the sign and append it to the queue. """ for i, r in enumerate(self.results): signdisp = r.get("sign").signdisp if not r.get("cmpgn") == "NULL": crtvs = self.filterValidCrtvs(r, signdisp) currCrtv = crtvs[random.randint(0, len(crtvs) - 1)] else: currCrtv = "NULL" if self.prevNodeQ[i] != {"sign": r.get("sign"), "currCrtv": currCrtv, "cmpgn": r.get("cmpgn")}: self.toNodeQ.append({"sign": r.get("sign"), "currCrtv": currCrtv, "cmpgn": r.get("cmpgn")}) self.prevNodeQ[i] = {"sign": r.get("sign"), "currCrtv": currCrtv, "cmpgn": r.get("cmpgn")} self.results = [] def filterValidCrtvs(self, r, signdisp): """ DocSring """ if signdisp == 1: return [crtv for crtv in r.get("cmpgn").crtv.all() if crtv.type == "poster"] if signdisp == 2: return [crtv for crtv in r.get("cmpgn").crtv.all() if crtv.type == "video"] if signdisp == 3: return [crtv for crtv in r.get("cmpgn").crtv.all()] def formatJsonMsg(self, sign, crtv, cmpgn): """ Json to be sent to the webinterface """ cropname = "c" + str(sign.ypixel) if not crtv == "NULL": return json.dumps( { "cmpgnname": cmpgn.name, "signid": sign.id, "username": cmpgn.user.username, "crtvfile": os.path.basename(crtv.file.path), "crtvtype": crtv.type, "cropname": cropname, } ) else: return json.dumps({"signid": sign.id, "crtvfile": "NULL"}) @staticmethod def formatInvoice(sign, crtv, cmpgn): """ Formatting to be sent to the invoicer """ return {"cmpgnname": cmpgn.name, "signid": sign.id, "crtvid": crtv.id} def printOut(self): """ Generic print to stout for debugging """ name = (v.name for v in self.prob.variables() if v.varValue) for n in name: print n def printCmpgnTally(self, r): """ DocSring """ prevcmpgn, count, total = 1, 0, 0 if prevcmpgn == r.get("cmpgn").id: count = count + 1 total = total + 1 else: print prevcmpgn, count, total count = 0 prevcmpgn = r.get("cmpgn").id print prevcmpgn, count, total