item controller: expose bulk item prices
This commit is contained in:
@@ -24,6 +24,8 @@
|
||||
*/
|
||||
package net.runelite.http.service.item;
|
||||
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import java.time.Duration;
|
||||
@@ -60,10 +62,26 @@ public class ItemController
|
||||
|
||||
private final ItemService itemService;
|
||||
|
||||
private final Supplier<ItemPrice[]> memorizedPrices;
|
||||
|
||||
@Autowired
|
||||
public ItemController(ItemService itemService)
|
||||
{
|
||||
this.itemService = itemService;
|
||||
|
||||
memorizedPrices = Suppliers.memoizeWithExpiration(() -> itemService.fetchPrices().stream()
|
||||
.map(priceEntry ->
|
||||
{
|
||||
Item item = new Item();
|
||||
item.setId(priceEntry.getItem()); // fake item
|
||||
|
||||
ItemPrice itemPrice = new ItemPrice();
|
||||
itemPrice.setItem(item);
|
||||
itemPrice.setPrice(priceEntry.getPrice());
|
||||
itemPrice.setTime(priceEntry.getTime());
|
||||
return itemPrice;
|
||||
})
|
||||
.toArray(ItemPrice[]::new), 30, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
@RequestMapping("/{itemId}")
|
||||
@@ -212,4 +230,12 @@ public class ItemController
|
||||
})
|
||||
.toArray(ItemPrice[]::new);
|
||||
}
|
||||
|
||||
@RequestMapping("/prices")
|
||||
public ResponseEntity<ItemPrice[]> prices()
|
||||
{
|
||||
return ResponseEntity.ok()
|
||||
.cacheControl(CacheControl.maxAge(30, TimeUnit.MINUTES).cachePublic())
|
||||
.body(memorizedPrices.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,6 +291,16 @@ public class ItemService
|
||||
}
|
||||
}
|
||||
|
||||
public List<PriceEntry> fetchPrices()
|
||||
{
|
||||
try (Connection con = sql2o.beginTransaction())
|
||||
{
|
||||
Query query = con.createQuery("select t2.item, t2.time, prices.price, prices.fetched_time from (select t1.item as item, max(t1.time) as time from prices t1 group by item) t2 join prices on t2.item=prices.item and t2.time=prices.time");
|
||||
List<PriceEntry> entries = query.executeAndFetch(PriceEntry.class);
|
||||
return entries;
|
||||
}
|
||||
}
|
||||
|
||||
private RSItem fetchRSItem(int itemId) throws IOException
|
||||
{
|
||||
HttpUrl itemUrl = RS_ITEM_URL
|
||||
|
||||
Reference in New Issue
Block a user