history cache

This commit is contained in:
2024-05-22 09:59:13 +02:00
parent e379f490a4
commit f75156bc62
9 changed files with 37 additions and 25 deletions
@@ -0,0 +1,26 @@
import { esiAxiosInstance } from "@/service";
export type EsiMarketOrderHistory = {
average: number;
date: string;
highest: number;
lowest: number;
order_count: number;
volume: number;
}
// TODO use pinia store
const historyCache: { [key: number]: { [key: number]: EsiMarketOrderHistory[] } } = {};
export const getHistory = async (regionId: number, tyeId: number): Promise<EsiMarketOrderHistory[]> => {
if (historyCache[regionId]?.[tyeId]) {
return historyCache[regionId][tyeId];
}
const value = (await esiAxiosInstance.get(`/markets/${regionId}/history/`, { params: { type_id: tyeId } })).data;
historyCache[regionId] = historyCache[regionId] ?? {};
historyCache[regionId][tyeId] = value;
return value;
}
+59
View File
@@ -0,0 +1,59 @@
import { EsiMarketOrderHistory } from "@/market";
export type HistoryQuartils = {
totalVolume: number,
q1: number,
median: number,
q3: number,
}
export const getHistoryQuartils = (history: EsiMarketOrderHistory[], days?: number): HistoryQuartils => {
const now = Date.now();
const volumes = history
.flatMap(h => {
const volume = h.volume;
if (volume === 0 || (days && new Date(h.date).getTime() < now - days * 24 * 60 * 60 * 1000)) {
return [];
}
const e = estimateVolume(h);
return [[h.highest, e], [h.lowest, volume - e]];
})
.filter(h => h[1] > 0)
.sort((a, b) => a[0] - b[0]);
const totalVolume = volumes.reduce((acc, [_, v]) => acc + v, 0);
const quartilVolume = totalVolume / 4;
const quartils: [number, number, number] = [0, 0, 0];
let currentVolume = 0;
let quartil = 0;
for (const [price, volume] of volumes) {
currentVolume += volume;
if (currentVolume >= quartilVolume * (quartil + 1)) {
quartils[quartil] = price;
if (quartil === 2) {
break;
}
quartil++;
}
}
return {
totalVolume,
q1: quartils[0],
median: quartils[1],
q3: quartils[2],
};
}
const estimateVolume = (history: EsiMarketOrderHistory): number => {
if (history.volume === 0) {
return 0;
}
return Math.max(1, Math.round(history.volume * ((history.average - history.lowest) / (history.highest - history.lowest))));
}
+2
View File
@@ -0,0 +1,2 @@
export * from './EsiMarketOrderHistory';
export * from './HistoryQuartils';