def test_get_data(self):
     """Build a RowStream from sql results."""
     stream = from_cursor(MockCursor())
     self.assertEqual(['a', 'b', 'c'], stream.fields)
     self.assertEqual(
         [
             {'a': 1, 'b': 2, 'c': 4},
             {'a': 5, 'b': 6, 'c': 7}], list(stream))
 def test_get_data(self):
     """Build a RowStream from sql results."""
     stream = from_cursor(MockCursor())
     self.assertEqual(['a', 'b', 'c'], stream.fields)
     self.assertEqual([{
         'a': 1,
         'b': 2,
         'c': 4
     }, {
         'a': 5,
         'b': 6,
         'c': 7
     }], list(stream))
Example #3
0
    def run_count_comm_rec_per_month(self, company, filter_spec, values):
        # Get a list of valid partners (according to our filter).
        qs_filtered = filter_spec.filter_partners(company)
        partner_ids = list(qs_filtered.values_list('id', flat=True))

        # If there are no partners, we're done.
        if not partner_ids:
            return []

        sql = count_comm_rec_per_month_per_partner_sql
        cursor = connection.cursor()
        cursor.execute(sql, {'partner_list': partner_ids})
        count_stream = from_cursor(cursor)

        # Iterate over counts results.
        # Find year range and organize counts for later.
        min_year = None
        max_year = None
        db_counts = {}
        for record in count_stream:
            partner_id = record['partner_id']
            year = record['year']
            month = record['month']
            comm_rec_count = record['comm_rec_count']

            if year < min_year or min_year is None:
                min_year = year
            if year > max_year or max_year is None:
                max_year = year

            key = (partner_id, year, month)
            db_counts[key] = comm_rec_count

        # If there are no communication records, assume current year.
        if not db_counts:
            min_year = datetime.now().year
            max_year = min_year

        # Create full range of time slots with counts.
        # DB won't fill in zero's without a complex query.
        # Do this instead.
        full_counts = {}
        for partner_id in partner_ids:
            count_list = []
            full_counts[partner_id] = count_list
            for year in xrange(min_year, max_year + 1):
                for month in xrange(1, 12 + 1):
                    key = (partner_id, year, month)
                    if key in db_counts:
                        count = db_counts[key]
                        count_list.append((year, month, count))
                    else:
                        count_list.append((year, month, 0))

        qs_optimized = qs_filtered.prefetch_related('tags', 'primary_contact')
        partners = from_django(self.partner_row_builder, qs_optimized)

        # Join the two streams
        joined_fields = partners.fields + ['year', 'month', 'comm_rec_count']
        joined_data = []
        for partner_record in partners:
            count_list = full_counts.get(partner_record['partner_id'], [])
            for count_tuple in count_list:
                year, month, count = count_tuple
                joined_record = dict(partner_record)
                joined_record.update({
                    'year': year,
                    'month': month,
                    'comm_rec_count': count,
                })
                joined_data.append(joined_record)
        joined = from_list(joined_fields, joined_data)

        return list(joined)
Example #4
0
    def run_count_comm_rec_per_month(self, company, filter_spec, values):
        # Get a list of valid partners (according to our filter).
        qs_filtered = filter_spec.filter_partners(company)
        partner_ids = list(qs_filtered.values_list('id', flat=True))

        # If there are no partners, we're done.
        if not partner_ids:
            return []

        sql = count_comm_rec_per_month_per_partner_sql
        cursor = connection.cursor()
        cursor.execute(sql, {'partner_list': partner_ids})
        count_stream = from_cursor(cursor)

        # Iterate over counts results.
        # Find year range and organize counts for later.
        min_year = None
        max_year = None
        db_counts = {}
        for record in count_stream:
            partner_id = record['partner_id']
            year = record['year']
            month = record['month']
            comm_rec_count = record['comm_rec_count']

            if year < min_year or min_year is None:
                min_year = year
            if year > max_year or max_year is None:
                max_year = year

            key = (partner_id, year, month)
            db_counts[key] = comm_rec_count

        # If there are no communication records, assume current year.
        if not db_counts:
            min_year = datetime.now().year
            max_year = min_year

        # Create full range of time slots with counts.
        # DB won't fill in zero's without a complex query.
        # Do this instead.
        full_counts = {}
        for partner_id in partner_ids:
            count_list = []
            full_counts[partner_id] = count_list
            for year in xrange(min_year, max_year + 1):
                for month in xrange(1, 12 + 1):
                    key = (partner_id, year, month)
                    if key in db_counts:
                        count = db_counts[key]
                        count_list.append((year, month, count))
                    else:
                        count_list.append((year, month, 0))

        qs_optimized = qs_filtered.prefetch_related('tags', 'primary_contact')
        partners = from_django(self.partner_row_builder, qs_optimized)

        # Join the two streams
        joined_fields = partners.fields + ['year', 'month', 'comm_rec_count']
        joined_data = []
        for partner_record in partners:
            count_list = full_counts.get(partner_record['partner_id'], [])
            for count_tuple in count_list:
                year, month, count = count_tuple
                joined_record = dict(partner_record)
                joined_record.update({
                    'year': year,
                    'month': month,
                    'comm_rec_count': count,
                    })
                joined_data.append(joined_record)
        joined = from_list(joined_fields, joined_data)

        return list(joined)