|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { ScannerService } from '../scanner/scanner.service'; |
| 3 | +import { RuleViolation } from '../scanner/interfaces/scanner.interface'; |
| 4 | +import { |
| 5 | + AnalysisReport, |
| 6 | + StorageSavings, |
| 7 | + FormattedViolation, |
| 8 | +} from './interfaces/analyzer.interface'; |
| 9 | + |
| 10 | +export interface IncrementalAnalysisOptions { |
| 11 | + useIncremental?: boolean; |
| 12 | + forceFull?: boolean; |
| 13 | + repoPath?: string; |
| 14 | + maxCacheAge?: number; // in milliseconds |
| 15 | +} |
| 16 | + |
| 17 | +export interface IncrementalAnalysisResult extends AnalysisReport { |
| 18 | + incrementalStats: { |
| 19 | + totalFiles: number; |
| 20 | + filesAnalyzed: number; |
| 21 | + cacheHitRate: number; |
| 22 | + analysisTime: number; |
| 23 | + isIncremental: boolean; |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +@Injectable() |
| 28 | +export class IncrementalAnalyzerSimpleService { |
| 29 | + private readonly logger = new Logger(IncrementalAnalyzerSimpleService.name); |
| 30 | + private readonly cache = new Map<string, any>(); |
| 31 | + |
| 32 | + constructor( |
| 33 | + private readonly scannerService: ScannerService, |
| 34 | + ) {} |
| 35 | + |
| 36 | + /** |
| 37 | + * Analyze code with incremental support |
| 38 | + */ |
| 39 | + async analyzeCodeIncremental( |
| 40 | + code: string, |
| 41 | + source: string, |
| 42 | + options: IncrementalAnalysisOptions = {} |
| 43 | + ): Promise<IncrementalAnalysisResult> { |
| 44 | + const startTime = Date.now(); |
| 45 | + |
| 46 | + // For single file analysis, always use full analysis |
| 47 | + const result = await this.analyzeCode(code, source); |
| 48 | + |
| 49 | + return { |
| 50 | + ...result, |
| 51 | + incrementalStats: { |
| 52 | + totalFiles: 1, |
| 53 | + filesAnalyzed: 1, |
| 54 | + cacheHitRate: 0, |
| 55 | + analysisTime: Date.now() - startTime, |
| 56 | + isIncremental: false, |
| 57 | + }, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Analyze repository with incremental support |
| 63 | + */ |
| 64 | + async analyzeRepositoryIncremental( |
| 65 | + repoPath: string, |
| 66 | + options: IncrementalAnalysisOptions = {} |
| 67 | + ): Promise<IncrementalAnalysisResult> { |
| 68 | + const startTime = Date.now(); |
| 69 | + const useIncremental = options.useIncremental !== false && !options.forceFull; |
| 70 | + |
| 71 | + try { |
| 72 | + // For now, implement a simple version that always does full analysis |
| 73 | + // This can be enhanced later with proper incremental functionality |
| 74 | + this.logger.log(`Performing repository analysis for: ${repoPath}`); |
| 75 | + |
| 76 | + // Simulate finding files (in real implementation, this would scan the directory) |
| 77 | + const files = await this.findSupportedFiles(repoPath); |
| 78 | + |
| 79 | + if (!useIncremental || files.length <= 10) { |
| 80 | + return this.performFullAnalysis(repoPath, files, startTime); |
| 81 | + } |
| 82 | + |
| 83 | + // For now, fallback to full analysis |
| 84 | + // TODO: Implement proper incremental analysis with file hashing |
| 85 | + return this.performFullAnalysis(repoPath, files, startTime); |
| 86 | + |
| 87 | + } catch (error) { |
| 88 | + this.logger.error(`Repository analysis failed: ${error.message}`, error.stack); |
| 89 | + throw error; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Perform full analysis on all files |
| 95 | + */ |
| 96 | + private async performFullAnalysis( |
| 97 | + repoPath: string, |
| 98 | + files: string[], |
| 99 | + startTime: number |
| 100 | + ): Promise<IncrementalAnalysisResult> { |
| 101 | + this.logger.log(`Performing full analysis on ${files.length} files`); |
| 102 | + |
| 103 | + const allViolations: RuleViolation[] = []; |
| 104 | + const analysisTime = Date.now() - startTime; |
| 105 | + |
| 106 | + // Analyze files in batches |
| 107 | + const batchSize = 50; |
| 108 | + for (let i = 0; i < files.length; i += batchSize) { |
| 109 | + const batch = files.slice(i, i + batchSize); |
| 110 | + |
| 111 | + for (const filePath of batch) { |
| 112 | + try { |
| 113 | + // In a real implementation, this would read the file content |
| 114 | + // For now, we'll simulate the analysis |
| 115 | + const scanResult = await this.scannerService.scanContent('', filePath); |
| 116 | + allViolations.push(...scanResult.violations); |
| 117 | + } catch (error) { |
| 118 | + this.logger.warn(`Failed to analyze file ${filePath}: ${error.message}`); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + const report = this.createAnalysisReport(repoPath, allViolations); |
| 124 | + |
| 125 | + return { |
| 126 | + ...report, |
| 127 | + incrementalStats: { |
| 128 | + totalFiles: files.length, |
| 129 | + filesAnalyzed: files.length, |
| 130 | + cacheHitRate: 0, |
| 131 | + analysisTime, |
| 132 | + isIncremental: false, |
| 133 | + }, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Create analysis report from violations |
| 139 | + */ |
| 140 | + private createAnalysisReport(repoPath: string, violations: RuleViolation[]): AnalysisReport { |
| 141 | + const formattedViolations = this.formatViolations(violations); |
| 142 | + const storageSavings = this.calculateStorageSavingsFromViolations(violations); |
| 143 | + |
| 144 | + return { |
| 145 | + source: repoPath, |
| 146 | + analysisTime: new Date().toISOString(), |
| 147 | + violations: formattedViolations, |
| 148 | + summary: this.generateSummary(violations), |
| 149 | + storageSavings, |
| 150 | + recommendations: this.generateRecommendations(violations), |
| 151 | + }; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Find all supported files in a directory (simplified version) |
| 156 | + */ |
| 157 | + private async findSupportedFiles(repoPath: string): Promise<string[]> { |
| 158 | + // This is a simplified implementation |
| 159 | + // In a real scenario, this would use fs.readdir and fs.stat to walk the directory |
| 160 | + const supportedExtensions = ['.rs', '.sol', '.vy']; |
| 161 | + |
| 162 | + // For now, return an empty array as a placeholder |
| 163 | + // In a real implementation, this would scan the directory recursively |
| 164 | + this.logger.log(`Scanning for supported files in ${repoPath}`); |
| 165 | + |
| 166 | + return []; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Get cache statistics for a repository |
| 171 | + */ |
| 172 | + async getCacheStats(repoPath: string): Promise<{ |
| 173 | + totalCachedFiles: number; |
| 174 | + cacheAge: number | null; |
| 175 | + dependencyNodes: number; |
| 176 | + dependencyEdges: number; |
| 177 | + }> { |
| 178 | + // Simplified cache stats |
| 179 | + return { |
| 180 | + totalCachedFiles: this.cache.size, |
| 181 | + cacheAge: null, |
| 182 | + dependencyNodes: 0, |
| 183 | + dependencyEdges: 0, |
| 184 | + }; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Clear incremental analysis cache for a repository |
| 189 | + */ |
| 190 | + async clearCache(repoPath: string): Promise<void> { |
| 191 | + // Clear cache entries for this repository |
| 192 | + const keysToDelete = Array.from(this.cache.keys()).filter(key => key.includes(repoPath)); |
| 193 | + keysToDelete.forEach(key => this.cache.delete(key)); |
| 194 | + |
| 195 | + this.logger.log(`Cleared incremental analysis cache for ${repoPath}`); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Invalidate cache for specific files |
| 200 | + */ |
| 201 | + async invalidateFiles(repoPath: string, filePaths: string[]): Promise<void> { |
| 202 | + // Invalidate specific file cache entries |
| 203 | + filePaths.forEach(filePath => { |
| 204 | + const key = `${repoPath}:${filePath}`; |
| 205 | + this.cache.delete(key); |
| 206 | + }); |
| 207 | + |
| 208 | + this.logger.log(`Invalidated cache for ${filePaths.length} files`); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Legacy method for backward compatibility |
| 213 | + */ |
| 214 | + private async analyzeCode(code: string, source: string): Promise<AnalysisReport> { |
| 215 | + const scanResult = await this.scannerService.scanContent(code, source); |
| 216 | + const formattedViolations = this.formatViolations(scanResult.violations); |
| 217 | + const storageSavings = this.calculateStorageSavingsFromViolations(scanResult.violations); |
| 218 | + |
| 219 | + return { |
| 220 | + source, |
| 221 | + analysisTime: new Date().toISOString(), |
| 222 | + violations: formattedViolations, |
| 223 | + summary: this.generateSummary(scanResult.violations), |
| 224 | + storageSavings, |
| 225 | + recommendations: this.generateRecommendations(scanResult.violations), |
| 226 | + }; |
| 227 | + } |
| 228 | + |
| 229 | + private formatViolations(violations: RuleViolation[]): FormattedViolation[] { |
| 230 | + return violations.map((violation) => ({ |
| 231 | + ...violation, |
| 232 | + severityIcon: this.getSeverityIcon(violation.severity), |
| 233 | + formattedMessage: this.formatViolationMessage(violation), |
| 234 | + })); |
| 235 | + } |
| 236 | + |
| 237 | + private getSeverityIcon(severity: string): string { |
| 238 | + switch (severity) { |
| 239 | + case 'error': |
| 240 | + return '🚨'; |
| 241 | + case 'warning': |
| 242 | + return '⚠️'; |
| 243 | + case 'info': |
| 244 | + return 'ℹ️'; |
| 245 | + default: |
| 246 | + return '📝'; |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + private formatViolationMessage(violation: RuleViolation): string { |
| 251 | + return `[${violation.severity.toUpperCase()}] Line ${violation.lineNumber}: ${violation.description}`; |
| 252 | + } |
| 253 | + |
| 254 | + private generateSummary(violations: RuleViolation[]): string { |
| 255 | + if (violations.length === 0) { |
| 256 | + return '✅ No violations found! Your contract is optimized.'; |
| 257 | + } |
| 258 | + |
| 259 | + const errors = violations.filter((v) => v.severity === 'error').length; |
| 260 | + const warnings = violations.filter((v) => v.severity === 'warning').length; |
| 261 | + const info = violations.filter((v) => v.severity === 'info').length; |
| 262 | + |
| 263 | + return `Scan Summary: ${violations.length} total violations (${errors} errors, ${warnings} warnings, ${info} info)`; |
| 264 | + } |
| 265 | + |
| 266 | + private calculateStorageSavingsFromViolations(violations: RuleViolation[]): StorageSavings { |
| 267 | + let unusedVariables = 0; |
| 268 | + let estimatedSavingsKb = 0; |
| 269 | + |
| 270 | + for (const violation of violations) { |
| 271 | + if (violation.ruleName === 'unused-state-variables') { |
| 272 | + unusedVariables++; |
| 273 | + estimatedSavingsKb += 2.5; |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + return { |
| 278 | + unusedVariables, |
| 279 | + estimatedSavingsKb, |
| 280 | + monthlyLedgerRentSavings: estimatedSavingsKb * 0.001, |
| 281 | + }; |
| 282 | + } |
| 283 | + |
| 284 | + private generateRecommendations(violations: RuleViolation[]): string[] { |
| 285 | + const recommendations: string[] = []; |
| 286 | + const unusedVars = violations.filter( |
| 287 | + (v) => v.ruleName === 'unused-state-variables', |
| 288 | + ).length; |
| 289 | + |
| 290 | + if (unusedVars > 0) { |
| 291 | + recommendations.push( |
| 292 | + `Remove ${unusedVars} unused state variables to reduce storage costs`, |
| 293 | + ); |
| 294 | + recommendations.push( |
| 295 | + 'Consider using more efficient data types where possible', |
| 296 | + ); |
| 297 | + recommendations.push( |
| 298 | + 'Implement lazy loading patterns for rarely accessed data', |
| 299 | + ); |
| 300 | + } |
| 301 | + |
| 302 | + if (violations.length === 0) { |
| 303 | + recommendations.push( |
| 304 | + 'Your contract looks good! Consider regular audits to maintain code quality.', |
| 305 | + ); |
| 306 | + } |
| 307 | + |
| 308 | + return recommendations; |
| 309 | + } |
| 310 | +} |
0 commit comments