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 ))
def scatter_chart_data(sqlFunc, **args): rs = run_query(sqlFunc(**args)) return_val = [['', '', '', '', 'Number']] if not rs: return HttpResponse(json.dumps([[]])) count_dict = {} for item in rs: if item['count'] in count_dict: count_dict[item['count']].append(item['name']) else: count_dict[item['count']] = [item['name']] x_axis_len = max([len(x) for x in count_dict.values()]) * 2 if (x_axis_len < 10): x_axis_len = 10 random.seed() for tot, pracs in count_dict.iteritems(): flag = [0] * x_axis_len for prac in pracs: x = random.randrange(1, x_axis_len) while (flag[x] != 0): x = random.randrange(1, x_axis_len) flag[x] = 1 return_val.append([prac, x, tot, "", tot]) 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))
def drop_down_val(request): if 'geog' in request.GET and request.GET['geog'] \ and 'id' in request.GET and request.GET['id']: geog = request.GET['geog'].lower() id = request.GET['id'] id = int(id) else: raise Http404() geog_list = ['country', 'state', 'district', 'block', 'village'] if geog == 'country': geog_parent = 'country' else: geog_parent = geog_list[geog_list.index(geog) - 1] html_option = """ <option value='-1'>Select {{geog|title}}</option> {% for row in rs %} <option value="{{row.id}}">{{row.name}}</option> {%endfor%} """ t = Template(html_option) rs = run_query( shared_sql.search_drop_down_list(geog=geog, geog_parent=geog_parent, id=id)) html = t.render(Context(dict(geog=geog, rs=rs))) return HttpResponse(html)
def screening_percent_lines(request): geog, id = get_geog_id(request) from_date, to_date, partners = get_dates_partners(request) rows = run_query( screening_analytics_sql.screening_percent_attendance( geog, id, from_date, to_date, partners)) return_val = [] for row in rows: if row['tot_exp_att'] == 0: rel_att = rel_exp_ado = 0 else: rel_att = (row['tot_per'] * 100) / row['tot_exp_att'] rel_exp_ado = (row['tot_ado'] * 100) / row['tot_exp_att'] if row['tot_per'] == 0: rel_exp_int = rel_exp_ques = 0 else: rel_exp_int = (row['tot_int'] * 100) / row['tot_per'] rel_exp_ques = (row['tot_que'] * 100) / row['tot_per'] return_val.append([str(row['date'])] + map(lambda x: round( x, 2), [rel_att, rel_exp_int, rel_exp_ado, rel_exp_ques])) return_val.insert(0, [ "Date", "Relative Attendance", "Relative Expressed Interest", "Relative Expressed Adoption", "Relative Expressed Question" ]) return HttpResponse(json.dumps(return_val))
def screening_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_val = get_dist_attendees_avg_att_avg_sc(geog, id, from_date, to_date, partners) adjusted_to_date = to_date if to_date else datetime.date.today() tot_active_vid_data = run_query(screening_analytics_sql.average_video_by_active_data(geog, id, from_date, adjusted_to_date, partners))[0] if tot_active_vid_data['tot_active_per']: avg_vid_by_active = tot_active_vid_data['tot_vid_by_active']/tot_active_vid_data['tot_active_per'] else: avg_vid_by_active = 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('screening_module.html',dict(search_box_params = search_box_params, tot_scr=tot_val['tot_scr'], tot_att=tot_val['dist_att'], avg_scr=tot_val['avg_sc_per_day'], avg_att=tot_val['avg_att_per_sc'], avg_vid_by_active = avg_vid_by_active, get_req_url = get_req_url ))
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 ))
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 practice_scatter_chart_data(sqlFunc, **args): rs = run_query(sqlFunc(**args)) if not rs: return HttpResponse(json.dumps([[]])); count_dict = {} for item in rs: if item['count'] in count_dict: count_dict[item['count']].append([item['name'],item['sec'],item['subsec'],item['top'],item['subtop'],item['sub']]) else: count_dict[item['count']] = [[item['name'],item['sec'],item['subsec'],item['top'],item['subtop'],item['sub']]] x_axis_len = max([len(x) for x in count_dict.values()]) * 2 if(x_axis_len<10): x_axis_len = 10; return_val = [['practice_name','Sector','Sub Sector','Topic','Sub Topic','Subject','','','','Number']] random.seed(); for tot, pracs_arr in count_dict.iteritems(): for pracs in pracs_arr: flag = [0] * x_axis_len x = random.randrange(1,x_axis_len) while(flag[x] != 0): x = random.randrange(1,x_axis_len) flag[x] = 1 return_val.append([pracs[0],pracs[1],pracs[2],pracs[3],pracs[4],pracs[5],x,tot,"",tot]) return HttpResponse(json.dumps(return_val))
def get_dist_attendees_avg_att_avg_sc(geog, id, from_date, to_date, partners, values_to_fetch=None): return_dict = {} sql_values_to_fetch = set() if values_to_fetch is None or 'avg_att_per_sc' in values_to_fetch: sql_values_to_fetch.update(["tot_scr", "tot_att"]) if values_to_fetch is None or 'avg_sc_per_day' in values_to_fetch: sql_values_to_fetch.add("tot_scr") if values_to_fetch is None or 'tot_scr' in values_to_fetch: sql_values_to_fetch.add('tot_scr') tot_val = run_query(shared_sql.get_totals(geog, id, from_date, to_date, partners, sql_values_to_fetch))[0]; if tot_val['tot_scr'] is None: tot_val['tot_scr'] = 0 if(values_to_fetch==None or 'dist_att' in values_to_fetch): query_result = run_query_raw(screening_analytics_sql.distinct_attendees(geog, id, from_date, to_date, partners))[0]; return_dict['dist_att'] = query_result[0] if query_result else 0 if(values_to_fetch==None or 'tot_scr' in values_to_fetch): return_dict['tot_scr'] = tot_val['tot_scr'] if(values_to_fetch==None or 'avg_att_per_sc' in values_to_fetch): return_dict['avg_att_per_sc'] = float(tot_val['tot_att'])/float(tot_val['tot_scr']) if tot_val['tot_scr'] else 0 if(values_to_fetch==None or 'avg_sc_per_day' in values_to_fetch): if from_date and to_date: tot_days = (datetime.date(*[int(i) for i in to_date.split('-')]) - datetime.date(*[int(i) for i in from_date.split('-')])).days + 1; else: from_date = run_query_raw(shared_sql.get_start_date(geog, id))[0][0] if not from_date: from_date = run_query_raw(shared_sql.calculate_start_date(geog, id))[0][0] if not from_date: from_date = datetime.date.today() tot_days = (datetime.date.today() - from_date).days + 1 return_dict['avg_sc_per_day'] = float(tot_val['tot_scr'])/tot_days if tot_days and tot_val['tot_scr'] else 0 return return_dict
def screening_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_val = get_dist_attendees_avg_att_avg_sc(geog, id, from_date, to_date, partners) adjusted_to_date = to_date if to_date else datetime.date.today() tot_active_vid_data = run_query( screening_analytics_sql.average_video_by_active_data( geog, id, from_date, adjusted_to_date, partners))[0] if tot_active_vid_data['tot_active_per']: avg_vid_by_active = tot_active_vid_data[ 'tot_vid_by_active'] / tot_active_vid_data['tot_active_per'] else: avg_vid_by_active = 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( 'screening_module.html', dict(search_box_params=search_box_params, tot_scr=tot_val['tot_scr'], tot_att=tot_val['dist_att'], avg_scr=tot_val['avg_sc_per_day'], avg_att=tot_val['avg_att_per_sc'], avg_vid_by_active=avg_vid_by_active, get_req_url=get_req_url))
def scatter_chart_data(sqlFunc, **args): rs = run_query(sqlFunc(**args)) return_val = [['','','','','Number']] if not rs: return HttpResponse(json.dumps([[]])); count_dict = {} for item in rs: if item['count'] in count_dict: count_dict[item['count']].append(item['name']) else: count_dict[item['count']] = [item['name']] x_axis_len = max([len(x) for x in count_dict.values()]) * 2 if(x_axis_len<10): x_axis_len = 10; random.seed(); for tot,pracs in count_dict.iteritems(): flag = [0] * x_axis_len for prac in pracs: x = random.randrange(1,x_axis_len) while(flag[x] != 0): x = random.randrange(1,x_axis_len) flag[x] = 1 return_val.append([prac,x,tot,"",tot]) return HttpResponse(json.dumps(return_val))
def drop_down_val(request): if 'geog' in request.GET and request.GET['geog'] \ and 'id' in request.GET and request.GET['id']: geog = request.GET['geog'].lower() id = request.GET['id'] id = int(id) else: raise Http404() geog_list = ['country','state','district','block','village'] if geog=='country': geog_parent = 'country' else: geog_parent = geog_list[geog_list.index(geog)-1] html_option = """ <option value='-1'>Select {{geog|title}}</option> {% for row in rs %} <option value="{{row.id}}">{{row.name}}</option> {%endfor%} """ t = Template(html_option); rs = run_query(shared_sql.search_drop_down_list(geog=geog,geog_parent=geog_parent,id=id)); html = t.render(Context(dict(geog=geog,rs=rs))) return HttpResponse(html)
def adoption_rate(geog, id, to_date, partners): if not to_date: to_date = datetime.date.today() adopt_rate_data = run_query(shared_sql.adoption_rate_totals(geog, id, to_date, partners))[0] if(adopt_rate_data and adopt_rate_data['tot_per']): return (adopt_rate_data['tot_adopt_per']*100)/adopt_rate_data['tot_per'] else: return 0
def get_dist_attendees_avg_att_avg_sc(geog, id, from_date, to_date, partners, values_to_fetch=None): return_dict = {} sql_values_to_fetch = set() if values_to_fetch is None or 'avg_att_per_sc' in values_to_fetch: sql_values_to_fetch.update(["tot_scr", "tot_att"]) if values_to_fetch is None or 'avg_sc_per_day' in values_to_fetch: sql_values_to_fetch.add("tot_scr") if values_to_fetch is None or 'tot_scr' in values_to_fetch: sql_values_to_fetch.add('tot_scr') tot_val = run_query( shared_sql.get_totals(geog, id, from_date, to_date, partners, sql_values_to_fetch))[0] if tot_val['tot_scr'] is None: tot_val['tot_scr'] = 0 if (values_to_fetch == None or 'dist_att' in values_to_fetch): query_result = run_query_raw( screening_analytics_sql.distinct_attendees(geog, id, from_date, to_date, partners))[0] return_dict['dist_att'] = query_result[0] if query_result else 0 if (values_to_fetch == None or 'tot_scr' in values_to_fetch): return_dict['tot_scr'] = tot_val['tot_scr'] if (values_to_fetch == None or 'avg_att_per_sc' in values_to_fetch): return_dict['avg_att_per_sc'] = float(tot_val['tot_att']) / float( tot_val['tot_scr']) if tot_val['tot_scr'] else 0 if (values_to_fetch == None or 'avg_sc_per_day' in values_to_fetch): if from_date and to_date: tot_days = ( datetime.date(*[int(i) for i in to_date.split('-')]) - datetime.date(*[int(i) for i in from_date.split('-')])).days + 1 else: from_date = run_query_raw(shared_sql.get_start_date(geog, id))[0][0] if not from_date: from_date = run_query_raw( shared_sql.caculate_start_date(geog, id))[0][0] if not from_date: from_date = datetime.date.today() tot_days = (datetime.date.today() - from_date).days + 1 return_dict['avg_sc_per_day'] = float( tot_val['tot_scr'] ) / tot_days if tot_days and tot_val['tot_scr'] else 0 return return_dict
def get_partner_list(geog,id, partners): sql = shared_sql.get_partners_sql(geog,id) if(sql): part_list = run_query(sql) if(not partners or len(part_list) == 1): return part_list filtered_partners = [x['id'] for x in part_list if str(x['id']) in partners] if not filtered_partners: return part_list for partner in part_list: if partner['id'] not in filtered_partners: partner['unmarked'] = 1 return part_list else: return {}
def get_partner_list(geog, id, partners): sql = shared_sql.get_partners_sql(geog, id) if (sql): part_list = run_query(sql) if (not partners or len(part_list) == 1): return part_list filtered_partners = [ x['id'] for x in part_list if str(x['id']) in partners ] if not filtered_partners: return part_list for partner in part_list: if partner['id'] not in filtered_partners: partner['unmarked'] = 1 return part_list else: return {}
def month_bar_data(sqlFunc, setting_from_date, setting_to_date, **args): rs = run_query(sqlFunc(**args)) if rs: min = int(rs[0]['YEAR']) max = int(rs[-1]['YEAR']) + 1 dic = {} for y in range(min, max): dic[y] = {} for item in rs: if int(item['YEAR']) > y: break if int(item['YEAR']) == y: dic[y][int(item['MONTH'])] = item['count'] else: return HttpResponse(json.dumps([['Name', 'Value']])) if (not (setting_from_date and setting_to_date)): setting_from_date = str(rs[0]['YEAR']) + '-' + str( rs[0]['MONTH']) + '-01' setting_to_date = (datetime.datetime.utcnow() - datetime.timedelta(1)).strftime('%Y-%m-%d') setting_from_date = MyDate( *[int(x) for x in reversed(setting_from_date.split('-')[:2])]) setting_to_date = MyDate( *[int(x) for x in reversed(setting_to_date.split('-')[:2])]) data = [['Jan'], ['Feb'], ['Mar'], ['Apr'], ['May'], ['Jun'], ['Jul'], ['Aug'], ['Sep'], ['Oct'], ['Nov'], ['Dec']] if (setting_from_date.y != setting_to_date.y): loop_from = MyDate(1, setting_from_date.y) loop_to = MyDate(12, setting_to_date.y) else: loop_from = MyDate(setting_from_date.m, setting_from_date.y) loop_to = MyDate(setting_to_date.m, setting_to_date.y) while (loop_from <= loop_to): value = dic[loop_from.y][ loop_from. m] if loop_from.y in dic and loop_from.m in dic[loop_from.y] else 0 data[loop_from.m - 1].append(float(value)) loop_from.addMonth(1) header = ["Month"] + map(str, range(setting_from_date.y, setting_to_date.y + 1)) return HttpResponse( json.dumps([header] + filter(lambda x: len(x) > 1, data)))
def get_partner_list(geog, id, partners): sql = shared_sql.get_partners_sql(geog, id) if(sql): part_list = run_query(sql) filtered_partners = [x['partner_id'] for x in part_list] coco_partners = Partner.objects.values_list('id', 'partner_name', 'full_partner_name').order_by('partner_name') partner_list = [] for id, partner_name, full_partner_name in coco_partners: partner_dict = {'partner_id': id, 'PARTNER_NAME': partner_name, 'FULL_PARTNER_NAME': full_partner_name} if partners: if str(id) not in partners: partner_dict['unmarked'] = 1 if id in filtered_partners: partner_dict['highlight'] = 1 partner_list.append(partner_dict) return partner_list else: return {}
def get_partner_list(geog, id, partners): sql = shared_sql.get_partners_sql(geog, id) if(sql): part_list = run_query(sql) filtered_partners = [x['partner_id'] for x in part_list] coco_partners = Partner.objects.values_list('id', 'partner_name').order_by('partner_name') partner_list = [] for id, partner_name in coco_partners: partner_dict = {'partner_id': id, 'PARTNER_NAME': partner_name} if partners: if str(id) not in partners: partner_dict['unmarked'] = 1 if id in filtered_partners: partner_dict['highlight'] = 1 partner_list.append(partner_dict) return partner_list else: return {}
def screening_percent_lines(request): geog, id = get_geog_id(request); from_date, to_date, partners = get_dates_partners(request); rows = run_query(screening_analytics_sql.screening_percent_attendance(geog, id, from_date, to_date, partners)) return_val = [] for row in rows: if row['tot_exp_att'] == 0: rel_att = rel_exp_ado = 0 else: rel_att = (row['tot_per'] * 100)/row['tot_exp_att'] rel_exp_ado = (row['tot_ado'] * 100)/row['tot_exp_att'] if row['tot_per'] == 0: rel_exp_int = rel_exp_ques = 0 else: rel_exp_int = (row['tot_int'] * 100)/row['tot_per'] rel_exp_ques = (row['tot_que'] * 100)/row['tot_per'] return_val.append([str(row['date'])]+ map(lambda x: round(x, 2) ,[rel_att, rel_exp_int, rel_exp_ado, rel_exp_ques])) return_val.insert(0,["Date","Relative Attendance","Relative Expressed Interest","Relative Expressed Adoption","Relative Expressed Question"]) return HttpResponse(json.dumps(return_val))
def month_bar_data(sqlFunc, setting_from_date, setting_to_date, **args): rs = run_query(sqlFunc(**args)); if rs: min = int(rs[0]['YEAR']) max = int(rs[-1]['YEAR'])+1 dic = {} for y in range(min,max): dic[y] = {} for item in rs: if int(item['YEAR'])>y: break if int(item['YEAR'])==y: dic[y][int(item['MONTH'])] = item['count'] else: return HttpResponse(json.dumps([['Name','Value']])); if(not(setting_from_date and setting_to_date)): setting_from_date = str(rs[0]['YEAR'])+'-'+str(rs[0]['MONTH'])+'-01' setting_to_date = (datetime.datetime.utcnow() - datetime.timedelta(1)).strftime('%Y-%m-%d') setting_from_date = MyDate(* [int(x) for x in reversed(setting_from_date.split('-')[:2])]) setting_to_date = MyDate(* [int(x) for x in reversed(setting_to_date.split('-')[:2])]) data = [['Jan'],['Feb'],['Mar'],['Apr'],['May'],['Jun'],['Jul'],['Aug'],['Sep'],['Oct'],['Nov'],['Dec']] if(setting_from_date.y != setting_to_date.y): loop_from = MyDate(1,setting_from_date.y) loop_to = MyDate(12, setting_to_date.y) else: loop_from = MyDate(setting_from_date.m, setting_from_date.y); loop_to = MyDate(setting_to_date.m, setting_to_date.y); while(loop_from <= loop_to): value = dic[loop_from.y][loop_from.m] if loop_from.y in dic and loop_from.m in dic[loop_from.y] else 0 data[loop_from.m - 1].append(float(value)) loop_from.addMonth(1) header = ["Month"] + map(str,range(setting_from_date.y, setting_to_date.y + 1)) return HttpResponse(json.dumps([header] + filter(lambda x: len(x) > 1, data)))
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 ))
def target_table(request): geog, id = get_geog_id(request); from_date, to_date, partners = get_dates_partners(request) today_date = datetime.date.today() if(not from_date): from_date = str(datetime.date(today_date.year,today_date.month,1)) if(not to_date): to_date = str(datetime.date(today_date.year+(today_date.month+1)/12,(today_date.month+1)%12, 1) - datetime.timedelta(days=1)) if(datetime.date(*[int(i) for i in str(from_date).split('-')]) > datetime.date(*[int(i) for i in str(to_date).split('-')])): raise Http404; achieved_vals = {} achieved_vals['csp_identified'] = run_query(get_csp_identified(geog, id, from_date, to_date, partners))[0]['count'] vil_operation_from_date = run_query(get_village_operational(geog, id, from_date, partners))[0]['count'] vil_operation_to_date = run_query(get_village_operational(geog, id, to_date, partners))[0]['count'] if(vil_operation_from_date!=None and vil_operation_to_date!=None): achieved_vals['village_operational'] = vil_operation_to_date - vil_operation_from_date achieved_vals['storyboard'] = run_query(get_storyboard_prepared(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['video_edited'] = run_query(get_video_edited(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['quality_check'] = run_query(get_quality_check(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['inter_per_dissemination'] = run_query(get_interest_per_dissemination(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['new_csp_training'] = run_query(get_fresh_csp_tot_training(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['refresher_csp_traing'] = run_query(get_csp_tot_training(geog, id, from_date, to_date, partners))[0]['count'] - achieved_vals['new_csp_training'] achieved_vals['new_crp_training'] = run_query(get_fresh_crp_tot_training(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['refresher_crp_traing'] = run_query(get_crp_tot_training(geog, id, from_date, to_date, partners))[0]['count'] - achieved_vals['new_crp_training'] achieved_vals['vil_identified'] = run_query(get_village_identified(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['geog_name'] = get_parent_geog_id(geog, id)[0] tot_val = run_query(shared_sql.get_totals(geog, id, from_date, to_date, partners, ['tot_vid', 'tot_ado', 'tot_att', 'tot_scr']))[0] achieved_vals['video_production'] = tot_val['tot_vid'] achieved_vals['disseminations'] = tot_val['tot_scr'] if(tot_val['tot_scr']): achieved_vals['att_per_diss'] = tot_val['tot_att']/tot_val['tot_scr'] achieved_vals['avg_adoption'] = tot_val['tot_ado']/tot_val['tot_scr'] else: achieved_vals['att_per_diss'] = 0 achieved_vals['avg_adoption'] = 0 achieved_vals['vids_uploaded'] = run_query(get_videos_uploaded(geog, id, from_date, to_date, partners))[0]['count'] achieved_vals['csp_identified'] = run_query(get_csp_identified(geog, id, from_date, to_date, partners))[0]['count'] target_vals = run_query(get_targets(geog, id, from_date, to_date, partners))[0] search_box_params = views.common.get_search_box(request) #overrider search_box from_date and to_date search_box_params['from_date'] = str(from_date) search_box_params['to_date'] = str(to_date) 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']) if(get_req_url): get_req_url = '&'+get_req_url return render_to_response('targets_table.html',dict(search_box_params = search_box_params,\ achieved_vals=achieved_vals,\ target_vals=target_vals, \ get_req_url = get_req_url ))
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 \ ))
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 \ ))