def public(): # Check for log-in if not session.get('logged_in'): return redirect(url_for('index')) # Check for all necessary parameters if helpers.argsMissing(): return redirect(helpers.redirectWithArgs('/public')) # Open database con = sqlite3.connect('logs115.db') cur = con.cursor() # Parse duration and dates from URL parameters duration_string, title_addon = ghelpers.parseDuration(request.args.get('durationstart'), request.args.get('durationend')) starting_date_string = request.args.get('datestart') ending_date_string = request.args.get('dateend') figures = [] # PUBLIC CHARTS _, public_diseases, _, _ = helpers.getDiseases() figures.append(ghelpers.publicLineChart(cur, 'all', [" type='public' "], starting_date_string, ending_date_string, duration_string, ["Calls to Public Hotline"], "Calls to Public Hotline by Day", False, "publiccalls")) figures.append(ghelpers.hotlineBreakdown(cur, 'public', ["Visit " + disease + " Information" for disease in public_diseases] + ['Make a Report', 'Request More Information', 'Request Ambulance Information'], [disease + "_menu IS NOT NULL" for disease in public_diseases] + ["hotline_menu='2'", "hotline_menu='3'", "hotline_menu='4'"], starting_date_string, ending_date_string, duration_string, "Public Hotline Breakdown", "publicbreakdown")) for disease in sorted(public_diseases): figures.append(ghelpers.publicLineChart(cur, 'public', [disease + "_menu IS NOT NULL", disease + "_menu=1", disease + "_menu=2"], starting_date_string, ending_date_string, duration_string, ["Visit Menu", "Listen to Overview Info", "Listen to Prevention Info"], "Calls to " + disease.title() + " Menu", True, "public" + disease)) figures.append(ghelpers.publicLineChart(cur, 'public', ["hotline_menu='2'"], starting_date_string, ending_date_string, duration_string, ["Public Disease Reports"], "Public Disease Reports by Day", False, "publicreports")) figures.append(ghelpers.publicLineChart(cur, 'public', ["hotline_menu='3'"], starting_date_string, ending_date_string, duration_string, ["Calls Requesting More Information"], "Calls Requesting More Information by Day", False, "publicmoreinfo")) figures.append(ghelpers.publicLineChart(cur, 'public', ["hotline_menu='4'"], starting_date_string, ending_date_string, duration_string, ["Calls Requesting Ambulance Information"], "Calls Requesting Ambulance Information by Day", False, "publicambulanceinfo")) # Close database con.close() return render_template("dashboardpage.html", figures=figures, ap = 'publiclink')
def editsettings(): # Check for log-in if not session.get('logged_in'): return redirect(url_for('index')) # Get former disease settings all_public, chosen_public, all_hc, chosen_hc = helpers.getDiseases() # Set the disease settings according to what was inputted in the HTML form shelpers.setDiseases(all_public, [key for key in request.form if not ('case' in key or 'death' in key)], all_hc, [key for key in request.form if ('case' in key or 'death' in key)]) return redirect(url_for('overview'))
def tbl_atr(): all_public, chosen_public, all_hc, chosen_hc = helpers.getDiseases() call_atr = [ 'call_id', 'date', 'datenum', 'month', 'year', 'time', 'week_id', 'duration', 'caller_id', 'status', 'type' ] public_atr = ['call_id', 'hotline_menu', 'disease_menu' ] + [disease + '_menu' for disease in all_public] hc_atr = ['call_id', 'caller_id', 'week_id'] + all_hc return call_atr, public_atr, hc_atr
def settings(): #global con #global cur # Check for log-in if not session.get('logged_in'): return redirect(url_for('index')) # Get disease ptions and selected diseases all_public, chosen_public, all_hc, chosen_hc = helpers.getDiseases() con = sqlite3.connect('logs115.db') cur = con.cursor() # For each public disease, determine whether it was used in each month from 2016 to present disease_presences, checked = shelpers.getDiseasePresences(all_public, chosen_public, "public_interactions", cur) hc_disease_presences, hc_checked = shelpers.getDiseasePresences(all_hc, chosen_hc, "hc_reports", cur) con.close() return render_template("settings.html", diseases=disease_presences, checked=checked, years=helpers.years(), hc_diseases=hc_disease_presences, hc_checked=hc_checked)
def download(): # Check for log-in if not session.get('logged_in'): return redirect(url_for('index')) # Check for all necessary parameters if helpers.timeArgsMissing(): return redirect(helpers.redirectWithArgs('/download')) # Parse dates from URL parameters starting_date_string = request.args.get('datestart') ending_date_string = request.args.get('dateend') _, public_diseases, _, hc_diseases = helpers.getDiseases() charts_with_ids = [] charts = helpers.getCharts() for i in range (0, len(charts)): chart = charts[i] charts_with_ids.append((str(i).zfill(2) + chart[0], chart[1], chart[2])) return render_template("download.html", redirect_string = "/downloading?datestart=" + starting_date_string + "&dateend=" + ending_date_string, figureoptions=charts_with_ids)
def hcDiseaseChart(cur, starting_date_string, ending_date_string, addon, target): series = [] # Initialize empty dictionary to hold reports reports = collections.OrderedDict() _, _, _, hc_diseases = helpers.getDiseases() diseases = [disease for disease in hc_diseases if addon in disease] for disease in sorted(diseases): reports[disease] = [] reports['dates'] = [] # Query database for sum of reports of each disease eah week cur.execute( "SELECT calls.week_id, " + ", ".join(["sum(" + disease + ")" for disease in diseases]) + " FROM calls JOIN hc_reports ON calls.call_id = hc_reports.call_id WHERE datenum >= " + "'" + helpers.dtoi(starting_date_string) + "'" + " AND datenum <= " + "'" + helpers.dtoi(ending_date_string) + "'" + " GROUP BY calls.week_id;") weeks = cur.fetchall() # Write sums from SQL query into database for week in weeks: reports['dates'].append(week[0]) for i in range(0, len(diseases)): reports[diseases[i]].append(week[i + 1]) series, labels, colors = [], [], [] i = 0 for key, value in reports.items(): if key != 'dates': series.append(value) labels.append(key.split("_")[1].title()) colors.append(palette[i]) i = i + 1 if target[-4:] == '.pdf': pdf = chartmaker.lineChartDownload( reports['dates'], series, labels, colors, "Reports of Disease " + addon[1:].title() + "s by Week", None, None, None, None, target) else: return lineChart( [helpers.toWordDate(week) for week in reports['dates']], series, labels, colors, "Reports of Disease " + addon[1:].title() + "s by Week", True, "None", "None", "Date (Beginning of Week)", target) + ("", "")
def addNewFields(cur, public_fields_available, hc_fields_available): all_public, chosen_public, all_hc, chosen_hc = helpers.getDiseases() new_public_diseases = [ disease for disease in public_fields_available if disease not in all_public ] new_hc_diseases = [ disease for disease in hc_fields_available if disease not in all_hc ] for disease in new_public_diseases: cur.execute("ALTER TABLE public_interactions ADD " + disease + "_menu INTEGER;") all_public, chosen_public, all_hc, chosen_hc = helpers.setDiseases( all_public + [disease], chosen_public + [disease], all_hc, chosen_hc) for disease in new_hc_diseases: cur.execute("ALTER TABLE hc_reports ADD " + disease + " INTEGER DEFAULT 0;") all_public, chosen_public, all_hc, chosen_hc = helpers.setDiseases( all_public, chosen_public, all_hc + [disease], chosen_hc + [disease]) return new_public_diseases, new_hc_diseases
def sendNotification(): thresholdcases = -1 thresholddeaths = -1 con = sqlite3.connect('logs115.db') cur = con.cursor() week_id = helpers.getWeekId(datetime.datetime.now()) _, _, hc_diseases, _ = helpers.getDiseases() totalcases = 0 for disease in [d for d in hc_diseases if "case" in d]: cur.execute("SELECT SUM(" + disease + ") FROM hc_reports WHERE week_id='" + week_id + "'") diseasecases = cur.fetchall() if diseasecases[0][0] is not None: totalcases = totalcases + int(diseasecases[0][0]) totaldeaths = 0 for disease in [d for d in hc_diseases if "death" in d]: cur.execute("SELECT SUM(" + disease + ") FROM hc_reports WHERE week_id='" + week_id + "'") diseasedeaths = cur.fetchall() if diseasedeaths[0][0] is not None: totaldeaths = totaldeaths + int(diseasedeaths[0][0]) if totalcases > thresholdcases or totaldeaths > thresholddeaths: body_text = "There were an exceptional number of cases and/or deaths reported on the 115 holine this week (the week of " + week_id + "). " + str(totalcases) + " cases were reported and " + str(totaldeaths) + " deaths were reported. The threshold number of cases is " + str(thresholdcases) + " and the threshold number of deaths is " + str(thresholddeaths) + ". Visit the 115 hotline dashboard to change these thresholds." dhelpers.sendEmail("115 Hotline - Exceptional Number of Cases/Deaths Reported", body_text, None, "*****@*****.**", None, None)
def setDiseases(all_public, chosen_public, all_hc, chosen_hc): vars = ['all_public', 'chosen_public', 'all_hc', 'chosen_hc'] with open('settings.json', 'w') as outfile: json.dump({'all_public':all_public, 'chosen_public':chosen_public, 'all_hc':all_hc, 'chosen_hc':chosen_hc}, outfile) return helpers.getDiseases()
def genReport(starting_date_string, ending_date_string, qualitative, to_emails, cc, bcc, chosenfigures, target): # File title includes current timestamp for unique identification now = datetime.datetime.now() filetitle = "report" + now.strftime("%d-%m-%y-%H-%M-%S") + ".pdf" print('writing qualitative', file=sys.stderr) #Set up PDF document and styles if True: doc = SimpleDocTemplate("hello.pdf", pagesize=letter) parts = [] styles = { 'paragraph': ParagraphStyle('paragraph', fontName='Helvetica', fontSize=10, alignment=TA_LEFT), 'title': ParagraphStyle('title', fontName='Helvetica', fontSize=20, alignment=TA_CENTER), 'report_title': ParagraphStyle('report_title', fontName='Helvetica', fontSize=30, alignment=TA_CENTER), } #Title Page parts.append(Spacer(1, 3 * inch)) parts.append(Paragraph("115 Hotline Report", styles['report_title'])) parts.append(Spacer(1, 0.7 * inch)) parts.append( Paragraph(starting_date_string + " to " + ending_date_string, styles['report_title'])) parts.append(PageBreak()) #Qualitative description page parts.append(Paragraph("Qualitative Description", styles['title'])) parts.append(Spacer(1, inch)) parts.append(Paragraph(qualitative, styles['paragraph'])) doc.build(parts) #Rest of report: statistics and graphs con = sqlite3.connect("logs115.db") cur = con.cursor() print('making sql queries and generating pngs', file=sys.stderr) duration_string, title_addon = ghelpers.parseDuration('0', 'end') _, public_diseases, _, _ = helpers.getDiseases() chartid = 0 ghelpers.publicLineChart(cur, "all", ["call_id != ''"], starting_date_string, ending_date_string, duration_string, ["Calls to Hotline"], "Calls to Hotline by Day", False, str(chartid).zfill(2) + "overview.pdf") chartid = chartid + 1 # Breakdown pie charts ghelpers.hotlineBreakdown( cur, 'all', ["Public Callers", "HC Workers"], [" type='public' ", " type='hc_worker' "], starting_date_string, ending_date_string, duration_string, "Hotline Breakdown", str(chartid).zfill(2) + "hotlinebreakdown.pdf") chartid = chartid + 1 ghelpers.hotlineBreakdown( cur, 'public', [ "Visit " + disease + " Information" for disease in public_diseases ] + [ 'Make a Report', 'Request More Information', 'Request Ambulance Information' ], [disease + "_menu IS NOT NULL" for disease in public_diseases] + ["hotline_menu='2'", "hotline_menu='3'", "hotline_menu='4'"], starting_date_string, ending_date_string, duration_string, "Public Hotline Breakdown", str(chartid).zfill(2) + "publicbreakdown.pdf") chartid = chartid + 1 # Status chart ghelpers.statusChart(cur, "public", starting_date_string, ending_date_string, duration_string, str(chartid).zfill(2) + "statuspublic.pdf") chartid = chartid + 1 ghelpers.statusChart(cur, "healthcare workers", starting_date_string, ending_date_string, duration_string, str(chartid).zfill(2) + "statushc.pdf") chartid = chartid + 1 # Line charts and statistics ghelpers.publicLineChart(cur, "all", [" type='public' "], starting_date_string, ending_date_string, duration_string, ["Calls to Public Hotline"], "Calls to Public Hotline by Day", False, str(chartid).zfill(2) + "public.pdf") chartid = chartid + 1 for disease in public_diseases: ghelpers.publicLineChart( cur, "public", [ disease + "_menu IS NOT NULL", disease + "_menu=1", disease + "_menu=2" ], starting_date_string, ending_date_string, duration_string, [ "Visit Menu", "Listen to Overview Info", "Listen to Prevention Info" ], "Calls to " + disease.title() + " Menu by Day", False, str(chartid).zfill(2) + disease + ".pdf") chartid = chartid + 1 ghelpers.publicLineChart(cur, "public", ["hotline_menu='2'"], starting_date_string, ending_date_string, duration_string, ["Public Disease Reports"], "Public Disease Reports by Day", False, str(chartid).zfill(2) + "publicreports.pdf") chartid = chartid + 1 ghelpers.publicLineChart( cur, "public", ["hotline_menu='3'"], starting_date_string, ending_date_string, duration_string, ["Calls Requesting Additional Information"], "Cals Requesting Additional Information by Day", False, str(chartid).zfill(2) + "moreinfo.pdf") chartid = chartid + 1 ghelpers.publicLineChart( cur, "public", ["hotline_menu='4'"], starting_date_string, ending_date_string, duration_string, ["Calls Requesting Ambulance Information"], "Cals Requesting Ambulance Information by Day", False, str(chartid).zfill(2) + "ambulance.pdf") chartid = chartid + 1 ghelpers.hcOntimeChart(cur, starting_date_string, ending_date_string, str(chartid).zfill(2) + "hcontime.pdf") for addon in ['_case', '_death']: chartid = chartid + 1 ghelpers.hcDiseaseChart( cur, starting_date_string, ending_date_string, addon, str(chartid).zfill(2) + "hc" + addon[1:] + ".pdf") con.close() # Combine PDF with text (title page, qualitative, statistics) and PDF with graphs print('combining all pdfs', file=sys.stderr) output = PdfFileWriter() append_pdf(PdfFileReader(open("hello.pdf", "rb")), output) for graph in list(sorted([figure + ".pdf" for figure in chosenfigures])): append_pdf(PdfFileReader(open(graph, "rb")), output) output.write(open(filetitle, "wb")) if target == 'email': # Send email sendEmail( "Your 115 Hotline Report", "The PDF report you requested is attached to this email.", filetitle, to_emails, cc, bcc) print('done!', file=sys.stderr) helpers.cleanUp() return else: return filetitle