-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexport-utils.ts
More file actions
632 lines (547 loc) · 20.9 KB
/
export-utils.ts
File metadata and controls
632 lines (547 loc) · 20.9 KB
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
import { CountMetricsSummary } from './metrics/count-metrics';
import { LCOMMetricsSummary } from './metrics/lcom-metrics';
import { DistanceMetricsSummary } from './metrics/distance-metrics';
import * as fs from 'fs';
import * as path from 'path';
export interface ExportOptions {
outputPath?: string;
title?: string;
includeTimestamp?: boolean;
customCss?: string;
}
export interface ComprehensiveMetricsSummary {
count: CountMetricsSummary;
lcom: LCOMMetricsSummary;
distance: DistanceMetricsSummary;
}
export interface ProjectMetricsSummary {
count?: CountMetricsSummary;
lcom?: LCOMMetricsSummary;
distance?: DistanceMetricsSummary;
}
export class MetricsExporter {
/**
* Export metrics summary as HTML file
*/
static async exportAsHTML(
summary: ProjectMetricsSummary,
options: ExportOptions
): Promise<void> {
const html = this.generateHTML(summary, options);
await this.writeFile(options.outputPath!, html);
}
/**
* Export comprehensive metrics (all types) as HTML file with default path
*/
static async exportComprehensiveAsHTML(
tsConfigPath?: string,
options: Partial<ExportOptions> = {}
): Promise<void> {
// Set default output path if not provided
const defaultPath = path.join('reports', 'metrics-report.html');
const outputPath = options.outputPath || defaultPath;
// Gather all metrics
const comprehensive = await this.gatherComprehensiveMetrics(tsConfigPath);
const finalOptions: ExportOptions = {
outputPath,
title: 'Comprehensive ArchUnitTS Metrics Report',
includeTimestamp: true,
...options,
};
await this.exportAsHTML(comprehensive, finalOptions);
}
/**
* Gather all available metrics for comprehensive reporting
*/
static async gatherComprehensiveMetrics(
tsConfigPath?: string
): Promise<ComprehensiveMetricsSummary> {
// Dynamically import metrics to avoid circular dependencies
const { metrics } = await import('./metrics');
const { DistanceMetricsBuilder } = await import('./metrics/distance-metrics');
// Get all metrics
const countSummary = await metrics().count().summary();
const lcomSummary = await metrics().lcom().summary();
const distanceSummary = await new DistanceMetricsBuilder(tsConfigPath).summary();
return {
count: countSummary,
lcom: lcomSummary,
distance: distanceSummary,
};
}
private static generateHTML(
summary: ProjectMetricsSummary,
options: ExportOptions
): string {
const title = options.title || 'ArchUnitTS Metrics Report';
const timestamp =
options.includeTimestamp !== false ? new Date().toLocaleString() : '';
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<style>
${this.getDefaultStyles()}
${options.customCss || ''}
</style>
</head>
<body>
<div class="container">
<header>
<h1>${title}, Beta</h1>
${timestamp ? `<p class="timestamp">Generated on: ${timestamp}</p>` : ''}
<strong>Use with caution, beta report.</strong>
</header>
<main>
${summary.count ? this.generateCountMetricsSection(summary.count) : ''}
${summary.lcom ? this.generateLCOMMetricsSection(summary.lcom) : ''}
${summary.distance ? this.generateDistanceMetricsSection(summary.distance) : ''}
</main>
<footer>
<p>Generated by ArchUnitTS Metrics System</p>
</footer>
</div>
</body>
</html>`;
}
private static generateCountMetricsSection(summary: CountMetricsSummary): string {
return `
<section class="metrics-section">
<h2>📊 Count Metrics</h2>
<div class="metrics-grid">
<div class="metric-card">
<h3>Project Overview</h3>
<div class="metric-value">${summary.totalFiles}</div>
<div class="metric-label">Total Files</div>
</div>
<div class="metric-card">
<h3>Classes</h3>
<div class="metric-value">${summary.totalClasses}</div>
<div class="metric-label">Total Classes</div>
</div>
</div>
<div class="metrics-grid">
<div class="metric-card">
<h3>Average Methods</h3>
<div class="metric-value">${summary.averageMethodsPerClass.toFixed(2)}</div>
<div class="metric-label">per Class</div>
</div>
<div class="metric-card">
<h3>Average Fields</h3>
<div class="metric-value">${summary.averageFieldsPerClass.toFixed(2)}</div>
<div class="metric-label">per Class</div>
</div>
<div class="metric-card">
<h3>Average Lines</h3>
<div class="metric-value">${summary.averageLinesOfCodePerFile.toFixed(2)}</div>
<div class="metric-label">per File</div>
</div>
<div class="metric-card">
<h3>Average Statements</h3>
<div class="metric-value">${summary.averageStatementsPerFile.toFixed(2)}</div>
<div class="metric-label">per File</div>
</div>
</div> <div class="highlights">
<div class="highlight-card">
<h4>📁 Largest File</h4>
<p><strong>${summary.largestFile.path}</strong></p>
<p>${summary.largestFile.lines} lines</p>
</div>
<div class="highlight-card">
<h4>🏗️ Largest Class</h4>
<p><strong>${summary.largestClass.name}</strong></p>
<p>${summary.largestClass.methods} methods</p>
</div>
</div>
</section>`;
}
private static generateLCOMMetricsSection(summary: LCOMMetricsSummary): string {
return `
<section class="metrics-section">
<h2>🔗 LCOM (Lack of Cohesion of Methods) Metrics</h2>
<div class="cohesion-overview">
<div class="metric-card large">
<h3>High Cohesion Classes</h3>
<div class="metric-value">${summary.highCohesionClassCount}</div>
<div class="metric-label">out of ${summary.totalClasses} total classes</div>
<div class="percentage">${((summary.highCohesionClassCount / summary.totalClasses) * 100).toFixed(1)}%</div>
</div>
</div>
<div class="lcom-variants">
<h3>LCOM Variants (Average Values)</h3>
<div class="metrics-grid">
<div class="metric-card">
<h4>LCOM96a</h4>
<div class="metric-value">${summary.averageLCOM96a.toFixed(3)}</div>
</div>
<div class="metric-card">
<h4>LCOM96b</h4>
<div class="metric-value">${summary.averageLCOM96b.toFixed(3)}</div>
</div>
<div class="metric-card">
<h4>LCOM1</h4>
<div class="metric-value">${summary.averageLCOM1.toFixed(3)}</div>
</div>
<div class="metric-card">
<h4>LCOM2</h4>
<div class="metric-value">${summary.averageLCOM2.toFixed(3)}</div>
</div>
<div class="metric-card">
<h4>LCOM3</h4>
<div class="metric-value">${summary.averageLCOM3.toFixed(3)}</div>
</div>
<div class="metric-card">
<h4>LCOM4</h4>
<div class="metric-value">${summary.averageLCOM4.toFixed(3)}</div>
</div>
<div class="metric-card">
<h4>LCOM5</h4>
<div class="metric-value">${summary.averageLCOM5.toFixed(3)}</div>
</div>
<div class="metric-card">
<h4>LCOM*</h4>
<div class="metric-value">${summary.averageLCOMStar.toFixed(3)}</div>
</div>
</div>
</div>
</section>`;
}
private static generateDistanceMetricsSection(
summary: DistanceMetricsSummary
): string {
// Determine architectural zones
const zoneOfPainWarning =
summary.averageAbstractness < 0.3 && summary.averageInstability < 0.3;
const zoneOfUselessnessWarning =
summary.averageAbstractness > 0.7 && summary.averageInstability > 0.7;
return `
<section class="metrics-section">
<h2>📏 Distance Metrics & Architectural Analysis</h2>
<div class="distance-overview">
<p class="section-description">
Distance metrics measure the architectural balance between abstraction and instability,
helping identify components that may need refactoring.
</p>
<div class="metrics-grid">
<div class="metric-card">
<h3>📁 Total Files</h3>
<div class="metric-value">${summary.totalFiles}</div>
<div class="metric-label">Files analyzed</div>
</div>
<div class="metric-card">
<h3>🎯 Files on Main Sequence</h3>
<div class="metric-value">${summary.filesOnMainSequence}</div>
<div class="metric-label">Well-balanced architecture</div>
</div>
</div>
<h3>🏗️ Core Architectural Metrics</h3>
<div class="metrics-grid">
<div class="metric-card ${summary.averageAbstractness < 0.3 ? 'warning' : summary.averageAbstractness > 0.7 ? 'good' : ''}">
<h3>📐 Average Abstractness (A)</h3>
<div class="metric-value">${summary.averageAbstractness.toFixed(3)}</div>
<div class="metric-label">0.0 (concrete) ↔ 1.0 (abstract)</div>
</div>
<div class="metric-card ${summary.averageInstability < 0.3 ? 'stable' : summary.averageInstability > 0.7 ? 'warning' : ''}">
<h3>⚖️ Average Instability (I)</h3>
<div class="metric-value">${summary.averageInstability.toFixed(3)}</div>
<div class="metric-label">0.0 (stable) ↔ 1.0 (unstable)</div>
</div>
<div class="metric-card ${summary.averageDistance < 0.2 ? 'good' : summary.averageDistance > 0.5 ? 'warning' : ''}">
<h3>📏 Distance from Main Sequence (D)</h3>
<div class="metric-value">${summary.averageDistance.toFixed(3)}</div>
<div class="metric-label">Deviation from ideal line (A + I = 1)</div>
</div>
</div>
<h3>🔗 Advanced Coupling Metrics</h3>
<div class="metrics-grid">
<div class="metric-card">
<h3>🔗 Average Coupling Factor (CF)</h3>
<div class="metric-value">${summary.averageCouplingFactor.toFixed(3)}</div>
<div class="metric-label">Degree of coupling between components</div>
</div>
<div class="metric-card">
<h3>📊 Normalized Distance (ND)</h3>
<div class="metric-value">${summary.averageNormalizedDistance.toFixed(3)}</div>
<div class="metric-label">Distance normalized by project context</div>
</div>
</div>
${
zoneOfPainWarning || zoneOfUselessnessWarning
? `
<div class="architectural-alerts">
<h3>⚠️ Architectural Alerts</h3>
${
zoneOfPainWarning
? `
<div class="alert alert-warning">
<strong>Zone of Pain Detected:</strong> Low abstractness (${summary.averageAbstractness.toFixed(3)})
and low instability (${summary.averageInstability.toFixed(3)}) indicate rigid, concrete components
that are difficult to change.
</div>`
: ''
}
${
zoneOfUselessnessWarning
? `
<div class="alert alert-info">
<strong>Zone of Uselessness Detected:</strong> High abstractness (${summary.averageAbstractness.toFixed(3)})
and high instability (${summary.averageInstability.toFixed(3)}) indicate abstract components
with excessive dependencies.
</div>`
: ''
}
</div>`
: ''
}
<div class="architectural-guidance">
<h3>📚 Interpretation Guide</h3>
<div class="guidance-grid">
<div class="guidance-card">
<h4>🎯 Ideal Zone (Main Sequence)</h4>
<p>Components on or near the main sequence (A + I ≈ 1) represent well-balanced architecture.</p>
</div>
<div class="guidance-card">
<h4>🔥 Zone of Pain</h4>
<p>Concrete & Stable (low A, low I) - Hard to extend, but changes are risky.</p>
</div>
<div class="guidance-card">
<h4>💸 Zone of Uselessness</h4>
<p>Abstract & Unstable (high A, high I) - Overly complex with little benefit.</p>
</div>
</div>
</div>
</div>
</section>`;
}
private static getDefaultStyles(): string {
return `
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f5f7fa;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
header {
text-align: center;
margin-bottom: 40px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 40px 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
font-weight: 300;
}
.timestamp {
font-size: 1rem;
opacity: 0.9;
}
.metrics-section {
background: white;
margin-bottom: 30px;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.metrics-section h2 {
font-size: 1.8rem;
margin-bottom: 25px;
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
.metrics-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-bottom: 25px;
}
.metric-card {
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
padding: 20px;
border-radius: 8px;
text-align: center;
border: 1px solid #dee2e6;
transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.metric-card:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}
.metric-card.large {
grid-column: span 2;
}
.metric-card h3, .metric-card h4 {
color: #495057;
margin-bottom: 10px;
font-size: 1.1rem;
}
.metric-value {
font-size: 2rem;
font-weight: bold;
color: #2c3e50;
margin-bottom: 5px;
}
.metric-label {
font-size: 0.9rem;
color: #6c757d;
}
.percentage {
font-size: 1.2rem;
color: #28a745;
font-weight: bold;
margin-top: 5px;
}
.highlights {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin-top: 25px;
}
.highlight-card {
background: linear-gradient(135deg, #ffeaa7 0%, #fab1a0 100%);
padding: 20px;
border-radius: 8px;
border-left: 4px solid #fdcb6e;
}
.highlight-card h4 {
color: #2d3436;
margin-bottom: 10px;
font-size: 1.2rem;
}
.highlight-card p {
margin-bottom: 5px;
color: #2d3436;
}
.cohesion-overview {
margin-bottom: 30px;
}
.lcom-variants h3 {
color: #2c3e50;
margin-bottom: 20px;
font-size: 1.3rem;
} .distance-overview .section-description {
background: #e8f4f8;
padding: 15px;
border-radius: 6px;
margin-bottom: 25px;
color: #34495e;
font-style: italic;
}
/* New metric card status classes */
.metric-card.warning {
background: linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%);
border-left: 4px solid #f39c12;
}
.metric-card.good {
background: linear-gradient(135deg, #d1f2eb 0%, #a3e9d0 100%);
border-left: 4px solid #27ae60;
}
.metric-card.stable {
background: linear-gradient(135deg, #d6eaf8 0%, #aed6f1 100%);
border-left: 4px solid #3498db;
}
/* Architectural alerts */
.architectural-alerts {
margin: 20px 0;
}
.alert {
padding: 15px;
border-radius: 6px;
margin-bottom: 15px;
border: 1px solid transparent;
}
.alert-warning {
background-color: #fff3cd;
border-color: #ffeaa7;
color: #856404;
}
.alert-info {
background-color: #d1ecf1;
border-color: #b8daff;
color: #0c5460;
}
/* Guidance grid */
.guidance-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
margin-top: 20px;
}
.guidance-card {
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
padding: 15px;
border-radius: 6px;
border-left: 4px solid #6c757d;
}
.guidance-card h4 {
color: #495057;
margin-bottom: 10px;
font-size: 1rem;
}
.guidance-card p {
color: #6c757d;
font-size: 0.9rem;
margin: 0;
}
footer {
text-align: center;
margin-top: 40px;
padding: 20px;
color: #6c757d;
font-size: 0.9rem;
}
@media (max-width: 768px) {
.container {
padding: 10px;
}
header h1 {
font-size: 2rem;
}
.metrics-grid {
grid-template-columns: 1fr;
}
.metric-card.large {
grid-column: span 1;
}
}
@media print {
body {
background-color: white;
}
.container {
max-width: none;
margin: 0;
padding: 0;
}
.metrics-section {
box-shadow: none;
border: 1px solid #ddd;
break-inside: avoid;
}
}`;
}
private static async writeFile(filePath: string, content: string): Promise<void> {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, content, 'utf8');
}
}