feat: expand template window from 6 months to 1 year and add robust fetcher

- Changed template time window from 6 months to 1 year
- Added robust incremental template fetcher that saves as it goes
- Successfully fetched and saved 399 templates (up from 199)
- Added npm script 'fetch:templates:robust' for better error handling
- Templates now save incrementally to prevent data loss on errors

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-06-20 00:52:26 +02:00
parent 12d23d65a0
commit 48fd319a1b
5 changed files with 138 additions and 7 deletions

Binary file not shown.

View File

@@ -23,6 +23,7 @@
"update:n8n": "node scripts/update-n8n-deps.js",
"update:n8n:check": "node scripts/update-n8n-deps.js --dry-run",
"fetch:templates": "node dist/scripts/fetch-templates.js",
"fetch:templates:robust": "node dist/scripts/fetch-templates-robust.js",
"test:templates": "node dist/scripts/test-templates.js",
"db:rebuild": "node dist/scripts/rebuild-database.js",
"db:init": "node -e \"new (require('./dist/services/sqlite-storage-service').SQLiteStorageService)(); console.log('Database initialized')\"",

View File

@@ -0,0 +1,130 @@
#!/usr/bin/env node
import { createDatabaseAdapter } from '../database/database-adapter';
import { TemplateRepository } from '../templates/template-repository';
import { TemplateFetcher } from '../templates/template-fetcher';
import * as fs from 'fs';
import * as path from 'path';
async function fetchTemplatesRobust() {
console.log('🌐 Fetching n8n workflow templates (last year)...\n');
// Ensure data directory exists
const dataDir = './data';
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
// Initialize database
const db = await createDatabaseAdapter('./data/nodes.db');
// Drop existing templates table to ensure clean schema
try {
db.exec('DROP TABLE IF EXISTS templates');
db.exec('DROP TABLE IF EXISTS templates_fts');
console.log('🗑️ Dropped existing templates tables\n');
} catch (error) {
// Ignore errors if tables don't exist
}
// Apply schema with updated constraint
const schema = fs.readFileSync(path.join(__dirname, '../../src/database/schema.sql'), 'utf8');
db.exec(schema);
// Create repository and fetcher
const repository = new TemplateRepository(db);
const fetcher = new TemplateFetcher();
// Progress tracking
let lastMessage = '';
const startTime = Date.now();
try {
// Fetch template list
console.log('📋 Phase 1: Fetching template list from n8n.io API\n');
const templates = await fetcher.fetchTemplates((current, total) => {
// Clear previous line
if (lastMessage) {
process.stdout.write('\r' + ' '.repeat(lastMessage.length) + '\r');
}
const progress = Math.round((current / total) * 100);
lastMessage = `📊 Fetching template list: ${current}/${total} (${progress}%)`;
process.stdout.write(lastMessage);
});
console.log('\n');
console.log(`✅ Found ${templates.length} templates from last year\n`);
// Fetch details and save incrementally
console.log('📥 Phase 2: Fetching details and saving to database\n');
let saved = 0;
let errors = 0;
for (let i = 0; i < templates.length; i++) {
const template = templates[i];
try {
// Clear previous line
if (lastMessage) {
process.stdout.write('\r' + ' '.repeat(lastMessage.length) + '\r');
}
const progress = Math.round(((i + 1) / templates.length) * 100);
lastMessage = `📊 Processing: ${i + 1}/${templates.length} (${progress}%) - Saved: ${saved}, Errors: ${errors}`;
process.stdout.write(lastMessage);
// Fetch detail
const detail = await fetcher.fetchTemplateDetail(template.id);
// Save immediately
repository.saveTemplate(template, detail);
saved++;
// Rate limiting
await new Promise(resolve => setTimeout(resolve, 200));
} catch (error: any) {
errors++;
console.error(`\n❌ Error processing template ${template.id} (${template.name}): ${error.message}`);
// Continue with next template
}
}
console.log('\n');
// Get stats
const elapsed = Math.round((Date.now() - startTime) / 1000);
const stats = await repository.getTemplateStats();
console.log('✅ Template fetch complete!\n');
console.log('📈 Statistics:');
console.log(` - Templates found: ${templates.length}`);
console.log(` - Templates saved: ${saved}`);
console.log(` - Errors: ${errors}`);
console.log(` - Success rate: ${Math.round((saved / templates.length) * 100)}%`);
console.log(` - Time elapsed: ${elapsed} seconds`);
console.log(` - Average time per template: ${(elapsed / saved).toFixed(2)} seconds`);
if (stats.topUsedNodes && stats.topUsedNodes.length > 0) {
console.log('\n🔝 Top used nodes:');
stats.topUsedNodes.slice(0, 10).forEach((node: any, index: number) => {
console.log(` ${index + 1}. ${node.node} (${node.count} templates)`);
});
}
} catch (error) {
console.error('\n❌ Fatal error:', error);
process.exit(1);
}
// Close database
if ('close' in db && typeof db.close === 'function') {
db.close();
}
}
// Run if called directly
if (require.main === module) {
fetchTemplatesRobust().catch(console.error);
}
export { fetchTemplatesRobust };

View File

@@ -42,8 +42,8 @@ export class TemplateFetcher {
private readonly pageSize = 100;
async fetchTemplates(progressCallback?: (current: number, total: number) => void): Promise<TemplateWorkflow[]> {
const sixMonthsAgo = new Date();
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6);
const oneYearAgo = new Date();
oneYearAgo.setMonth(oneYearAgo.getMonth() - 12);
const allTemplates: TemplateWorkflow[] = [];
let page = 1;
@@ -66,13 +66,13 @@ export class TemplateFetcher {
// Filter templates by date
const recentTemplates = workflows.filter((w: TemplateWorkflow) => {
const createdDate = new Date(w.createdAt);
return createdDate >= sixMonthsAgo;
return createdDate >= oneYearAgo;
});
// If we hit templates older than 6 months, stop fetching
// If we hit templates older than 1 year, stop fetching
if (recentTemplates.length < workflows.length) {
hasMore = false;
logger.info(`Reached templates older than 6 months at page ${page}`);
logger.info(`Reached templates older than 1 year at page ${page}`);
}
allTemplates.push(...recentTemplates);
@@ -98,7 +98,7 @@ export class TemplateFetcher {
}
}
logger.info(`Fetched ${allTemplates.length} templates from last 6 months`);
logger.info(`Fetched ${allTemplates.length} templates from last year`);
return allTemplates;
}

View File

@@ -112,7 +112,7 @@ export class TemplateService {
progressCallback?.('Fetching template list', current, total);
});
logger.info(`Found ${templates.length} templates from last 6 months`);
logger.info(`Found ${templates.length} templates from last year`);
// Fetch details for each template
logger.info('Fetching template details');