def parse_xapi(predicate): query = [] query_objs = [] groups = re.findall(r'(?:\[(.*?)\])', predicate) for g in groups: (left, right) = g.split('=') if left == '@uid': query.append('uid = %s') query_objs.append(int(right)) elif left == '@changeset': query.append('changeset_id = %s') query_objs.append(int(right)) elif left == 'bbox': try: (l, b, r, t) = parse_bbox(right) except ValueError, e: raise QueryError('Invalid bbox.') if l > r: raise QueryError('Left > Right.') if b > t: raise QueryError('Bottom > Top.') if b < -90 or b > 90: raise QueryError('Bottom is out of range.') if t < -90 or t > 90: raise QueryError('Top is out of range.') if l < -180 or l > 180: raise QueryError('Left is out of range.') if r < -180 or r > 180: raise QueryError('Right is out of range.') query.append('ST_Intersects(geom, ST_GeometryFromText(\'POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))\', 4326))') query_objs.extend([l, b, l, t, r, t, r, b, l, b]) else: ors = [] orvs = [] keys = left.split('|') vals = right.split('|') for (l,r) in itertools.product(keys, vals): if r == '*': ors.append('(tags ? %s)') orvs.append(l) else: ors.append('(tags @> hstore(%s, %s))') orvs.append(l) orvs.append(r) query.append('(' + ' OR '.join(ors) + ')') query_objs.extend(orvs)
def get(self,survey_id,start=None,end=None,aggregate="false"): try: survey_id=HashId.decode(survey_id) except ValueError: return "No survey_id or uuid provided" lol = IrapiData(survey_id,start,end,aggregate) all_responses= lol.get_data() # return all_responses #return all_responses all_survey= lol.get_uuid_labels() if "referenced" in all_survey[0]: parent_survey= all_survey[0]['referenced']['$oid'] # parent_survey= HashId.decode(parent_survey) s= IrapiData(parent_survey,start,end,aggregate) all_survey=s.get_uuid_labels() else: all_survey= lol.get_uuid_labels() # return all_responses # try: # all_survey=all_survey[0] # except : # pass ret=[] # return all_survey for i in range(len(all_survey)): j_data=all_survey[i] uuid= j_data['cid'] response_data=all_responses try: options=[] option_code={} for i in range(len(j_data['field_options']['options'])): options.append(j_data['field_options']['options'][i]['label']) option_code["a_"+str(i+1)]=j_data['field_options']['options'][i]['label'] except:pass """Response Count """ temp=[] for a in range(len(response_data)): try: temp.append(response_data[a]['responses'][uuid]) except: pass """Option Count""" options_count={} options_count_segg={} sentiment={'positive':0,'negative':0,'neutral':0} long_text="" if j_data['field_type'] not in ["ranking","rating","group_rating"]: for b in temp: if j_data['field_type']=='long_text': dat= DatumBox() sent= dat.get_sentiment(b) sentiment[sent]+=1 options_count_segg[b]=sent long_text+=" "+b if j_data['field_type']=='multiple_choice': split_b= b.split('###') if len(split_b)==0: if split_b[0] in options_count_segg: options_count_segg[split_b[0]]+=1 else:options_count_segg[split_b[0]]=1 elif len(split_b)!=0: for i in split_b: if i in options_count_segg: options_count_segg[i]+=1 else:options_count_segg[i]=1 if b in options_count:pass else:options_count[b]=temp.count(b) elif j_data['field_type'] in ["ranking"]: values={} for c in temp:#temp is an array of responses aTempList=c.split("###") #split a##2###b##1### [ "a##1", "b##3", "c##2"] for d in aTempList: bTempList=d.split("##") #["a","1"] e= bTempList[0] #Values are a ,b rank_key= bTempList[1] #values are 1,2,3,4 if e in values: if rank_key in values[e]: values[e][rank_key]+=1 else: # values[e]={} values[e][rank_key]=1 else: values[e]={} values[e][rank_key]=1 if e in options_count: """ e is for eg : a_1 or a_2 aTempList is the total options bTempList is option:value pair """ options_count[e]=int(options_count[e])+len(aTempList)-int(bTempList[1]) else: options_count[e]=len(aTempList)-int(bTempList[1]) elif j_data['field_type']=="group_rating": for f in temp: aTempList=f.split("###") for g in aTempList: bTempList=g.split("##") l= bTempList[0] k=bTempList[1] if l in options_count: if k in options_count[l]: options_count[l][k]+=1 else:options_count[l][k]=1 else: options_count[l]={} options_count[l][k]=1 elif j_data['field_type']=="rating": for i in temp: i = str(i) if i in options_count: options_count[i]+=1 else:options_count[i]=1 #return options_count response={} response['cid']=uuid response['label']=j_data['label'] response['type']=j_data['field_type'] response['option_code']=option_code if j_data['field_type']!='long_text': response['options_count']=options_count if j_data['field_type']=='long_text': response['sentiment']=sentiment response['sentiment_segg']=options_count_segg keywords=dat.get_keywords(long_text) response['keywords']=keywords #response['garbage']=temp if j_data['field_type']=='ranking': response['ranking_count']=values if j_data['field_type']=='multiple_choice': response['options_count_segg']=options_count_segg response['total_resp']= len(temp) if j_data['field_type']=="rating": avg=0 for i in temp: avg+=int(i) # response['avg_rating']= float(avg)/float(len(temp)) response['avg_rating']= float(avg)/len(temp) if j_data['field_type']=="group_rating": avg={} for key in options_count: counter=0 for bkey in options_count[key]: if int(bkey)!=0: counter+=float(bkey)*options_count[key][bkey] else:pass avg[key]= round(float(counter)/len(temp),2) response['avg_rating']=avg ret.append(response) return ret
def get(self,survey_id,start=None,end=None,aggregate="false"): try: survey_id=HashId.decode(survey_id) except ValueError: return "No survey_id or uuid provided" lol = IrapiData(survey_id,start,end,aggregate) all_responses= lol.get_data() #return all_responses all_survey= lol.get_uuid_labels() if "referenced" in all_survey[0]: parent_survey= all_survey[0]['referenced']['$oid'] # parent_survey= HashId.decode(parent_survey) s= IrapiData(parent_survey,start,end) all_survey=s.get_uuid_labels() else: all_survey= lol.get_uuid_labels() # return all_responses # try: # all_survey=all_survey[0] # except : # pass ret=[] # return all_survey for i in range(len(all_survey)): j_data=all_survey[i] uuid= j_data['cid'] response_data=all_responses try: options=[] option_code={} for i in range(len(j_data['field_options']['options'])): options.append(j_data['field_options']['options'][i]['label']) option_code["a_"+str(i+1)]=j_data['field_options']['options'][i]['label'] except:pass """Response Count """ temp=[] for a in range(len(response_data)): temp.append(response_data[a]['responses'][uuid]) """Option Count""" options_count={} if j_data['field_type'] not in ["ranking","rating","group_rating"]: for b in temp: if b in options_count:pass else:options_count[b]=temp.count(b) elif j_data['field_type'] in ["ranking"]: for c in temp: aTempList=c.split("###") for d in aTempList: bTempList=d.split("##") e= bTempList[0] if e in options_count: options_count[e]=int(options_count[e])+len(aTempList)-int(bTempList[1]) else: options_count[e]=len(aTempList)-int(bTempList[1]) elif j_data['field_type']=="group_rating": for f in temp: aTempList=f.split("###") for g in aTempList: bTempList=g.split("##") l= bTempList[0] k=bTempList[1] if l in options_count: if k in options_count[l]: options_count[l][k]+=1 else:options_count[l][k]=1 else: options_count[l]={} options_count[l][k]=1 elif j_data['field_type']=="rating": for i in temp: i = str(i) if i in options_count: options_count[i]+=1 else:options_count[i]=1 #return options_count response={} response['cid']=uuid response['label']=j_data['label'] response['type']=j_data['field_type'] response['option_code']=option_code response['options_count']=options_count response['total_resp']= len(temp) if j_data['field_type']=="rating": avg=0 for i in temp: avg+=int(i) # response['avg_rating']= float(avg)/float(len(temp)) response['avg_rating']= float(avg)/len(temp) if j_data['field_type']=="group_rating": avg={} for key in options_count: counter=0 for bkey in options_count[key]: if int(bkey)!=0: counter+=float(bkey)*options_count[key][bkey] else:pass avg[key]= round(float(counter)/len(temp),2) response['avg_rating']=avg ret.append(response) return ret