示例#1
0
def video_module(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)

    geog_list = [None, 'COUNTRY', 'STATE', 'DISTRICT', 'BLOCK', 'VILLAGE']
    if (geog not in geog_list):
        raise Http404()

    tot_vid = run_query_raw(
        shared_sql.get_totals(geog, id, from_date, to_date, partners,
                              "tot_vid"))[0][0]
    tot_vid = 0 if tot_vid is None else tot_vid
    tot_vids_screened = run_query(
        video_analytics_sql.video_tot_scr(geog=geog,
                                          id=id,
                                          from_date=from_date,
                                          to_date=to_date,
                                          partners=partners))[0]['count']
    #    prod_duration_ls = map(lambda x: x[0], run_query_raw(video_analytics_sql.video_prod_duration(geog=geog,id=id,from_date=from_date,to_date=to_date,partners=partners)))
    #    tot_avg =  float(sum(prod_duration_ls))/len(prod_duration_ls) if prod_duration_ls else 0
    search_box_params = views.common.get_search_box(request)

    get_req_url = request.META['QUERY_STRING']
    get_req_url = '&'.join([
        i for i in get_req_url.split('&') if i[:4] != 'geog' and i[:2] != 'id'
    ])

    return render(request, 'video_module.html',dict(search_box_params = search_box_params,\
                                                          tot_video=tot_vid,\
                                                          tot_vids_screened=tot_vids_screened, \
                                                          #tot_average= tot_avg, \
                                                          get_req_url = get_req_url
                                                          ))
示例#2
0
def adoption_module(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)
    
    geog_list = [None,'COUNTRY','STATE','DISTRICT','BLOCK','VILLAGE']
    if(geog not in geog_list):
        raise Http404()
    
    totals = run_query(shared_sql.get_totals(geog, id, from_date, to_date, partners, values_to_fetch=['tot_att', 'tot_ado', 'tot_scr']))[0]
    #total adoptions, total distinct practice adopted, distinct farmer adopting
    main_stats = run_query(adoption_analytics_sql.adoption_tot_ado(geog, id, from_date, to_date, partners))[0]
    main_stats['tot_ado'] = totals['tot_ado'] if totals['tot_ado'] is not None else 0
    
    #Adoption rate
    date_var = to_date if to_date else str(datetime.date.today())
    adopt_rate_data = run_query(shared_sql.adoption_rate_totals(geog, id, date_var, partners))[0]
    if(adopt_rate_data and adopt_rate_data['tot_per']):
        main_stats.update(adopt_rate = (adopt_rate_data['tot_adopt_per']*100)/adopt_rate_data['tot_per'])
        main_stats.update(avg_ado_per_farmer = adopt_rate_data['tot_active_adop'] / adopt_rate_data['tot_per'])
    else:
        main_stats.update(adopt_rate = 0)
        main_stats.update(avg_ado_per_farmer = 0)
        
    #Probability of Adoption
    if(totals['tot_att'] and main_stats['tot_ado']):
        main_stats.update(adopt_prob = float(main_stats['tot_ado'])/float(totals['tot_att']) * 100)
    else:
        main_stats.update(adopt_prob = 0)
    
    #Number of practices repeated adopted by same farmer
    repeat_pract_per = run_query_raw(adoption_analytics_sql.adoption_repeat_adoption_practice_count(geog, id, from_date, to_date, partners))
    if repeat_pract_per != None and main_stats['tot_farmers']:
        main_stats.update(repeat_pract = float(repeat_pract_per[0][0] * 100)/main_stats['tot_farmers'])
    else:
        main_stats.update(repeat_pract = 0)
        
    #Avg adoption per Video
    tot_vids_seen = run_query(video_analytics_sql.video_tot_scr(geog=geog,id=id,from_date=from_date,to_date=to_date,partners=partners))[0]['count']
    if tot_vids_seen and main_stats['tot_ado']:
        main_stats.update(avg_ado_per_vid = float(main_stats['tot_ado']) / tot_vids_seen)
    else:
        main_stats.update(avg_ado_per_vid = 0)
        
    #Avg adoption per Screening
    if(totals['tot_scr'] and main_stats['tot_ado']):
        main_stats.update(avg_ado_per_scr = float(main_stats['tot_ado']) / float(totals['tot_scr']))
    else:
        main_stats.update(avg_ado_per_scr = 0)
    
    search_box_params = views.common.get_search_box(request)

    get_req_url = request.META['QUERY_STRING']
    get_req_url = '&'.join([i for i in get_req_url.split('&') if i[:4]!='geog' and i[:2]!='id'])

    return render_to_response('adoption_module.html', dict(search_box_params = search_box_params,
                                                          get_req_url = get_req_url,
                                                          **main_stats
                                                          ))
def adoption_module(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)
    
    geog_list = [None,'COUNTRY','STATE','DISTRICT','BLOCK','VILLAGE']
    if(geog not in geog_list):
        raise Http404()
    
    totals = run_query(shared_sql.get_totals(geog, id, from_date, to_date, partners, values_to_fetch=['tot_att', 'tot_ado', 'tot_scr']))[0]
    #total adoptions, total distinct practice adopted, distinct farmer adopting
    main_stats = run_query(adoption_analytics_sql.adoption_tot_ado(geog, id, from_date, to_date, partners))[0]
    main_stats['tot_ado'] = totals['tot_ado'] if totals['tot_ado'] is not None else 0
    
    #Adoption rate
    date_var = to_date if to_date else (datetime.datetime.utcnow() - datetime.timedelta(1)).strftime('%Y-%m-%d')
    adopt_rate_data = run_query(shared_sql.adoption_rate_totals(geog, id, date_var, partners))[0]
    if(adopt_rate_data and adopt_rate_data['tot_per']):
        main_stats.update(adopt_rate = (adopt_rate_data['tot_adopt_per']*100)/adopt_rate_data['tot_per'])
        main_stats.update(avg_ado_per_farmer = adopt_rate_data['tot_active_adop'] / adopt_rate_data['tot_per'])
    else:
        main_stats.update(adopt_rate = 0)
        main_stats.update(avg_ado_per_farmer = 0)
        
    #Probability of Adoption
    if(totals['tot_att'] and main_stats['tot_ado']):
        main_stats.update(adopt_prob = float(main_stats['tot_ado'])/float(totals['tot_att']) * 100)
    else:
        main_stats.update(adopt_prob = 0)
    
    #Number of practices repeated adopted by same farmer
    repeat_pract_per = run_query_raw(adoption_analytics_sql.adoption_repeat_adoption_practice_count(geog, id, from_date, to_date, partners))
    if repeat_pract_per != None and main_stats['tot_farmers']:
        main_stats.update(repeat_pract = float(repeat_pract_per[0][0] * 100)/main_stats['tot_farmers'])
    else:
        main_stats.update(repeat_pract = 0)
        
    #Avg adoption per Video
    tot_vids_seen = run_query(video_analytics_sql.video_tot_scr(geog=geog,id=id,from_date=from_date,to_date=to_date,partners=partners))[0]['count']
    if tot_vids_seen and main_stats['tot_ado']:
        main_stats.update(avg_ado_per_vid = float(main_stats['tot_ado']) / tot_vids_seen)
    else:
        main_stats.update(avg_ado_per_vid = 0)
        
    #Avg adoption per Screening
    if(totals['tot_scr'] and main_stats['tot_ado']):
        main_stats.update(avg_ado_per_scr = float(main_stats['tot_ado']) / float(totals['tot_scr']))
    else:
        main_stats.update(avg_ado_per_scr = 0)
    
    search_box_params = views.common.get_search_box(request)

    get_req_url = request.META['QUERY_STRING']
    get_req_url = '&'.join([i for i in get_req_url.split('&') if i[:4]!='geog' and i[:2]!='id'])

    return render_to_response('adoption_module.html', dict(search_box_params = search_box_params,
                                                          get_req_url = get_req_url,
                                                          **main_stats
                                                          ))
示例#4
0
def video_module(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)
    
    geog_list = [None, 'COUNTRY','STATE','DISTRICT','BLOCK','VILLAGE']
    if(geog not in geog_list):
        raise Http404()

    tot_vid = run_query_raw(shared_sql.get_totals(geog, id, from_date, to_date, partners, "tot_vid"))[0][0];
    tot_vid = 0 if tot_vid is None else tot_vid 
    tot_vids_screened = run_query(video_analytics_sql.video_tot_scr(geog=geog,id=id,from_date=from_date,to_date=to_date,partners=partners))[0]['count']
    prod_duration_ls = map(lambda x: x[0], run_query_raw(video_analytics_sql.video_prod_duration(geog=geog,id=id,from_date=from_date,to_date=to_date,partners=partners)))
    tot_avg =  float(sum(prod_duration_ls))/len(prod_duration_ls) if prod_duration_ls else 0
    search_box_params = views.common.get_search_box(request)

    get_req_url = request.META['QUERY_STRING']
    get_req_url = '&'.join([i for i in get_req_url.split('&') if i[:4]!='geog' and i[:2]!='id'])

    return render_to_response('video_module.html',dict(search_box_params = search_box_params,\
                                                          tot_video=tot_vid,\
                                                          tot_vids_screened=tot_vids_screened, \
                                                          tot_average= tot_avg, \
                                                          get_req_url = get_req_url
                                                          ))
示例#5
0
def overview_module(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)
    geog_list = [None,'COUNTRY','STATE','DISTRICT','BLOCK','VILLAGE']
    if(geog is not None and geog not in geog_list):
        raise Http404()
    geog_child = geog_par = "NULL"
    if geog != None:
        geog_par = geog_list[geog_list.index(geog)-1]
    if geog != "VILLAGE":
        geog_child = geog_list[geog_list.index(geog)+1]

    #Constructing table data
    vid_prod = run_query_dict(shared_sql.overview(type='production',geog=geog,id=id, from_date = from_date, to_date=to_date, partners=partners),'id');
    vid_screening = run_query_dict(shared_sql.overview(type='screening',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    adoption = run_query_dict(shared_sql.overview(type='adoption',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    tot_prac = run_query_dict(shared_sql.overview(type='practice',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    tot_per = run_query_dict(shared_sql.overview(type='person',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    tot_vil = run_query_dict(shared_sql.overview(type='village',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');

    #Merging all dictionaries (vid_prod, tot_prac, etc) into one big one 'table_data'
    table_data = run_query(shared_sql.child_geog_list(geog=geog,id=id, from_date = from_date, to_date=to_date))
    for i in table_data:
        if i['id'] in vid_prod:
            i['tot_pro'] = vid_prod[i['id']][0]
        else:
            i['tot_pro'] = 0

        if i['id'] in adoption:
            i['tot_ado'] = adoption[i['id']][0]
        else:
            i['tot_ado'] = 0

        if i['id'] in vid_screening:
            i['tot_scr'] = vid_screening[i['id']][0]
        else:
            i['tot_scr'] = 0

        if i['id'] in tot_prac:
            i['tot_pra'] = tot_prac[i['id']][0]
        else:
            i['tot_pra'] = 0

        if i['id'] in tot_per:
            i['tot_per'] = tot_per[i['id']][0]
        else:
            i['tot_per'] = 0

        if i['id'] in tot_vil:
            i['tot_vil'] = tot_vil[i['id']][0]
        else:
            i['tot_vil'] = 0

        i['geog'] =  geog_child


    #par_geog is summed data in the below table
    par_geog_data = {}
    par_geog_data['tot_vid'] = reduce(lambda x,y: x+y, map(lambda x: x[0], vid_prod.values()), 0)
    par_geog_data['tot_scr'] = reduce(lambda x,y: x+y, map(lambda x: x[0], vid_screening.values()), 0)
    par_geog_data['tot_per'] = reduce(lambda x,y: x+y, map(lambda x: x[0], tot_per.values()), 0)
    par_geog_data['tot_pra'] = reduce(lambda x,y: x+y, map(lambda x: x[0], tot_prac.values()), 0)
    par_geog_data['tot_vil'] = reduce(lambda x,y: x+y, map(lambda x: x[0], tot_vil.values()), 0)
    par_geog_data['tot_ado'] = reduce(lambda x,y: x+y, map(lambda x: x[0], adoption.values()), 0)
    par_geog_data['name'], par_geog_data['id'] = get_parent_geog_id(geog, id)
    par_geog_data['geog'] = geog_par


    #country data is the top-data
    country_data = {}
    #Total Person Group
    country_data.update(run_query(overview_analytics_sql.overview_tot_pg(geog, id, from_date, to_date, partners))[0])

    if(to_date):
        date_var = to_date
    else:
        date_var = (datetime.datetime.utcnow() - datetime.timedelta(1)).strftime('%Y-%m-%d')
    #Operational Village (Last 60 days)
    country_data.update(vil_oper = run_query(targets_sql.get_village_operational(geog, id, date_var, partners))[0]['count'])
    tot_val = views.screening_analytics.get_dist_attendees_avg_att_avg_sc(geog, id, from_date, to_date, partners, ['avg_sc_per_day', 'avg_att_per_sc']);
    #Average attendance
    #Average Screening
    country_data.update(avg_att = tot_val['avg_att_per_sc'])
    country_data.update(avg_scr = tot_val['avg_sc_per_day'])

    #Adoption Rate
    country_data.update(adopt_rate = views.adoption_analytics.adoption_rate(geog, id, to_date, partners))
    #Distinct videos screened
    country_data.update(vid_screened = run_query(video_analytics_sql.video_tot_scr(geog=geog,id=id,from_date=from_date,to_date=to_date,partners=partners))[0]['count'])


    #search box params are the parameters for the search box i.e. dates, geography drop-down and partners if any
    search_box_params = views.common.get_search_box(request)

    get_req_url = request.META['QUERY_STRING']
    # print get_req_url
    get_req_url = '&'.join([i for i in get_req_url.split('&') if i[:4]!='geog' and i[:2]!='id'])
    if(geog_child != "NULL"):
        header_geog = geog_child
    else:
        header_geog = "Village"

    return render(request, 'overview_module.html', dict(search_box_params = search_box_params, \
                                                           country_data = country_data, \
                                                           table_data = table_data, \
                                                           par_geog_data = par_geog_data, \
                                                           get_req_url = get_req_url, \
                                                           header_geog = header_geog \
                                                           ))
示例#6
0
def overview_module(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)
    geog_list = [None,'COUNTRY','STATE','DISTRICT','BLOCK','VILLAGE']
    if(geog is not None and geog not in geog_list):
        raise Http404()
    geog_child = geog_par = "NULL"
    if geog != None:
        geog_par = geog_list[geog_list.index(geog)-1]
    if geog != "VILLAGE":
        geog_child = geog_list[geog_list.index(geog)+1]

    #Constructing table data
    vid_prod = run_query_dict(shared_sql.overview(type='production',geog=geog,id=id, from_date = from_date, to_date=to_date, partners=partners),'id');
    vid_screening = run_query_dict(shared_sql.overview(type='screening',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    adoption = run_query_dict(shared_sql.overview(type='adoption',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    tot_prac = run_query_dict(shared_sql.overview(type='practice',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    tot_per = run_query_dict(shared_sql.overview(type='person',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');
    tot_vil = run_query_dict(shared_sql.overview(type='village',geog=geog,id=id,from_date = from_date, to_date=to_date, partners=partners),'id');

    #Merging all dictionaries (vid_prod, tot_prac, etc) into one big one 'table_data'
    table_data = run_query(shared_sql.child_geog_list(geog=geog,id=id, from_date = from_date, to_date=to_date, partners=partners))
    for i in table_data:
        if i['id'] in vid_prod:
            i['tot_pro'] = vid_prod[i['id']][0]
        else:
            i['tot_pro'] = 0

        if i['id'] in adoption:
            i['tot_ado'] = adoption[i['id']][0]
        else:
            i['tot_ado'] = 0

        if i['id'] in vid_screening:
            i['tot_scr'] = vid_screening[i['id']][0]
        else:
            i['tot_scr'] = 0

        if i['id'] in tot_prac:
            i['tot_pra'] = tot_prac[i['id']][0]
        else:
            i['tot_pra'] = 0

        if i['id'] in tot_per:
            i['tot_per'] = tot_per[i['id']][0]
        else:
            i['tot_per'] = 0
            
        if i['id'] in tot_vil:
            i['tot_vil'] = tot_vil[i['id']][0]
        else:
            i['tot_vil'] = 0

        i['geog'] =  geog_child


    #par_geog is summed data in the below table
    par_geog_data = {}
    par_geog_data['tot_vid'] = reduce(lambda x,y: x+y, map(lambda x: x[0], vid_prod.values()), 0)
    par_geog_data['tot_scr'] = reduce(lambda x,y: x+y, map(lambda x: x[0], vid_screening.values()), 0)
    par_geog_data['tot_per'] = reduce(lambda x,y: x+y, map(lambda x: x[0], tot_per.values()), 0)
    par_geog_data['tot_pra'] = reduce(lambda x,y: x+y, map(lambda x: x[0], tot_prac.values()), 0)
    par_geog_data['tot_vil'] = reduce(lambda x,y: x+y, map(lambda x: x[0], tot_vil.values()), 0)
    par_geog_data['tot_ado'] = reduce(lambda x,y: x+y, map(lambda x: x[0], adoption.values()), 0)
    par_geog_data['name'], par_geog_data['id'] = get_parent_geog_id(geog, id)
    par_geog_data['geog'] = geog_par


    #country data is the top-data    
    country_data = {}
    #Total Person Group
    country_data.update(run_query(overview_analytics_sql.overview_tot_pg(geog, id, from_date, to_date, partners))[0])
    
    if(to_date):
        date_var = to_date
    else:
        date_var = (datetime.datetime.utcnow() - datetime.timedelta(1)).strftime('%Y-%m-%d')
    #Operational Village (Last 60 days)
    country_data.update(vil_oper = run_query(targets_sql.get_village_operational(geog, id, date_var, partners))[0]['count'])
    tot_val = views.screening_analytics.get_dist_attendees_avg_att_avg_sc(geog, id, from_date, to_date, partners, ['avg_sc_per_day', 'avg_att_per_sc']);
    #Average attendance 
    #Average Screening
    country_data.update(avg_att = tot_val['avg_att_per_sc'])
    country_data.update(avg_scr = tot_val['avg_sc_per_day'])
    
    #Adoption Rate
    country_data.update(adopt_rate = views.adoption_analytics.adoption_rate(geog, id, to_date, partners))
    #Distinct videos screened
    country_data.update(vid_screened = run_query(video_analytics_sql.video_tot_scr(geog=geog,id=id,from_date=from_date,to_date=to_date,partners=partners))[0]['count'])
    

    #search box params are the parameters for the search box i.e. dates, geography drop-down and partners if any
    search_box_params = views.common.get_search_box(request)

    get_req_url = request.META['QUERY_STRING']
    print get_req_url
    get_req_url = '&'.join([i for i in get_req_url.split('&') if i[:4]!='geog' and i[:2]!='id'])
    if(geog_child != "NULL"):
        header_geog = geog_child
    else:
        header_geog = "Village"
    
    return render_to_response('overview_module.html', dict(search_box_params = search_box_params, \
                                                           country_data = country_data, \
                                                           table_data = table_data, \
                                                           par_geog_data = par_geog_data, \
                                                           get_req_url = get_req_url, \
                                                           header_geog = header_geog \
                                                           ))