Skip to content

Commit 08f8a82

Browse files
rainerstudiosclaude
andcommitted
Update price history job to track volume and listings
Volume Tracking Enhancement - adds marketplace listing counts and calculates 24h sales volume 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 10cb285 commit 08f8a82

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

jobs/price-history-snapshot.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ async function recordItemSnapshot(itemName, priceData) {
6868
const lowestPrice = priceData.lowestPrice || null;
6969
const highestPrice = priceData.highestPrice || null;
7070

71+
// Get listings counts
72+
const buff163Listings = priceData.prices?.buff163?.listings || null;
73+
const csfloatListings = priceData.prices?.csfloat?.listings || null;
74+
const skinportListings = priceData.prices?.skinport?.listings || null;
75+
7176
// Only record if we have at least one price
7277
const hasPrices = [steamPrice, buff163Price, csfloatPrice, skinportPrice, csdealsPrice]
7378
.some(price => price !== null && price > 0);
@@ -77,6 +82,37 @@ async function recordItemSnapshot(itemName, priceData) {
7782
return false;
7883
}
7984

85+
// Get previous snapshot to calculate volume (24h ago)
86+
const previousSnapshot = await pool.query(`
87+
SELECT buff163_listings, csfloat_listings, skinport_listings
88+
FROM price_history
89+
WHERE item_name = $1
90+
AND recorded_at >= NOW() - INTERVAL '25 hours'
91+
AND recorded_at <= NOW() - INTERVAL '23 hours'
92+
ORDER BY recorded_at DESC
93+
LIMIT 1
94+
`, [itemName]);
95+
96+
// Calculate volume (listings decreased = items sold)
97+
let volume24h = 0;
98+
if (previousSnapshot.rows.length > 0) {
99+
const prev = previousSnapshot.rows[0];
100+
101+
// Sum up listing decreases (items sold) across all marketplaces
102+
if (prev.buff163_listings && buff163Listings) {
103+
const diff = prev.buff163_listings - buff163Listings;
104+
if (diff > 0) volume24h += diff;
105+
}
106+
if (prev.csfloat_listings && csfloatListings) {
107+
const diff = prev.csfloat_listings - csfloatListings;
108+
if (diff > 0) volume24h += diff;
109+
}
110+
if (prev.skinport_listings && skinportListings) {
111+
const diff = prev.skinport_listings - skinportListings;
112+
if (diff > 0) volume24h += diff;
113+
}
114+
}
115+
80116
await pool.query(`
81117
INSERT INTO price_history (
82118
item_name,
@@ -87,8 +123,12 @@ async function recordItemSnapshot(itemName, priceData) {
87123
csdeals_price,
88124
lowest_price,
89125
highest_price,
126+
buff163_listings,
127+
csfloat_listings,
128+
skinport_listings,
129+
volume_24h,
90130
recorded_at
91-
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW())
131+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, NOW())
92132
`, [
93133
itemName,
94134
steamPrice,
@@ -97,7 +137,11 @@ async function recordItemSnapshot(itemName, priceData) {
97137
skinportPrice,
98138
csdealsPrice,
99139
lowestPrice,
100-
highestPrice
140+
highestPrice,
141+
buff163Listings,
142+
csfloatListings,
143+
skinportListings,
144+
volume24h
101145
]);
102146

103147
return true;

0 commit comments

Comments
 (0)