cahce apraisal

This commit is contained in:
2023-09-23 10:56:17 +02:00
parent 1c882e0d1c
commit 14b2f01ef1
3 changed files with 42 additions and 11 deletions

View File

@@ -1,4 +1,6 @@
import { evepraisalAxiosInstance } from '@/service'; import { evepraisalAxiosInstance } from '@/service';
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { MarketType } from "./type"; import { MarketType } from "./type";
export type MarketTypePrice = { export type MarketTypePrice = {
@@ -7,9 +9,35 @@ export type MarketTypePrice = {
sell: number sell: number
} }
export const getPrice = async (type: MarketType): Promise<MarketTypePrice> => (await getPrices([type]))[0]; type MarketTypePriceCache = {
export const getPrices = async (types: MarketType[]): Promise<MarketTypePrice[]> => (await evepraisalAxiosInstance.post(`/appraisal.json?market=jita&persist=no&raw_textarea=${types.map(t => t.name).join("%0A")}`)).data.appraisal.items.map((item: any) => ({ price: MarketTypePrice,
type: types.find(t => t.name === item.typeName), date: Date
buy: item.prices.buy.max, }
sell: item.prices.sell.min
})); const cacheDuration = 1000 * 60 * 5; // 5 minutes
export const useApraisalStore = defineStore('appraisal', () => {
const cache = ref<Record<number, MarketTypePriceCache>>({});
const getPricesUncached = async (types: MarketType[]): Promise<MarketTypePrice[]> => (await evepraisalAxiosInstance.post(`/appraisal.json?market=jita&persist=no&raw_textarea=${types.map(t => t.name).join("%0A")}`)).data.appraisal.items.map((item: any) => ({
type: types.find(t => t.name === item.typeName),
buy: item.prices.buy.max,
sell: item.prices.sell.min
}));
const getPrice = async (type: MarketType): Promise<MarketTypePrice> => (await getPrices([type]))[0];
const getPrices = async (types: MarketType[]): Promise<MarketTypePrice[]> => {
const now = new Date().getTime();
const cached = types.map(t => cache.value[t.id]).filter(c => c && now - c.date.getTime() < cacheDuration);
const uncached = types.filter(t => !cached.find(c => c.price.type.id === t.id));
if (uncached.length > 0) {
const prices = await getPricesUncached(uncached);
prices.forEach(p => cache.value[p.type.id] = { price: p, date: new Date() });
return [...cached.map(c => c.price), ...prices];
}
return cached.map(c => c.price);
};
return { getPrice, getPrices };
});

View File

@@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { MarketType, MarketTypePrice, getHistory, getMarketType, getMarketTypes, getPrice, getPrices, jitaId } from "@/market"; import { MarketType, MarketTypePrice, getHistory, getMarketType, getMarketTypes, jitaId, useApraisalStore } from "@/market";
import { ScanResult, ScanResultTable, useMarkeyScanStore } from '@/market/scan'; import { ScanResult, ScanResultTable, useMarkeyScanStore } from '@/market/scan';
import { BuyModal } from '@/market/track'; import { BuyModal } from '@/market/track';
import { ref, watch } from 'vue'; import { ref, watch } from 'vue';
@@ -9,13 +9,14 @@ const buyModal = ref<typeof BuyModal>();
const item = ref(""); const item = ref("");
const apraisalStore = useApraisalStore();
const markeyScanStore = useMarkeyScanStore(); const markeyScanStore = useMarkeyScanStore();
const items = ref<ScanResult[]>([]); const items = ref<ScanResult[]>([]);
const addOrRelaod = async (type: MarketType) => { const addOrRelaod = async (type: MarketType) => {
const typeID = type.id; const typeID = type.id;
const [history, price] = await Promise.all([ const [history, price] = await Promise.all([
getHistory(jitaId, typeID), getHistory(jitaId, typeID),
getPrice(type) apraisalStore.getPrice(type)
]); ]);
const item = { const item = {
type, type,
@@ -50,7 +51,7 @@ watch(() => markeyScanStore.types, async t => {
return; return;
} }
const prices = await getPrices(await getMarketTypes(typesToLoad)); const prices = await apraisalStore.getPrices(await getMarketTypes(typesToLoad));
items.value = [...items.value, ...(await Promise.all(typesToLoad.map(async i => { items.value = [...items.value, ...(await Promise.all(typesToLoad.map(async i => {
const price = prices.find(p => p.type.id === i) as MarketTypePrice; const price = prices.find(p => p.type.id === i) as MarketTypePrice;

View File

@@ -1,11 +1,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { MarketTypePrice, getMarketTypes, getPrices } from "@/market"; import { MarketTypePrice, getMarketTypes, useApraisalStore } from "@/market";
import { BuyModal, SellModal, TrackResultTable, TrackedItem, useTrackedItemStore } from '@/market/track'; import { BuyModal, SellModal, TrackResultTable, TrackedItem, useTrackedItemStore } from '@/market/track';
import { ref, watch } from 'vue'; import { ref, watch } from 'vue';
const buyModal = ref<typeof BuyModal>(); const buyModal = ref<typeof BuyModal>();
const sellModal = ref<typeof SellModal>(); const sellModal = ref<typeof SellModal>();
const apraisalStore = useApraisalStore();
const trackedItemStore = useTrackedItemStore(); const trackedItemStore = useTrackedItemStore();
const items = ref<TrackedItem[]>([]); const items = ref<TrackedItem[]>([]);
@@ -14,7 +16,7 @@ watch(() => trackedItemStore.items.value, async itms => {
return; return;
} }
const prices = await getPrices(await getMarketTypes(itms.map(i => i.typeID))); const prices = await apraisalStore.getPrices(await getMarketTypes(itms.map(i => i.typeID)));
items.value = itms.map(i => { items.value = itms.map(i => {
const price = prices.find(p => p.type.id === i.typeID) as MarketTypePrice; const price = prices.find(p => p.type.id === i.typeID) as MarketTypePrice;