def refresh_metrics_cache_data(self):
     self.refresh_data_for_metrics_cache(MetricsEnrollmentStatusCacheDao())
     logging.info('Refresh MetricsEnrollmentStatusCache done.')
     self.refresh_data_for_metrics_cache(
         MetricsGenderCacheDao(MetricsCacheType.METRICS_V2_API))
     logging.info('Refresh MetricsGenderCache for Metrics2API done.')
     self.refresh_data_for_metrics_cache(
         MetricsGenderCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API))
     logging.info('Refresh MetricsGenderCache for Public Metrics API done.')
     self.refresh_data_for_metrics_cache(
         MetricsAgeCacheDao(MetricsCacheType.METRICS_V2_API))
     logging.info('Refresh MetricsAgeCache for Metrics2API done.')
     self.refresh_data_for_metrics_cache(
         MetricsAgeCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API))
     logging.info('Refresh MetricsAgeCache for Public Metrics API done.')
     self.refresh_data_for_metrics_cache(
         MetricsRaceCacheDao(MetricsCacheType.METRICS_V2_API))
     logging.info('Refresh MetricsRaceCache for Metrics2API done.')
     self.refresh_data_for_metrics_cache(
         MetricsRaceCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API))
     logging.info('Refresh MetricsRaceCache for Public Metrics API done.')
     self.refresh_data_for_metrics_cache(MetricsRegionCacheDao())
     logging.info('Refresh MetricsRegionCache done.')
     self.refresh_data_for_metrics_cache(MetricsLanguageCacheDao())
     logging.info('Refresh MetricsLanguageCache done.')
     self.refresh_data_for_metrics_cache(
         MetricsLifecycleCacheDao(MetricsCacheType.METRICS_V2_API))
     logging.info('Refresh MetricsLifecycleCache for Metrics2API done.')
     self.refresh_data_for_metrics_cache(
         MetricsLifecycleCacheDao(
             MetricsCacheType.PUBLIC_METRICS_EXPORT_API))
     logging.info(
         'Refresh MetricsLifecycleCache for Public Metrics API done.')
    def refresh_metrics_cache_data(self):

        self.refresh_data_for_metrics_cache(MetricsEnrollmentStatusCacheDao())
        self.refresh_data_for_metrics_cache(MetricsGenderCacheDao())
        self.refresh_data_for_metrics_cache(
            MetricsAgeCacheDao(MetricsCacheType.METRICS_V2_API))
        self.refresh_data_for_metrics_cache(
            MetricsAgeCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API))
        self.refresh_data_for_metrics_cache(MetricsRaceCacheDao())
        self.refresh_data_for_metrics_cache(MetricsRegionCacheDao())
        self.refresh_data_for_metrics_cache(MetricsLanguageCacheDao())
        self.refresh_data_for_metrics_cache(MetricsLifecycleCacheDao())
Ejemplo n.º 3
0
  def get_filtered_results(self, stratification, start_date, end_date, awardee_ids,
                           enrollment_statuses, version):
    """Queries DB, returns results in format consumed by front-end

    :param start_date: Start date object
    :param end_date: End date object
    :param awardee_ids: indicate awardee ids
    :param enrollment_statuses: indicate the enrollment status
    :param stratification: How to stratify (layer) results, as in a stacked bar chart
    :param version: indicate the version of the result filter
    :return: Filtered, stratified results by date
    """

    if stratification == Stratifications.TOTAL:
      dao = MetricsEnrollmentStatusCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API)
      return dao.get_total_interested_count(start_date, end_date, awardee_ids)
    elif stratification == Stratifications.ENROLLMENT_STATUS:
      dao = MetricsEnrollmentStatusCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API)
      return dao.get_latest_version_from_cache(start_date, end_date, awardee_ids)
    elif stratification == Stratifications.GENDER_IDENTITY:
      dao = MetricsGenderCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API, version)
      return dao.get_latest_version_from_cache(start_date, end_date, awardee_ids,
                                               enrollment_statuses)
    elif stratification == Stratifications.AGE_RANGE:
      dao = MetricsAgeCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API)
      return dao.get_latest_version_from_cache(start_date, end_date, awardee_ids,
                                               enrollment_statuses)
    elif stratification == Stratifications.RACE:
      dao = MetricsRaceCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API, version)
      return dao.get_latest_version_from_cache(start_date, end_date, awardee_ids,
                                               enrollment_statuses)
    elif stratification in [Stratifications.GEO_STATE, Stratifications.GEO_CENSUS,
                            Stratifications.GEO_AWARDEE]:
      dao = MetricsRegionCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API)
      return dao.get_latest_version_from_cache(end_date, stratification, awardee_ids,
                                               enrollment_statuses)
    elif stratification == Stratifications.LANGUAGE:
      dao = MetricsLanguageCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API)
      return dao.get_latest_version_from_cache(start_date, end_date, awardee_ids,
                                               enrollment_statuses)
    elif stratification == Stratifications.LIFECYCLE:
      dao = MetricsLifecycleCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API)
      return dao.get_latest_version_from_cache(end_date, awardee_ids)
    elif stratification == Stratifications.PRIMARY_CONSENT:
      dao = MetricsLifecycleCacheDao(MetricsCacheType.PUBLIC_METRICS_EXPORT_API)
      return dao.get_primary_consent_count_over_time(start_date, end_date, awardee_ids)
    elif stratification == Stratifications.EHR_METRICS:
      params = {
        'start_date': convert_to_datetime(start_date),
        'end_date': convert_to_datetime(end_date),
        'hpo_ids': awardee_ids,
        'interval': INTERVAL_DAY
      }
      result_set = MetricsEhrService().get_metrics(**params)
      if 'metrics_over_time' in result_set:
        return result_set['metrics_over_time']
      else:
        return []
    else:
      raise BadRequest('Invalid stratification: %s' % str(stratification))
    def refresh_metrics_cache_data(self):

        self.refresh_data_for_metrics_cache(MetricsEnrollmentStatusCacheDao())
        self.refresh_data_for_metrics_cache(MetricsGenderCacheDao())
        self.refresh_data_for_metrics_cache(MetricsAgeCacheDao())
        self.refresh_data_for_metrics_cache(MetricsRaceCacheDao())
        self.refresh_data_for_metrics_cache(MetricsRegionCacheDao())
        self.refresh_data_for_metrics_cache(MetricsLifecycleCacheDao())
    def get_filtered_results(self, stratification, start_date, end_date,
                             history, awardee_ids, enrollment_statuses,
                             sample_time_def):
        """Queries DB, returns results in format consumed by front-end

    :param start_date: Start date object
    :param end_date: End date object
    :param awardee_ids: indicate awardee ids
    :param enrollment_statuses: indicate the enrollment status
    :param sample_time_def: indicate how to filter the core participant
    :param history: query for history data from metrics cache table
    :param stratification: How to stratify (layer) results, as in a stacked bar chart
    :return: Filtered, stratified results by date
    """

        # Filters for participant_summary (ps) and participant (p) table
        # filters_sql_ps is used in the general case when we're querying participant_summary
        # filters_sql_p is used when also LEFT OUTER JOINing p and ps
        facets = {
            'enrollment_statuses':
            [EnrollmentStatus(val) for val in enrollment_statuses],
            'awardee_ids':
            awardee_ids
        }
        filters_sql_ps = self.get_facets_sql(facets, stratification)
        filters_sql_p = self.get_facets_sql(facets,
                                            stratification,
                                            table_prefix='p')

        if str(history) == 'TRUE' and stratification == Stratifications.TOTAL:
            dao = MetricsEnrollmentStatusCacheDao()
            return dao.get_total_interested_count(start_date, end_date,
                                                  awardee_ids)
        elif str(
                history
        ) == 'TRUE' and stratification == Stratifications.ENROLLMENT_STATUS:
            dao = MetricsEnrollmentStatusCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids)
        elif str(
                history
        ) == 'TRUE' and stratification == Stratifications.GENDER_IDENTITY:
            dao = MetricsGenderCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids,
                                                     enrollment_statuses)
        elif str(history
                 ) == 'TRUE' and stratification == Stratifications.AGE_RANGE:
            dao = MetricsAgeCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids,
                                                     enrollment_statuses)
        elif str(history) == 'TRUE' and stratification == Stratifications.RACE:
            dao = MetricsRaceCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids,
                                                     enrollment_statuses)
        elif str(history) == 'TRUE' and stratification in [
                Stratifications.FULL_STATE, Stratifications.FULL_CENSUS,
                Stratifications.FULL_AWARDEE, Stratifications.GEO_STATE,
                Stratifications.GEO_CENSUS, Stratifications.GEO_AWARDEE
        ]:
            dao = MetricsRegionCacheDao()
            return dao.get_latest_version_from_cache(end_date, stratification,
                                                     awardee_ids,
                                                     enrollment_statuses)
        elif str(history
                 ) == 'TRUE' and stratification == Stratifications.LANGUAGE:
            dao = MetricsLanguageCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids,
                                                     enrollment_statuses)
        elif str(history
                 ) == 'TRUE' and stratification == Stratifications.LIFECYCLE:
            dao = MetricsLifecycleCacheDao()
            return dao.get_latest_version_from_cache(end_date, awardee_ids)
        elif stratification == Stratifications.TOTAL:
            strata = ['TOTAL']
            sql = self.get_total_sql(filters_sql_ps)
        elif stratification == Stratifications.ENROLLMENT_STATUS:
            strata = [str(val) for val in EnrollmentStatus]
            sql = self.get_enrollment_status_sql(filters_sql_p,
                                                 sample_time_def)
        elif stratification == Stratifications.EHR_CONSENT:
            strata = ['EHR_CONSENT']
            sql = self.get_total_sql(filters_sql_ps, ehr_count=True)
        elif stratification == Stratifications.EHR_RATIO:
            strata = ['EHR_RATIO']
            sql = self.get_ratio_sql(filters_sql_ps)
        else:
            raise BadRequest('Invalid stratification: %s' % stratification)

        params = {'start_date': start_date, 'end_date': end_date}

        results_by_date = []

        with self.session() as session:
            cursor = session.execute(sql, params)

        # Iterate through each result (by date), transforming tabular SQL results
        # into expected list-of-dictionaries response format
        try:
            results = cursor.fetchall()
            for result in results:
                date = result[-1]
                metrics = {}
                values = result[:-1]
                for i, value in enumerate(values):
                    key = strata[i]
                    if value is None or (stratification
                                         == Stratifications.ENROLLMENT_STATUS
                                         and enrollment_statuses
                                         and key not in enrollment_statuses):
                        value = 0
                    metrics[key] = float(
                        value
                    ) if stratification == Stratifications.EHR_RATIO else int(
                        value)
                results_by_date.append({'date': str(date), 'metrics': metrics})
        finally:
            cursor.close()

        return results_by_date
    def get_filtered_results(self,
                             start_date,
                             end_date,
                             filters,
                             filter_by,
                             history,
                             stratification='ENROLLMENT_STATUS'):
        """Queries DB, returns results in format consumed by front-end

    :param start_date: Start date object
    :param end_date: End date object
    :param filters: Objects representing filters specified in UI
    :param filter_by: indicate how to filter the core participant
    :param history: query for history data from metrics cache table
    :param stratification: How to stratify (layer) results, as in a stacked bar chart
    :return: Filtered, stratified results by date
    """

        # save the enrollment statuses requirements for filtering the SQL result later
        if 'enrollment_statuses' in filters and filters[
                'enrollment_statuses'] is not None:
            enrollment_statuses = [
                str(val) for val in filters['enrollment_statuses']
            ]
        else:
            enrollment_statuses = []

        if 'awardee_ids' in filters and filters['awardee_ids'] is not None:
            awardee_ids = filters['awardee_ids']
        else:
            awardee_ids = []

        # Filters for participant_summary (ps) and participant (p) table
        # filters_sql_ps is used in the general case when we're querying participant_summary
        # filters_sql_p is used when also LEFT OUTER JOINing p and ps
        filters_sql_ps = self.get_facets_sql(filters, stratification)
        filters_sql_p = self.get_facets_sql(filters,
                                            stratification,
                                            table_prefix='p')

        if str(history) == 'TRUE' and str(stratification) == 'TOTAL':
            dao = MetricsEnrollmentStatusCacheDao()
            return dao.get_total_interested_count(start_date, end_date,
                                                  awardee_ids)
        elif str(history) == 'TRUE' and str(
                stratification) == 'ENROLLMENT_STATUS':
            dao = MetricsEnrollmentStatusCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids)
        elif str(history) == 'TRUE' and str(
                stratification) == 'GENDER_IDENTITY':
            dao = MetricsGenderCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids)
        elif str(history) == 'TRUE' and str(stratification) == 'AGE_RANGE':
            dao = MetricsAgeCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids)
        elif str(history) == 'TRUE' and str(stratification) == 'RACE':
            dao = MetricsRaceCacheDao()
            return dao.get_latest_version_from_cache(start_date, end_date,
                                                     awardee_ids)
        elif str(history) == 'TRUE' and str(stratification) in [
                'FULL_STATE', 'FULL_CENSUS', 'FULL_AWARDEE'
        ]:
            dao = MetricsRegionCacheDao()
            return dao.get_latest_version_from_cache(end_date, stratification,
                                                     awardee_ids)
        elif str(history) == 'TRUE' and str(stratification) == 'LIFECYCLE':
            dao = MetricsLifecycleCacheDao()
            return dao.get_latest_version_from_cache(end_date, awardee_ids)
        elif str(stratification) == 'TOTAL':
            strata = ['TOTAL']
            sql = self.get_total_sql(filters_sql_ps)
        elif str(stratification) == 'ENROLLMENT_STATUS':
            strata = [str(val) for val in EnrollmentStatus]
            sql = self.get_enrollment_status_sql(filters_sql_p, filter_by)
        elif str(stratification) == 'EHR_CONSENT':
            strata = ['EHR_CONSENT']
            sql = self.get_total_sql(filters_sql_ps, ehr_count=True)
        elif str(stratification) == 'EHR_RATIO':
            strata = ['EHR_RATIO']
            sql = self.get_ratio_sql(filters_sql_ps)
        else:
            raise BadRequest('Invalid stratification: %s' % stratification)

        params = {'start_date': start_date, 'end_date': end_date}

        results_by_date = []

        with self.session() as session:
            cursor = session.execute(sql, params)

        # Iterate through each result (by date), transforming tabular SQL results
        # into expected list-of-dictionaries response format
        try:
            results = cursor.fetchall()
            for result in results:
                date = result[-1]
                metrics = {}
                values = result[:-1]
                for i, value in enumerate(values):
                    key = strata[i]
                    if value is None or (str(stratification)
                                         == 'ENROLLMENT_STATUS'
                                         and enrollment_statuses
                                         and key not in enrollment_statuses):
                        value = 0
                    metrics[key] = float(value) if str(
                        stratification) == 'EHR_RATIO' else int(value)
                results_by_date.append({'date': str(date), 'metrics': metrics})
        finally:
            cursor.close()

        return results_by_date