Пример #1
0
    def get_stats(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start): ''})
        red_exts = {}
        red_attrs = [(makecol(x[0]), x[1]) for x in queries.RED_DATA['attrs']]
        red_exts.update(
            dict([(makecol(x[0]), ('COUNT(*)', x[0]))
                  for x in queries.RED_DATA['attrs']]))
        cols = ['COUNT(*) AS total']
        red = Redalert.fetch_redalerts(cnds, cols, red_exts)

        rar_exts = {}
        rar_attrs = [(makecol(x[0]), x[1]) for x in queries.RAR_DATA['attrs']]
        rar_outs = [(makecol(x[0]), x[1]) for x in queries.RAR_DATA['outs']]
        rar_exts.update(
            dict([(makecol(x[0]), ('COUNT(*)', x[0]))
                  for x in queries.RAR_DATA['attrs']]))
        rar_exts.update(
            dict([(makecol(x[0]), ('COUNT(*)', x[0]))
                  for x in queries.RAR_DATA['outs']]))
        rar = Redalert.fetch_redresults(cnds, cols, rar_exts)
        #print red_attrs, rar_attrs, red[0].__dict__, rar[0].__dict__
        return [red_attrs, rar_attrs, rar_outs, red, rar]
Пример #2
0
    def get_stats(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start): ''})
        cols = ['COUNT(*) AS total']
        exts = {}
        nat = [
            ("pre", "Pregnancy", Pregnancy.fetch_pregnancies(cnds, cols,
                                                             exts)),
            ("anc", "Antenatal Consultation",
             Ancvisit.fetch_ancvisits(cnds, cols, exts)),
            ("ref", "Refusal", Refusal.fetch_refusals(cnds, cols, exts)),
            ("red", "Red Alert", Redalert.fetch_redalerts(cnds, cols, exts)),
            ("rar", "Red Alert Result",
             Redalert.fetch_redresults(cnds, cols, exts)),
            ("risk", "Risk", Risk.fetch_risks(cnds, cols, exts)),
            ("res", "Risk Result", Risk.fetch_riskresults(cnds, cols, exts)),
            ("dep", "Departure", Departure.fetch_departures(cnds, cols, exts)),
            ("bir", "Birth", Birth.fetch_births(cnds, cols, exts)),
            ("pnc", "Postnatal Care",
             Pncvisit.fetch_pncvisits(cnds, cols, exts)),
            ("nbc", "Newborn Care", Nbcvisit.fetch_nbcvisits(cnds, cols,
                                                             exts)),
            ("chi", "Child Health",
             Childhealth.fetch_childhealths(cnds, cols, exts)),
            ("cbn", "Community Based Nutrition",
             Nutrition.fetch_nutritions(cnds, cols, exts)),
            ("ccm", "Community Case Management",
             CCM.fetch_ccms(cnds, cols, exts)),
            ("cmr", "Case Management Response",
             CCM.fetch_cmrs(cnds, cols, exts)),
            ("dth", "Death", Death.fetch_deaths(cnds, cols, exts)),
            ("smn", "Severe Malaria",
             Malaria.fetch_malaria(
                 self.extra_cnds(cnds, extra={"keyword = 'SMN'": ''}), cols,
                 exts)),
            ("smr", "Severe Malaria Result",
             Malaria.fetch_malaria(
                 self.extra_cnds(cnds, extra={"keyword = 'SMR'": ''}), cols,
                 exts)),
            ("rso", "Risk Of Stock Out",
             Stock.fetch_stock(
                 self.extra_cnds(cnds, extra={"keyword = 'RSO'": ''}), cols,
                 exts)),
            ("so", "Stock out",
             Stock.fetch_stock(
                 self.extra_cnds(cnds, extra={"keyword = 'SO'": ''}), cols,
                 exts)),
            ("ss", "Stock Supplied",
             Stock.fetch_stock(
                 self.extra_cnds(cnds, extra={"keyword = 'SS'": ''}), cols,
                 exts)),
        ]

        data = {}
        attrs = [(x[0], x[1]) for x in nat]
        total = 0
        for an in nat:
            try:
                value = an[2][0].total
                print value, an[0]
                data.update({an[0]: value})
                total += value
            except Exception, e:
                print e
                data.update({an[0]: 0})
                continue
Пример #3
0
    def get_tables(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start): ''})
        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'), ('message', 'Message'),
                    ('created_at', 'Submission Date')])

        INDICS = [
            ("pre", 'total', "Pregnancy", Pregnancy.fetch_log_pregnancies,
             Pregnancy.fetch_pregnancies_by_location),
            ("anc", 'total', "Antenatal Consultation",
             Ancvisit.fetch_log_ancvisits,
             Ancvisit.fetch_ancvisits_by_location),
            ("ref", 'total', "Refusal", Refusal.fetch_log_refusals,
             Refusal.fetch_refusals_by_location),
            ("red", 'total', "Red Alert", Redalert.fetch_log_redalerts,
             Redalert.fetch_redalerts_by_location),
            ("rar", 'total', "Red Alert Result", Redalert.fetch_log_redresults,
             Redalert.fetch_redresults_by_location),
            ("risk", 'total', "Risk", Risk.fetch_log_risks,
             Risk.fetch_risks_by_location),
            ("res", 'total', "Risk Result", Riskresult.fetch_log_riskresults,
             Riskresult.fetch_riskresults_by_location),
            ("dep", 'total', "Departure", Departure.fetch_log_departures,
             Departure.fetch_departures_by_location),
            ("bir", 'total', "Birth", Birth.fetch_log_births,
             Birth.fetch_births_by_location),
            ("pnc", 'total', "Postnatal Care", Pncvisit.fetch_log_pncvisits,
             Pncvisit.fetch_pncvisits_by_location),
            ("nbc", 'total', "Newborn Care", Nbcvisit.fetch_log_nbcvisits,
             Nbcvisit.fetch_nbcvisits_by_location),
            ("chi", 'total', "Child Health",
             Childhealth.fetch_log_childhealths,
             Childhealth.fetch_childhealths_by_location),
            ("cbn", 'total', "Community Based Nutrition",
             Nutrition.fetch_log_nutritions,
             Nutrition.fetch_nutritions_by_location),
            ("ccm", 'total', "Community Case Management", CCM.fetch_log_ccms,
             CCM.fetch_ccms_by_location),
            ("cmr", 'total', "Case Management Response", CCM.fetch_log_cmrs,
             CCM.fetch_cmrs_by_location),
            ("dth", 'total', "Death", Death.fetch_log_deaths,
             Death.fetch_deaths_by_location),
            ("smn", 'total', "Severe Malaria", Malaria.fetch_log_malaria,
             Malaria.fetch_malaria_by_location),
            ("smr", 'total', "Severe Malaria Result",
             Malaria.fetch_log_malaria, Malaria.fetch_malaria_by_location),
            ("rso", 'total', "Risk Of Stock Out", Stock.fetch_log_stock,
             Stock.fetch_stock_by_location),
            ("so", 'total', "Stock out", Stock.fetch_log_stock,
             Stock.fetch_stock_by_location),
            ("ss", 'total', "Stock Supplied", Stock.fetch_log_stock,
             Stock.fetch_stock_by_location),
        ]

        INDICSDICT = {x[0]: (x[1], x[2], x[3], x[4]) for x in INDICS}

        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('subcat'):
            sc = self.navb.kw.get('subcat')
            wcl = INDICSDICT[sc]  #;print wcl, INDICSDICT
            INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
            INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
            if sc in ['smn', 'smr', 'rso', 'so', 'ss']:
                cnds = self.extra_cnds(
                    cnds, extra={"keyword = '%s'" % sc.upper(): ''})
            dcols = [x[0] for x in cols]
            nat = wcl[2](cnds, dcols)
        else:
            nat = Redalert.fetch_log_redalerts(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')
            }

            if self.navb.kw.get('subcat'):
                #print INDICS, LOCS, cnds, group_by
                wcl = INDICSDICT[sc]
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                locateds = wcl[3](cnds, group_by=group_by, INDICS=INDICS)
            else:
                INDICS = REDINDICS + RARINDICS
                locateds = Redalert.fetch_redalerts_by_location(
                    cnds, group_by=group_by, INDICS=REDINDICS)
                locateds += Redalert.fetch_redresults_by_location(
                    cnds, group_by=group_by, INDICS=RARINDICS)
                #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 = 'Reports%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc or group)))

        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
Пример #4
0
    def get_tables(self):
        cnds = self.navb.conditions()
        cnds.update({"(created_at) <= '%s'" % (self.navb.finish): ''})
        cnds.update({"(created_at) >= '%s'" % (self.navb.start): ''})

        cnds, markup, cols = self.navb.neater_tables(
            cnds=cnds,
            extras=[('national_id', 'Mother ID'),
                    ('user_phone', 'Reporter Phone'),
                    ('birth_date', 'Birth date'),
                    ('child_number', 'Child Number'),
                    ('child_weight', 'Child Weight'),
                    ('mother_weight', 'Mother Weight'),
                    ('created_at', 'Submission Date'), ('indexcol', "ID")])

        REDINDICS = [
            ('red', 'total', 'Total RED'),
        ] + [(makecol(x[0]), x[0], x[1]) for x in queries.RED_DATA['attrs']]
        REDINDICSDICT = {x[0]: (x[1], x[2]) for x in REDINDICS}

        RARINDICS = [
            ('rar', 'total', 'Total RAR'),
        ] + [
            (makecol(x[0]), x[0], x[1]) for x in queries.RAR_DATA['attrs']
        ] + [(makecol(x[0]), x[0], x[1]) for x in queries.RAR_DATA['outs']]
        RARINDICSDICT = {x[0]: (x[1], x[2]) for x in RARINDICS}

        INDICS = REDINDICS
        title, sc, group, attrs, nat, tabular, locateds, INDICS_HEADERS = (
            '', '', '', [], [], [], [], [])

        if self.navb.kw.get('subcat'):
            sc = self.navb.kw.get('subcat')
            if self.navb.kw.get('group') == 'rar' or self.navb.kw.get(
                    'subcat') == 'rar':
                wcl = RARINDICSDICT[sc]  #;print wcl, RARINDICSDICT
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                if wcl and wcl[0] != 'total': cnds.update({wcl[0]: ''})
                dcols = [x[0] for x in cols]
                nat = Redalert.fetch_log_redresults(cnds, dcols)
            else:
                wcl = REDINDICSDICT[sc]  #;print wcl, CCMINDICSDICT
                INDICS = [(sc, wcl[0], wcl[1])] if wcl else []
                INDICS_HEADERS = dict([(makecol(x[0]), x[2]) for x in INDICS])
                if wcl and wcl[0] != 'total': cnds.update({wcl[0]: ''})
                dcols = [x[0] for x in cols]
                nat = Redalert.fetch_log_redalerts(cnds, dcols)

        else:
            if self.navb.kw.get('group') == 'rar' or self.navb.kw.get(
                    'subcat') == 'rar':
                dcols = [x[0] for x in cols]
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=rar&id=%s">View</a>' %
                    (x),
                })
                nat = Redalert.fetch_log_redresults(cnds, dcols)
            else:
                dcols = [x[0] for x in cols]
                markup.update({
                    'indexcol':
                    lambda x, _, __:
                    '<a href="/dashboards/report?tbl=red&id=%s">View</a>' %
                    (x),
                })
                nat = Redalert.fetch_log_redalerts(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')
            }

            if self.navb.kw.get('subcat') in [x[0] for x in REDINDICS]:
                #print REDINDICS, LOCS, cnds, group_by
                locateds = Redalert.fetch_redalerts_by_location(
                    cnds, group_by=group_by, INDICS=REDINDICS)
            elif self.navb.kw.get('subcat') in [x[0] for x in RARINDICS]:
                #print RARINDICS, LOCS, cnds, group_by
                locateds = Redalert.fetch_redresults_by_location(
                    cnds, group_by=group_by, INDICS=RARINDICS)
            else:
                INDICS = REDINDICS + RARINDICS
                locateds = Redalert.fetch_redalerts_by_location(
                    cnds, group_by=group_by, INDICS=REDINDICS)
                locateds += Redalert.fetch_redresults_by_location(
                    cnds, group_by=group_by, INDICS=RARINDICS)
                #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 = 'Red alerts%s' % (
            ' (%s)' % (self.navb.find_descr([(makecol(x[0]), x[2])
                                             for x in INDICS], sc or group)))

        #print INDICS_HEADERS, tabular, locateds
        return (title, desc, group, attrs, markup, cols, nat, tabular,
                locateds, INDICS_HEADERS)
Пример #5
0
 def get_total(self):
     cnds = self.navb.conditions()
     exts = {}
     cols = ['COUNT(*) AS total']
     total = Redalert.fetch_redalerts(cnds, cols, exts)[0]
     return total