I'm working on an integration with our warehouse. We receive an XML message with all our SKUs and their inventory levels. For each SKU I need to update the inventory level in my rails app. However, every time I request the XML message, the processing is very slow, to the point where the browser times out. I suspect this is because I have to do a look up for each SKU by name and update the inventory level for it. There should be around 1000 SKUs returned in the XML, this means 2 lookups for each SKU and 1 save.
Is there a way for me to optimize the performance for this integration? I am fairly new to Rails but I feel like there should be a more efficient way of doing this.
Example XML:
<InventoryXML> <Item> <ItemID>ABC-12-2345</ItemID> <ItemQty>14</ItemQty> <Warehouses> <Warehouse> <Name>Constitution</Name> <Quantity>9</Quantity> </Warehouse> <Warehouse> <Name>Vegas</Name> <Quantity>1</Quantity> </Warehouse> <Warehouse> <Name>Atlanta</Name> <Quantity>4</Quantity> </Warehouse> </Warehouses> </Item> </InventoryXML> Code:
def process_inventory @doc.xpath('//Item').each do |item| name = item.xpath('//ItemID').text warehouses = item.xpath('//Warehouses') warehouses.xpath('Warehouse').each do |warehouse| if warehouse.xpath('Name').text == "Vegas" self.update_inventory_for_sku(2, name, warehouse.xpath('Quantity').text.to_i) elsif warehouse.xpath('Name').text == "PA" self.update_inventory_for_sku(1, name, warehouse.xpath('Quantity').text.to_i) end end end return "Succesfully updated SKU inventory" end def update_inventory_for_sku(vendor_id, name, quantity) sku = VendorSku.where({vendor_id: vendor_id, name: name}).first if sku sku.update_attributes(inventory_quantity: quantity) else self.hub_error_message += "\nSKU not found: " + name.to_s + "for vendor_id: " + vendor_id.to_s end end
No comments:
Post a Comment