Пример #1
0
def agg_arrival_for(indicator, entity, year, month):
    if month is not None:
        periods = [MonthPeriod.get_or_create(year, month)]
    else:
        periods = MonthPeriod.all_from(MonthPeriod.get_or_create(year, 1),
                                       MonthPeriod.get_or_create(year, 12))

    expected_entities = expected_entities_for(indicator, entity)

    qs = DataRecord.objects.filter(indicator=indicator,
                                   period__in=periods) \
                           .filter(entity__in=expected_entities)

    nb_expected_reports = len(expected_entities)
    nb_arrived_reports = qs.count()
    nb_prompt_reports = qs.filter(arrival_status=DataRecord.ARRIVED_ON_TIME) \
                          .count()

    return {
        'nb_expected_reports': nb_expected_reports,
        'nb_arrived_reports': nb_arrived_reports,
        'nb_prompt_reports': nb_prompt_reports,
        'completeness': nb_arrived_reports / nb_expected_reports,
        'promptness': nb_prompt_reports / nb_expected_reports
    }
Пример #2
0
def agg_arrival_for(indicator, entity, year, month):
    if month is not None:
        periods = [MonthPeriod.get_or_create(year, month)]
    else:
        periods = MonthPeriod.all_from(MonthPeriod.get_or_create(year, 1),
                                       MonthPeriod.get_or_create(year, 12))

    expected_entities = expected_entities_for(indicator, entity)

    qs = DataRecord.objects.filter(indicator=indicator,
                                   period__in=periods) \
                           .filter(entity__in=expected_entities)

    nb_expected_reports = len(expected_entities)
    nb_arrived_reports = qs.count()
    nb_prompt_reports = qs.filter(arrival_status=DataRecord.ARRIVED_ON_TIME) \
                          .count()

    return {
        'nb_expected_reports': nb_expected_reports,
        'nb_arrived_reports': nb_arrived_reports,
        'nb_prompt_reports': nb_prompt_reports,
        'completeness': nb_arrived_reports / nb_expected_reports,
        'promptness': nb_prompt_reports / nb_expected_reports
    }
Пример #3
0
 def year_data_for(self, entity, year, month=None):
     if month is not None:
         periods = [MonthPeriod.get_or_create(year, month)]
     else:
         periods = MonthPeriod.all_from(MonthPeriod.get_or_create(year, 1),
                                        MonthPeriod.get_or_create(year, 12))
     return self.data_for(entity, periods)
Пример #4
0
 def year_data_for(self, entity, year, month=None):
     if month is not None:
         periods = [MonthPeriod.get_or_create(year, month)]
     else:
         periods = MonthPeriod.all_from(
             MonthPeriod.get_or_create(year, 1),
             MonthPeriod.get_or_create(year, 12))
     return self.data_for(entity, periods)
Пример #5
0
def png_map_for(request,
                perioda_str,
                periodb_str,
                entity_name,
                indicator_number,
                with_title=True,
                with_legend=True):

    entity = Entity.get_by_short_name(entity_name)
    if entity is None:
        raise Http404(
            _("Unknown entity with name `{u}`").format(u=entity_name))

    if perioda_str is None and periodb_str is None \
            and indicator_number is None:
        periods = None
        indicator = None
        with_title = False
        fname = "initial.png"
    else:
        with_title = True
        perioda = process_period_filter(request, perioda_str,
                                        'perioda').get('perioda')
        periodb = process_period_filter(request, periodb_str,
                                        'periodb').get('periodb')
        periods = MonthPeriod.all_from(perioda, periodb)
        if not len(periods):
            raise Http404(
                _("Unknown period interval `{pa}` `{pb}`").format(
                    pa=perioda_str, pb=periodb_str))

        indicator = Indicator.get_by_number(indicator_number)
        if indicator is None:
            raise Http404(
                _("Unknown indicator `{s}`").format(s=indicator_number))

        fname = fname_for(entity, periods, indicator)

    fpath = os.path.join('png_map', fname)
    abspath = os.path.join(settings.EXPORT_REPOSITORY, fpath)

    if not os.path.exists(abspath):
        try:
            gen_map_for(entity,
                        periods,
                        indicator,
                        save_as=abspath,
                        with_title=with_title,
                        with_index=with_title)
        except IOError:
            logger.error("Missing map png folder in exports.")
            raise

    # return redirect('export', fpath=fpath)
    return serve_exported_files(request, fpath=fpath)
Пример #6
0
Файл: api.py Проект: yeleman/dmd
def png_map_for(request, perioda_str, periodb_str,
                entity_name, indicator_number,
                with_title=True, with_legend=True):

    entity = Entity.get_by_short_name(entity_name)
    if entity is None:
        raise Http404(_("Unknown entity with name `{u}`")
                      .format(u=entity_name))

    if perioda_str is None and periodb_str is None \
            and indicator_number is None:
        periods = None
        indicator = None
        with_title = False
        fname = "initial.png"
    else:
        with_title = True
        perioda = process_period_filter(
            request, perioda_str, 'perioda').get('perioda')
        periodb = process_period_filter(
            request, periodb_str, 'periodb').get('periodb')
        periods = MonthPeriod.all_from(perioda, periodb)
        if not len(periods):
            raise Http404(_("Unknown period interval `{pa}` `{pb}`")
                          .format(pa=perioda_str, pb=periodb_str))

        indicator = Indicator.get_by_number(indicator_number)
        if indicator is None:
            raise Http404(_("Unknown indicator `{s}`")
                          .format(s=indicator_number))

        fname = fname_for(entity, periods, indicator)

    fpath = os.path.join('png_map', fname)
    abspath = os.path.join(settings.EXPORT_REPOSITORY, fpath)

    if not os.path.exists(abspath):
        try:
            gen_map_for(entity, periods, indicator,
                        save_as=abspath,
                        with_title=with_title,
                        with_index=with_title)
        except IOError:
            logger.error("Missing map png folder in exports.")
            raise

    # return redirect('export', fpath=fpath)
    return serve_exported_files(request, fpath=fpath)
Пример #7
0
def view(request,
         entity_uuid=None,
         perioda_str=None,
         periodb_str=None,
         indicator_slug=None,
         **kwargs):
    context = {'page': 'analysis_section1'}

    # handling entity
    context.update(process_entity_filter(request, entity_uuid))

    # handling periods
    context.update(process_period_filter(request, perioda_str, 'perioda'))
    context.update(process_period_filter(request, periodb_str, 'periodb'))
    if context['perioda'] > context['periodb']:
        context['perioda'], context['periodb'] = \
            context['periodb'], context['perioda']
    periods = MonthPeriod.all_from(context['perioda'], context['periodb'])
    context.update({'selected_periods': periods})

    all_indicators = Indicator.objects.all()
    if indicator_slug:
        qs = all_indicators.filter(slug=indicator_slug)
        indicator = qs.get()
    else:
        qs = indicator = None

    context.update({
        'section':
        text_type(SECTION_ID),
        'section_name':
        SECTION_NAME,
        'elements':
        build_context(periods=periods, entity=context['entity'], qs=qs),
        'indicators':
        all_indicators,
        'indicator':
        indicator,
    })

    # absolute URI for links
    context.update({'baseurl': request.build_absolute_uri()})

    return render(request, kwargs.get('template_name',
                                      'analysis_section1.html'), context)
Пример #8
0
def analysis(request,
             section_id,
             entity_uuid=None,
             perioda_str=None,
             periodb_str=None,
             *args,
             **kwargs):
    ''' Generic view for simple sections using an entity and periods '''

    context = {'page': 'analysis_section{}'.format(section_id)}

    if section_id not in SECTIONS:
        raise Http404(_("Unknown section ID `{sid}`").format(sid=section_id))

    section = import_path('dmd.analysis.section{}'.format(section_id))

    # handling entity
    context.update(process_entity_filter(request, entity_uuid))

    # handling periods
    context.update(process_period_filter(request, perioda_str, 'perioda'))
    context.update(process_period_filter(request, periodb_str, 'periodb'))
    if context['perioda'] > context['periodb']:
        context['perioda'], context['periodb'] = \
            context['periodb'], context['perioda']
    periods = MonthPeriod.all_from(context['perioda'], context['periodb'])
    context.update({'selected_periods': periods})

    context.update({
        'section':
        section_id,
        'section_name':
        SECTIONS.get(section_id),
        'elements':
        section.build_context(periods=periods, entity=context['entity'])
    })

    # absolute URI for links
    context.update({'baseurl': request.build_absolute_uri()})

    return render(
        request,
        kwargs.get('template_name',
                   'analysis_section{}.html'.format(section_id)), context)
Пример #9
0
def view(request, entity_uuid=None, perioda_str=None, periodb_str=None,
         indicator_slug=None, **kwargs):
    context = {'page': 'analysis_section1'}

    # handling entity
    context.update(process_entity_filter(request, entity_uuid))

    # handling periods
    context.update(process_period_filter(request, perioda_str, 'perioda'))
    context.update(process_period_filter(request, periodb_str, 'periodb'))
    if context['perioda'] > context['periodb']:
        context['perioda'], context['periodb'] = \
            context['periodb'], context['perioda']
    periods = MonthPeriod.all_from(context['perioda'], context['periodb'])
    context.update({'selected_periods': periods})

    all_indicators = Indicator.objects.all()
    if indicator_slug:
        qs = all_indicators.filter(slug=indicator_slug)
        indicator = qs.get()
    else:
        qs = indicator = None

    context.update({
        'section': text_type(SECTION_ID),
        'section_name': SECTION_NAME,
        'elements': build_context(periods=periods,
                                  entity=context['entity'],
                                  qs=qs),
        'indicators': all_indicators,
        'indicator': indicator,
    })

    # absolute URI for links
    context.update({'baseurl': request.build_absolute_uri()})

    return render(request,
                  kwargs.get('template_name',
                             'analysis_section1.html'),
                  context)
Пример #10
0
def analysis(request, section_id,
             entity_uuid=None, perioda_str=None, periodb_str=None,
             *args, **kwargs):
    ''' Generic view for simple sections using an entity and periods '''

    context = {'page': 'analysis_section{}'.format(section_id)}

    if section_id not in SECTIONS:
        raise Http404(_("Unknown section ID `{sid}`").format(sid=section_id))

    section = import_path('dmd.analysis.section{}'.format(section_id))

    # handling entity
    context.update(process_entity_filter(request, entity_uuid))

    # handling periods
    context.update(process_period_filter(request, perioda_str, 'perioda'))
    context.update(process_period_filter(request, periodb_str, 'periodb'))
    if context['perioda'] > context['periodb']:
        context['perioda'], context['periodb'] = \
            context['periodb'], context['perioda']
    periods = MonthPeriod.all_from(context['perioda'], context['periodb'])
    context.update({'selected_periods': periods})

    context.update({
        'section': section_id,
        'section_name': SECTIONS.get(section_id),
        'elements': section.build_context(periods=periods,
                                          entity=context['entity'])
    })

    # absolute URI for links
    context.update({'baseurl': request.build_absolute_uri()})

    return render(request,
                  kwargs.get('template_name',
                             'analysis_section{}.html'.format(section_id)),
                  context)
Пример #11
0
def view(request, entity_uuid=None, perioda_str=None, periodb_str=None,
         indicator_slug=None, **kwargs):
    context = {'page': 'analysis_section2'}

    # handling entity
    context.update(process_entity_filter(request, entity_uuid))

    # handling periods
    # context.update(process_period_filter(request, period_str, 'period'))
    context.update(process_period_filter(request, perioda_str, 'perioda'))
    context.update(process_period_filter(request, periodb_str, 'periodb'))
    if context['perioda'] > context['periodb']:
        context['perioda'], context['periodb'] = \
            context['periodb'], context['perioda']
    periods = MonthPeriod.all_from(context['perioda'], context['periodb'])
    context.update({'selected_periods': periods})

    def cached_data_list(entity, periods, indicator):
        return [get_cached_data('section2-arrivals',
                                entity=entity,
                                period=period,
                                indicator=indicator)
                for period in periods]

    context.update({
        'section': text_type(SECTION_ID),
        'section_name': SECTION_NAME,
        'arrivals': OrderedDict(
            [(indicator, cached_data_list(context['entity'],
                                          periods, indicator))
             for indicator in Indicator.get_all_routine()])
    })

    # evolution graph
    cp = {
        'periods': [period.to_tuple() for period in periods],
        'points': [get_cached_data('section2-points',
                                   entity=context['entity'], period=period)
                   for period in periods]
    }
    perioda = periods[0]
    periodb = periods[-1]
    context.update({
        'cp_title': "Évolution de la complétude à {name} entre {pa} et {pb}"
                    .format(name=context['entity'].short_name,
                            pa=perioda.strid,
                            pb=periodb.strid),
        'cp_fname': "completeness-_{pa}_{pb}"
                    .format(pa=perioda.strid, pb=periodb.strid),
        'cp_categories': [p[1].name for p in cp['periods']],
        'cp_series': [{'name': "Complétude", 'data': cp['points']}]
    })

    # absolute URI for links
    context.update({
        'baseurl': request.build_absolute_uri(),
        'lineage': [Entity.PROVINCE]})

    return render(request,
                  kwargs.get('template_name',
                             'analysis_section2.html'),
                  context)
Пример #12
0
def view(request,
         entity_uuid=None,
         perioda_str=None,
         periodb_str=None,
         indicator_slug=None,
         **kwargs):
    context = {'page': 'analysis_section2'}

    # handling entity
    context.update(process_entity_filter(request, entity_uuid))

    # handling periods
    # context.update(process_period_filter(request, period_str, 'period'))
    context.update(process_period_filter(request, perioda_str, 'perioda'))
    context.update(process_period_filter(request, periodb_str, 'periodb'))
    if context['perioda'] > context['periodb']:
        context['perioda'], context['periodb'] = \
            context['periodb'], context['perioda']
    periods = MonthPeriod.all_from(context['perioda'], context['periodb'])
    context.update({'selected_periods': periods})

    def cached_data_list(entity, periods, indicator):
        return [
            get_cached_data('section2-arrivals',
                            entity=entity,
                            period=period,
                            indicator=indicator) for period in periods
        ]

    context.update({
        'section':
        text_type(SECTION_ID),
        'section_name':
        SECTION_NAME,
        'arrivals':
        OrderedDict([(indicator,
                      cached_data_list(context['entity'], periods, indicator))
                     for indicator in Indicator.get_all_routine()])
    })

    # evolution graph
    cp = {
        'periods': [period.to_tuple() for period in periods],
        'points': [
            get_cached_data('section2-points',
                            entity=context['entity'],
                            period=period) for period in periods
        ]
    }
    perioda = periods[0]
    periodb = periods[-1]
    context.update({
        'cp_title':
        "Évolution de la complétude à {name} entre {pa} et {pb}".format(
            name=context['entity'].short_name,
            pa=perioda.strid,
            pb=periodb.strid),
        'cp_fname':
        "completeness-_{pa}_{pb}".format(pa=perioda.strid, pb=periodb.strid),
        'cp_categories': [p[1].name for p in cp['periods']],
        'cp_series': [{
            'name': "Complétude",
            'data': cp['points']
        }]
    })

    # absolute URI for links
    context.update({
        'baseurl': request.build_absolute_uri(),
        'lineage': [Entity.PROVINCE]
    })

    return render(request, kwargs.get('template_name',
                                      'analysis_section2.html'), context)