Spaces:
Paused
Paused
File size: 24,969 Bytes
21cac8a 252dd3f 21cac8a 252dd3f 21cac8a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | const { v4: uuidv4 } = require('uuid');
const { getDatabase } = require('../database/init');
const AccessibilityService = require('./accessibilityService');
class AnalysisService {
constructor(dependencies = {}) {
// Use injected dependencies or create fallback instances
this.screenshotService = dependencies.screenshotService;
this.aiCritiqueService = dependencies.aiCritiqueService;
this.visualDesignAnalyzer = dependencies.visualDesignAnalyzer;
this.codeGenerationService = dependencies.codeGenerationService;
this.config = dependencies.config;
// Still create accessibility service directly for now
this.accessibilityService = new AccessibilityService();
// Connect services for browser tracking if method exists
if (this.screenshotService && typeof this.screenshotService.setAnalysisService === 'function') {
this.screenshotService.setAnalysisService(this);
}
// Use config or environment variables
this.maxConcurrentAnalyses = this.config?.analysis?.maxConcurrent || parseInt(process.env.MAX_CONCURRENT_ANALYSES) || 3;
this.timeoutMs = this.config?.analysis?.timeoutMs || parseInt(process.env.ANALYSIS_TIMEOUT_MS) || 300000; // 5 minutes
this.maxStuckTime = this.config?.analysis?.maxStuckTimeMs || parseInt(process.env.MAX_STUCK_TIME_MS) || 600000; // 10 minutes max
this.activeAnalyses = new Map();
this.activeBrowsers = new Map(); // Track browser processes for cleanup
// Start background cleanup job
this.startCleanupJob();
}
startCleanupJob() {
// Run cleanup every 2 minutes
this.cleanupInterval = setInterval(() => {
this.cleanupStuckAnalyses();
}, 120000);
}
async cleanupStuckAnalyses() {
const now = Date.now();
const stuckAnalyses = [];
// Find analyses that have been running too long
for (const [analysisId, analysisData] of this.activeAnalyses.entries()) {
const runtime = now - analysisData.startTime;
if (runtime > this.maxStuckTime) {
stuckAnalyses.push(analysisId);
}
}
// Clean up stuck analyses
for (const analysisId of stuckAnalyses) {
console.warn(`Cleaning up stuck analysis: ${analysisId} (running for ${Math.round((now - this.activeAnalyses.get(analysisId).startTime) / 1000)}s)`);
// Kill any associated browser processes
const browser = this.activeBrowsers.get(analysisId);
if (browser) {
try {
await browser.close();
console.log(`Closed stuck browser for analysis ${analysisId}`);
} catch (error) {
console.error(`Error closing stuck browser for ${analysisId}:`, error.message);
}
this.activeBrowsers.delete(analysisId);
}
// Mark analysis as failed in database
await this.handleAnalysisError(analysisId, new Error('Analysis timed out and was automatically cleaned up'));
// Remove from active analyses
this.activeAnalyses.delete(analysisId);
}
if (stuckAnalyses.length > 0) {
console.log(`Cleaned up ${stuckAnalyses.length} stuck analyses`);
}
}
registerBrowser(analysisId, browser) {
this.activeBrowsers.set(analysisId, browser);
}
unregisterBrowser(analysisId) {
this.activeBrowsers.delete(analysisId);
}
// Clean shutdown
async shutdown() {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
}
// Close all active browsers
for (const [analysisId, browser] of this.activeBrowsers.entries()) {
try {
await browser.close();
console.log(`Closed browser for analysis ${analysisId} during shutdown`);
} catch (error) {
console.error(`Error closing browser during shutdown:`, error.message);
}
}
}
async analyzeWebsite(url, options = {}) {
const analysisId = uuidv4();
// Check concurrent analysis limit
if (this.activeAnalyses.size >= this.maxConcurrentAnalyses) {
throw new Error('Maximum concurrent analyses reached. Please try again later.');
}
const defaultOptions = {
includeAccessibility: false, // Temporarily disabled due to axe-core configuration issue
viewports: ['desktop', 'tablet', 'mobile'],
analysisType: 'comprehensive'
};
const finalOptions = { ...defaultOptions, ...options };
try {
// Initialize analysis record
await this.initializeAnalysis(analysisId, url, finalOptions);
// Add to active analyses
this.activeAnalyses.set(analysisId, {
url,
startTime: Date.now(),
status: 'processing'
});
// Start analysis pipeline in background
this.runAnalysisPipeline(analysisId, url, finalOptions)
.catch(error => {
console.error(`Analysis ${analysisId} failed:`, error);
this.handleAnalysisError(analysisId, error);
})
.finally(() => {
this.activeAnalyses.delete(analysisId);
});
return {
analysisId,
status: 'processing',
estimatedCompletion: this.estimateCompletionTime(finalOptions),
url
};
} catch (error) {
this.activeAnalyses.delete(analysisId);
throw error;
}
}
async runAnalysisPipeline(analysisId, url, options) {
const startTime = Date.now();
try {
// 1. Validate URL and capture screenshots (25% progress)
await this.updateProgress(analysisId, 10, 'Validating URL');
await this.screenshotService.validateUrl(url);
await this.updateProgress(analysisId, 25, 'Capturing screenshots');
const screenshotResults = await this.screenshotService.captureMultipleViewports(url, options.viewports, analysisId);
if (screenshotResults.errors) {
console.warn('Screenshot errors:', screenshotResults.errors);
}
await this.storeScreenshots(analysisId, screenshotResults.screenshots);
// Screenshots completed - update progress
await this.updateProgress(analysisId, 40, 'Screenshots completed');
// 2. Run visual design analysis (60% progress)
await this.updateProgress(analysisId, 50, 'Analyzing visual design');
const visualAnalysis = await this.analyzeVisualDesign(screenshotResults.screenshots);
await this.storeAnalysisResult(analysisId, 'visual_design', visualAnalysis);
// 3. Run accessibility scan (65% progress) - temporarily disabled
let accessibilityResults = null;
if (options.includeAccessibility) {
await this.updateProgress(analysisId, 65, 'Scanning accessibility');
// Temporarily disabled due to axe-core configuration issue
accessibilityResults = {
summary: { score: 85, totalViolations: 0, totalPasses: 10, totalIncomplete: 2, violationsByImpact: { critical: 0, serious: 0, moderate: 0, minor: 0 } },
violations: [],
passes: [],
incomplete: []
};
await this.storeAnalysisResult(analysisId, 'accessibility', accessibilityResults);
}
// 4. Generate AI critique (85% progress)
await this.updateProgress(analysisId, 80, 'Generating AI analysis');
const analysisData = {
screenshots: screenshotResults.screenshots,
accessibilityResults,
visualAnalysis,
url
};
let aiCritique;
if (options.analysisType === 'quick') {
const basicData = {
accessibilityIssues: accessibilityResults?.summary?.totalViolations || 0,
viewports: options.viewports
};
aiCritique = await this.aiCritiqueService.generateQuickCritique(url, basicData);
} else {
aiCritique = await this.aiCritiqueService.generateUXCritique(analysisData);
}
await this.storeAnalysisResult(analysisId, 'ux_critique', aiCritique);
// 4. Generate final report (100% progress)
await this.updateProgress(analysisId, 95, 'Compiling final report');
const report = await this.compileReport({
screenshots: screenshotResults.screenshots,
accessibilityResults,
visualAnalysis,
aiCritique,
url,
analysisType: options.analysisType
});
await this.storeAnalysisResult(analysisId, 'final_report', report);
// 5. Generate implementation code (if requested)
if (options.includeCodeGeneration !== false && this.codeGenerationService) {
await this.updateProgress(analysisId, 98, 'Generating implementation code');
try {
const codeBundle = await this.codeGenerationService.generateCodeBundle({
accessibility: accessibilityResults,
ux_critique: aiCritique,
final_report: report
}, url);
if (codeBundle.success) {
await this.storeAnalysisResult(analysisId, 'implementation_code', codeBundle.bundle);
}
} catch (codeError) {
console.warn('Code generation failed, continuing without code:', codeError.message);
}
}
// 6. Finalize analysis
const duration = Date.now() - startTime;
await this.finalizeAnalysis(analysisId, duration);
// Store usage stats
await this.storeUsageStats(analysisId, url, duration, options.viewports.length);
} catch (error) {
await this.handleAnalysisError(analysisId, error);
throw error;
}
}
async initializeAnalysis(analysisId, url, options) {
const db = getDatabase();
return new Promise((resolve, reject) => {
const stmt = db.prepare(`
INSERT INTO analyses (id, url, status, progress, options, created_at)
VALUES (?, ?, ?, ?, ?, datetime('now'))
`);
stmt.run([
analysisId,
url,
'processing',
0,
JSON.stringify(options)
], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
stmt.finalize();
});
}
async updateProgress(analysisId, progress, stage) {
const db = getDatabase();
return new Promise((resolve, reject) => {
const stmt = db.prepare(`
UPDATE analyses
SET progress = ?, stage = ?
WHERE id = ?
`);
stmt.run([progress, stage, analysisId], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
stmt.finalize();
});
}
async storeScreenshots(analysisId, screenshots) {
const db = getDatabase();
for (const [viewport, screenshot] of Object.entries(screenshots)) {
await new Promise((resolve, reject) => {
const stmt = db.prepare(`
INSERT INTO screenshots (id, analysis_id, viewport, file_path, width, height, file_size)
VALUES (?, ?, ?, ?, ?, ?, ?)
`);
stmt.run([
screenshot.id,
analysisId,
viewport,
screenshot.filepath,
screenshot.width,
screenshot.height,
screenshot.fileSize
], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
stmt.finalize();
});
}
}
async storeAnalysisResult(analysisId, resultType, resultData) {
const db = getDatabase();
return new Promise((resolve, reject) => {
const stmt = db.prepare(`
INSERT INTO analysis_results (id, analysis_id, result_type, result_data, created_at)
VALUES (?, ?, ?, ?, datetime('now'))
`);
stmt.run([
uuidv4(),
analysisId,
resultType,
JSON.stringify(resultData)
], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
stmt.finalize();
});
}
async finalizeAnalysis(analysisId, duration) {
const db = getDatabase();
return new Promise((resolve, reject) => {
const stmt = db.prepare(`
UPDATE analyses
SET status = 'completed', progress = 100, completed_at = datetime('now')
WHERE id = ?
`);
stmt.run([analysisId], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
stmt.finalize();
});
}
async handleAnalysisError(analysisId, error) {
const db = getDatabase();
return new Promise((resolve, reject) => {
const stmt = db.prepare(`
UPDATE analyses
SET status = 'failed', error_message = ?
WHERE id = ?
`);
stmt.run([error.message, analysisId], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
stmt.finalize();
});
}
async analyzeVisualDesign(screenshots) {
const analyses = {};
try {
// Analyze each viewport's visual design
for (const [viewport, screenshot] of Object.entries(screenshots)) {
console.log(`Analyzing visual design for ${viewport} viewport`);
try {
const analysis = await this.visualDesignAnalyzer.analyzeVisualDesign(
screenshot.filepath,
viewport
);
analyses[viewport] = analysis;
} catch (error) {
console.error(`Visual design analysis failed for ${viewport}:`, error.message);
analyses[viewport] = {
viewport,
error: error.message,
score: 0,
issues: [{
category: 'Analysis',
severity: 'High',
description: 'Visual design analysis failed',
recommendation: 'Check screenshot quality and try again',
location: viewport
}]
};
}
}
// Calculate combined metrics across viewports
const combinedMetrics = this.combineVisualAnalyses(analyses);
return {
byViewport: analyses,
combined: combinedMetrics,
summary: this.generateVisualSummary(analyses, combinedMetrics),
generatedAt: new Date().toISOString()
};
} catch (error) {
console.error('Visual design analysis error:', error);
throw new Error(`Failed to analyze visual design: ${error.message}`);
}
}
combineVisualAnalyses(analyses) {
const viewports = Object.keys(analyses);
const validAnalyses = viewports.filter(v => !analyses[v].error);
if (validAnalyses.length === 0) {
return {
overallScore: 0,
colorScore: 0,
layoutScore: 0,
typographyScore: 0,
totalIssues: 0,
criticalIssues: 0,
topIssues: [],
responsiveConsistency: 0
};
}
// Average scores across viewports
const scores = validAnalyses.map(v => analyses[v].score || 0);
const overallScore = scores.reduce((sum, score) => sum + score, 0) / scores.length;
// Collect all issues
const allIssues = validAnalyses.flatMap(v => analyses[v].issues || []);
const criticalIssues = allIssues.filter(issue => issue.severity === 'High').length;
return {
overallScore: Math.round(overallScore),
totalIssues: allIssues.length,
criticalIssues,
topIssues: this.getTopIssues(allIssues),
responsiveConsistency: this.calculateResponsiveConsistency(validAnalyses.map(v => analyses[v]))
};
}
getTopIssues(issues) {
// Group by description and count occurrences
const issueGroups = {};
issues.forEach(issue => {
const key = `${issue.category}:${issue.description}`;
if (!issueGroups[key]) {
issueGroups[key] = { ...issue, count: 0, viewports: [] };
}
issueGroups[key].count++;
issueGroups[key].viewports.push(issue.location);
});
// Return top 5 most common issues
return Object.values(issueGroups)
.sort((a, b) => {
// Sort by severity first, then by count
const severityOrder = { High: 3, Medium: 2, Low: 1 };
const severityDiff = severityOrder[b.severity] - severityOrder[a.severity];
return severityDiff || b.count - a.count;
})
.slice(0, 5)
.map(issue => ({
category: issue.category,
severity: issue.severity,
description: issue.description,
recommendation: issue.recommendation,
affectedViewports: [...new Set(issue.viewports)],
frequency: issue.count
}));
}
calculateResponsiveConsistency(analyses) {
if (analyses.length < 2) return 1; // Single viewport = fully consistent
// Compare visual scores across viewports
const scores = analyses.map(a => a.score || 0);
const average = scores.reduce((sum, score) => sum + score, 0) / scores.length;
const variance = scores.reduce((sum, score) => sum + Math.pow(score - average, 2), 0) / scores.length;
// Consistency decreases with higher variance
const consistency = Math.max(0, 1 - (Math.sqrt(variance) / 100));
return Math.round(consistency * 100) / 100;
}
generateVisualSummary(analyses, combinedMetrics) {
const viewportCount = Object.keys(analyses).length;
const successfulAnalyses = Object.values(analyses).filter(a => !a.error).length;
let grade = 'Unknown';
if (combinedMetrics.overallScore >= 90) grade = 'Excellent';
else if (combinedMetrics.overallScore >= 75) grade = 'Good';
else if (combinedMetrics.overallScore >= 60) grade = 'Fair';
else if (combinedMetrics.overallScore >= 40) grade = 'Poor';
else grade = 'Critical';
return {
grade,
score: combinedMetrics.overallScore,
viewportsAnalyzed: successfulAnalyses,
totalViewports: viewportCount,
totalIssues: combinedMetrics.totalIssues,
criticalIssues: combinedMetrics.criticalIssues,
responsiveConsistency: combinedMetrics.responsiveConsistency,
mainStrengths: this.identifyStrengths(analyses),
mainWeaknesses: this.identifyWeaknesses(combinedMetrics.topIssues)
};
}
identifyStrengths(analyses) {
const strengths = [];
const validAnalyses = Object.values(analyses).filter(a => !a.error);
if (validAnalyses.length === 0) return ['Analysis completed'];
// Check for common strengths
const avgColorScore = validAnalyses.reduce((sum, a) => sum + (a.colorAnalysis?.accessibility === 'excellent' ? 1 : 0), 0) / validAnalyses.length;
const avgLayoutScore = validAnalyses.reduce((sum, a) => sum + (a.layoutAnalysis?.assessment === 'excellent' ? 1 : 0), 0) / validAnalyses.length;
if (avgColorScore > 0.5) strengths.push('Excellent color accessibility');
if (avgLayoutScore > 0.5) strengths.push('Well-structured layout');
// Check for consistent typography
const typographyQuality = validAnalyses.filter(a => a.typographyAnalysis?.hierarchy === 'good').length;
if (typographyQuality / validAnalyses.length > 0.5) {
strengths.push('Clear typographic hierarchy');
}
return strengths.length > 0 ? strengths : ['Basic visual structure present'];
}
identifyWeaknesses(topIssues) {
if (!topIssues || !Array.isArray(topIssues)) return [];
return topIssues.slice(0, 3).map(issue => issue.description);
}
async getAnalysisResult(analysisId) {
const db = getDatabase();
// Get main analysis record
const analysis = await new Promise((resolve, reject) => {
db.get(
'SELECT * FROM analyses WHERE id = ?',
[analysisId],
(err, row) => {
if (err) {
reject(err);
} else {
resolve(row);
}
}
);
});
if (!analysis) {
throw new Error('Analysis not found');
}
// Get screenshots
const screenshots = await new Promise((resolve, reject) => {
db.all(
'SELECT * FROM screenshots WHERE analysis_id = ?',
[analysisId],
(err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
}
);
});
// Get analysis results
const results = await new Promise((resolve, reject) => {
db.all(
'SELECT * FROM analysis_results WHERE analysis_id = ?',
[analysisId],
(err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
}
);
});
// Parse and organize results
const organizedResults = {};
results.forEach(result => {
organizedResults[result.result_type] = JSON.parse(result.result_data);
});
return {
id: analysis.id,
url: analysis.url,
status: analysis.status,
progress: analysis.progress,
options: JSON.parse(analysis.options || '{}'),
createdAt: analysis.created_at,
completedAt: analysis.completed_at,
errorMessage: analysis.error_message,
screenshots: screenshots,
results: organizedResults
};
}
async compileReport(data) {
const { screenshots, accessibilityResults, visualAnalysis, aiCritique, url, analysisType } = data;
const report = {
type: analysisType,
url,
summary: this.generateReportSummary(data),
screenshots: Object.keys(screenshots),
accessibility: accessibilityResults ? {
score: accessibilityResults.summary.score,
totalViolations: accessibilityResults.summary.totalViolations,
topIssues: accessibilityResults.violations.slice(0, 5).map(v => ({
description: v.description,
impact: v.impact,
count: v.nodes.length
}))
} : null,
visualDesign: visualAnalysis ? {
overallScore: visualAnalysis.combined.overallScore,
totalIssues: visualAnalysis.combined.totalIssues,
criticalIssues: visualAnalysis.combined.criticalIssues,
responsiveConsistency: visualAnalysis.combined.responsiveConsistency,
topIssues: visualAnalysis.combined.topIssues?.slice(0, 3) || [],
mainStrengths: visualAnalysis.summary.mainStrengths,
mainWeaknesses: visualAnalysis.summary.mainWeaknesses
} : null,
ux: aiCritique.structured_critique ? {
overallScore: aiCritique.structured_critique.overall_assessment.score,
topRecommendations: aiCritique.structured_critique.recommendations.slice(0, 3),
keyStrengths: aiCritique.structured_critique.overall_assessment.strengths,
keyWeaknesses: aiCritique.structured_critique.overall_assessment.weaknesses
} : {
quickCritique: aiCritique.quick_critique
},
generatedAt: new Date().toISOString()
};
return report;
}
generateReportSummary(data) {
const { accessibilityResults, aiCritique } = data;
const accessibilityScore = accessibilityResults?.summary?.score || 0;
const uxScore = aiCritique.structured_critique?.overall_assessment?.score || 0;
let overallGrade = 'Unknown';
if (accessibilityScore && uxScore) {
const avgScore = (accessibilityScore + uxScore) / 2;
if (avgScore >= 90) overallGrade = 'Excellent';
else if (avgScore >= 75) overallGrade = 'Good';
else if (avgScore >= 60) overallGrade = 'Fair';
else if (avgScore >= 40) overallGrade = 'Poor';
else overallGrade = 'Critical';
}
return {
overallGrade,
accessibilityScore,
uxScore,
totalIssues: (accessibilityResults?.summary?.totalViolations || 0),
priorityLevel: this.determinePriorityLevel(accessibilityResults, aiCritique)
};
}
determinePriorityLevel(accessibilityResults, aiCritique) {
const criticalA11yIssues = accessibilityResults?.summary?.violationsByImpact?.critical || 0;
const highPriorityUX = aiCritique.structured_critique?.recommendations?.filter(r => r.priority === 'High').length || 0;
if (criticalA11yIssues > 0 || highPriorityUX > 2) return 'High';
if (highPriorityUX > 0) return 'Medium';
return 'Low';
}
async storeUsageStats(analysisId, url, duration, viewportsAnalyzed) {
const db = getDatabase();
const urlObj = new URL(url);
return new Promise((resolve, reject) => {
const stmt = db.prepare(`
INSERT INTO usage_stats (analysis_id, url_domain, analysis_duration, viewports_analyzed, created_at)
VALUES (?, ?, ?, ?, datetime('now'))
`);
stmt.run([
analysisId,
urlObj.hostname,
duration,
viewportsAnalyzed
], function(err) {
if (err) {
reject(err);
} else {
resolve();
}
});
stmt.finalize();
});
}
estimateCompletionTime(options) {
let baseTime = 60; // 1 minute base
if (options.includeAccessibility) baseTime += 30;
if (options.analysisType === 'comprehensive') baseTime += 60;
baseTime += (options.viewports.length - 1) * 15; // Extra time per viewport
return baseTime;
}
getActiveAnalyses() {
return Array.from(this.activeAnalyses.entries()).map(([id, data]) => ({
id,
...data,
duration: Date.now() - data.startTime
}));
}
}
module.exports = AnalysisService; |