item service: include wiki prices in price data
This commit is contained in:
@@ -32,4 +32,5 @@ public class ItemPrice
|
|||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private int price;
|
private int price;
|
||||||
|
private int wikiPrice;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,12 +24,12 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.http.service.item;
|
package net.runelite.http.service.item;
|
||||||
|
|
||||||
import com.google.common.base.Supplier;
|
|
||||||
import com.google.common.base.Suppliers;
|
import com.google.common.base.Suppliers;
|
||||||
import com.google.common.hash.HashCode;
|
import com.google.common.hash.HashCode;
|
||||||
import com.google.common.hash.Hasher;
|
import com.google.common.hash.Hasher;
|
||||||
import com.google.common.hash.Hashing;
|
import com.google.common.hash.Hashing;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import net.runelite.http.api.item.ItemPrice;
|
import net.runelite.http.api.item.ItemPrice;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -83,11 +83,24 @@ public class ItemController
|
|||||||
itemPrice.setId(priceEntry.getItem());
|
itemPrice.setId(priceEntry.getItem());
|
||||||
itemPrice.setName(priceEntry.getName());
|
itemPrice.setName(priceEntry.getName());
|
||||||
itemPrice.setPrice(priceEntry.getPrice());
|
itemPrice.setPrice(priceEntry.getPrice());
|
||||||
|
itemPrice.setWikiPrice(computeWikiPrice(priceEntry));
|
||||||
return itemPrice;
|
return itemPrice;
|
||||||
})
|
})
|
||||||
.toArray(ItemPrice[]::new)), priceCache, TimeUnit.MINUTES);
|
.toArray(ItemPrice[]::new)), priceCache, TimeUnit.MINUTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int computeWikiPrice(PriceEntry priceEntry)
|
||||||
|
{
|
||||||
|
if (priceEntry.getLow() > 0 && priceEntry.getHigh() > 0)
|
||||||
|
{
|
||||||
|
return (priceEntry.getLow() + priceEntry.getHigh()) / 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Math.max(priceEntry.getLow(), priceEntry.getHigh());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/prices")
|
@GetMapping("/prices")
|
||||||
public ResponseEntity<ItemPrice[]> prices()
|
public ResponseEntity<ItemPrice[]> prices()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@@ -141,7 +140,7 @@ public class ItemService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<PriceEntry> fetchPrice(int itemId)
|
private void fetchPrice(int itemId)
|
||||||
{
|
{
|
||||||
RSPrices rsprice;
|
RSPrices rsprice;
|
||||||
try
|
try
|
||||||
@@ -151,12 +150,11 @@ public class ItemService
|
|||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
log.warn("unable to fetch price for item {}", itemId, ex);
|
log.warn("unable to fetch price for item {}", itemId, ex);
|
||||||
return null;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (Connection con = sql2o.beginTransaction())
|
try (Connection con = sql2o.beginTransaction())
|
||||||
{
|
{
|
||||||
List<PriceEntry> entries = new ArrayList<>();
|
|
||||||
Instant now = Instant.now();
|
Instant now = Instant.now();
|
||||||
|
|
||||||
Query query = con.createQuery("insert into prices (item, price, time, fetched_time) values (:item, :price, :time, :fetched_time) "
|
Query query = con.createQuery("insert into prices (item, price, time, fetched_time) values (:item, :price, :time, :fetched_time) "
|
||||||
@@ -169,13 +167,6 @@ public class ItemService
|
|||||||
|
|
||||||
Instant time = Instant.ofEpochMilli(ts);
|
Instant time = Instant.ofEpochMilli(ts);
|
||||||
|
|
||||||
PriceEntry priceEntry = new PriceEntry();
|
|
||||||
priceEntry.setItem(itemId);
|
|
||||||
priceEntry.setPrice(price);
|
|
||||||
priceEntry.setTime(time);
|
|
||||||
priceEntry.setFetched_time(now);
|
|
||||||
entries.add(priceEntry);
|
|
||||||
|
|
||||||
query
|
query
|
||||||
.addParameter("item", itemId)
|
.addParameter("item", itemId)
|
||||||
.addParameter("price", price)
|
.addParameter("price", price)
|
||||||
@@ -186,8 +177,6 @@ public class ItemService
|
|||||||
|
|
||||||
query.executeBatch();
|
query.executeBatch();
|
||||||
con.commit(false);
|
con.commit(false);
|
||||||
|
|
||||||
return entries;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,9 +184,11 @@ public class ItemService
|
|||||||
{
|
{
|
||||||
try (Connection con = sql2o.beginTransaction())
|
try (Connection con = sql2o.beginTransaction())
|
||||||
{
|
{
|
||||||
Query query = con.createQuery("select t2.item, t3.name, 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 " +
|
Query query = con.createQuery("select t2.item, t3.name, t2.time, prices.price, prices.fetched_time, t4.high, t4.low" +
|
||||||
" join prices on t2.item=prices.item and t2.time=prices.time" +
|
" from (select t1.item as item, max(t1.time) as time from prices t1 group by item) t2" +
|
||||||
" join items t3 on t2.item=t3.id");
|
" join prices on t2.item=prices.item and t2.time=prices.time" +
|
||||||
|
" join items t3 on t2.item=t3.id" +
|
||||||
|
" join wiki_prices t4 on t2.item=t4.item_id");
|
||||||
return query.executeAndFetch(PriceEntry.class);
|
return query.executeAndFetch(PriceEntry.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,4 +35,6 @@ class PriceEntry
|
|||||||
private int price;
|
private int price;
|
||||||
private Instant time;
|
private Instant time;
|
||||||
private Instant fetched_time;
|
private Instant fetched_time;
|
||||||
|
private int high;
|
||||||
|
private int low;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user