コード例 #1
0
ファイル: forms.py プロジェクト: ansarbek/commcare-hq
    def update_report(self):
        from corehq.apps.userreports.views import delete_data_source_shared

        matching_data_source = self.ds_builder.get_existing_match()
        if matching_data_source:
            reactivated = False
            if matching_data_source._id != self.existing_report.config_id:

                # If no one else is using the current data source, delete it.
                data_source = DataSourceConfiguration.get(self.existing_report.config_id)
                if data_source.get_report_count() <= 1:
                    data_source.deactivate()

                self.existing_report.config_id = matching_data_source._id
            elif matching_data_source.is_deactivated:
                existing_sources = DataSourceConfiguration.by_domain(self.domain)
                active_sources = filter(lambda config: not config.is_deactivated, existing_sources)
                if len(active_sources) >= 5:
                    raise forms.ValidationError(_(
                        "Editing this report would require a new data source. The limit is 5. "
                        "To continue, first delete all of the reports using a particular "
                        "data source (or the data source itself) and try again. "
                    ))
                matching_data_source.is_deactivated = False
                reactivated = True
            changed = False
            indicators = self.ds_builder.indicators(self._number_columns)
            if matching_data_source.configured_indicators != indicators:
                matching_data_source.configured_indicators = indicators
                changed = True
            if changed or reactivated:
                matching_data_source.save()
                tasks.rebuild_indicators.delay(matching_data_source._id)
        else:
            # We need to create a new data source
            existing_sources = DataSourceConfiguration.by_domain(self.domain)
            active_sources = filter(lambda config: not config.is_deactivated, existing_sources)

            # Delete the old one if no other reports use it
            old_data_source = DataSourceConfiguration.get(self.existing_report.config_id)
            if old_data_source.get_report_count() <= 1:
                old_data_source.deactivate()

            # Make sure the user can create more data sources
            elif len(active_sources) >= 5:
                raise forms.ValidationError(_(
                    "Editing this report would require a new data source. The limit is 5. "
                    "To continue, first delete all of the reports using a particular "
                    "data source (or the data source itself) and try again. "
                ))
            data_source_config_id = self._build_data_source()
            self.existing_report.config_id = data_source_config_id

        self.existing_report.aggregation_columns = self._report_aggregation_cols
        self.existing_report.columns = self._report_columns
        self.existing_report.filters = self._report_filters
        self.existing_report.configured_charts = self._report_charts
        self.existing_report.validate()
        self.existing_report.save()
        return self.existing_report
コード例 #2
0
    def test_get_by_domain(self):
        results = DataSourceConfiguration.by_domain('foo')
        self.assertEqual(2, len(results))
        for item in results:
            self.assertTrue(item.table_id in ('foo1', 'foo2'))

        results = DataSourceConfiguration.by_domain('not-foo')
        self.assertEqual(0, len(results))
コード例 #3
0
    def test_get_by_domain(self):
        results = DataSourceConfiguration.by_domain('foo')
        self.assertEqual(2, len(results))
        for item in results:
            self.assertTrue(item.table_id in ('foo1', 'foo2'))

        results = DataSourceConfiguration.by_domain('not-foo')
        self.assertEqual(0, len(results))
コード例 #4
0
ファイル: views.py プロジェクト: ekush/commcare-hq
def _shared_context(domain):
    custom_data_sources = list(CustomDataSourceConfiguration.by_domain(domain))
    return {
        'domain': domain,
        'reports': ReportConfiguration.by_domain(domain),
        'data_sources': DataSourceConfiguration.by_domain(domain) + custom_data_sources,
    }
コード例 #5
0
ファイル: forms.py プロジェクト: johan--/commcare-hq
    def clean(self):
        """
        Raise a validation error if there are already 5 data sources and this
        report won't be able to use one of the existing ones.
        """
        cleaned_data = super(DataSourceForm, self).clean()
        source_type = cleaned_data.get('source_type')
        report_source = cleaned_data.get('report_source')
        app_id = cleaned_data.get('application')

        if report_source and source_type and app_id:

            app = Application.get(app_id)
            ds_builder = DataSourceBuilder(self.domain, app, source_type, report_source)

            existing_sources = DataSourceConfiguration.by_domain(self.domain)
            if len(existing_sources) >= 5:
                if not ds_builder.get_existing_match():
                    raise forms.ValidationError(_(
                        "Too many data sources!\n"
                        "Creating this report would cause you to go over the maximum "
                        "number of data sources allowed in this domain. The current "
                        "limit is 5. "
                        "To continue, delete all of the reports using a particular "
                        "data source (or the data source itself) and try again. "
                    ))

        return cleaned_data
コード例 #6
0
    def _ucr(self):
        static_datasources = StaticDataSourceConfiguration.by_domain(
            self.domain)
        dynamic_datasources = DataSourceConfiguration.by_domain(self.domain)
        self._print_value('Static UCR data sources', len(static_datasources))
        self._print_value('Dynamic UCR data sources', len(dynamic_datasources))

        def _get_count(config):
            table_name = get_table_name(config.domain, config.table_id)
            db_name = connection_manager.get_django_db_alias(config.engine_id)
            return _get_count_from_explain_raw(
                db_name, 'SELECT * FROM "%s"' % table_name, [])

        def _get_table_size(config):
            table_name = get_table_name(config.domain, config.table_id)
            db_name = connection_manager.get_django_db_alias(config.engine_id)
            db_cursor = connections[db_name].cursor()
            with db_cursor as cursor:
                cursor.execute(
                    "SELECT pg_total_relation_size('\"%s\"')" % table_name, [])
                bytes = cursor.fetchone()[0]
                return bytes

        rows = sorted(
            [(datasource.display_name, _get_count(datasource),
              datasource.referenced_doc_type, _get_table_size(datasource))
             for datasource in static_datasources + dynamic_datasources],
            key=lambda r: r[-1])

        self._print_table([
            'Datasource name', 'Row count (approximate)', 'Doc type', 'Size',
            'Size (bytes)'
        ], rows)
コード例 #7
0
ファイル: forms.py プロジェクト: ekush/commcare-hq
    def clean(self):
        """
        Raise a validation error if there are already 5 data sources and this
        report won't be able to use one of the existing ones.
        """
        cleaned_data = super(DataSourceForm, self).clean()
        source_type = cleaned_data.get('source_type')
        report_source = cleaned_data.get('report_source')
        app_id = cleaned_data.get('application')

        if report_source and source_type and app_id:

            app = Application.get(app_id)
            ds_builder = DataSourceBuilder(self.domain, app, source_type,
                                           report_source)

            existing_sources = DataSourceConfiguration.by_domain(self.domain)
            if len(existing_sources) >= 5:
                if not ds_builder.get_existing_match():
                    raise forms.ValidationError(
                        _("Too many data sources!\n"
                          "Creating this report would cause you to go over the maximum "
                          "number of data sources allowed in this domain. The current "
                          "limit is 5. "
                          "To continue, delete all of the reports using a particular "
                          "data source (or the data source itself) and try again. "
                          ))

        return cleaned_data
コード例 #8
0
ファイル: fields.py プロジェクト: bradmerlin/commcare-hq
 def __init__(self, domain, *args, **kwargs):
     self.domain = domain
     available_data_sources = DataSourceConfiguration.by_domain(self.domain)
     super(ReportDataSourceField, self).__init__(
         choices=[(src._id, src.display_name) for src in available_data_sources],
         *args, **kwargs
     )
コード例 #9
0
    def handle(self, domain, **options):
        tables = StaticDataSourceConfiguration.by_domain(domain)
        tables.extend(DataSourceConfiguration.by_domain(domain))

        print("Rebuilding {} tables".format(len(tables)))

        for table in tables:
            tasks.rebuild_indicators(table._id)
コード例 #10
0
    def handle(self, domain, **options):
        tables = StaticDataSourceConfiguration.by_domain(domain)
        tables.extend(DataSourceConfiguration.by_domain(domain))

        print("Rebuilding {} tables".format(len(tables)))

        for table in tables:
            tasks.rebuild_indicators(table._id)
コード例 #11
0
ファイル: views.py プロジェクト: ansarbek/commcare-hq
def _shared_context(domain):
    static_reports = list(StaticReportConfiguration.by_domain(domain))
    static_data_sources = list(StaticDataSourceConfiguration.by_domain(domain))
    return {
        'domain': domain,
        'reports': ReportConfiguration.by_domain(domain) + static_reports,
        'data_sources': DataSourceConfiguration.by_domain(domain) + static_data_sources,
    }
コード例 #12
0
ファイル: views.py プロジェクト: saketkanth/commcare-hq
 def main_context(self):
     static_reports = list(StaticReportConfiguration.by_domain(self.domain))
     static_data_sources = list(StaticDataSourceConfiguration.by_domain(self.domain))
     context = super(BaseUserConfigReportsView, self).main_context
     context.update({
         'reports': ReportConfiguration.by_domain(self.domain) + static_reports,
         'data_sources': DataSourceConfiguration.by_domain(self.domain) + static_data_sources,
     })
     return context
コード例 #13
0
ファイル: fields.py プロジェクト: LifeCoaching/commcare-hq
 def __init__(self, domain, *args, **kwargs):
     self.domain = domain
     standard_sources = DataSourceConfiguration.by_domain(self.domain)
     custom_sources = list(CustomDataSourceConfiguration.by_domain(domain))
     available_data_sources = standard_sources + custom_sources
     super(ReportDataSourceField, self).__init__(
         choices=[(src._id, src.display_name) for src in available_data_sources],
         *args, **kwargs
     )
コード例 #14
0
 def __init__(self, domain, *args, **kwargs):
     self.domain = domain
     standard_sources = DataSourceConfiguration.by_domain(self.domain)
     custom_sources = list(StaticDataSourceConfiguration.by_domain(domain))
     available_data_sources = standard_sources + custom_sources
     super(ReportDataSourceField, self).__init__(
         choices=[(src._id, src.display_name) for src in available_data_sources],
         *args, **kwargs
     )
コード例 #15
0
ファイル: views.py プロジェクト: ekush/commcare-hq
def _shared_context(domain):
    custom_data_sources = list(CustomDataSourceConfiguration.by_domain(domain))
    return {
        'domain':
        domain,
        'reports':
        ReportConfiguration.by_domain(domain),
        'data_sources':
        DataSourceConfiguration.by_domain(domain) + custom_data_sources,
    }
コード例 #16
0
def _shared_context(domain):
    static_reports = list(StaticReportConfiguration.by_domain(domain))
    static_data_sources = list(StaticDataSourceConfiguration.by_domain(domain))
    return {
        'domain':
        domain,
        'reports':
        ReportConfiguration.by_domain(domain) + static_reports,
        'data_sources':
        DataSourceConfiguration.by_domain(domain) + static_data_sources,
    }
コード例 #17
0
 def __init__(self, domain, *args, **kwargs):
     self.domain = domain
     standard_sources = DataSourceConfiguration.by_domain(self.domain)
     custom_sources = list(StaticDataSourceConfiguration.by_domain(domain))
     available_data_sources = standard_sources + custom_sources
     if toggles.AGGREGATE_UCRS.enabled(domain):
         from corehq.apps.aggregate_ucrs.models import AggregateTableDefinition
         available_data_sources += AggregateTableDefinition.objects.filter(domain=self.domain)
     super(ReportDataSourceField, self).__init__(
         choices=[(src.data_source_id, src.display_name) for src in available_data_sources],
         *args, **kwargs
     )
コード例 #18
0
    def handle(self, *args, **options):
        if len(args) < 1:
            raise CommandError('Usage is rebuild_tables_by_domain %s' %
                               self.args)

        domain = args[0]
        tables = StaticDataSourceConfiguration.by_domain(domain)
        tables.extend(DataSourceConfiguration.by_domain(domain))

        print("Rebuilding {} tables".format(len(tables)))

        for table in tables:
            tasks.rebuild_indicators(table._id)
コード例 #19
0
 def main_context(self):
     static_reports = list(StaticReportConfiguration.by_domain(self.domain))
     static_data_sources = list(
         StaticDataSourceConfiguration.by_domain(self.domain))
     context = super(BaseUserConfigReportsView, self).main_context
     context.update({
         'reports':
         ReportConfiguration.by_domain(self.domain) + static_reports,
         'data_sources':
         DataSourceConfiguration.by_domain(self.domain) +
         static_data_sources,
     })
     return context
コード例 #20
0
ファイル: forms.py プロジェクト: ekush/commcare-hq
    def update_report(self):
        from corehq.apps.userreports.views import delete_data_source_shared

        matching_data_source = self.ds_builder.get_existing_match()
        if matching_data_source:
            if matching_data_source['id'] != self.existing_report.config_id:

                # If no one else is using the current data source, delete it.
                data_source = DataSourceConfiguration.get(
                    self.existing_report.config_id)
                if data_source.get_report_count() <= 1:
                    delete_data_source_shared(self.domain, data_source._id)

                self.existing_report.config_id = matching_data_source['id']

        else:
            # We need to create a new data source
            existing_sources = DataSourceConfiguration.by_domain(self.domain)

            # Delete the old one if no other reports use it
            old_data_source = DataSourceConfiguration.get(
                self.existing_report.config_id)
            if old_data_source.get_report_count() <= 1:
                delete_data_source_shared(self.domain, old_data_source._id)

            # Make sure the user can create more data sources
            elif len(existing_sources) >= 5:
                raise forms.ValidationError(
                    _("Editing this report would require a new data source. The limit is 5. "
                      "To continue, first delete all of the reports using a particular "
                      "data source (or the data source itself) and try again. "
                      ))

            data_source_config_id = self._build_data_source()
            self.existing_report.config_id = data_source_config_id

        self.existing_report.aggregation_columns = self._report_aggregation_cols
        self.existing_report.columns = self._report_columns
        self.existing_report.filters = self._report_filters
        self.existing_report.configured_charts = self._report_charts
        self.existing_report.validate()
        self.existing_report.save()
        return self.existing_report
コード例 #21
0
    def _ucr(self):
        static_datasources = StaticDataSourceConfiguration.by_domain(
            self.domain)
        dynamic_datasources = DataSourceConfiguration.by_domain(self.domain)
        self._print_value('Static UCR data sources', len(static_datasources))
        self._print_value('Dynamic UCR data sources', len(dynamic_datasources))

        def _get_count(config):
            table_name = get_table_name(config.domain, config.table_id)
            db_name = connection_manager.get_django_db_alias(config.engine_id)
            query = ('SELECT * FROM "%s"' % table_name, [])
            try:
                return estimate_row_count(query, db_name)
            except ProgrammingError:
                return "Table not found"

        def _get_table_size(config):
            table_name = get_table_name(config.domain, config.table_id)
            db_name = connection_manager.get_django_db_alias(config.engine_id)
            db_cursor = connections[db_name].cursor()
            with db_cursor as cursor:
                try:
                    cursor.execute(
                        "SELECT pg_total_relation_size('\"%s\"')" % table_name,
                        [])
                    bytes = cursor.fetchone()[0]
                    return bytes
                except ProgrammingError:
                    return "Table not found"

        rows = sorted(
            [(datasource.display_name, _get_count(datasource),
              datasource.referenced_doc_type, _get_table_size(datasource))
             for datasource in static_datasources + dynamic_datasources],
            key=lambda r: r[-1] if r[-1] != 'Table not found' else 0)

        self.stdout.write('UCR datasource sizes')
        self._print_table([
            'Datasource name', 'Row count (approximate)', 'Doc type',
            'Size (bytes)'
        ], rows)
コード例 #22
0
ファイル: forms.py プロジェクト: johan--/commcare-hq
    def update_report(self):
        from corehq.apps.userreports.views import delete_data_source_shared

        matching_data_source = self.ds_builder.get_existing_match()
        if matching_data_source:
            if matching_data_source['id'] != self.existing_report.config_id:

                # If no one else is using the current data source, delete it.
                data_source = DataSourceConfiguration.get(self.existing_report.config_id)
                if data_source.get_report_count() <= 1:
                    delete_data_source_shared(self.domain, data_source._id)

                self.existing_report.config_id = matching_data_source['id']

        else:
            # We need to create a new data source
            existing_sources = DataSourceConfiguration.by_domain(self.domain)

            # Delete the old one if no other reports use it
            old_data_source = DataSourceConfiguration.get(self.existing_report.config_id)
            if old_data_source.get_report_count() <= 1:
                delete_data_source_shared(self.domain, old_data_source._id)

            # Make sure the user can create more data sources
            elif len(existing_sources) >= 5:
                raise forms.ValidationError(_(
                    "Editing this report would require a new data source. The limit is 5. "
                    "To continue, first delete all of the reports using a particular "
                    "data source (or the data source itself) and try again. "
                ))

            data_source_config_id = self._build_data_source()
            self.existing_report.config_id = data_source_config_id

        self.existing_report.aggregation_columns = self._report_aggregation_cols
        self.existing_report.columns = self._report_columns
        self.existing_report.filters = self._report_filters
        self.existing_report.configured_charts = self._report_charts
        self.existing_report.validate()
        self.existing_report.save()
        return self.existing_report
コード例 #23
0
ファイル: views.py プロジェクト: bradmerlin/commcare-hq
def _shared_context(domain):
    return {
        'domain': domain,
        'reports': ReportConfiguration.by_domain(domain),
        'data_sources': DataSourceConfiguration.by_domain(domain),
    }