def get(self,survey_id,aggregate="false"): ##First get for all surveys survey_id=HashId.decode(survey_id) # survey_id=HashId.decode("goojkg5jyVnGj9V6Lnw") # parent_survey=survey_id # survey_id= HashId.decode("3NNl87yvoZXN4lypAjq") parent_survey= survey_id l = IrapiData(survey_id,1,1,aggregate) # survey_strct= l.survey_strct() #Check if survey has children. #Check for parent too. flag0= l.get_parent() if flag0!=False: """There is a parent""" parent_survey= flag0 flag= l.flag() if flag ==False: r= {} r['parent_survey']= self.logic(survey_id,parent_survey,aggregate) return r else: if aggregate=="true": response={} response['parent_survey']= self.logic(survey_id,parent_survey,aggregate) # return self.logic(survey_id,parent_survey) # response={} units=[] for i in flag: units.append(self.logic(HashId.decode(i),parent_survey,aggregate)) # return response units.append(self.logic(survey_id,parent_survey,"false")) response['units']=units return response else: r= {} r['parent_survey']= self.logic(survey_id,parent_survey,aggregate) return r
def get(self,survey_id,aggregate="false"): ##First get for all surveys survey_id=HashId.decode(survey_id) # survey_id=HashId.decode("goojkg5jyVnGj9V6Lnw") # parent_survey=survey_id # survey_id= HashId.decode("3NNl87yvoZXN4lypAjq") parent_survey= survey_id l = IrapiData(survey_id,1,1,aggregate) # survey_strct= l.survey_strct() #Check if survey has children. #Check for parent too. flag0= l.get_parent() if flag0!=False: """There is a parent""" parent_survey= flag0 flag= l.flag() if flag ==False: r= {} r['parent_survey']= self.logic(survey_id,parent_survey,aggregate) return r else: if aggregate=="true": response={} response['parent_survey']= self.logic(survey_id,parent_survey,aggregate) units=[] for i in flag: units.append(self.logic(HashId.decode(i),parent_survey,aggregate)) # return response units.append(self.logic(survey_id,parent_survey,"false")) response['units']=units # return "true" return response else: r= {} r['parent_survey']= self.logic(survey_id,parent_survey,aggregate) return r
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 logic(self,survey_id,parent_survey,aggregate="false"): """ Logic : The child needs to copy their parents survey structure , pass the parent survey strc """ lol= IrapiData(survey_id,1,1,aggregate) csi= lol.get_child_data(survey_id)[0]#child survey info response_data= d(lol.get_data()) #return response_data # survey_strct= d(lol.survey_strct()) if parent_survey==survey_id: survey_strct= d(lol.survey_strct()) elif parent_survey!=survey_id: s= IrapiData(parent_survey,1,1,aggregate) survey_strct=d(s.survey_strct()) try: survey_name= csi['unit_name'] # return survey_name created_by=csi['created_by'][0]['$oid'] # return csi except: survey_name="Parent Survey" created_by="Not Applicable" # else:pass #return survey_strct """ALT""" cids= [] # return survey_strct for i in survey_strct: # return i x= i['field_options'] if "deletable" in x: # return x['options'] cids.append(i['cid']) # """ END""" res=[] r= {} for cid in cids: alol = DataSort(parent_survey,cid,aggregate) survey_data= alol.get_uuid_label()#?So wrong # return survey_data #I have the total responses j_data= d(survey_data) # return survey_data[0]['field_options'] if "options" in survey_data['field_options']: 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 else:pass # return option_code temp= [] timed={} import time for i in response_data: # temp.append(i) if cid in i['responses']: temp.append(i['responses'][cid]) timestamp= i['metadata']['modified']['$date']/1000 timestamp=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp)) timed[timestamp]=i['responses'][cid] # return temp options_count={} timed_agg={} timed_agg_counter={} try: for time , value in timed.items(): if time[:10] not in timed_agg_counter: timed_agg_counter[time[:10]]=0 if time[:10] in timed_agg: timed_agg[time[:10]]+=int(value) timed_agg_counter[time[:10]]+=1 else: timed_agg[time[:10]]=int(value) timed_agg_counter[time[:10]]=1 timed_final={} for time,value in timed_agg.items(): avg = round(float(timed_agg[time])/float(timed_agg_counter[time]),2) timed_final[time]=avg except:pass if j_data['field_type']=="group_rating": for i in temp: # return i aTempList= i.split("###") # return aTempList for j in aTempList: bTempList= j.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 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) #return option_code, options_count # for i in range(len(response_data)): # temp.append(response_data[i]['responses'][cid]) # return temp[9] #timed={} elif j_data['field_type']=="rating": for i in temp: if str(i) in options_count: options_count[str(i)]+=1 else: options_count[str(i)]=1 # avg= 0.0 ll= 0 for j in temp:ll= float(ll)+float(j) # avg = round(float(avg)/float(len(temp))) avg=round(ll/len(temp),2) # return avg response={} response['cid']= cid try: response['avg_rating']=avg except:pass if j_data['field_type']=='rating': response['timed_agg']=timed_final response['timed']=timed if j_data['field_type']=="group_rating": response['options_code']=option_code else:pass # response['survey_id']=survey_id response['options_count']=options_count response['label']=survey_data['label'] try: response['unit_name']=survey_name response['created_by']=created_by except:pass response['total_resp']=len(response_data) res.append(response) # try: # res['unit_name']=survey_name # res['created_by']=created_by # except:pass return res
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
def logic(self,survey_id,parent_survey,aggregate): """ Logic : The child needs to copy their parents survey structure , pass the parent survey strc """ lol= IrapiData(survey_id,1,1,aggregate) csi= lol.get_child_data(survey_id)[0]#child survey info response_data= d(lol.get_data()) # return len(response_data) # survey_strct= d(lol.survey_strct()) if parent_survey==survey_id: survey_strct= d(lol.survey_strct()) elif parent_survey!=survey_id: s= IrapiData(parent_survey,1,1,aggregate) survey_strct=d(s.survey_strct()) try: survey_name= csi['unit_name'] # return survey_name created_by=csi['created_by'][0]['$oid'] # return csi except: survey_name="Parent Survey" created_by="Not Applicable" # else:pass #return survey_strct """ALT""" cids= [] # return survey_strct for i in survey_strct: # return i x= i['field_options'] if "deletable" in x: # return x['options'] cids.append(i['cid']) # """ END""" res=[] r= {} for cid in cids: alol = DataSort(parent_survey,cid,aggregate) survey_data= alol.get_uuid_label()#?So wrong # return survey_data #I have the total responses j_data= d(survey_data) # return survey_data[0]['field_options'] if "options" in survey_data['field_options']: 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 else:pass # return option_code temp= [] for i in response_data: # temp.append(i) if cid in i['responses']: temp.append(i['responses'][cid]) # return temp options_count={} if j_data['field_type']=="group_rating": for i in temp: # return i aTempList= i.split("###") # return aTempList for j in aTempList: bTempList= j.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 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) # return option_code, options_count # for i in range(len(response_data)): # temp.append(response_data[i]['responses'][cid]) # return temp[9] elif j_data['field_type']=="rating": for i in temp: if str(i) in options_count: options_count[str(i)]+=1 else: options_count[str(i)]=1 # avg= 0.0 ll= 0 for j in temp:ll= float(ll)+float(j) # avg = round(float(avg)/float(len(temp))) avg=round(ll/len(temp),2) # return avg response={} response['cid']= cid try: response['avg_rating']=avg except:pass if j_data['field_type']=="group_rating": response['options_code']=option_code else:pass # response['survey_id']=survey_id response['options_count']=options_count response['label']=survey_data['label'] try: response['unit_name']=survey_name response['created_by']=created_by except:pass res.append(response) # try: # res['unit_name']=survey_name # res['created_by']=created_by # except:pass return res