Beispiel #1
0
def screening_geog_pie_data(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)
    geog_list = [None, 'COUNTRY','STATE','DISTRICT','BLOCK','VILLAGE', 'DUMMY']
    if(geog not in geog_list[:-1]):
        raise Http404()
    
    get_req_url = request.META['QUERY_STRING']
    get_req_url = [i for i in get_req_url.split('&') if i[:4]!='geog' and i[:2]!='id']
    get_req_url.append("geog="+geog_list[geog_list.index(geog)+1].lower())

    url = "/analytics/screening_module?"

    scr_geog = run_query(shared_sql.overview(geog,id, from_date, to_date, partners,'screening'))
    geog_name = run_query_dict(shared_sql.child_geog_list(geog,id, from_date, to_date),'id')
    
    return_val = []
    return_val.append(['title','value','url'])
    for item in scr_geog:
        if(geog is None or geog.upper()!= "VILLAGE"):
            temp_get_req_url = get_req_url[:]
            temp_get_req_url.append("id="+str(item['id']))
            return_val.append([geog_name[item['id']][0], float(item['tot_scr']), url+'&'.join(temp_get_req_url)])
        else:
            return_val.append([geog_name[item['id']][0], float(item['tot_scr']), ''])       
    return HttpResponse(json.dumps(return_val))
def adoption_geog_pie_data(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)
    geog_list = [None,'COUNTRY','STATE','DISTRICT','BLOCK','VILLAGE', 'DUMMY']
    if(geog not in geog_list[:-1]):
        raise Http404()
    
    get_req_url = request.META['QUERY_STRING']
    get_req_url = [i for i in get_req_url.split('&') if i[:4]!='geog' and i[:2]!='id']
    get_req_url.append("geog="+geog_list[geog_list.index(geog)+1].lower())

    url = "/analytics/adoption_module?"


    ado_prod = run_query(shared_sql.overview(geog,id, from_date, to_date, partners, 'adoption'))
    geog_name = run_query_dict(shared_sql.child_geog_list(geog, id, from_date, to_date),'id')

    return_val = []
    return_val.append(['name','value','url'])
    for item in ado_prod:
        if(geog is None or geog.upper()!= "VILLAGE"):
            temp_get_req_url = get_req_url[:]
            temp_get_req_url.append("id="+str(item['id']))
            return_val.append([geog_name[item['id']][0], float(item['tot_ado']) ,url+'&'.join(temp_get_req_url)])
        else:
            return_val.append([geog_name[item['id']][0], float(item['tot_ado']), ''])

    return HttpResponse(json.dumps(return_val))
Beispiel #3
0
def pie_chart_data(sqlFunc, pieNameDict, desc, **args):
    rs = run_query_dict(sqlFunc(**args), 'pie_key')
    str_list = [['Gender', 'value']]
    if not rs:
        return HttpResponse(json.dumps(str_list))

    for key, value in pieNameDict.iteritems():
        if (key in rs):
            str_list.append([value, rs[key][0]])
        else:
            str_list.append([value, 0])

    return HttpResponse(json.dumps(str_list))
Beispiel #4
0
def pie_chart_data(sqlFunc,pieNameDict, desc, **args):
    rs = run_query_dict(sqlFunc(**args),'pie_key')
    str_list = [['Gender','value']]
    if not rs:
        return HttpResponse(json.dumps(str_list))
    
    for key, value in pieNameDict.iteritems():
        if(key in rs):
            str_list.append([value,rs[key][0]])
        else:
            str_list.append([value,0])
            
    return HttpResponse(json.dumps(str_list))
Beispiel #5
0
def overview_line_graph(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)

    if ('type' in request.GET):
        graph_type = request.GET.getlist('type')
    else:
        graph_type = ['prod', 'screen', 'prac', 'person', 'adopt']

    if ('prod' in graph_type):
        vid_prod_rs = run_query_dict(
            shared_sql.overview_line_chart(type='production',
                                           geog=geog,
                                           id=id,
                                           from_date=from_date,
                                           to_date=to_date,
                                           partners=partners), 'date')
    else:
        vid_prod_rs = []

    if ('screen' in graph_type):
        sc_rs = run_query_dict(
            shared_sql.overview_line_chart(type='screening',
                                           geog=geog,
                                           id=id,
                                           from_date=from_date,
                                           to_date=to_date,
                                           partners=partners), 'date')
    else:
        sc_rs = []

    if ('adopt' in graph_type):
        adopt_rs = run_query_dict(
            shared_sql.overview_line_chart(type='adoption',
                                           geog=geog,
                                           id=id,
                                           from_date=from_date,
                                           to_date=to_date,
                                           partners=partners), 'date')
    else:
        adopt_rs = []

    if ('prac' in graph_type):
        prac_rs = run_query_dict(
            shared_sql.overview_line_chart(type='practice',
                                           geog=geog,
                                           id=id,
                                           from_date=from_date,
                                           to_date=to_date,
                                           partners=partners), 'date')
    else:
        prac_rs = []

    if ('person' in graph_type):
        person_rs = run_query_dict(
            shared_sql.overview_line_chart(type='person',
                                           geog=geog,
                                           id=id,
                                           from_date=from_date,
                                           to_date=to_date,
                                           partners=partners), 'date')
    else:
        person_rs = []

    start_date = today = datetime.date.today()
    if vid_prod_rs:
        start_date = min(start_date, *(vid_prod_rs.keys()))
    if sc_rs:
        start_date = min(start_date, *(sc_rs.keys()))
    if adopt_rs:
        start_date = min(start_date, *(adopt_rs.keys()))
    if prac_rs:
        start_date = min(start_date, *(prac_rs.keys()))
    if person_rs:
        start_date = min(start_date, *(person_rs.keys()))
    diff = (today - start_date).days

    str_list = []
    sum_vid = sum_sc = sum_adopt = sum_prac = sum_person = 0
    for i in range(0, diff + 1):
        iter_date = start_date + datetime.timedelta(days=i)

        if iter_date in vid_prod_rs:
            sum_vid += vid_prod_rs[iter_date][0]
        if iter_date in sc_rs:
            sum_sc += sc_rs[iter_date][0]
        if iter_date in adopt_rs:
            sum_adopt += adopt_rs[iter_date][0]
        if iter_date in prac_rs:
            sum_prac += prac_rs[iter_date][0]
        if iter_date in person_rs:
            sum_person += person_rs[iter_date][0]

        append_str = [str(iter_date)]
        if ('prod' in graph_type): append_str.append(float(sum_vid))
        if ('screen' in graph_type): append_str.append(float(sum_sc))
        if ('adopt' in graph_type): append_str.append(float(sum_adopt))
        if ('prac' in graph_type): append_str.append(float(sum_prac))
        if ('person' in graph_type): append_str.append(float(sum_person))

        str_list.append(append_str)

    #For settings
    header = ['date']
    if ('prod' in graph_type):
        header.append('Total videos produced')
    if ('screen' in graph_type):
        header.append('Total disseminations')
    if ('adopt' in graph_type):
        header.append('Total adoptions')
    if ('prac' in graph_type):

        header.append('Total practices')
    if ('person' in graph_type):
        header.append('Total viewers')

    str_list.insert(0, header)
    return HttpResponse(json.dumps(str_list))
Beispiel #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))
    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 \
                                                           ))
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 \
                                                           ))
Beispiel #8
0
def overview_line_graph(request):
    geog, id = get_geog_id(request)
    from_date, to_date, partners = get_dates_partners(request)

    if('type' in request.GET):
        graph_type = request.GET.getlist('type')
    else:
        graph_type = ['prod', 'screen', 'prac', 'person', 'adopt']

    if('prod' in graph_type):
        vid_prod_rs = run_query_dict(shared_sql.overview_line_chart(type='production',geog=geog,id=id, from_date=from_date, to_date=to_date, partners=partners),'date');
    else:
        vid_prod_rs = []

    if('screen' in graph_type):
        sc_rs = run_query_dict(shared_sql.overview_line_chart(type='screening',geog=geog,id=id, from_date=from_date, to_date=to_date, partners=partners),'date');
    else:
        sc_rs = []

    if('adopt' in graph_type):
        adopt_rs = run_query_dict(shared_sql.overview_line_chart(type='adoption',geog=geog,id=id, from_date=from_date, to_date=to_date, partners=partners),'date');
    else:
        adopt_rs = []

    if('prac' in graph_type):
        prac_rs = run_query_dict(shared_sql.overview_line_chart(type='practice',geog=geog,id=id, from_date=from_date, to_date=to_date, partners=partners),'date');
    else:
        prac_rs = []

    if('person' in graph_type):
        person_rs = run_query_dict(shared_sql.overview_line_chart(type='person',geog=geog,id=id, from_date=from_date, to_date=to_date, partners=partners),'date');
    else:
        person_rs = []
    

    start_date = today = datetime.date.today()
    if vid_prod_rs:
        start_date = min(start_date, *(vid_prod_rs.keys()))
    if sc_rs:
        start_date = min(start_date,*(sc_rs.keys()))
    if adopt_rs:
        start_date = min(start_date,*(adopt_rs.keys()))
    if prac_rs:
        start_date = min(start_date,*(prac_rs.keys()))
    if person_rs:
        start_date = min(start_date,*(person_rs.keys()))
    diff = (today - start_date).days

    str_list = []
    sum_vid = sum_sc = sum_adopt =sum_prac = sum_person = 0
    for i in range(0,diff+1):
        iter_date = start_date + datetime.timedelta(days=i)

        if iter_date in vid_prod_rs:
            sum_vid += vid_prod_rs[iter_date][0]
        if iter_date in sc_rs:
            sum_sc += sc_rs[iter_date][0]
        if iter_date in adopt_rs:
            sum_adopt += adopt_rs[iter_date][0]
        if iter_date in prac_rs:
            sum_prac += prac_rs[iter_date][0]
        if iter_date in person_rs:
            sum_person += person_rs[iter_date][0]

        append_str = [str(iter_date)]
        if('prod' in graph_type): append_str.append(float(sum_vid))
        if('screen' in graph_type): append_str.append(float(sum_sc))
        if('adopt' in graph_type): append_str.append(float(sum_adopt))
        if('prac' in graph_type): append_str.append(float(sum_prac))
        if('person' in graph_type): append_str.append(float(sum_person))

        str_list.append(append_str)

    #For settings
    header=['date']
    if('prod' in graph_type):
        header.append('Total videos produced')
    if('screen' in graph_type):
        header.append('Total disseminations')
    if('adopt' in graph_type):
        header.append('Total adoptions')
    if('prac' in graph_type):

        header.append('Total practices')
    if('person' in graph_type):
        header.append('Total viewers')

    str_list.insert(0,header)
    return HttpResponse(json.dumps(str_list))