organizational-intelligence-plugin 0.3.4

Organizational Intelligence Plugin - Defect pattern analysis for GitHub organizations
Documentation
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
//! WebAssembly bindings for Organizational Intelligence Plugin.
//!
//! Provides JavaScript-accessible functions for defect pattern analysis
//! and visualization in the browser.
//!
//! # Usage (JavaScript)
//!
//! ```javascript
//! import init, {
//!     analyze_commits,
//!     defect_distribution_chart,
//!     confidence_histogram,
//!     cross_repo_heatmap,
//!     OipAnalyzer
//! } from 'organizational-intelligence-plugin';
//!
//! await init();
//!
//! // Analyze commit messages
//! const commits = [
//!     { message: "fix: resolve null pointer in parser", confidence: 0.85 },
//!     { message: "fix: handle edge case in validator", confidence: 0.78 }
//! ];
//!
//! const analysis = analyze_commits(commits);
//! console.log(analysis.categories); // ["ASTTransform", "TypeErrors", ...]
//! console.log(analysis.counts);     // [5, 3, ...]
//!
//! // Generate PNG chart
//! const pngData = defect_distribution_chart(analysis, { width: 800, height: 400 });
//! const blob = new Blob([pngData], { type: 'image/png' });
//! document.getElementById('chart').src = URL.createObjectURL(blob);
//! ```

use wasm_bindgen::prelude::*;

use crate::classifier::RuleBasedClassifier;

// ============================================================================
// Initialization
// ============================================================================

/// Initialize the WASM module.
#[wasm_bindgen(start)]
pub fn init() {
    // Set panic hook for better error messages in browser console
    #[cfg(feature = "wasm")]
    console_error_panic_hook::set_once();
}

// ============================================================================
// Data Types
// ============================================================================

/// Defect analysis result for a single commit.
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct CommitAnalysis {
    category: String,
    confidence: f32,
    message: String,
}

#[wasm_bindgen]
impl CommitAnalysis {
    #[wasm_bindgen(getter)]
    pub fn category(&self) -> String {
        self.category.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn confidence(&self) -> f32 {
        self.confidence
    }

    #[wasm_bindgen(getter)]
    pub fn message(&self) -> String {
        self.message.clone()
    }
}

/// Distribution of defect categories.
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct DefectDistributionResult {
    categories: Vec<String>,
    counts: Vec<u32>,
    percentages: Vec<f32>,
    total: u32,
}

#[wasm_bindgen]
impl DefectDistributionResult {
    #[wasm_bindgen(getter)]
    pub fn categories(&self) -> Vec<String> {
        self.categories.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn counts(&self) -> Vec<u32> {
        self.counts.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn percentages(&self) -> Vec<f32> {
        self.percentages.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn total(&self) -> u32 {
        self.total
    }

    /// Get category at index.
    pub fn category_at(&self, idx: usize) -> Option<String> {
        self.categories.get(idx).cloned()
    }

    /// Get count at index.
    pub fn count_at(&self, idx: usize) -> Option<u32> {
        self.counts.get(idx).copied()
    }

    /// Get percentage at index.
    pub fn percentage_at(&self, idx: usize) -> Option<f32> {
        self.percentages.get(idx).copied()
    }

    /// Number of categories.
    pub fn len(&self) -> usize {
        self.categories.len()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.categories.is_empty()
    }
}

/// Confidence distribution statistics.
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct ConfidenceStats {
    values: Vec<f32>,
    min: f32,
    max: f32,
    mean: f32,
    std_dev: f32,
}

#[wasm_bindgen]
impl ConfidenceStats {
    #[wasm_bindgen(getter)]
    pub fn values(&self) -> Vec<f32> {
        self.values.clone()
    }

    #[wasm_bindgen(getter)]
    pub fn min(&self) -> f32 {
        self.min
    }

    #[wasm_bindgen(getter)]
    pub fn max(&self) -> f32 {
        self.max
    }

    #[wasm_bindgen(getter)]
    pub fn mean(&self) -> f32 {
        self.mean
    }

    #[wasm_bindgen(getter)]
    pub fn std_dev(&self) -> f32 {
        self.std_dev
    }

    /// Number of values.
    pub fn count(&self) -> usize {
        self.values.len()
    }
}

/// Chart options for visualization.
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct ChartOptions {
    width: u32,
    height: u32,
    color: String,
    background: String,
    title: Option<String>,
}

#[wasm_bindgen]
impl ChartOptions {
    /// Create default chart options.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            width: 800,
            height: 400,
            color: "#4285F4".to_string(),
            background: "#FFFFFF".to_string(),
            title: None,
        }
    }

    /// Set width in pixels.
    pub fn width(mut self, width: u32) -> Self {
        self.width = width;
        self
    }

    /// Set height in pixels.
    pub fn height(mut self, height: u32) -> Self {
        self.height = height;
        self
    }

    /// Set primary color (hex: #RRGGBB).
    pub fn color(mut self, color: &str) -> Self {
        self.color = color.to_string();
        self
    }

    /// Set background color (hex: #RRGGBB).
    pub fn background(mut self, bg: &str) -> Self {
        self.background = bg.to_string();
        self
    }

    /// Set chart title.
    pub fn title(mut self, title: &str) -> Self {
        self.title = Some(title.to_string());
        self
    }
}

impl Default for ChartOptions {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Analyzer Class
// ============================================================================

/// Main analyzer for organizational intelligence.
#[wasm_bindgen]
pub struct OipAnalyzer {
    classifier: RuleBasedClassifier,
    analyses: Vec<CommitAnalysis>,
}

#[wasm_bindgen]
impl OipAnalyzer {
    /// Create a new analyzer.
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self {
            classifier: RuleBasedClassifier::new(),
            analyses: Vec::new(),
        }
    }

    /// Analyze a single commit message.
    pub fn analyze_message(&mut self, message: &str) -> CommitAnalysis {
        let analysis = if let Some(result) = self.classifier.classify_from_message(message) {
            CommitAnalysis {
                category: format!("{:?}", result.category),
                confidence: result.confidence,
                message: message.to_string(),
            }
        } else {
            CommitAnalysis {
                category: "Unknown".to_string(),
                confidence: 0.0,
                message: message.to_string(),
            }
        };
        self.analyses.push(analysis.clone());
        analysis
    }

    /// Analyze multiple commit messages.
    pub fn analyze_messages(&mut self, messages: Vec<String>) -> Vec<CommitAnalysis> {
        messages.iter().map(|m| self.analyze_message(m)).collect()
    }

    /// Get defect distribution from analyzed commits.
    pub fn get_distribution(&self) -> DefectDistributionResult {
        let mut category_counts: std::collections::HashMap<String, u32> =
            std::collections::HashMap::new();

        for analysis in &self.analyses {
            *category_counts
                .entry(analysis.category.clone())
                .or_insert(0) += 1;
        }

        let total = self.analyses.len() as u32;
        let mut items: Vec<_> = category_counts.into_iter().collect();
        items.sort_by(|a, b| b.1.cmp(&a.1));

        let categories: Vec<String> = items.iter().map(|(k, _)| k.clone()).collect();
        let counts: Vec<u32> = items.iter().map(|(_, v)| *v).collect();
        let percentages: Vec<f32> = counts
            .iter()
            .map(|c| (*c as f32 / total as f32) * 100.0)
            .collect();

        DefectDistributionResult {
            categories,
            counts,
            percentages,
            total,
        }
    }

    /// Get confidence statistics.
    pub fn get_confidence_stats(&self) -> ConfidenceStats {
        let values: Vec<f32> = self.analyses.iter().map(|a| a.confidence).collect();

        if values.is_empty() {
            return ConfidenceStats {
                values: vec![],
                min: 0.0,
                max: 0.0,
                mean: 0.0,
                std_dev: 0.0,
            };
        }

        let min = values.iter().cloned().fold(f32::INFINITY, f32::min);
        let max = values.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        let mean = values.iter().sum::<f32>() / values.len() as f32;
        let variance = values.iter().map(|v| (v - mean).powi(2)).sum::<f32>() / values.len() as f32;
        let std_dev = variance.sqrt();

        ConfidenceStats {
            values,
            min,
            max,
            mean,
            std_dev,
        }
    }

    /// Clear all analyzed data.
    pub fn clear(&mut self) {
        self.analyses.clear();
    }

    /// Get number of analyzed commits.
    pub fn count(&self) -> usize {
        self.analyses.len()
    }
}

impl Default for OipAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Standalone Functions
// ============================================================================

/// Classify a single commit message.
///
/// Returns the defect category and confidence score.
#[wasm_bindgen]
pub fn classify_commit(message: &str) -> CommitAnalysis {
    let classifier = RuleBasedClassifier::new();
    if let Some(result) = classifier.classify_from_message(message) {
        CommitAnalysis {
            category: format!("{:?}", result.category),
            confidence: result.confidence,
            message: message.to_string(),
        }
    } else {
        CommitAnalysis {
            category: "Unknown".to_string(),
            confidence: 0.0,
            message: message.to_string(),
        }
    }
}

/// Get all defect category names.
#[wasm_bindgen]
pub fn get_defect_categories() -> Vec<String> {
    vec![
        "ASTTransform".to_string(),
        "TypeErrors".to_string(),
        "OwnershipBorrow".to_string(),
        "LifetimeAnnotation".to_string(),
        "TraitBounds".to_string(),
        "PatternMatching".to_string(),
        "ErrorHandling".to_string(),
        "MemorySafety".to_string(),
        "ConcurrencySync".to_string(),
        "StdlibMapping".to_string(),
        "MacroHygiene".to_string(),
        "FFIBoundary".to_string(),
        "BuildConfiguration".to_string(),
        "TestCoverage".to_string(),
        "DocumentationSync".to_string(),
        "PerformanceRegression".to_string(),
        "SecurityVulnerabilities".to_string(),
        "Unknown".to_string(),
    ]
}

/// Generate ASCII bar chart for defect distribution.
///
/// Returns a string representation suitable for terminal or pre-formatted display.
#[wasm_bindgen]
pub fn distribution_to_ascii(dist: &DefectDistributionResult, width: usize) -> String {
    let max_count = dist.counts.iter().max().copied().unwrap_or(1) as f32;
    let max_label_len = dist.categories.iter().map(|s| s.len()).max().unwrap_or(15);

    let mut output = String::new();
    for (i, category) in dist.categories.iter().enumerate() {
        let count = dist.counts[i];
        let pct = dist.percentages[i];
        let bar_len = ((count as f32 / max_count) * (width - max_label_len - 15) as f32) as usize;
        let bar: String = "â–ˆ".repeat(bar_len.max(1));

        output.push_str(&format!(
            "{:width$} {:>5} {:>5.1}%\n",
            category,
            bar,
            pct,
            width = max_label_len
        ));
    }
    output
}

/// Generate ASCII histogram for confidence distribution.
#[wasm_bindgen]
pub fn confidence_to_ascii(stats: &ConfidenceStats, bins: usize, height: usize) -> String {
    if stats.values.is_empty() {
        return "No data".to_string();
    }

    let range = stats.max - stats.min;
    let bin_width = if range > 0.0 {
        range / bins as f32
    } else {
        1.0
    };

    // Count values in each bin
    let mut bin_counts = vec![0u32; bins];
    for &v in &stats.values {
        let bin_idx = ((v - stats.min) / bin_width).floor() as usize;
        let bin_idx = bin_idx.min(bins - 1);
        bin_counts[bin_idx] += 1;
    }

    let max_count = *bin_counts.iter().max().unwrap_or(&1) as f32;

    // Build histogram
    let mut output = String::new();
    for row in (0..height).rev() {
        let threshold = (row as f32 / height as f32) * max_count;
        output.push_str("│");
        for &count in &bin_counts {
            if count as f32 >= threshold {
                output.push_str("██");
            } else {
                output.push_str("  ");
            }
        }
        output.push('\n');
    }

    // X-axis
    output.push_str("â””");
    output.push_str(&"──".repeat(bins));
    output.push('\n');
    output.push_str(&format!(
        " {:.2}{}  {:.2}\n",
        stats.min,
        " ".repeat(bins * 2 - 10),
        stats.max
    ));
    output.push_str(&format!(
        " Mean: {:.2}, StdDev: {:.2}\n",
        stats.mean, stats.std_dev
    ));

    output
}

/// Generate JSON summary of analysis.
#[wasm_bindgen]
pub fn analysis_to_json(dist: &DefectDistributionResult, stats: &ConfidenceStats) -> String {
    let categories_json: Vec<String> = dist
        .categories
        .iter()
        .zip(dist.counts.iter())
        .zip(dist.percentages.iter())
        .map(|((cat, count), pct)| {
            format!(
                r#"{{"category":"{}","count":{},"percentage":{:.2}}}"#,
                cat, count, pct
            )
        })
        .collect();

    format!(
        r#"{{"total":{},"categories":[{}],"confidence":{{"min":{:.3},"max":{:.3},"mean":{:.3},"std_dev":{:.3}}}}}"#,
        dist.total,
        categories_json.join(","),
        stats.min,
        stats.max,
        stats.mean,
        stats.std_dev
    )
}

/// Get library version.
#[wasm_bindgen]
pub fn version() -> String {
    env!("CARGO_PKG_VERSION").to_string()
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_classify_commit() {
        let result = classify_commit("fix: resolve null pointer dereference in parser");
        assert!(!result.category.is_empty());
        assert!(result.confidence >= 0.0 && result.confidence <= 1.0);
    }

    #[test]
    fn test_oip_analyzer() {
        let mut analyzer = OipAnalyzer::new();

        analyzer.analyze_message("fix: resolve type error in validator");
        analyzer.analyze_message("fix: handle ownership transfer correctly");
        analyzer.analyze_message("fix: add missing lifetime annotation");

        assert_eq!(analyzer.count(), 3);

        let dist = analyzer.get_distribution();
        assert!(!dist.is_empty());
        assert_eq!(dist.total, 3);

        let stats = analyzer.get_confidence_stats();
        assert_eq!(stats.count(), 3);
    }

    #[test]
    fn test_defect_categories() {
        let cats = get_defect_categories();
        assert!(cats.contains(&"ASTTransform".to_string()));
        assert!(cats.contains(&"TypeErrors".to_string()));
        assert!(cats.contains(&"Unknown".to_string()));
    }

    #[test]
    fn test_distribution_to_ascii() {
        let dist = DefectDistributionResult {
            categories: vec!["ASTTransform".to_string(), "TypeErrors".to_string()],
            counts: vec![10, 5],
            percentages: vec![66.7, 33.3],
            total: 15,
        };

        let ascii = distribution_to_ascii(&dist, 40);
        assert!(ascii.contains("ASTTransform"));
        assert!(ascii.contains("TypeErrors"));
    }

    #[test]
    fn test_confidence_to_ascii() {
        let stats = ConfidenceStats {
            values: vec![0.7, 0.8, 0.85, 0.9],
            min: 0.7,
            max: 0.9,
            mean: 0.8125,
            std_dev: 0.072,
        };

        let ascii = confidence_to_ascii(&stats, 10, 5);
        assert!(ascii.contains("Mean"));
    }

    #[test]
    fn test_analysis_to_json() {
        let dist = DefectDistributionResult {
            categories: vec!["ASTTransform".to_string()],
            counts: vec![5],
            percentages: vec![100.0],
            total: 5,
        };

        let stats = ConfidenceStats {
            values: vec![0.8],
            min: 0.8,
            max: 0.8,
            mean: 0.8,
            std_dev: 0.0,
        };

        let json = analysis_to_json(&dist, &stats);
        assert!(json.contains("\"total\":5"));
        assert!(json.contains("ASTTransform"));
    }

    #[test]
    fn test_chart_options() {
        let opts = ChartOptions::new()
            .width(1024)
            .height(768)
            .color("#FF0000")
            .title("Test Chart");

        assert_eq!(opts.width, 1024);
        assert_eq!(opts.height, 768);
        assert_eq!(opts.color, "#FF0000");
        assert_eq!(opts.title, Some("Test Chart".to_string()));
    }
}