def test_column_label_containing_at_sign(self): export_with_column_containing_at_sign = ExportInstance( is_odata_config=True, tables=[ TableConfiguration(columns=[ ExportColumn( label='@label', item=ExportItem(path=[PathNode(name='val')]), selected=True, ) ]) ]) export_with_column_containing_at_sign.save() self.addCleanup(export_with_column_containing_at_sign.delete) cleaned_export = ExportInstance.get( export_with_column_containing_at_sign.get_id) self.assertEqual(cleaned_export.tables[0].columns[0].label, 'label')
def test_save_basic_export_to_blobdb(self): export = ExportInstance(daily_saved_export=True, domain="test") export.save() export.set_payload("content") self.assertTrue(export.has_file()) self.assertIn(DAILY_SAVED_EXPORT_ATTACHMENT_NAME, export.external_blobs) self.assertEqual(export.file_size, 7) with export.get_payload(stream=True) as fh: self.assertEqual(fh.read(), b"content")
def test_row_number_column_is_removed(self): export_with_row_number_column = ExportInstance( is_odata_config=True, tables=[ TableConfiguration(columns=[ RowNumberColumn(label='row-number', ), ExportColumn( label='label', item=ExportItem(path=[PathNode(name='val')]), selected=True, ) ]) ]) export_with_row_number_column.save() self.addCleanup(export_with_row_number_column.delete) cleaned_export = ExportInstance.get( export_with_row_number_column.get_id) tables = cleaned_export.tables self.assertEqual(len(tables), 1) columns = tables[0].columns self.assertEqual(len(columns), 1) self.assertFalse(isinstance(columns[0], RowNumberColumn)) self.assertEqual(columns[0].label, 'label')
def test_generate_instance_from_schema_deleted(self): """Given a higher app_version, all the old questions should not be shown or selected""" instance = ExportInstance.generate_instance_from_schema(self.schema, 4) self.assertEqual(len(instance.tables), 2) selected = filter( lambda column: column.selected, instance.tables[0].columns + instance.tables[1].columns ) shown = filter( lambda column: column.selected, instance.tables[0].columns + instance.tables[1].columns ) self.assertEqual(len(selected), 0) self.assertEqual(len(shown), 0)
def test_generate_instance_from_schema(self): """Only questions that are in the main table and of the same version should be shown""" instance = ExportInstance.generate_instance_from_schema(self.schema, 3) self.assertEqual(len(instance.tables), 2) selected = filter( lambda column: column.selected, instance.tables[0].columns + instance.tables[1].columns ) shown = filter( lambda column: column.selected, instance.tables[0].columns + instance.tables[1].columns ) self.assertEqual(len(selected), 1) self.assertEqual(len(shown), 1)
def handle(self, domain, **options): db = ExportInstance.get_db() exports = db.view( 'export_instances_by_domain/view', startkey=[domain], endkey=[domain, {}], include_docs=False, reduce=False, ).all() if not exports: print("No exports to delete here, exiting.") return filter_exports = lambda _type: [row for row in exports if _type in row['key']] form_exports = filter_exports('FormExportInstance') case_exports = filter_exports('CaseExportInstance') confirm = input( "There are {f_count} form exports, and {c_count} case exports. " "Are you sure you want to delete all these exports [y/N]?\n".format( f_count=len(form_exports), c_count=len(case_exports) ) ) to_delete = [] if confirm.lower() == 'y': _type = input( "Enter 'case' to delete all case exports, " "'form to delete all form exports, " "'all' to delete both form and case exports. " "Entery anything else to exit.\n" ) if _type == 'form': to_delete = form_exports print("Deleting form exports") elif _type == 'case': to_delete = case_exports print("Deleting case exports") elif _type == 'all': to_delete = form_exports + case_exports print("Deleting all exports") else: print("Not deleting anything, exiting!") return total_count = iter_bulk_delete(db, [doc['id'] for doc in to_delete]) print("Deleted total of {} exports succesfully!".format(total_count))
def record_feed_access_in_datadog(request, config_id, duration, response): config = ExportInstance.get(config_id) username = request.couch_user.username json_response = json.loads(response.content.decode('utf-8')) rows = json_response['value'] row_count = len(rows) try: column_count = len(rows[0]) except IndexError: column_count = 0 metrics_histogram( 'commcare.odata_feed.test_v3', duration, bucket_tag='duration_bucket', buckets=(1, 5, 20, 60, 120, 300, 600), bucket_unit='s', tags={ 'domain': request.domain, 'feed_id': config_id, 'feed_type': config.type, 'username': username, 'row_count': row_count, 'column_count': column_count, 'size': len(response.content) } )
def record_feed_access_in_datadog(request, config_id, duration, response): config = ExportInstance.get(config_id) username = request.couch_user.username json_response = json.loads(response.content.decode('utf-8')) rows = json_response['value'] row_count = len(rows) try: column_count = len(rows[0]) except IndexError: column_count = 0 datadog_counter('commcare.odata_feed.test_v3', tags=[ 'domain:{}'.format(request.domain), 'feed_id:{}'.format(config_id), 'feed_type:{}'.format(config.type), 'username:{}'.format(username), 'row_count:{}'.format(row_count), 'column_count:{}'.format(column_count), 'size:{}'.format(len(response.content)), 'duration:{}'.format(duration), 'duration_bucket:{}'.format( bucket_value(duration, (1, 5, 20, 60, 120, 300, 600), 's')), ])
def test_file_save_and_load(self): payload = 'something small and simple' export = ExportInstance(daily_saved_export=True, domain="test") export.save() export.set_payload(payload) self.assertEqual(payload, export.get_payload())
def export_type(self): return ExportInstance.get(self.export_id).type
def handle(self, domain, **options): db = ExportInstance.get_db() exports = db.view( 'export_instances_by_domain/view', startkey=[domain], endkey=[domain, {}], include_docs=False, reduce=False, ).all() if not exports: print("No exports to delete here, exiting.") return if options['days_inactive'] > 0: import datetime inactive_since = datetime.datetime.today() - datetime.timedelta(days=int(options['days_inactive'])) inactive_exports = [] for export in exports: e = ExportInstance.get(export['id']) if e.last_accessed and e.last_accessed <= inactive_since: inactive_exports.append(export) if not inactive_exports: print("No exports have been inactive for more than {days_inactive} days, exiting.".format( days_inactive=options['days_inactive']) ) return confirm = input( "There are {total_exports} exports in {domain}. Are you sure you want to delete " "{total_inactive_exports} that are older than {days_inactive} days [y/N]?".format( total_exports=len(exports), total_inactive_exports=len(inactive_exports), domain=domain, days_inactive=int(options['days_inactive']) ) ) if confirm.lower() == 'y': exports = inactive_exports else: return filter_exports = lambda _type: [row for row in exports if _type in row['key']] form_exports = filter_exports('FormExportInstance') case_exports = filter_exports('CaseExportInstance') confirm = input( "There are {f_count} form exports, and {c_count} case exports. " "Are you sure you want to delete all these exports [y/N]?\n".format( f_count=len(form_exports), c_count=len(case_exports) ) ) to_delete = [] if confirm.lower() == 'y': _type = input( "Enter 'case' to delete all case exports, " "'form to delete all form exports, " "'all' to delete both form and case exports. " "Enter anything else to exit.\n" ) if _type == 'form': to_delete = form_exports print("Deleting form exports") elif _type == 'case': to_delete = case_exports print("Deleting case exports") elif _type == 'all': to_delete = form_exports + case_exports print("Deleting all exports") else: print("Not deleting anything, exiting!") return total_count = iter_bulk_delete(db, [doc['id'] for doc in to_delete]) print("Deleted total of {} exports succesfully!".format(total_count))
def test_file_save_and_load(self): payload = b'something small and simple' export = ExportInstance(daily_saved_export=True, domain="test") export.save() export.set_payload(payload) self.assertEqual(payload, export.get_payload())