Skip to content

Commit 0edd081

Browse files
committed
set oldest and current null points
1 parent ef02a81 commit 0edd081

4 files changed

Lines changed: 48 additions & 27 deletions

File tree

src/lib/lttstore/product/ProductMoveRateGraph.svelte

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
let {
1313
productName = typed<string | undefined>(),
1414
stockHistory: stockHistoryRaw = typed<StockHistoryTableRow[]>(),
15-
chartUpdateNumber = typed<number>(1)
15+
chartUpdateNumber = typed<number>(1),
16+
historyDays = typed<number | "all">("all"),
17+
stockAsOf = typed<number>(Date.now())
1618
} = $props();
1719
1820
let wrapperDiv = $state<HTMLDivElement>();
@@ -45,28 +47,33 @@
4547
let data = $derived.by(() => {
4648
return [
4749
[
50+
...(historyDays === "all" ? [] : [Math.round((Date.now() - (historyDays * 24 * 60 * 60e3)) / 1e3)]),
4851
...stockHistory.map((h, i) => i === 0
4952
? Math.round(h.timestamp / 1e3)
5053
: Math.round((stockHistory[i-1].timestamp + h.timestamp) / 2 / 1e3)
5154
),
52-
...(stockHistory.length > 1 ? [Math.round(stockHistory[stockHistory.length-1].timestamp / 1e3)] : []),
55+
Math.round(stockAsOf / 1e3)
5356
],
5457
...(onlyTotal ? ["total"] : Object.keys(someStock))
55-
.map(k => stockHistory.map((h, i, a) => {
56-
if(i > 0) {
57-
const previous = a[i-1];
58-
const previousStock = previous.stock[k]
59-
const currentStock = h.stock[k];
60-
if(previousStock === null || currentStock === null) return null;
61-
const timeDiff = h.timestamp - previous.timestamp;
62-
const stockDiff = previousStock - currentStock;
63-
const rate = stockDiff / (timeDiff / (60 * 60e3));
64-
if(rate < 0) return null;
65-
return rate;
66-
} else {
67-
return null;
68-
}
69-
}))
58+
.map(k => [
59+
...(historyDays === "all" ? [] : [null]),
60+
...stockHistory.map((h, i, a) => {
61+
if(i > 0) {
62+
const previous = a[i-1];
63+
const previousStock = previous.stock[k]
64+
const currentStock = h.stock[k];
65+
if(previousStock === null || currentStock === null) return null;
66+
const timeDiff = h.timestamp - previous.timestamp;
67+
const stockDiff = previousStock - currentStock;
68+
const rate = stockDiff / (timeDiff / (60 * 60e3));
69+
if(rate < 0) return null;
70+
return rate;
71+
} else {
72+
return null;
73+
}
74+
}),
75+
null
76+
])
7077
]
7178
});
7279

src/lib/lttstore/product/ProductStockHistoryGraph.svelte

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
productName = typed<string | undefined>(),
6262
stockHistory = typed<StockHistoryTableRow[]>(),
6363
productOptions = typed<ProductOption[]>(),
64-
chartUpdateNumber = typed<number>(1)
64+
chartUpdateNumber = typed<number>(1),
65+
historyDays = typed<number | "all">("all"),
66+
stockAsOf = typed<number>(Date.now())
6567
} = $props();
6668
6769
let wrapperDiv = $state<HTMLDivElement>();
@@ -93,14 +95,24 @@
9395
9496
let data = $derived.by(() => {
9597
const filteredKeys = filter ? Object.keys(someStock).filter(k => k.includes(filter!)) : Object.keys(someStock);
96-
return [
97-
stockHistory.map((h: StockHistoryTableRow) => Math.round(h.timestamp / 1e3)),
98+
const d = [
99+
[
100+
...(historyDays === "all" ? [] : [Math.round((Date.now() - (historyDays * 24 * 60 * 60e3)) / 1e3)]),
101+
...stockHistory.map((h: StockHistoryTableRow) => Math.round(h.timestamp / 1e3)),
102+
Math.round(stockAsOf / 1e3)
103+
],
98104
...(onlyTotal ? ["total"] : filteredKeys)
99-
.map((k: string) => stockHistory.map((h: { stock: string }) => {
100-
const stock = JSON.parse(h.stock)[k];
101-
return typeof stock === "number" ? stock : null;
102-
}))
103-
]
105+
.map((k: string) => [
106+
...(historyDays === "all" ? [] : [null]),
107+
...stockHistory.map((h: { stock: string }) => {
108+
const stock = JSON.parse(h.stock)[k];
109+
return typeof stock === "number" ? stock : null;
110+
}),
111+
null
112+
])
113+
];
114+
console.debug({d})
115+
return d;
104116
});
105117
106118
let width = $derived(wrapperDiv?.clientWidth ?? 1490);

src/routes/(info)/lttstore/products/[handle]/+page.server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const load = (async ({platform, params, url}) => {
7171
}
7272

7373
let historyDays: number | string = Number(url.searchParams.get("historyDays") ?? defaultHistoryDays);
74+
const stockAsOf = Date.now();
7475

7576
let stockHistory: Promise<StockHistoryTableRow[]>;
7677
if((url.searchParams.get("historyDays") ?? defaultHistoryDays) === "all") {
@@ -115,5 +116,6 @@ export const load = (async ({platform, params, url}) => {
115116
stockHistory: await stockHistory,
116117
changeHistory,
117118
similarProducts,
119+
stockAsOf
118120
}
119121
}) satisfies PageServerLoad

src/routes/(info)/lttstore/products/[handle]/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,10 @@
692692
<option value="all">all-time</option>
693693
</select>
694694
{#key data}
695-
<ProductStockHistoryGraph stockHistory={data.stockHistory} productName={productInfo.title} productOptions={productInfo.options} {chartUpdateNumber}/>
695+
<ProductStockHistoryGraph stockHistory={data.stockHistory} productName={productInfo.title} productOptions={productInfo.options} historyDays={data.historyDays} stockAsOf={data.stockAsOf} {chartUpdateNumber}/>
696696
<div class="min-h-[710px]">
697697
<LazyLoad>
698-
<ProductMoveRateGraph stockHistory={data.stockHistory} productName={productInfo.title} {chartUpdateNumber}/>
698+
<ProductMoveRateGraph stockHistory={data.stockHistory} productName={productInfo.title} historyDays={data.historyDays} stockAsOf={data.stockAsOf} {chartUpdateNumber}/>
699699
</LazyLoad>
700700
</div>
701701
{/key}

0 commit comments

Comments
 (0)