def process_request(self, request_objects): try: data = self.repo.area.get_all_area(request_objects) total = self.repo.area.get_total_area(request_objects) total_page = ceil(total / request_objects.limit) serializer_area = AllAreaBaseSchema().dump(data, many=True) response = { 'success': True, 'code': Config.STATUS_CODES[Config.SUCCESS], 'message': Config.SUCCESS.lower(), 'meta': { 'page': request_objects.page, 'limit': request_objects.limit, 'totalRecords': total, 'totalPages': total_page, }, 'data': serializer_area } return ro.ResponseSuccess(response) except requests.Timeout: return CommonResponse.build_common_message("time out error", Config.SYSTEM_ERROR) except Exception as e: return CommonResponse.build_common_message(str(e), Config.SYSTEM_ERROR)
def process_request(self, request_objects): try: data = self.repo.area.get_all_area(request_objects) total = self.repo.area.get_total_area(request_objects) data_plankton = self.repo.plankton.get_variant( '/variants?noCache=true') plankton_include = self.repo.plankton.get_variant( '/variants?include=product,offers&page[number]=1&page[size]=100&filter[status]=published&channel=b2b&noCache=true' ) total_page = ceil(total / request_objects.limit) serializer_area = AllAreaBaseSchema().dump(data, many=True) plankton_tmp = [] for i, row in enumerate(data_plankton.get('data')): if i == 3: break else: plankton_tmp.append({ 'skuNo': row.get('skuNo'), 'fullname': row.get('fullname') }) for i, data in enumerate(serializer_area): if i == 11: break else: data['plankton'] = plankton_tmp response = { 'success': True, 'code': Config.STATUS_CODES[Config.SUCCESS], 'message': Config.SUCCESS.lower(), 'meta': { 'page': request_objects.page, 'limit': request_objects.limit, 'totalRecords': total, 'totalPages': total_page, }, 'data': serializer_area } return ro.ResponseSuccess(response) except requests.Timeout: return CommonResponse.build_common_message("time out error", Config.SYSTEM_ERROR) except Exception as e: return CommonResponse.build_common_message(str(e), Config.SYSTEM_ERROR)
async def process_request(self, request_objects): try: task1 = asyncio.create_task(self.repo.get_all_area(request_objects), name='data_area') task2 = asyncio.create_task(self.repo.get_total_area(request_objects), name='total_area') all_tasks = [task1, task2] done_tasks, pending_tasks = await asyncio.wait(all_tasks, timeout=50, return_when=asyncio.ALL_COMPLETED) init_pass = True count = 0 while init_pass or len(pending_tasks) > 0: init_pass = False if count == 3: return CommonResponse.build_common_message("pending tasks", Config.SYSTEM_ERROR) for done in done_tasks: name = done.get_name() if name == 'data_area': data = done.result() elif name == 'total_area': total = done.result() done_tasks, pending_tasks = await asyncio.wait(all_tasks, timeout=50, return_when=asyncio.ALL_COMPLETED) count += 1 total_page = ceil(total / request_objects.limit) serializer_area = AllAreaBaseSchema().dump(data, many=True) response = { 'success': True, 'code': Config.STATUS_CODES[Config.SUCCESS], 'message': Config.SUCCESS.lower(), 'meta': { 'page': request_objects.page, 'limit': request_objects.limit, 'totalRecords': total, 'totalPages': total_page, }, 'data': serializer_area } return ro.ResponseSuccess(response) except asyncio.TimeoutError: return CommonResponse.build_common_message("time out error", Config.SYSTEM_ERROR) except Exception as e: return CommonResponse.build_common_message(str(e), Config.SYSTEM_ERROR)
async def process_request(self, request_objects): try: data_expedition = await asyncio.create_task(self.repo.expedition.get_all(request_objects)) data_area = await asyncio.create_task(self.repo.area.get_subdistrict_by_zipcode(17111)) data_plankton = await asyncio.create_task(self.repo.plankton.get_variant('/variants?filter[skuNo]=3316920142')) # await data_expedition # await data_area # await data_plankton serializer = ExpeditionBaseSchema().dump(data_expedition, many=True) # for data in serializer: # di serialize dulu wak # data['subDistrict'] = data_area # data['plankton'] = data_plankton return ro.ResponseSuccess(serializer) except Exception as e: print(e, "got an error") return CommonResponse.build_common_message(str(e), Config.SYSTEM_ERROR)
def process_request(self, request_objects): try: data_expedition = self.repo.expedition.get_all(request_objects) data_total_expedition = self.repo.expedition.get_total(request_objects) data_area = self.repo.area.get_subdistrict_by_zipcode(17111) data_plankton = self.repo.plankton.get_variant('/variants?filter[skuNo]=3316920142&noCache=true') total_page = ceil(data_total_expedition / request_objects.limit) serializer_expedition = ExpeditionBaseSchema().dump(data_expedition, many=True) serializer_area = AreaBaseSchema().dump(data_area, many=True) plankton_tmp = [] for plankton in data_plankton.get('data'): plankton_tmp.append( { 'skuNo': plankton.get('skuNo'), 'fullname': plankton.get('fullname') } ) for data in serializer_expedition: data['subDistrict'] = serializer_area data['plankton'] = plankton_tmp response = { 'success': True, 'code': Config.STATUS_CODES[Config.SUCCESS], 'message': Config.SUCCESS.lower(), 'meta': { 'page': request_objects.page, 'limit': request_objects.limit, 'totalRecords': data_total_expedition, 'totalPages': total_page, }, 'data': serializer_expedition } return ro.ResponseSuccess(response) except Exception as e: return CommonResponse.build_common_message(str(e), Config.SYSTEM_ERROR)
async def process_request(self, request_objects): try: task1 = asyncio.create_task(self.repo.area.get_all_area(request_objects), name='data_area') task2 = asyncio.create_task(self.repo.area.get_total_area(request_objects), name='total_area') task3 = asyncio.create_task(self.repo.plankton.get_variant('/variants?noCache=true'), name='data_variant') task4 = asyncio.create_task( self.repo.plankton.get_variant('/variants?include=product,offers&page[number]=1&page[size]=100&filter[status]=published&channel=b2b&noCache=true'), name='plankton_include' ) all_tasks = [task1, task2, task3, task4] done_tasks, pending_tasks = await asyncio.wait(all_tasks, timeout=50, return_when=asyncio.ALL_COMPLETED) data = None total = None data_plankton = None plankton_include = None init_pass = True count = 0 while init_pass or len(pending_tasks) > 0: init_pass = False if count == 3: return CommonResponse.build_common_message("pending tasks", Config.SYSTEM_ERROR) for done in done_tasks: name = done.get_name() if name == 'data_area': data = done.result() elif name == 'total_area': total = done.result() elif name == 'data_variant': data_plankton = done.result() elif name == 'plankton_include': plankton_include = done.result() done_tasks, pending_tasks = await asyncio.wait(all_tasks, timeout=50, return_when=asyncio.ALL_COMPLETED) count += 1 total_page = ceil(total / request_objects.limit) serializer_area = AllAreaBaseSchema().dump(data, many=True) plankton_tmp = [] if data_plankton: for i, row in enumerate(data_plankton.get('data')): if i == 3: break else: plankton_tmp.append( { 'skuNo': row.get('skuNo'), 'fullname': row.get('fullname') } ) for i, data in enumerate(serializer_area): if i == 11: break else: data['plankton'] = plankton_tmp response = { 'success': True, 'code': Config.STATUS_CODES[Config.SUCCESS], 'message': Config.SUCCESS.lower(), 'meta': { 'page': request_objects.page, 'limit': request_objects.limit, 'totalRecords': total, 'totalPages': total_page, }, 'data': serializer_area } return ro.ResponseSuccess(response) except asyncio.TimeoutError: return CommonResponse.build_common_message("time out error", Config.SYSTEM_ERROR) except asyncio.CancelledError: return CommonResponse.build_common_message("cancelled error", Config.SYSTEM_ERROR) except Exception as e: return CommonResponse.build_common_message(str(e), Config.SYSTEM_ERROR)
async def process_request(self, request_objects): try: task1 = asyncio.create_task( self.repo.expedition.get_all(request_objects)) task2 = asyncio.create_task( self.repo.expedition.get_total(request_objects)) task3 = asyncio.create_task( self.repo.area.get_subdistrict_by_zipcode(17111)) task4 = asyncio.create_task( self.repo.plankton.get_variant( '/variants?filter[skuNo]=3316920142&noCache=true')) all_tasks = [task1, task2, task3, task4] done_tasks, pending_tasks = await asyncio.wait( all_tasks, return_when=asyncio.ALL_COMPLETED) for done in done_tasks: name = done._coro.cr_code.co_name if name == 'get_all': data_expedition = done.result() elif name == 'get_total': data_total_expedition = done.result() elif name == 'get_subdistrict_by_zipcode': data_area = done.result() elif name == 'get_variant': data_plankton = done.result() total_page = ceil(data_total_expedition / request_objects.limit) serializer_expedition = ExpeditionBaseSchema().dump( data_expedition, many=True) serializer_area = AreaBaseSchema().dump(data_area, many=True) plankton_tmp = [] for plankton in data_plankton.get('data'): plankton_tmp.append({ 'skuNo': plankton.get('skuNo'), 'fullname': plankton.get('fullname') }) for data in serializer_expedition: data['subDistrict'] = serializer_area data['plankton'] = plankton_tmp response = { 'success': True, 'code': Config.STATUS_CODES[Config.SUCCESS], 'message': Config.SUCCESS.lower(), 'meta': { 'page': request_objects.page, 'limit': request_objects.limit, 'totalRecords': data_total_expedition, 'totalPages': total_page, }, 'data': serializer_expedition } return ro.ResponseSuccess(response) except Exception as e: return CommonResponse.build_common_message(str(e), Config.SYSTEM_ERROR)