Example #1
0
    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,uuid,aggregate="false"):
        survey_id=HashId.decode(survey_id)  #Uncomment on Production
        # survey_id="IamASurveyId"

        lol= DataSort(survey_id,uuid,aggregate)
        all_survey= lol.get_survey()
        if "referenced" in all_survey[0]:
           # return "reference"
            parent_survey= all_survey[0]['referenced']['$oid']
            
            # parent_survey= HashId.decode(parent_survey)
            s= DataSort(parent_survey,uuid)
            survey_data= s.get_uuid_label()

            
            
        else:
           
            survey_data= lol.get_uuid_label()
        j_data= d(survey_data)
        #return j_data
        # Get Responses for  a cid
        response_data= lol.get_data()
        #return j_data['field_options']['options'][0]['label']
        # 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:

            return "error"

        #Response Count
        temp= []
        
        for i in range(len(response_data)):

            temp.append(response_data[i]['responses'][uuid])



        #eturn j_data['field_type']
        options_count={}
        if j_data['field_type'] not in ["ranking","rating","group_rating"]:
            for i in temp:
                if i in options_count:pass
                else:options_count[i]= temp.count(i)

        elif j_data['field_type'] in ["ranking"]:
            for i in temp:
                aTempList= i.split("###")
                for j in aTempList:
                    bTempList= j.split("##")
                    l = bTempList[0]
                    if l in options_count:

                        options_count[l]= int(options_count[l])+len(aTempList)-int(bTempList[1])
                    else:
                        options_count[l]=len(aTempList)-int(bTempList[1])

        # elif j_data['field_type']=="rating":
        #   for i in temp:
        #       aTempList= i.split("###") #Check if this breaks the logic
        #       for j in aTempList:
        #           bTempList= j.split("##")
        #           if j in options_count:
        #               options_count[j]= int(options_count[j])+ int(bTempList[1])
        #           else:
        #               options_count[j]= int(bTempList[1])
        elif j_data['field_type']=="group_rating":
            for i in temp:
                aTempList= i.split("###")
                #options_count={}

                for j in aTempList:
                    bTempList= j.split("##")
                    l = bTempList[0]

                    #o_c= {a_1:}
                    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:
                if str(i) in options_count:
                    options_count[str(i)] +=1
                else:
                    options_count[str(i)]=1
                # if int(i)>6:
                #   if "above_5" in options_count:
                #       options_count["above_5"]= options_count["above_5"]+1
                #   else:
                #       options_count["above_5"]=1
                # elif int(i)<6 and int(i)>3 :
                #   if "above_3" in options_count:
                #       options_count["above_3"]= options_count["above_3"]+1
                #   else:
                #       options_count["above_3"]=1
                # elif int(i)<=3:
                #   if "below_3" in options_count:
                #       options_count["below_3"]= options_count["below_3"]+1
                #   else:
                #       options_count["below_3"]=1


        # elif j_data['field_type']=="short_text":
        #     return "lol"

        response= {}
        if j_data['field_type']=="rating":
            avg= 0
            for i in temp: avg= avg + int(i)
            response['avg_rating']= round(float(avg)/float(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)

                # avg[key]=float(sum(options_count[key].values()))/float(len(temp))
            response['avg_rating']=avg

        response['cid']= uuid
        # survey_id= j_data['survey_id']
        response['survey_id']=survey_id
        response['label']=j_data['label']
        response['type']=j_data['field_type']
        response['option_code']=option_code
        response['option_count']=options_count
        #return option_code
        response['total_resp']=len(temp)
        response['garbage']= temp

        return d(response)
Example #3
0
    def get(self,survey_id,uuid,aggregate="false"):
        survey_id=HashId.decode(survey_id)  #Uncomment on Production
        # survey_id="IamASurveyId"

        lol= DataSort(survey_id,uuid,aggregate)
        all_survey= lol.get_survey()
        if "referenced" in all_survey[0]:
           # return "reference"
            parent_survey= all_survey[0]['referenced']['$oid']
            
            # parent_survey= HashId.decode(parent_survey)
            s= DataSort(parent_survey,uuid,aggregate)
            survey_data= s.get_uuid_label()

        else:
            survey_data= lol.get_uuid_label()
        j_data= d(survey_data)
        #return j_data
        # Get Responses for  a cid
        response_data= lol.get_data()
        #return j_data['field_options']['options'][0]['label']
        # 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:
            return "error"

        #Response Count
        temp= []
    
        for i in range(len(response_data)):
            temp.append(response_data[i]['responses'][uuid])



        #eturn j_data['field_type']
        options_count={}
        if j_data['field_type'] not in ["ranking","rating","group_rating"]:
            for i in temp:
                if i in options_count:pass
                else:options_count[i]= temp.count(i)

        elif j_data['field_type'] in ["ranking"]:
            for i in temp:
                aTempList= i.split("###")
                for j in aTempList:
                    bTempList= j.split("##")
                    l = bTempList[0]
                    if l in options_count:

                        options_count[l]= int(options_count[l])+len(aTempList)-int(bTempList[1])
                    else:
                        options_count[l]=len(aTempList)-int(bTempList[1])

        # elif j_data['field_type']=="rating":
        #   for i in temp:
        #       aTempList= i.split("###") #Check if this breaks the logic
        #       for j in aTempList:
        #           bTempList= j.split("##")
        #           if j in options_count:
        #               options_count[j]= int(options_count[j])+ int(bTempList[1])
        #           else:
        #               options_count[j]= int(bTempList[1])
        elif j_data['field_type']=="group_rating":
            for i in temp:
                aTempList= i.split("###")
                #options_count={}

                for j in aTempList:
                    bTempList= j.split("##")
                    l = bTempList[0]

                    #o_c= {a_1:}
                    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:
                if str(i) in options_count:
                    options_count[str(i)] +=1
                else:
                    options_count[str(i)]=1
                # if int(i)>6:
                #   if "above_5" in options_count:
                #       options_count["above_5"]= options_count["above_5"]+1
                #   else:
                #       options_count["above_5"]=1
                # elif int(i)<6 and int(i)>3 :
                #   if "above_3" in options_count:
                #       options_count["above_3"]= options_count["above_3"]+1
                #   else:
                #       options_count["above_3"]=1
                # elif int(i)<=3:
                #   if "below_3" in options_count:
                #       options_count["below_3"]= options_count["below_3"]+1
                #   else:
                #       options_count["below_3"]=1


        # elif j_data['field_type']=="short_text":
        #     return "lol"

        response= {}
        if j_data['field_type']=="rating":
            avg= 0
            for i in temp: avg= avg + int(i)
            response['avg_rating']= round(float(avg)/float(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)

                # avg[key]=float(sum(options_count[key].values()))/float(len(temp))
            response['avg_rating']=avg

        response['cid']= uuid
        # survey_id= j_data['survey_id']
        response['survey_id']=survey_id
        response['label']=j_data['label']
        response['type']=j_data['field_type']
        response['option_code']=option_code
        response['option_count']=options_count
        #return option_code
        response['total_resp']=len(temp)
        response['garbage']= temp

        return d(response)
    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