def getSpecificCampaignNew(campaign): db2_obj = openDB2() db2 = db2_obj[0] cur2 = db2_obj[1] campaign = campaign.replace('^&^', '#') campaign = campaign.replace('^^^', '?') data = queryToData(cur2,queries.list_one_campaign.format(campaign),need_json=0) if data[0]['mobile_ids'] is not None and data[0]['mobile_ids'] != '0': c_id = ",".join(['"'+i+'"' for i in data[0]['mobile_ids'].split(',') if i != 0]) else: c_id = "'999'" if int(data[0]['is_sms']) == 0: total_su = queryToData(cur2,queries.signups_total.format(c_id, data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='total_signups', need_json=0) total_web_su = queryToData(cur2,queries.signups_web.format(data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='web_su', need_json=0) total_nm = queryToData(cur2,queries.new_members_total.format(c_id, data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='new_members_total', need_json=0) total_rb = queryToData(cur2,queries.report_back_total_web.format(data[0]['nid'], '10000', '2000-01-01', '3000-01-01'), index=0, keyname='rb', need_json=0) total_impact = queryToData(cur2,queries.impact_total.format(data[0]['nid'], '10000', '2000-01-01', '3000-01-01'), index=0, keyname='impact', need_json=0) total_traffic = queryToData(cur2,queries.traffic_total.format(data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='traffic', need_json=0) overall = OrderedDict([('Sign Ups',total_su), ('New Members',total_nm), ('Reportbacks',total_rb), ('Impact',total_impact), ('Traffic',total_traffic), ('Conversion Rate',round(float(total_web_su)/float(total_traffic) * 100, 2))]) overall = json.dumps(overall) su = queryToData(cur2,queries.new_sign_ups_new.format(c_id, data[0]['nid'], '2000-01-01', '3000-01-01')) nm = queryToData(cur2,queries.new_members_new.format(c_id, data[0]['nid'], '2000-01-01', '3000-01-01')) rb = queryToData(cur2,queries.reportback_web_daily.format(data[0]['nid'], '10000', '2000-01-01', '3000-01-01')) impact = queryToData(cur2,queries.impact_daily.format(data[0]['nid'], '10000', '2000-01-01', '3000-01-01')) srcs = queryToData(cur2,queries.sources_new.format(data[0]['nid'], '2000-01-01', '3000-01-01')) traffic = queryToData(cur2,queries.traffic_daily.format(data[0]['nid'], '2000-01-01', '3000-01-01')) if int(data[0]['is_sms']) == 1: total_su = queryToData(cur2,queries.new_sign_ups_new_mobile_total.format(c_id, '2000-01-01', '3000-01-01'), index=0, keyname='mobile_signup_total', need_json=0) total_nm = queryToData(cur2,queries.new_members_new_mobile_total.format(c_id, '2000-01-01', '3000-01-01'), index=0, keyname='mobile_new_members_total', need_json=0) total_alpha = queryToData(cur2,queries.new_sign_ups_new_alphas.format(c_id, '2000-01-01', '3000-01-01'), index=0, keyname='alphas', need_json=0) total_traffic = queryToData(cur2,queries.traffic_total.format(data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='traffic', need_json=0) overall = OrderedDict([('Sign Ups',total_su), ('New Members',total_nm), ('Reportbacks',total_alpha), ('Impact',total_alpha), ('Traffic',total_traffic), ('Conversion Rate',round(float(total_alpha)/float(total_traffic) * 100, 2))]) overall = json.dumps(overall) su = queryToData(cur2,queries.new_sign_ups_new_mobile.format(c_id, '2000-01-01', '3000-01-01')) nm = queryToData(cur2,queries.new_members_new_mobile.format(c_id, '2000-01-01', '3000-01-01')) rb = queryToData(cur2,queries.reportback_sms_daily.format(c_id, '2000-01-01', '3000-01-01')) impact = 0 srcs = queryToData(cur2,queries.sources_new.format(data[0]['nid'], '2000-01-01', '3000-01-01')) traffic = queryToData(cur2,queries.traffic_daily.format(data[0]['nid'], '2000-01-01', '3000-01-01')) cur2.close() db2.close() return render_template('campaign-new.html', is_sms=data[0]['is_sms'], campaign=campaign, su=su, nm=nm, rb=rb, impact=impact, srcs=srcs, overall=overall, traffic=traffic)
def causeStaffPicks(cause): title = cause.capitalize() causes_list = cause.split("+") if request.args.get('staff') is None: staff = "y" else: staff = request.args.get('staff') quoted_causes = ['"'+str(cause)+'"' for cause in causes_list] formatted_causes = ','.join(quoted_causes) if cause != 'all': q = 'select concat(upper(substring(replace(campaign,"_"," "),1,1)),substring(replace(campaign,"_"," "),2)) as campaign, sign_ups, new_members, report_backs from overall.overall where staff_pick = "%s" and cause in (%s) and date_add(end_date, interval 7 day) >= curdate() order by sign_ups desc' % (staff,formatted_causes) else: q = 'select concat(upper(substring(replace(campaign,"_"," "),1,1)),substring(replace(campaign,"_"," "),2)) as campaign, sign_ups, new_members, report_backs from overall.overall where staff_pick = "%s" and date_add(end_date, interval 7 day) >= curdate() order by sign_ups desc' % (staff) cur = openDB() cur.execute(q) data = cur.fetchall() cur.close() j = json.dumps(data) if len(data) > 0: return render_template('cause-campaigns.html', title=title,causes=cause, j=j) else: return render_template('cause-campaigns-nodata.html', title=title,causes=cause)
def getCausesdata(): cur = openDB() #all_casues needed for chart q = """ select 'Sex+Relationships' as cause, group_concat(distinct cause) as all_causes, sum(sign_ups) as sign_ups, sum(new_members) as new_members, sum(report_backs) as report_backs, sum(all_traffic) as traffic, round(avg(avg_gate_conversion)*100,2) as conv, count(*) as campaigns from overall.overall where date_add(end_date, interval 14 day) >= curdate() and cause in ('Sex',"Relationships") union all select 'Homelessness+Poverty' as cause, group_concat(distinct cause) as all_causes, sum(sign_ups) as sign_ups, sum(new_members) as new_members, sum(report_backs) as report_backs, sum(all_traffic) as traffic, round(avg(avg_gate_conversion)*100,2) as conv, count(*) as campaigns from overall.overall where date_add(end_date, interval 14 day) >= curdate() and cause in ('Homelessness',"Poverty") union all select 'Bullying+Violence' as cause, group_concat(distinct cause) as all_causes, sum(sign_ups) as sign_ups, sum(new_members) as new_members, sum(report_backs) as report_backs, sum(all_traffic) as traffic, round(avg(avg_gate_conversion)*100,2) as conv, count(*) as campaigns from overall.overall where date_add(end_date, interval 14 day) >= curdate() and cause in ('Bullying',"Violence") union all select 'Mental Health+Physical Health' as cause, group_concat(distinct cause) as all_causes, sum(sign_ups) as sign_ups, sum(new_members) as new_members, sum(report_backs) as report_backs, sum(all_traffic) as traffic, round(avg(avg_gate_conversion)*100,2) as conv, count(*) as campaigns from overall.overall where date_add(end_date, interval 14 day) >= curdate() and cause in ('Mental Health',"Physical Health") union all select cause, group_concat(distinct cause) as all_causes, sum(sign_ups) as sign_ups, sum(new_members) as new_members, sum(report_backs) as report_backs, sum(all_traffic) as traffic, round(avg(avg_gate_conversion)*100,2) as conv, count(*) as campaigns from overall.overall where date_add(end_date, interval 14 day) >= curdate() and cause not in ('Bullying',"Violence",'Mental Health',"Physical Health",'Homelessness',"Poverty",'Sex',"Relationships") group by cause """ cur.execute(q) data = cur.fetchall() cur.close() json.dumps(data) return json.dumps(data)
def listCampaigns(): db2_obj = openDB2() db2 = db2_obj[0] cur2 = db2_obj[1] data = queryToData(cur2,queries.list_all_campaigns, need_json=0) data = [{'title':i['title'].decode('ascii', 'ignore')} for i in data] data = json.dumps(data) cur2.close() db2.close() return render_template('list-campaigns.html', data=data)
def listCampaigns(): db2_obj = openDB2() db2 = db2_obj[0] cur2 = db2_obj[1] data = queryToData(cur2, queries.list_all_campaigns, need_json=0) data = [{'title': i['title'].decode('ascii', 'ignore')} for i in data] data = json.dumps(data) cur2.close() db2.close() return render_template('list-campaigns.html', data=data)
def monthly(): cur = openDB() cur.execute('select date_format(date, "%M %Y") as date, new_members_last_12_percent as new, engaged_members_last_12_percent as engaged, active_members_last_12_percent as active, verified_members_last_12_percent as verified from members.bod_2014 order by date_format(date, "%Y-%m-%d")' ) d = cur.fetchall() cur.close() data = json.dumps(d) return render_template('monthly-stats.html', data=data )
def campaignDataEnpoint(nid): try: nid = int(nid) q_metadata = queries.campaignDataEnpoint_basic_campaign_metadata % (nid) db_obj = openDB() db = db_obj[0] cur = db_obj[1] text = queryToData(cur,q_metadata) cur.close() db.close() return text except: return json.dumps({'error':500})
def campaignDataEnpoint(nid): try: nid = int(nid) q_metadata = queries.campaignDataEnpoint_basic_campaign_metadata % ( nid) db_obj = openDB() db = db_obj[0] cur = db_obj[1] text = queryToData(cur, q_metadata) cur.close() db.close() return text except: return json.dumps({'error': 500})
def formatDemo(query, name_present): cur2.execute(query) out = cur2.fetchall() #format json data for lists master_list = {'header':out[0]['header'], 'item_list':list()} for item in out: counts = [int(num) for num in item['count'].split(",")] total = sum(counts) #create percent str percents = [str(round((i/float(total))*100,2)) + '%' for i in counts] metric = item['metric'].split(",") inner_dict = dict(zip(metric,percents)) if name_present is True: temp_dict = {item['name']:inner_dict} else: temp_dict = inner_dict master_list['item_list'].append(temp_dict) return json.dumps(master_list)
def formatDemo(query, name_present): cur2.execute(query) out = cur2.fetchall() #format json data for lists master_list = {'header': out[0]['header'], 'item_list': list()} for item in out: counts = [int(num) for num in item['count'].split(",")] total = sum(counts) #create percent str percents = [ str(round((i / float(total)) * 100, 2)) + '%' for i in counts ] metric = item['metric'].split(",") inner_dict = dict(zip(metric, percents)) if name_present is True: temp_dict = {item['name']: inner_dict} else: temp_dict = inner_dict master_list['item_list'].append(temp_dict) return json.dumps(master_list)
def query(name,query): cur.execute(query) info = json.dumps(cur.fetchall()) data[name]=info
def dateRange(): #gets new start and end from ajax post and pulls queries with new parameters start = request.form['start'] end = request.form['end'] campaign = request.form['campaign'] db2_obj = openDB2() db2 = db2_obj[0] cur2 = db2_obj[1] data = queryToData(cur2,queries.list_one_campaign.format(campaign),need_json=0) if data[0]['mobile_ids'] is not None and data[0]['mobile_ids'] != '0': c_id = ",".join(['"'+i+'"' for i in data[0]['mobile_ids'].split(',') if i != 0]) else: c_id = "'999'" if int(data[0]['is_sms']) == 0: total_su = queryToData(cur2,queries.signups_total.format(c_id, data[0]['nid'], start, end), index=0, keyname='total_signups', need_json=0) total_web_su = queryToData(cur2,queries.signups_web.format(data[0]['nid'], start, end), index=0, keyname='web_su', need_json=0) total_nm = queryToData(cur2,queries.new_members_total.format(c_id, data[0]['nid'], start, end), index=0, keyname='new_members_total', need_json=0) total_rb = queryToData(cur2,queries.report_back_total_web.format(data[0]['nid'], '10000', start, end), index=0, keyname='rb', need_json=0) total_impact = queryToData(cur2,queries.impact_total.format(data[0]['nid'], '10000', start, end), index=0, keyname='impact', need_json=0) total_traffic = queryToData(cur2,queries.traffic_total.format(data[0]['nid'], start, end), index=0, keyname='traffic', need_json=0) try: conv_rate = round(float(total_web_su)/float(total_traffic) * 100, 2) except: conv_rate = 0.0 overall = OrderedDict([('Sign Ups',total_su), ('New Members',total_nm), ('Reportbacks',total_rb), ('Impact',total_impact), ('Traffic',total_traffic), ('Conversion Rate',conv_rate)]) overall = json.dumps(overall) su = queryToData(cur2,queries.new_sign_ups_new.format(c_id, data[0]['nid'], start, end)) nm = queryToData(cur2,queries.new_members_new.format(c_id, data[0]['nid'], start, end)) rb = queryToData(cur2,queries.reportback_web_daily.format(data[0]['nid'], '10000', start, end)) impact = queryToData(cur2,queries.impact_daily.format(data[0]['nid'], '10000', start, end)) srcs = queryToData(cur2,queries.sources_new.format(data[0]['nid'], start, end)) traffic = queryToData(cur2,queries.traffic_daily.format(data[0]['nid'], start, end)) if int(data[0]['is_sms']) == 1: total_su = queryToData(cur2,queries.new_sign_ups_new_mobile_total.format(c_id, start, end), index=0, keyname='mobile_signup_total', need_json=0) total_nm = queryToData(cur2,queries.new_members_new_mobile_total.format(c_id, start, end), index=0, keyname='mobile_new_members_total', need_json=0) total_alpha = queryToData(cur2,queries.new_sign_ups_new_alphas.format(c_id, start, end), index=0, keyname='alphas', need_json=0) total_traffic = queryToData(cur2,queries.traffic_total.format(data[0]['nid'], start, end), index=0, keyname='traffic', need_json=0) try: conv_rate = round(float(total_alpha)/float(total_traffic) * 100, 2) except: conv_rate = 0.0 overall = OrderedDict([('Sign Ups',total_su), ('New Members',total_nm), ('Reportbacks',total_alpha), ('Impact',total_alpha), ('Traffic',total_traffic), ('Conversion Rate',conv_rate)]) overall = json.dumps(overall) su = queryToData(cur2,queries.new_sign_ups_new_mobile.format(c_id, start, end)) nm = queryToData(cur2,queries.new_members_new_mobile.format(c_id, start, end)) rb = queryToData(cur2,queries.reportback_sms_daily.format(c_id, start, end)) impact = 0 srcs = queryToData(cur2,queries.sources_new.format(data[0]['nid'], start, end)) traffic = queryToData(cur2,queries.traffic_daily.format(data[0]['nid'], start, end)) cur2.close() db2.close() return jsonify(is_sms=data[0]['is_sms'], campaign=campaign, su=su, nm=nm, rb=rb, impact=impact, srcs=srcs, overall=overall, traffic=traffic)
def dateRange(): #gets new start and end from ajax post and pulls queries with new parameters start = request.form['start'] end = request.form['end'] campaign = request.form['campaign'] db2_obj = openDB2() db2 = db2_obj[0] cur2 = db2_obj[1] data = queryToData(cur2, queries.list_one_campaign.format(campaign), need_json=0) if data[0]['mobile_ids'] is not None and data[0]['mobile_ids'] != '0': c_id = ",".join([ '"' + i + '"' for i in data[0]['mobile_ids'].split(',') if i != 0 ]) else: c_id = "'999'" if int(data[0]['is_sms']) == 0: total_su = queryToData(cur2, queries.signups_total.format( c_id, data[0]['nid'], start, end), index=0, keyname='total_signups', need_json=0) total_web_su = queryToData(cur2, queries.signups_web.format( data[0]['nid'], start, end), index=0, keyname='web_su', need_json=0) total_nm = queryToData(cur2, queries.new_members_total.format( c_id, data[0]['nid'], start, end), index=0, keyname='new_members_total', need_json=0) total_rb = queryToData(cur2, queries.report_back_total_web.format( data[0]['nid'], '10000', start, end), index=0, keyname='rb', need_json=0) total_impact = queryToData(cur2, queries.impact_total.format( data[0]['nid'], '10000', start, end), index=0, keyname='impact', need_json=0) total_traffic = queryToData(cur2, queries.traffic_total.format( data[0]['nid'], start, end), index=0, keyname='traffic', need_json=0) try: conv_rate = round( float(total_web_su) / float(total_traffic) * 100, 2) except: conv_rate = 0.0 overall = OrderedDict([('Sign Ups', total_su), ('New Members', total_nm), ('Reportbacks', total_rb), ('Impact', total_impact), ('Traffic', total_traffic), ('Conversion Rate', conv_rate)]) overall = json.dumps(overall) su = queryToData( cur2, queries.new_sign_ups_new.format(c_id, data[0]['nid'], start, end)) nm = queryToData( cur2, queries.new_members_new.format(c_id, data[0]['nid'], start, end)) rb = queryToData( cur2, queries.reportback_web_daily.format(data[0]['nid'], '10000', start, end)) impact = queryToData( cur2, queries.impact_daily.format(data[0]['nid'], '10000', start, end)) srcs = queryToData( cur2, queries.sources_new.format(data[0]['nid'], start, end)) traffic = queryToData( cur2, queries.traffic_daily.format(data[0]['nid'], start, end)) if int(data[0]['is_sms']) == 1: total_su = queryToData(cur2, queries.new_sign_ups_new_mobile_total.format( c_id, start, end), index=0, keyname='mobile_signup_total', need_json=0) total_nm = queryToData(cur2, queries.new_members_new_mobile_total.format( c_id, start, end), index=0, keyname='mobile_new_members_total', need_json=0) total_alpha = queryToData(cur2, queries.new_sign_ups_new_alphas.format( c_id, start, end), index=0, keyname='alphas', need_json=0) total_traffic = queryToData(cur2, queries.traffic_total.format( data[0]['nid'], start, end), index=0, keyname='traffic', need_json=0) try: conv_rate = round( float(total_alpha) / float(total_traffic) * 100, 2) except: conv_rate = 0.0 overall = OrderedDict([('Sign Ups', total_su), ('New Members', total_nm), ('Reportbacks', total_alpha), ('Impact', total_alpha), ('Traffic', total_traffic), ('Conversion Rate', conv_rate)]) overall = json.dumps(overall) su = queryToData( cur2, queries.new_sign_ups_new_mobile.format(c_id, start, end)) nm = queryToData( cur2, queries.new_members_new_mobile.format(c_id, start, end)) rb = queryToData(cur2, queries.reportback_sms_daily.format(c_id, start, end)) impact = 0 srcs = queryToData( cur2, queries.sources_new.format(data[0]['nid'], start, end)) traffic = queryToData( cur2, queries.traffic_daily.format(data[0]['nid'], start, end)) cur2.close() db2.close() return jsonify(is_sms=data[0]['is_sms'], campaign=campaign, su=su, nm=nm, rb=rb, impact=impact, srcs=srcs, overall=overall, traffic=traffic)
def getSpecificCampaignNew(campaign): db2_obj = openDB2() db2 = db2_obj[0] cur2 = db2_obj[1] campaign = campaign.replace('^&^', '#') campaign = campaign.replace('^^^', '?') data = queryToData(cur2, queries.list_one_campaign.format(campaign), need_json=0) if data[0]['mobile_ids'] is not None and data[0]['mobile_ids'] != '0': c_id = ",".join([ '"' + i + '"' for i in data[0]['mobile_ids'].split(',') if i != 0 ]) else: c_id = "'999'" if int(data[0]['is_sms']) == 0: total_su = queryToData(cur2, queries.signups_total.format( c_id, data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='total_signups', need_json=0) total_web_su = queryToData(cur2, queries.signups_web.format( data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='web_su', need_json=0) total_nm = queryToData(cur2, queries.new_members_total.format( c_id, data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='new_members_total', need_json=0) total_rb = queryToData(cur2, queries.report_back_total_web.format( data[0]['nid'], '10000', '2000-01-01', '3000-01-01'), index=0, keyname='rb', need_json=0) total_impact = queryToData(cur2, queries.impact_total.format( data[0]['nid'], '10000', '2000-01-01', '3000-01-01'), index=0, keyname='impact', need_json=0) total_traffic = queryToData(cur2, queries.traffic_total.format( data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='traffic', need_json=0) overall = OrderedDict([ ('Sign Ups', total_su), ('New Members', total_nm), ('Reportbacks', total_rb), ('Impact', total_impact), ('Traffic', total_traffic), ('Conversion Rate', round(float(total_web_su) / float(total_traffic) * 100, 2)) ]) overall = json.dumps(overall) su = queryToData( cur2, queries.new_sign_ups_new.format(c_id, data[0]['nid'], '2000-01-01', '3000-01-01')) nm = queryToData( cur2, queries.new_members_new.format(c_id, data[0]['nid'], '2000-01-01', '3000-01-01')) rb = queryToData( cur2, queries.reportback_web_daily.format(data[0]['nid'], '10000', '2000-01-01', '3000-01-01')) impact = queryToData( cur2, queries.impact_daily.format(data[0]['nid'], '10000', '2000-01-01', '3000-01-01')) srcs = queryToData( cur2, queries.sources_new.format(data[0]['nid'], '2000-01-01', '3000-01-01')) traffic = queryToData( cur2, queries.traffic_daily.format(data[0]['nid'], '2000-01-01', '3000-01-01')) if int(data[0]['is_sms']) == 1: total_su = queryToData(cur2, queries.new_sign_ups_new_mobile_total.format( c_id, '2000-01-01', '3000-01-01'), index=0, keyname='mobile_signup_total', need_json=0) total_nm = queryToData(cur2, queries.new_members_new_mobile_total.format( c_id, '2000-01-01', '3000-01-01'), index=0, keyname='mobile_new_members_total', need_json=0) total_alpha = queryToData(cur2, queries.new_sign_ups_new_alphas.format( c_id, '2000-01-01', '3000-01-01'), index=0, keyname='alphas', need_json=0) total_traffic = queryToData(cur2, queries.traffic_total.format( data[0]['nid'], '2000-01-01', '3000-01-01'), index=0, keyname='traffic', need_json=0) overall = OrderedDict([ ('Sign Ups', total_su), ('New Members', total_nm), ('Reportbacks', total_alpha), ('Impact', total_alpha), ('Traffic', total_traffic), ('Conversion Rate', round(float(total_alpha) / float(total_traffic) * 100, 2)) ]) overall = json.dumps(overall) su = queryToData( cur2, queries.new_sign_ups_new_mobile.format(c_id, '2000-01-01', '3000-01-01')) nm = queryToData( cur2, queries.new_members_new_mobile.format(c_id, '2000-01-01', '3000-01-01')) rb = queryToData( cur2, queries.reportback_sms_daily.format(c_id, '2000-01-01', '3000-01-01')) impact = 0 srcs = queryToData( cur2, queries.sources_new.format(data[0]['nid'], '2000-01-01', '3000-01-01')) traffic = queryToData( cur2, queries.traffic_daily.format(data[0]['nid'], '2000-01-01', '3000-01-01')) cur2.close() db2.close() return render_template('campaign-new.html', is_sms=data[0]['is_sms'], campaign=campaign, su=su, nm=nm, rb=rb, impact=impact, srcs=srcs, overall=overall, traffic=traffic)