Пример #1
0
    def _facility_data(self):
        config = {
            'domain': self.domain,
            'location_id': self.request.GET.get('location_id'),
            'startdate': self.datespan.startdate_utc,
            'enddate': self.datespan.enddate_utc,
            'request': self.request,
        }
        statuses = list(ReportingStatusDataSource(config).get_data())

        results = []
        for status in statuses:
            results.append([
                status['name'],
                status['parent_name'],
                status['last_reporting_date'].date()
                if status['last_reporting_date'] else _('Never'),
                _('Yes')
                if status['reporting_status'] == 'reporting' else _('No'),
            ])

        master_tally = self.status_tally(
            [site['reporting_status'] for site in statuses])

        return master_tally, results
Пример #2
0
    def _aggregate_data(self):
        config = {
            'domain': self.domain,
            'location_id': self.request.GET.get('location_id'),
            'startdate': self.datespan.startdate_utc,
            'enddate': self.datespan.enddate_utc,
            'request': self.request,
        }
        statuses = list(ReportingStatusDataSource(config).get_data())

        def child_loc(path):
            root = self.active_location
            ix = path.index(root._id) if root else -1
            try:
                return path[ix + 1]
            except IndexError:
                return None

        def case_iter():
            for site in statuses:
                if child_loc(site['loc_path']) is not None:
                    yield (site['loc_path'], site['reporting_status'])

        status_by_agg_site = map_reduce(
            lambda (path, status): [(child_loc(path), status)],
            data=case_iter())
        sites_by_agg_site = map_reduce(
            lambda (path, status): [(child_loc(path), path[-1])],
            data=case_iter())

        status_counts = dict(
            (loc_id, self.status_tally(statuses))
            for loc_id, statuses in status_by_agg_site.iteritems())

        master_tally = self.status_tally(
            [site['reporting_status'] for site in statuses])

        locs = (SQLLocation.objects.filter(
            is_archived=False,
            location_id__in=status_counts.keys()).order_by('name'))

        def fmt(pct):
            return '%.1f%%' % (100. * pct)

        def fmt_pct_col(loc_id, col_type):
            return fmt(status_counts[loc_id].get(col_type, {'pct': 0.})['pct'])

        def fmt_count_col(loc_id, col_type):
            return status_counts[loc_id].get(col_type, {'count': 0})['count']

        def _rows():
            for loc in locs:
                row = [loc.name, len(sites_by_agg_site[loc.location_id])]
                for k in ('reporting', 'nonreporting'):
                    row.append(fmt_count_col(loc.location_id, k))
                    row.append(fmt_pct_col(loc.location_id, k))

                yield row

        return master_tally, _rows()
Пример #3
0
    def _data(self):
        config = {
            'domain': self.domain,
            'location_id': self.request.GET.get('location_id'),
            'startdate': self.datespan.startdate_utc,
            'enddate': self.datespan.enddate_utc,
            'request': self.request,
        }
        statuses = list(ReportingStatusDataSource(config).get_data())
        def child_loc(path):
            root = self.active_location
            ix = path.index(root._id) if root else -1
            try:
                return path[ix + 1]
            except IndexError:
                return None

        def case_iter():
            for site in statuses:
                if child_loc(site['loc_path']) is not None:
                    yield (site['loc_path'], site['reporting_status'])
        status_by_agg_site = map_reduce(lambda (path, status): [(child_loc(path), status)],
                                        data=case_iter())
        sites_by_agg_site = map_reduce(lambda (path, status): [(child_loc(path), path[-1])],
                                       data=case_iter())

        def status_tally(statuses):
            total = len(statuses)

            return map_reduce(lambda s: [(s,)],
                              lambda v: {'count': len(v), 'pct': len(v) / float(total)},
                              data=statuses)
        status_counts = dict((loc_id, status_tally(statuses))
                             for loc_id, statuses in status_by_agg_site.iteritems())

        master_tally = status_tally([site['reporting_status'] for site in statuses])

        locs = sorted(Location.view('_all_docs', keys=status_counts.keys(), include_docs=True),
                      key=lambda loc: loc.name)

        def fmt(pct):
            return '%.1f%%' % (100. * pct)

        def fmt_pct_col(loc, col_type):
            return fmt(status_counts[loc._id].get(col_type, {'pct': 0.})['pct'])

        def fmt_count_col(loc, col_type):
            return status_counts[loc._id].get(col_type, {'count': 0})['count']

        def _rows():
            for loc in locs:
                row = [loc.name, len(sites_by_agg_site[loc._id])]
                for k in ('reporting', 'nonreporting'):
                    row.append(fmt_count_col(loc, k))
                    row.append(fmt_pct_col(loc, k))

                yield row

        return master_tally, _rows()