extract evepraisal from apraisal

This commit is contained in:
2023-11-16 09:54:23 +01:00
parent f115381955
commit 98ce81dfb2
7 changed files with 43 additions and 30 deletions

View File

@@ -0,0 +1,11 @@
import { MarketType } from "../type";
export type MarketTypePrice = {
type: MarketType;
buy: number;
sell: number;
orderCount: number;
};
export type PriceGetter = (types: MarketType[]) => Promise<MarketTypePrice[]>;

View File

@@ -0,0 +1,44 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { MarketType } from "../type";
import { MarketTypePrice } from './MarketTypePrice';
import { getEvepraisalPrices } from './evepraisal';
type MarketTypePriceCache = {
price: MarketTypePrice,
date: Date
}
const cacheDuration = 1000 * 60 * 5; // 5 minutes
export const useApraisalStore = defineStore('appraisal', () => {
const cache = ref<Record<number, MarketTypePriceCache>>({});
const getPricesUncached = getEvepraisalPrices;
const getPrice = async (type: MarketType): Promise<MarketTypePrice> => (await getPrices([type]))[0];
const getPrices = async (types: MarketType[]): Promise<MarketTypePrice[]> => {
const now = new Date();
const cached: MarketTypePrice[] = [];
const uncached: MarketType[] = [];
types.forEach(t => {
const cachedPrice = cache.value[t.id];
if (cachedPrice && now.getTime() - cachedPrice.date.getTime() < cacheDuration) {
cached.push(cachedPrice.price);
} else {
uncached.push(t);
}
});
if (uncached.length > 0) {
const prices = await getPricesUncached(uncached);
prices.forEach(p => cache.value[p.type.id] = { price: p, date: now });
return [...cached, ...prices];
}
return cached;
};
return { getPrice, getPrices };
});

View File

@@ -0,0 +1,21 @@
import { evepraisalAxiosInstance } from '@/service';
import { MarketType } from "../type";
import { MarketTypePrice, PriceGetter } from './MarketTypePrice';
const batchSize = 100;
export const getEvepraisalPrices: PriceGetter = async (types: MarketType[]): Promise<MarketTypePrice[]> => {
const batches = [];
for (let i = 0; i < types.length; i += batchSize) {
batches.push(evepraisalAxiosInstance.post(`/appraisal.json?market=jita&persist=no&raw_textarea=${types.slice(i, i + batchSize).map(t => t.name).join("%0A")}`));
}
return (await Promise.all(batches))
.flatMap(b => b.data.appraisal.items)
.map((item: any) => ({
type: types.find(t => t.name === item.typeName) as MarketType,
buy: item.prices.buy.max,
sell: item.prices.sell.min,
orderCount: item.prices.all.order_count
}));
};

View File

@@ -0,0 +1,2 @@
export * from './MarketTypePrice';
export * from './appraisal';