Frontend updates: caching basic data (json) from CDragon
Implement caching in the patch_detector, consume the cache from API routes in frontend
This commit is contained in:
34
dev/scripts/fetch-cdragon.js
Normal file
34
dev/scripts/fetch-cdragon.js
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { spawn } from 'child_process';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
// Cache directory - use dev/cdragon by default
|
||||
const cacheDir = process.env.CDRAGON_CACHE_DIR || path.join(__dirname, '..', 'data', 'cdragon');
|
||||
|
||||
// Dev MongoDB credentials (matching docker-compose.yml defaults)
|
||||
const mongoUser = process.env.MONGO_USER || 'root';
|
||||
const mongoPass = process.env.MONGO_PASS || 'password';
|
||||
const mongoHost = process.env.MONGO_HOST || 'localhost:27017';
|
||||
|
||||
// Run patch_detector with the cache directory and dev MongoDB credentials
|
||||
const patchDetector = spawn('npx', ['tsx', '../patch_detector/index.ts'], {
|
||||
cwd: path.join(__dirname, '..'),
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: 'development',
|
||||
CDRAGON_CACHE_DIR: cacheDir,
|
||||
MONGO_USER: mongoUser,
|
||||
MONGO_PASS: mongoPass,
|
||||
MONGO_HOST: mongoHost
|
||||
},
|
||||
stdio: 'inherit'
|
||||
});
|
||||
|
||||
patchDetector.on('close', (code) => {
|
||||
process.exit(code || 0);
|
||||
});
|
||||
@@ -23,7 +23,7 @@ async function setupDatabase() {
|
||||
const patchFile = path.join(dataDir, "patches.json");
|
||||
if(!fs.existsSync(dataDir) || !fs.existsSync(patchFile)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
console.log('📥 No data files found. Downloading latest snapshot...');
|
||||
console.log('🚫 No data files found. Downloading latest snapshot...');
|
||||
await downloadAndExtractSnapshot();
|
||||
}
|
||||
|
||||
@@ -90,7 +90,11 @@ async function setupDatabase() {
|
||||
console.log('✅ Skipping matches import - sufficient data already present');
|
||||
}
|
||||
|
||||
// 7. Run match collector to generate stats
|
||||
// 7. Fetch CDragon data for the current patch
|
||||
console.log('🎮 Fetching CDragon data...');
|
||||
await fetchCDragonData();
|
||||
|
||||
// 8. Run match collector to generate stats
|
||||
console.log('📊 Generating champion stats...');
|
||||
await generateChampionStats();
|
||||
|
||||
@@ -322,6 +326,24 @@ async function generateChampionStats() {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchCDragonData() {
|
||||
try {
|
||||
console.log('🔄 Running CDragon fetcher...');
|
||||
|
||||
// Run the fetch-cdragon script
|
||||
const fetchCDragonPath = path.join(__dirname, 'fetch-cdragon.js');
|
||||
execSync(`node ${fetchCDragonPath}`, {
|
||||
stdio: 'inherit',
|
||||
cwd: path.join(__dirname, '..')
|
||||
});
|
||||
|
||||
console.log('✅ CDragon data fetched');
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to fetch CDragon data:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function getMatchCount(patchVersion) {
|
||||
const client = new MongoClient(getMongoUri());
|
||||
await client.connect();
|
||||
|
||||
Reference in New Issue
Block a user