Example #1
0
 def get_by_name(self, name) :
     d = self.db.ctypes.find_one({'name' : name})
     return CType.from_dict(d)
Example #2
0
 def create(self, d) :
     c = CType.from_dict(d)
     self.db.ctypes.insert(c.to_dict())
     return c
 def create(self, d) :
     c = CType.from_dict(d)
     self.db.ctypes.insert(c.to_dict())
     return c
 def get_by_name(self, name) :
     d = self.db.ctypes.find_one({'name' : name})
     return CType.from_dict(d)
Example #5
0
 def getBonusDetails(self,
                     moduleVarnameValuetype={},
                     isoTasks=dict(),
                     isoModules=dict()):
     #cycle through hits
     crosswalk = {
     }  # format taskid -> module -> varname -> {"possibleWorkers":set(),"actualWorkers":dict(),"bonus":{}}
     d = self.db.chits.find({}, {
         'tasks': 1,
         'taskconditions': 1,
         'completed_hits': 1,
         'hitid': 1
     })
     for row in d:
         hitid = row['hitid']
         tasks = row['tasks']
         taskconditions = row['taskconditions']
         for completed_hit in row['completed_hits']:
             workerid = completed_hit["worker_id"]
             #now we cycle through tasks
             for i, task in enumerate(tasks):
                 includeTask = False
                 couldBeReached = False
                 if taskconditions[i] == None:
                     includeTask = True
                 else:
                     #check the task condition
                     condition = jsonpickle.decode(taskconditions[i])
                     allVariables = dict()
                     has_error = False
                     for v in condition.varlist:
                         if v == "$workerid":
                             allVariables["$workerid"] = workerid
                         else:
                             frags = v.split('*')
                             if len(frags) != 3:
                                 has_error = True
                             else:
                                 docs = self.db.cresponses.find({
                                     "$and": [{
                                         'workerid': workerid
                                     }, {
                                         'hitid': hitid
                                     }, {
                                         'taskid': frags[0]
                                     }]
                                 }).sort('submitted')
                                 lastDoc = None
                                 for d in docs:
                                     lastDoc = d
                                 if lastDoc != None:
                                     response = lastDoc["response"]
                                     for module in response:
                                         if module["name"] == frags[1]:
                                             for q in module["responses"]:
                                                 if q["varname"] == frags[
                                                         2] and ("response"
                                                                 in q):
                                                     allVariables[v] = q[
                                                         "response"]
                                                     if q["response"] not in moduleVarnameValuetype[
                                                             module["name"]][
                                                                 q["varname"]][
                                                                     "aprioripermissable"]:
                                                         couldBeReached = True
                     allSets = dict()
                     for s in condition.setlist:
                         allSets[s] = [
                             r['member']
                             for r in self.db.sets.find({'name': s})
                         ]
                     if has_error:
                         continue
                     else:
                         status = Status()
                         if condition.check_conditions(
                                 allVariables, allSets, status):
                             #this task was shown to the worker
                             includeTask = True
                 #check if task was reached or could have been reached
                 if includeTask or couldBeReached:
                     if task not in crosswalk:
                         crosswalk[task] = {}
                     #now cycle through the modules and variables
                     m = self.db.ctasks.find_one({'taskid': task},
                                                 {'modules': 1})
                     r = self.db.cresponses.find_one(
                         {
                             'taskid': task,
                             'hitid': hitid,
                             'workerid': workerid
                         }, {'response': 1})
                     for module in m["modules"]:
                         if module not in crosswalk[task]:
                             crosswalk[task][module] = {}
                         #now find questions for this module
                         d = self.db.ctypes.find_one({'name': module})
                         mod = CType.from_dict(d)
                         for q in mod.questions:
                             if q.bonuspoints == 0:
                                 continue
                             includedQuestionOrReachable = False
                             if q.varname not in crosswalk[task][module]:
                                 crosswalk[task][module][q.varname] = {
                                     'possibleWorkers': set(),
                                     'primaryWorkers': {},
                                     'actualWorkers': {},
                                     'bonus': q.get_bonus()
                                 }
                             if r != None and ('response' in r):
                                 #find the correct module
                                 for qr in r['response']:
                                     if qr['name'] == module:
                                         if q.satisfies_condition(
                                                 qr['responses']):
                                             for vr in qr['responses']:
                                                 if vr['varname'] == q.varname:
                                                     crosswalk[task][module][
                                                         q.
                                                         varname]['actualWorkers'][
                                                             workerid] = q.getBonusValue(
                                                                 vr['response']
                                                             )
                                                     crosswalk[task][module][
                                                         q.
                                                         varname]['primaryWorkers'][
                                                             workerid] = q.getBonusValue(
                                                                 vr['response']
                                                             )
                                         includedQuestionOrReachable = q.satisfies_condition(
                                             qr['responses'],
                                             moduleVarnameValuetype[module])
                             else:
                                 includedQuestionOrReachable = True
                             if includedQuestionOrReachable:
                                 crosswalk[task][module][q.varname][
                                     'possibleWorkers'].add(workerid)
     #map isomorphic modules and tasks
     for task in crosswalk:
         #cycle through all the isomorphic tasks
         if task in isoTasks:
             for isotask in isoTasks[task]:
                 if isotask in crosswalk:
                     for m in crosswalk[isotask]:
                         for mappedM in isoModules[m]:
                             if mappedM in crosswalk[task]:
                                 for q in crosswalk[isotask][m]:
                                     if q in crosswalk[task][mappedM]:
                                         for workerid in crosswalk[isotask][
                                                 m][q]["possibleWorkers"]:
                                             crosswalk[task][mappedM][q][
                                                 "possibleWorkers"].add(
                                                     workerid)
                                         for workerid in crosswalk[isotask][
                                                 m][q]["actualWorkers"]:
                                             crosswalk[task][mappedM][q][
                                                 "actualWorkers"][
                                                     workerid] = crosswalk[
                                                         isotask][m][q][
                                                             "actualWorkers"][
                                                                 workerid]
                                 break
     #print(crosswalk)
     return crosswalk