Beispiel #1
0
def load_reports():
    reports = [u.extract_report(r) for r in PBFReport.objects.all().select_related()]

    wait_buckets = [(2, '<2'), (4, '2-4'), (None, '>4')]

    for r in reports:
        u.anonymize_contact(r)
        ts = datetime.strptime(r['timestamp'], '%Y-%m-%dT%H:%M:%S')
        r['month'] = ts.strftime('%b %Y')
        r['_month'] = ts.strftime('%Y-%m')
        for thresh, label in wait_buckets:
            if thresh is None or r['waiting_time'] < thresh:
                r['wait_bucket'] = label
                break

    return reports
Beispiel #2
0
def load_reports(user=None):
    # TODO: filtering by state
    PBFReport = get_model('dashboard', 'PBFReport')
    ReportComment = get_model('dashboard', 'ReportComment')
    ReportCommentView = get_model('dashboard', 'ReportCommentView')

    facilities = map_reduce(get_facilities(), lambda e: [(e['id'], e)], lambda v: v[0])
    reports = [u.extract_report(r) for r in PBFReport.objects.all().select_related()]
    comments = map_reduce(ReportComment.objects.filter(pbf_report__isnull=False), lambda c: [(c.pbf_report_id, c)])

    wait_buckets = [(2, '<2'), (4, '2-4'), (None, '>4')]

    views = []
    if user:
        views = ReportCommentView.objects.filter(user=user)\
                                         .values_list('report_comment', flat=True)
    def _get_json(comment):  # Add whether or not the comment has been viewed.
        json = comment.json()
        json.update({'viewed': comment.pk in views if user else None})
        return json

    for r in reports:
        u.anonymize_contact(r)
        ts = datetime.strptime(r['timestamp'], '%Y-%m-%dT%H:%M:%S')
        r['thread'] = [_get_json(c) for c in sorted(comments.get(r['id'], []), key=lambda c: c.date)]
        r['month'] = ts.strftime('%b %Y')
        r['_month'] = ts.strftime('%Y-%m')
        r['display_time'] = datetime.strptime(r['timestamp'], '%Y-%m-%dT%H:%M:%S').strftime('%d/%m/%y at %H:%M')
        r['site_name'] = facilities[r['facility']]['name'] if r['for_this_site'] else r['site_other']
        if r['waiting_time'] is not None:
            for thresh, label in wait_buckets:
                if thresh is None or r['waiting_time'] < thresh:
                    r['wait_bucket'] = label
                    break
        else:
            r['wait_bucket'] = None

    return reports
Beispiel #3
0
def load_reports(state=None, anonymize=True):
    _loc = u._fac_cache('fug')
    def extract_report(r):
        data = u.extract_report(r)
        fug_id = data['facility']
        fug = _loc(fug_id)
        data['fug'] = fug.name
        data['facility'] = fug.parent_id
        return data

    reports = [extract_report(r) for r in FadamaReport.objects.all().select_related()]

    facs = facilities_by_id()
    reports = [r for r in reports if state is None or state == facs[r['facility']]['state']]
    # todo: these should probably be loaded on-demand for individual reports
    comments = map_reduce(ReportComment.objects.all(), lambda c: [(c.report_id, c)])

    def _ts(r):
        return datetime.strptime(r['timestamp'], '%Y-%m-%dT%H:%M:%S')

    for r in reports:
        if not anonymize:
            r['_contact'] = r['contact']
        u.anonymize_contact(r)
        r['month'] = _ts(r).strftime('%b %Y')
        r['_month'] = _ts(r).strftime('%Y-%m')
        r['thread'] = [c.json() for c in sorted(comments.get(r['id'], []), key=lambda c: c.date)]
        r['display_time'] = _ts(r).strftime('%d/%m/%y %H:%M')
        r['site_name'] = facs[r['facility']]['name']

    reports_by_contact = map_reduce((r for r in reports if not r['proxy']), lambda r: [(r['contact'], r)])

    for r in reports:
        r['from_same'] = [k['id'] for k in reports_by_contact.get(r['contact'], []) if k != r and abs(_ts(r) - _ts(k)) <= settings.RECENT_REPORTS_FROM_SAME_PHONE_WINDOW]

    return reports
Beispiel #4
0
def load_reports(user=None, state=None, anonymize=True):
    FadamaReport = get_model('dashboard', 'FadamaReport')
    ReportComment = get_model('dashboard', 'ReportComment')
    ReportCommentView = get_model('dashboard', 'ReportCommentView')

    facs = facilities_by_id()
    fugs = u._fac_cache('fug')

    def extract_report(r):
        data = u.extract_report(r)
        loc_id = data['facility']

        fug = fugs(loc_id)
        if fug:
            data['fug'] = fug.name
            data['facility'] = fug.parent_id

        elif facs.get(loc_id):
            data['fug'] = None
            data['facility'] = loc_id

        else:
            data['fug'] = None
            data['facility'] = None

        return data

    reports = [extract_report(r) for r in FadamaReport.objects.all().select_related()]

    def filter_state(r, state):
        if state is None:
            return True
        else:
            reporting_fug = fugs(r['reporting_facility'])
            if reporting_fug:
                reporting_fca_id = reporting_fug.parent_id
            else:
                reporting_fca_id = r['reporting_facility']
            reporting_state = facs[reporting_fca_id]['state']
            return state == reporting_state

    reports = [r for r in reports if filter_state(r, state)]
    # todo: these should probably be loaded on-demand for individual reports
    comments = map_reduce(ReportComment.objects.filter(fadama_report__isnull=False), lambda c: [(c.fadama_report_id, c)])

    def _ts(r):
        return datetime.strptime(r['timestamp'], '%Y-%m-%dT%H:%M:%S')

    views = []
    if user:
        views = ReportCommentView.objects.filter(user=user)\
                                         .values_list('report_comment', flat=True)
    def _get_json(comment):  # Add whether or not the comment has been viewed.
        json = comment.json()
        json.update({'viewed': comment.pk in views if user else None})
        return json

    for r in reports:
        if not anonymize:
            r['_contact'] = r['contact']
        u.anonymize_contact(r)
        r['month'] = _ts(r).strftime('%b %Y')
        r['_month'] = _ts(r).strftime('%Y-%m')
        r['thread'] = [_get_json(c) for c in sorted(comments.get(r['id'], []), key=lambda c: c.date)]
        r['display_time'] = _ts(r).strftime('%d/%m/%y at %H:%M')
        r['site_name'] = facs[r['facility']]['name'] if r['for_this_site'] else r['site_other']

    reports_by_contact = map_reduce((r for r in reports if not r['proxy']), lambda r: [(r['contact'], r)])

    for r in reports:
        r['from_same'] = [k['id'] for k in reports_by_contact.get(r['contact'], []) if k != r and abs(_ts(r) - _ts(k)) <= settings.RECENT_REPORTS_FROM_SAME_PHONE_WINDOW]

    return reports