Ejemplo n.º 1
0
 def partial_response(self):
     indicator_slug = self.request.GET.get('indicator')
     response = {
         'error': True,
         'message': 'Indicator could not be processed.'
     }
     cache_key = self.indicator_cache_key(indicator_slug)
     cached_data = cache.get(cache_key)
     if cached_data and self.cache_indicators:
         response = cached_data
     elif indicator_slug:
         try:
             indicator = DynamicIndicatorDefinition.get_current(MVP.NAMESPACE, self.domain, indicator_slug,
                 wrap_correctly=True)
             response = self.get_response_for_indicator(indicator)
             if response is not None:
                 cache.set(cache_key, response, 3600)
             else:
                 response = {
                     'error': True,
                     'message': 'This Indicator is currently undergoing updates. Please check back shortly.'
                 }
         except (ResourceNotFound, AttributeError):
             response['message'] = "Indicator '%s' could not be found." % indicator_slug
     return HttpResponse(json.dumps(response))
Ejemplo n.º 2
0
    def handle(self, *args, **options):
        all_indicators = DynamicIndicatorDefinition.view("indicators/dynamic_indicator_definitions",
            reduce=False,
            include_docs=True,
            startkey=["namespace domain slug", MVP.NAMESPACE],
            endkey=["namespace domain slug", MVP.NAMESPACE, {}]
        ).all()
        for ind in all_indicators:
            ind.delete()

        for domain in MVP.DOMAINS:
            shared_args=(
                MVP.NAMESPACE,
                domain
                )
            shared_kwargs = dict(
                version=1
            )

            self.create_indicators_of_type(CouchIndicatorDef,
                SIMPLE_COUCH_VIEW_INDICATORS,
                shared_args, shared_kwargs)

            self.create_indicators_of_type(MedianCouchIndicatorDef,
                MEDIAN_INDICATORS,
                shared_args, shared_kwargs)

            self.create_indicators_of_type(MVPActiveCasesIndicatorDefinition,
                ACTIVE_CASES_INDICATORS,
                shared_args, shared_kwargs)

            self.create_indicators_of_type(MVPChildCasesByAgeIndicatorDefinition,
                ACTIVE_CHILD_CASES_INDICATORS,
                shared_args, shared_kwargs)

            self.create_indicators_of_type(CountUniqueCouchIndicatorDef,
                COUNT_UNIQUE_INDICATORS,
                shared_args, shared_kwargs)

            self.create_indicators_of_type(SumLastEmittedCouchIndicatorDef,
                SUM_LAST_UNIQUE_INICATORS,
                shared_args, shared_kwargs)

            for indicator_slug, indicator_kwargs in COMPOSITE_INDICATORS.items():
                indicator_kwargs.update(shared_kwargs)
                indicator_def = CombinedCouchViewIndicatorDefinition.increment_or_create_unique(
                    *shared_args,
                    slug=indicator_slug,
                    **indicator_kwargs
                )
                indicator_def.save()

            days_since_last_transmission = MVPDaysSinceLastTransmission.increment_or_create_unique(
                *shared_args,
                slug="days_since_last_transmission",
                description="Days since last transmission",
                title="Days since last transmission"
            )
            days_since_last_transmission.save()
Ejemplo n.º 3
0
 def available_slugs(self):
     key = [self.cleaned_data['namespace'], self.domain]
     slugs = DynamicIndicatorDefinition.get_db().view("indicators/available_to_combine",
                                                      group=True,
                                                      group_level=3,
                                                      startkey=key,
                                                      endkey=key+[{}]
     ).all()
     return [s['key'][-1] for s in slugs]
Ejemplo n.º 4
0
 def available_slugs(self):
     key = [self.cleaned_data['namespace'], self.domain]
     slugs = DynamicIndicatorDefinition.get_db().view(
         "indicators/available_to_combine",
         group=True,
         group_level=3,
         startkey=key,
         endkey=key + [{}]).all()
     return [s['key'][-1] for s in slugs]
Ejemplo n.º 5
0
    def report_context(self):
        report_matrix = []
        month_headers = None
        for category_group in self.indicator_slugs:
            category_indicators = []
            total_rowspan = 0
            for slug in category_group['indicator_slugs']:
                try:
                    indicator = DynamicIndicatorDefinition.get_current(
                        MVP.NAMESPACE,
                        self.domain,
                        slug,
                        wrap_correctly=True,
                    )
                    if self.is_rendered_as_email:
                        retrospective = indicator.get_monthly_retrospective(
                            user_ids=self.user_ids)
                    else:
                        retrospective = indicator.get_monthly_retrospective(
                            return_only_dates=True)
                    if not month_headers:
                        month_headers = self.get_month_headers(retrospective)

                    if isinstance(indicator,
                                  CombinedCouchViewIndicatorDefinition):
                        table = self.get_indicator_table(retrospective)
                        indicator_rowspan = 3
                    else:
                        table = self.get_indicator_row(retrospective)
                        indicator_rowspan = 1

                    total_rowspan += indicator_rowspan + 1
                    category_indicators.append(
                        dict(title=indicator.description,
                             table=table,
                             load_url="%s?indicator=%s" %
                             (self.get_url(self.domain, render_as='partial'),
                              indicator.slug),
                             rowspan=indicator_rowspan))
                except (AttributeError, ResourceNotFound):
                    logging.info("Could not grab indicator %s in domain %s" %
                                 (slug, self.domain))
            report_matrix.append(
                dict(
                    category_title=category_group['category_title'],
                    category_slug=category_group['category_slug'],
                    rowspan=total_rowspan,
                    indicators=category_indicators,
                ))
        return dict(
            months=month_headers,
            report=report_matrix,
        )
Ejemplo n.º 6
0
    def handle(self, **options):
        all_indicators = DynamicIndicatorDefinition.view(
            "indicators/dynamic_indicator_definitions",
            reduce=False,
            include_docs=True,
            startkey=["namespace domain slug", MVP.NAMESPACE],
            endkey=["namespace domain slug", MVP.NAMESPACE, {}]).all()
        for ind in all_indicators:
            ind.delete()

        for domain in MVP.DOMAINS:
            shared_args = (MVP.NAMESPACE, domain)
            shared_kwargs = dict(version=1)

            self.create_indicators_of_type(CouchIndicatorDef,
                                           SIMPLE_COUCH_VIEW_INDICATORS,
                                           shared_args, shared_kwargs)

            self.create_indicators_of_type(MedianCouchIndicatorDef,
                                           MEDIAN_INDICATORS, shared_args,
                                           shared_kwargs)

            self.create_indicators_of_type(MVPActiveCasesIndicatorDefinition,
                                           ACTIVE_CASES_INDICATORS,
                                           shared_args, shared_kwargs)

            self.create_indicators_of_type(
                MVPChildCasesByAgeIndicatorDefinition,
                ACTIVE_CHILD_CASES_INDICATORS, shared_args, shared_kwargs)

            self.create_indicators_of_type(CountUniqueCouchIndicatorDef,
                                           COUNT_UNIQUE_INDICATORS,
                                           shared_args, shared_kwargs)

            self.create_indicators_of_type(SumLastEmittedCouchIndicatorDef,
                                           SUM_LAST_UNIQUE_INICATORS,
                                           shared_args, shared_kwargs)

            for indicator_slug, indicator_kwargs in COMPOSITE_INDICATORS.items(
            ):
                indicator_kwargs.update(shared_kwargs)
                indicator_def = CombinedCouchViewIndicatorDefinition.increment_or_create_unique(
                    *shared_args, slug=indicator_slug, **indicator_kwargs)
                indicator_def.save()

            days_since_last_transmission = MVPDaysSinceLastTransmission.increment_or_create_unique(
                *shared_args,
                slug="days_since_last_transmission",
                description="Days since last transmission",
                title="Days since last transmission")
            days_since_last_transmission.save()
Ejemplo n.º 7
0
 def indicators(self):
     all_indicators = list()
     for section in self.indicator_slugs:
         section_indicators = list()
         for indicator_content in section.get('indicators', []):
             slug = indicator_content.get('slug')
             if slug:
                 indicator = DynamicIndicatorDefinition.get_current(MVP.NAMESPACE,
                     self.domain, slug, wrap_correctly=True)
                 if indicator is not None:
                     section_indicators.append(indicator)
                 else:
                     logging.error("could not load indicator %s" % slug)
         all_indicators.append(section_indicators)
     return all_indicators
Ejemplo n.º 8
0
 def indicators(self):
     all_indicators = list()
     for section in self.indicator_slugs:
         section_indicators = list()
         for indicator_content in section.get('indicators', []):
             slug = indicator_content.get('slug')
             if slug:
                 indicator = DynamicIndicatorDefinition.get_current(
                     MVP.NAMESPACE, self.domain, slug, wrap_correctly=True)
                 if indicator is not None:
                     section_indicators.append(indicator)
                 else:
                     logging.error("could not load indicator %s" % slug)
         all_indicators.append(section_indicators)
     return all_indicators
Ejemplo n.º 9
0
    def report_context(self):
        report_matrix = []
        month_headers = None
        for category_group in self.indicator_slugs:
            category_indicators = []
            total_rowspan = 0
            for slug in category_group['indicator_slugs']:
                try:
                    indicator = DynamicIndicatorDefinition.get_current(
                        MVP.NAMESPACE, self.domain, slug,
                        wrap_correctly=True,
                        use_new_db=self.use_new_db,
                    )
                    if self.is_rendered_as_email:
                        retrospective = indicator.get_monthly_retrospective(user_ids=self.user_ids)
                    else:
                        retrospective = indicator.get_monthly_retrospective(return_only_dates=True)
                    if not month_headers:
                        month_headers = self.get_month_headers(retrospective)

                    if isinstance(indicator, CombinedCouchViewIndicatorDefinition):
                        table = self.get_indicator_table(retrospective)
                        indicator_rowspan = 3
                    else:
                        table = self.get_indicator_row(retrospective)
                        indicator_rowspan = 1

                    total_rowspan += indicator_rowspan + 1
                    category_indicators.append(dict(
                        title=indicator.description,
                        table=table,
                        load_url="%s?indicator=%s" % (self.get_url(self.domain,
                                                      render_as='partial'), indicator.slug),
                        rowspan=indicator_rowspan
                    ))
                except (AttributeError, ResourceNotFound):
                    logging.info("Could not grab indicator %s in domain %s" % (slug, self.domain))
            report_matrix.append(dict(
                category_title=category_group['category_title'],
                category_slug=category_group['category_slug'],
                rowspan=total_rowspan,
                indicators=category_indicators,
            ))
        return dict(
            months=month_headers,
            report=report_matrix,
        )
Ejemplo n.º 10
0
 def get_indicator_response(self, indicator_slug):
     indicator = DynamicIndicatorDefinition.get_current(MVP.NAMESPACE, self.domain, indicator_slug,
                 wrap_correctly=True)
     result = indicator.get_value(self.user_ids, datespan=self.datespan)
     print result
 def get_indicator_response(self, indicator_slug):
     indicator = DynamicIndicatorDefinition.get_current(MVP.NAMESPACE, self.domain, indicator_slug,
                 wrap_correctly=True)
     result = indicator.get_value(self.user_ids, datespan=self.datespan)
     print(result)