def calc_imeis_to_export(self, param): """Method to calculate imeis that needs to be exported.""" recs_to_export = [] recs_to_update = [] self.logger.info('calculating imeis for {0} registration list'.format(param)) imei_records = ApprovedImeis.imei_to_export() self.logger.info('{0} imei records to analyze for export'.format(len(imei_records))) max_workers = app.config['DRS_CONFIG']['lists']['max_workers'] self.logger.info('using {0} thread workers for calculation'.format(max_workers)) self.logger.info('spliting imeis into {0} batches'.format(max_workers)) imei_records = numpy.array_split(imei_records, max_workers) self.logger.info('splited imeis into {0} batches'.format(len(imei_records))) input_queue = Queue() output_queue = Queue() for worker in range(max_workers): # pylint: disable=unused-variable imei_worker = IMEIWorker(in_queue=input_queue, out_queue=output_queue) imei_worker.daemon = True imei_worker.start() for imei_record in imei_records: input_queue.put((imei_record, param)) input_queue.join() for i in range(max_workers): # pylint: disable=unused-variable rec_update, rec_export = output_queue.get() if not rec_update: recs_to_update.extend(rec_update) if not rec_export: recs_to_export.extend(rec_export) ApprovedImeis.bulk_insert_imeis(recs_to_update) return recs_to_export
def test_bulk_insert_imeis(db, session): # pylint: disable=unused-argument """Verify that the bulk_insert_imeis() works as expected.""" imei_norm1 = '67895678901234' imei_norm2 = '76545678906548' imei_norm3 = '54375699900000' request_id = 2376322 status = 'status' delta_status = 'delta status' imeis = [ ApprovedImeis(imei_norm1, request_id, status, delta_status), ApprovedImeis(imei_norm2, request_id, status, delta_status), ApprovedImeis(imei_norm3, request_id, status, delta_status) ] ApprovedImeis.bulk_insert_imeis(imeis) assert ApprovedImeis.exists(imei_norm1) assert ApprovedImeis.exists(imei_norm2) assert ApprovedImeis.exists(imei_norm3)
def test_bulk_insert_imeis(): # pylint: disable=unused-argument """Verify that the bulk_insert_imeis() works as expected.""" imei_list = [ '67895678901234', '76545678906548', '54375699900000', '35282004000637', '35282004000638', '35282004000639', '35282004000640', '35282004000641', '35282004000642', '35282004000643', '35282004000644', '35282004000645', '35282004000646', '35282004000647', '35282004000648', '35282004000649', '35282004000650', '35282004000651', '35282004000652' ] request_id = 2376322 status = 'whitelist' delta_status = 'delta status' imeis = [ ApprovedImeis(imei, request_id, status, delta_status) for imei in imei_list ] ApprovedImeis.bulk_insert_imeis(imeis)
def test_imeis_to_export(db, session): # pylint: disable=unused-argument """Verify that imeis_to_export() works as expected.""" imei_norm1 = '67890000001234' imei_norm2 = '71111178906548' imei_norm3 = '54322333900000' request_id = 2376322 status = 'status' delta_status = 'delta status' imeis = [ ApprovedImeis(imei_norm1, request_id, status, delta_status), ApprovedImeis(imei_norm2, request_id, status, delta_status), ApprovedImeis(imei_norm3, request_id, status, delta_status) ] ApprovedImeis.bulk_insert_imeis(imeis) imeis_to_export = ApprovedImeis.imei_to_export() assert imeis_to_export for imei in imeis_to_export: assert imei.removed is False
def de_register_imeis(imeis): """Method to De-Register imeis. """ # find imei devices and related imeis if len(imeis) == 1: query = """SELECT normalized_imei FROM public.imeidevice WHERE device_id IN (SELECT device_id FROM public.imeidevice WHERE normalized_imei='{imei}')""".format( imei=imeis[0]) else: query = """SELECT normalized_imei FROM public.imeidevice WHERE device_id IN (SELECT device_id FROM public.imeidevice WHERE normalized_imei IN {imeis})""".format( imeis=tuple(imeis)) try: res = db.engine.execute(query).fetchall() matched_imeis = list(chain.from_iterable(res)) # de register imeis from approved imeis updated_imeis = [] for imei in matched_imeis: imei_ = ApprovedImeis.get_imei(imei) if imei_.status != 'removed': imei_.status = 'removed' imei_.delta_status = 'remove' updated_imeis.append(imei_) if len(updated_imeis) > 0: ApprovedImeis.bulk_insert_imeis(updated_imeis) return True else: return False except Exception as e: app.logger.error( 'An exception occurred while De-Registering IMEIs see exception log:' ) app.logger.exception(e) return None
def test_de_registration_request_approval(flask_app, db, app): """Verify that the system approves the request when all the sections are approved and de-register imeis successfully. """ headers = {'Content-Type': 'application/json'} data = { 'device_count': 1, 'imei_per_device': 1, 'imeis': "[['121621090005119']]", 'm_location': 'local', 'user_name': 'reg req approve rev user 9', 'user_id': 'reg-req-approve-rev-user-9' } request = create_assigned_dummy_request(data, 'Registration', 'dereg-req-approve-rev-user-10', 'dereg req approve rev user 10') assert request request_id = request.id user_id = request.user_id device_data = { 'brand': 'samsung', 'operating_system': 'android', 'model_name': 's9', 'model_num': '30jjd', 'device_type': 'Smartphone', 'technologies': '2G,3G,4G', 'reg_id': request_id } request = create_dummy_devices(device_data, 'Registration', request) assert request # create user quota quota = DeviceQuota.create(user_id, 'individual') assert quota de_registration_data = { 'file': 'de-reg-test-file.txt', 'device_count': 1, 'user_id': 'dereg-req-approve-rev-user-10', 'user_name': 'dereg req approve rev user 10', 'reason': 'because we have to run tests successfully' } request = create_assigned_dummy_request(de_registration_data, 'De_Registration', 'dereg-req-approve-rev-user-10', 'dereg req approve rev user 10') device_data = { 'devices': """[ { "tac": "12162109", "model_name": "TA-1034", "brand_name": "NOKIA", "model_num": "TA-1034", "technology": "NONE", "device_type": "Mobile Phone/Feature phone", "count": 2, "operating_system": "N/A" } ]""", 'dereg_id': request.id } request = create_dummy_devices(device_data, 'De_Registration', request, db, file_content=['121621090005119'], file_path='{0}/{1}/{2}'.format(app.config['DRS_UPLOADS'], request.tracking_id, de_registration_data.get('file'))) assert request request_id = request.id # add imei to approvedimeis as whitelist to de register approved_imei = ApprovedImeis.get_imei('12162109000511') approved_imei.status = 'whitelist' approved_imei.delta_status = 'update' ApprovedImeis.bulk_insert_imeis([approved_imei]) DeRegComments.add('device_description', 'test comment on section', 'dereg-req-approve-rev-user-10', 'dereg req approve rev user 10', 6, request_id) DeRegComments.add('imei_classification', 'test comment on section', 'dereg-req-approve-rev-user-10', 'dereg req approve rev user 10', 6, request_id) DeRegComments.add('imei_registration', 'test comment on section', 'dereg-req-approve-rev-user-10', 'dereg req approve rev user 10', 6, request_id) DeRegComments.add('approval_documents', 'test comment on section', 'dereg-req-approve-rev-user-10', 'dereg req approve rev user 10', 6, request_id) body_data = { 'request_id': request_id, 'request_type': 'de_registration_request', 'reviewer_id': 'dereg-req-approve-rev-user-10' } rv = flask_app.put(SUBMIT_REVIEW_API, data=json.dumps(body_data), headers=headers) assert rv.status_code == 201 response = json.loads(rv.data.decode('utf-8')) imei = ApprovedImeis.get_imei('12162109000511') assert response['request_type'] == 'de_registration_request' assert response['status'] == 6 assert response['request_id'] == request_id assert imei.status == 'removed'