def get_stats(self): cnds = self.navb.conditions() cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''}) cnds.update({ "(delivery_date + INTERVAL \'%d days\') >= '%s'" % (PNC_GESTATION, self.navb.start): '' }) exts = {} cols = ['COUNT(*) AS total'] title, group, attrs, nat = ('', '', [], []) #; print self.navb.kw if self.navb.kw.get('group') == 'no_risk': title = 'No Risk' group = 'no_risk' exts.update({ self.navb.kw.get('group'): ('COUNT(*)', queries.PNC_DATA['NO_RISK']['query_str']) }) cols = ['COUNT(*) AS total'] nat = Pncvisit.fetch_pncvisits(cnds, cols, exts) elif self.navb.kw.get('group') == 'at_risk': title = 'At Risk' group = 'at_risk' attrs = [(makecol(x[0]), x[1]) for x in queries.PNC_DATA['RISK']['attrs']] exts.update( dict([(makecol(x[0]), ('COUNT(*)', x[0])) for x in queries.PNC_DATA['RISK']['attrs']])) exts.update({ self.navb.kw.get('group'): ('COUNT(*)', queries.PNC_DATA['RISK']['query_str']) }) cols = ['COUNT(*) AS total'] nat = Pncvisit.fetch_pncvisits(cnds, cols, exts) elif self.navb.kw.get('group') in [ makecol(x[0]) for x in queries.PNC_DATA['PNC']['attrs'] ]: title = makedict( queries.PNC_DATA['PNC']['attrs'])[self.navb.kw.get('group')][1] group = self.navb.kw.get('group') exts.update({ self.navb.kw.get('group'): ('COUNT(*)', makedict(queries.PNC_DATA['PNC']['attrs'])[ self.navb.kw.get('group')][0]) }) cols = ['COUNT(*) AS total'] nat = Pncvisit.fetch_pncvisits(cnds, cols, exts) else: cols = ['COUNT(*) AS total'] exts = { 'no_risk': ('COUNT(*)', queries.PNC_DATA['NO_RISK']['query_str']), 'at_risk': ('COUNT(*)', queries.PNC_DATA['RISK']['query_str']) } exts.update( dict([(makecol(x[0]), ('COUNT(*)', x[0])) for x in queries.PNC_DATA['PNC']['attrs']])) nat = Pncvisit.fetch_pncvisits(cnds, cols, exts) #print title, group, attrs, "NAT: ", nat[0].__dict__ return (title, group, attrs, nat)
def get_tables(self): cnds = self.navb.conditions() cnds.update({ "(created_at) >= '%s'" % (self.navb.start): '', "(created_at) <= '%s'" % (self.navb.finish): '' }) exts = {} cnds, markup, cols = self.navb.neater_tables( cnds=cnds, extras=[('province_pk', 'Province'), ('district_pk', 'District'), ('referral_facility_pk', 'Hospital'), ('facility_pk', 'Health Centre'), ('sector_pk', 'Sector'), ('cell_pk', 'Cell'), ('village_pk', 'Village'), ('user_phone', 'Reporter Phone'), ('created_at', 'Submission Datetime'), ('indexcol', 'Message')]) markup.update({ 'drugs': lambda x, _, __: '%s' % (Stock.get_report_drugs(x)) }) markup.update({ 'message': lambda x, _, __: '%s' % (Stock.get_report_details(x)) }) markup.update({ 'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=so&id=%s">View</a>' % (x), }) DESCRI = [] STOCKDICT = makedict(queries.STOCK_DATA['attrs']) INDICS = [] attrs = [] group = "Stock" title = "Stock Notifications" sc = self.navb.kw.get('subcat') if self.navb.kw.get('subcat') and self.navb.kw.get('subcat') in [ makecol(x[0]) for x in queries.STOCK_DATA['attrs'] ]: cnds.update({queries.STOCK_DATA['query_str']: ''}) cnds.update({STOCKDICT[self.navb.kw.get('subcat')][0]: ''}) INDICS = [STOCKDICT[self.navb.kw.get('subcat')]] dcols = [x[0] for x in cols] nat = Stock.fetch_log_stock(cnds, dcols) #DESCRI.append((group, title)) desc = 'Stock-out cases%s' % (' (%s)' % (self.navb.find_descr( DESCRI + [(makecol(x[0]), x[1]) for x in INDICS], sc or self.navb.kw.get('subcat')))) #print title, group, attrs, "NAT: ", nat[0].__dict__ return (title, desc, group, attrs, markup, cols, nat)
def get_tables(self): #print "KW: ", self.navb.kw cnds = self.navb.conditions() cnds.update({"(anc_date) <= '%s'" % (self.navb.finish): ''}) cnds.update({ "(lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start): '' }) precnds = self.navb.conditions() precnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''}) precnds.update({ "(lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start): '' }) # cnds, markup, cols = self.navb.neater_tables( cnds=cnds, extras=[('indexcol', 'ID'), ('national_id', 'Mother ID'), ('user_phone', 'Reporter Phone'), ('lmp', 'LMP'), ('mother_weight', 'Weight'), ('muac', 'Muac'), ('created_at', 'Submission Date')]) markup.update({ 'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=anc&id=%s">View</a>' % (x), }) INDICS = [(makecol(x[0]), x[0], x[1]) for x in queries.ANC_DATA['attrs']] PRE_INDICS = [('anc1', 'total', 'ANC1'), ('stdanc1', '(lmp + INTERVAL \'90 days\') <= created_at', 'Standard ANC1')] ANCDICT = makedict(queries.ANC_DATA['attrs']) PREDICT = {x[0]: (x[1], x[2]) for x in PRE_INDICS} title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = ( '', '', '', [], [], [], [], []) if self.navb.kw.get('subcat') is not None: sc = self.navb.kw.get('subcat') dcols = [x[0] for x in cols] if self.navb.kw.get('subcat') in [x[0] for x in PRE_INDICS]: wcl = PREDICT.get(sc) #; print wcl, PREDICT INDICS = [(sc, wcl[0], wcl[1])] if wcl else [] INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in PRE_INDICS]) if wcl and wcl[0] != 'total': precnds.update({wcl[0]: ''}) nat = Pregnancy.fetch_log_pregnancies(precnds, dcols) else: wcl = ANCDICT.get(sc) INDICS = [(sc, wcl[0], wcl[1])] if wcl else [] INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS]) cnds.update({wcl[0]: ''}) nat = Ancvisit.fetch_log_ancvisits(cnds, dcols) #;print "INDICS: ", INDICS #print wcl, ANCDICT, INDICS else: dcols = [x[0] for x in cols] #nat = Ancvisit.fetch_log_ancvisits(cnds, dcols) nat = Pregnancy.fetch_log_pregnancies(precnds, dcols) if self.navb.kw.get( 'view') == 'table' or self.navb.kw.get('view') != 'log': group_by = [] group_by += [ 'province_pk' ] if self.navb.kw.get('nation') or not group_by else [] group_by += ['district_pk'] if self.navb.kw.get('province') else [] group_by += ['referral_facility_pk' ] if self.navb.kw.get('district') else [] group_by += ['facility_pk'] if self.navb.kw.get('hd') else [] #print "\nGROUP BY: %s \n" % group_by LOCS = { 'nation': self.navb.kw.get('nation'), 'province': self.navb.kw.get('province'), 'district': self.navb.kw.get('district'), 'hospital': self.navb.kw.get('hd'), 'location': self.navb.kw.get('hc') } if self.navb.kw.get('subcat') in [x[0] for x in PRE_INDICS]: #print PRE_INDICS, LOCS, precnds, group_by locateds = Pregnancy.fetch_pregnancies_by_location( precnds, group_by=group_by, INDICS=PRE_INDICS) elif self.navb.kw.get('subcat') in [x[0] for x in INDICS]: #print INDICS, LOCS, cnds, group_by locateds = Ancvisit.fetch_ancvisits_by_location( cnds, group_by=group_by, INDICS=INDICS) else: INDICS = PRE_INDICS + INDICS locateds = Ancvisit.fetch_ancvisits_by_location( cnds, group_by=group_by, INDICS=INDICS) locateds += Pregnancy.fetch_pregnancies_by_location( precnds, group_by=group_by, INDICS=PRE_INDICS) #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs() tabular = give_me_table(locateds, self.navb.locs(), INDICS=INDICS, LOCS=LOCS) #print locateds, "\n", tabular INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS]) desc = 'Antenatal care visits%s' % ( ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2]) for x in INDICS], sc))) #print INDICS_HEADERS, tabular, locateds return (title, desc, group, attrs, markup, cols, nat, tabular, locateds, INDICS_HEADERS)
def get_tables(self): cnds = self.navb.conditions() cnds.update({ "(created_at) >= '%s'" % (self.navb.start): '', "(created_at) <= '%s'" % (self.navb.finish): '', "keyword = 'SMN'": '' }) exts = {} cnds, markup, cols = self.navb.neater_tables( cnds=cnds, extras=[('has_gone_hc', 'Received at HC'), ('has_gone_hd', 'Received at DH'), ('province_pk', 'Province'), ('district_pk', 'District'), ('referral_facility_pk', 'Hospital'), ('facility_pk', 'Health Centre'), ('sector_pk', 'Sector'), ('cell_pk', 'Cell'), ('village_pk', 'Village'), ('national_id', 'National ID'), ('user_phone', 'Reporter Phone'), ('created_at', 'Submission Datetime'), ('indexcol', 'Message')]) markup.update({ 'action': lambda x, _, __: '<a href="/dashboards/diagnosisdash?id=%s">Fill Diagnosis Form %s</a>' % (x, x), }) markup.update({ 'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=smn&id=%s">View</a>' % (x), }) markup.update({ 'has_gone_hc': lambda x, _, __: '%s' % ('<b style="color:red;">HC: NO</b>' if not x else '<b style="color:green;">HC: YES</b>') }) markup.update({ 'has_gone_hd': lambda x, _, __: '%s' % ('<b style="color:red;">DH: NO</b>' if not x else '<b style="color:green;">DH: YES</b>') }) markup.update({ 'symptoms': lambda x, _, __: '%s' % (Malaria.get_report_symptoms(x)) }) markup.update({ 'drugs': lambda x, _, __: '%s' % (Malaria.get_report_drugs(x)) }) markup.update({ 'message': lambda x, _, __: '%s' % (Malaria.get_report_details(x)) }) DESCRI = [] MALARIADICT = makedict(queries.MALARIA_DATA['attrs']) INDICS = [('all', 'total', 'Total')] + [ (makecol(x[0]), x[0], x[1]) for x in queries.MALARIA_DATA['attrs'] ] attrs = [] group = "Severe Malaria" title = "Severe Malaria Notifications" sc = self.navb.kw.get('subcat') title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = ( '', '', '', [], [], [], [], []) if self.navb.kw.get('subcat') and self.navb.kw.get('subcat') in [ makecol(x[0]) for x in queries.MALARIA_DATA['attrs'] ]: sc = self.navb.kw.get('subcat') wcl = MALARIADICT.get(sc) cnds.update({wcl[0]: ''}) INDICS = [(sc, wcl[0], wcl[1])] if wcl else [] #print INDICS, MALARIADICT if self.navb.kw.get( 'view') == 'table' or self.navb.kw.get('view') != 'log': group_by = [] group_by += [ 'province_pk' ] if self.navb.kw.get('nation') or not group_by else [] group_by += ['district_pk'] if self.navb.kw.get('province') else [] group_by += ['referral_facility_pk' ] if self.navb.kw.get('district') else [] group_by += ['facility_pk'] if self.navb.kw.get('hd') else [] #print "\nGROUP BY: %s \n" % group_by LOCS = { 'nation': self.navb.kw.get('nation'), 'province': self.navb.kw.get('province'), 'district': self.navb.kw.get('district'), 'hospital': self.navb.kw.get('hd'), 'location': self.navb.kw.get('hc') } locateds = Malaria.fetch_malaria_by_location(cnds, group_by=group_by, INDICS=INDICS) #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs() tabular = give_me_table(locateds, self.navb.locs(), INDICS=INDICS, LOCS=LOCS) #print tabular INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS]) dcols = [x[0] for x in cols] nat = Malaria.fetch_log_malaria(cnds, dcols) #DESCRI.append((group, title)) desc = 'Severe malaria cases%s' % (' (%s)' % (self.navb.find_descr( DESCRI + [(makecol(x[0]), x[2]) for x in INDICS], sc or self.navb.kw.get('subcat')))) #print title, group, attrs, "NAT: ", nat[0].__dict__ return (title, desc, group, attrs, markup, cols, nat, tabular, locateds, INDICS_HEADERS)
def get_tables(self): cnds = self.navb.conditions() cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''}) cnds.update({ "(lmp + INTERVAL \'%d days\') >= '%s'" % (GESTATION, self.navb.start): '' }) #;print cnds exts = {} cnds, markup, cols = self.navb.neater_tables( cnds=cnds, extras=[('national_id', 'Mother ID'), ('user_phone', 'Reporter Phone'), ('lmp', 'LMP'), ('gravidity', 'Gravidity'), ('parity', 'Parity'), ('mother_weight', 'Weight'), ('mother_height', 'Height'), ('bmi', 'BMI'), ('created_at', 'Submission Date'), ('indexcol', 'ID')]) markup.update({ 'indexcol': lambda x, _, __: '<a href="/dashboards/report?tbl=pre&id=%s">View</a>' % (x), }) #print cnds, markup, cols DESCRI = [] HIGHRISKDICT = makedict(queries.HIGH_RISK['attrs']) RISKDICT = makedict(queries.RISK['attrs']) INDICS = [] title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = ( '', '', '', [], [], [], [], []) #; print self.navb.kw if self.navb.kw.get('subcat') is not None: sc = self.navb.kw.get('subcat') #;print RISKDICT wcl = HIGHRISKDICT.get(sc) or RISKDICT.get(sc) #;print wcl INDICS = [(sc, wcl[0], wcl[1])] if wcl else [] cnds.update({wcl[0]: ''}) dcols = [x[0] for x in cols] #;print "INDICS: ", INDICS #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) elif self.navb.kw.get('group') == 'no_risk': title = 'No Risk' group = 'no_risk' INDICS = [(group, queries.NO_RISK['query_str'], title)] cnds.update({queries.NO_RISK['query_str']: ''}) dcols = [x[0] for x in cols] #;print INDICS #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) elif self.navb.kw.get( 'group') == 'at_risk' and self.navb.kw.get('subcat') is None: title = 'At Risk' group = 'at_risk' INDICS = [(group, queries.RISK['query_str'], title)] for k in RISKDICT.keys(): INDICS.append((k, RISKDICT[k][0], RISKDICT[k][1])) cnds.update({queries.RISK['query_str']: ''}) dcols = [x[0] for x in cols] #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) elif self.navb.kw.get( 'group') == 'high_risk' and self.navb.kw.get('subcat') is None: title = 'High Risk' group = 'high_risk' INDICS = [(group, queries.HIGH_RISK['query_str'], title)] for k in HIGHRISKDICT.keys(): INDICS.append((k, HIGHRISKDICT[k][0], HIGHRISKDICT[k][1])) cnds.update({queries.HIGH_RISK['query_str']: ''}) dcols = [x[0] for x in cols] #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) elif self.navb.kw.get('group') == 'toilet': title = 'With Toilet' group = 'toilet' INDICS = [(group, "LOWER(toilet) = 'to'", title)] cnds.update({"LOWER(toilet) = 'to'": ''}) dcols = [x[0] for x in cols] #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) elif self.navb.kw.get('group') == 'handwash': title = 'With Handwashing' group = 'handwash' INDICS = [(group, "LOWER(handwash) = 'hw'", title)] cnds.update({"LOWER(handwash) = 'hw'": ''}) dcols = [x[0] for x in cols] #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) elif self.navb.kw.get('group') == 'notoilet': title = 'No Toilet' group = 'notoilet' INDICS = [(group, "LOWER(toilet) = 'nt'", title)] cnds.update({"LOWER(toilet) = 'nt'": ''}) dcols = [x[0] for x in cols] #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) elif self.navb.kw.get('group') == 'nohandwash': title = 'No Handwashing' group = 'nohandwash' INDICS = [(group, "LOWER(handwash) = 'nh'", title)] cnds.update({"LOWER(handwash) = 'nh'": ''}) dcols = [x[0] for x in cols] #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) else: dcols = [x[0] for x in cols] INDICS = [ ("no_risk", queries.NO_RISK['query_str'], 'No Risk'), ("at_risk", queries.RISK['query_str'], 'At Risk'), ("high_risk", queries.HIGH_RISK['query_str'], 'High Risk'), ("toilet", "LOWER(toilet) = 'to'", 'With toilet'), ("handwash", "LOWER(handwash) = 'hw'", 'With handwashing'), ("notoilet", "LOWER(toilet) = 'nt'", 'With no toilet'), ("nohandwash", "LOWER(handwash) = 'nh'", 'With no Handwashing') ] #nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) if self.navb.kw.get( 'view') == 'table' or self.navb.kw.get('view') != 'log': group_by = [] group_by += [ 'province_pk' ] if self.navb.kw.get('nation') or not group_by else [] group_by += ['district_pk'] if self.navb.kw.get('province') else [] group_by += ['referral_facility_pk' ] if self.navb.kw.get('district') else [] group_by += ['facility_pk'] if self.navb.kw.get('hd') else [] #print "\nGROUP BY: %s \n" % group_by LOCS = { 'nation': self.navb.kw.get('nation'), 'province': self.navb.kw.get('province'), 'district': self.navb.kw.get('district'), 'hospital': self.navb.kw.get('hd'), 'location': self.navb.kw.get('hc') } locateds = Pregnancy.fetch_pregnancies_by_location( cnds, group_by=group_by, INDICS=INDICS) #print [[y.__dict__ for y in x] for x in locateds]#, INDICS, LOCS, self.navb.locs() tabular = give_me_table(locateds, self.navb.locs(), INDICS=INDICS, LOCS=LOCS) #print tabular INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS]) nat = Pregnancy.fetch_log_pregnancies(cnds, dcols) DESCRI.append((group, title)) desc = 'Pregnancies%s' % (' (%s)' % (self.navb.find_descr( DESCRI + [(makecol(x[0]), x[2]) for x in INDICS], sc or self.navb.kw.get('group')))) #print title, group, attrs, "NAT: ", nat[0].__dict__ return (title, desc, group, attrs, markup, cols, nat, tabular, locateds, INDICS_HEADERS)