File size: 5,984 Bytes
9bd422a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * LayerStats - Displays operator type statistics for a loaded ONNX model
 * Computes counts, percentages, and summary totals; highlights nodes by type on click.
 * Requirements: 19.1, 19.2, 19.3, 19.4, 19.5
 */

class LayerStats {
  /**
   * @param {string} containerId - ID of the container element
   */
  constructor(containerId) {
    this._containerId = containerId;
    this._container = document.getElementById(containerId);
    this._stats = null;

    if (!this._container) {
      console.warn(`[LayerStats] Container #${containerId} not found`);
    }

    this._setupEventListeners();
  }

  // ─── Private ──────────────────────────────────────────────────────────────

  /**
   * Listen for model:loaded events to auto-update stats.
   */
  _setupEventListeners() {
    if (window.EventBus && CONFIG && CONFIG.EVENTS) {
      window.EventBus.on(CONFIG.EVENTS.MODEL_LOADED, (data) => {
        if (data && data.model) {
          const stats = this.compute(data.model);
          this.render(stats);
        }
      });
    }
  }

  /**
   * Escape HTML special characters.
   * @param {string} str
   * @returns {string}
   */
  _escapeHtml(str) {
    const div = document.createElement('div');
    div.appendChild(document.createTextNode(String(str)));
    return div.innerHTML;
  }

  /**
   * Attach click and keyboard handlers to operator rows in the stats table.
   */
  _attachRowHandlers() {
    if (!this._container) return;
    const rows = this._container.querySelectorAll('.layer-stats-row');
    rows.forEach((row) => {
      const handler = () => {
        const opType = row.dataset.opType;
        if (opType && window.EventBus) {
          window.EventBus.emit('layerstats:highlight', { opType });
        }
      };
      row.addEventListener('click', handler);
      row.addEventListener('keydown', (e) => {
        if (e.key === 'Enter' || e.key === ' ') {
          e.preventDefault();
          handler();
        }
      });
    });
  }

  // ─── Public API ───────────────────────────────────────────────────────────

  /**
   * Compute operator statistics from a parsed model.
   * @param {ParsedModel} parsedModel
   * @returns {LayerStatsData}
   */
  compute(parsedModel) {
    const nodes = (parsedModel && parsedModel.graph && parsedModel.graph.nodes) ? parsedModel.graph.nodes : [];
    const edges = (parsedModel && parsedModel.graph && parsedModel.graph.edges) ? parsedModel.graph.edges : [];
    const initializers = (parsedModel && parsedModel.initializers) ? parsedModel.initializers : [];

    const totalNodes = nodes.length;
    const totalEdges = edges.length;
    const totalInitializers = initializers.length;

    // Count each operator type
    const countMap = {};
    nodes.forEach((node) => {
      const op = node.opType || 'Unknown';
      countMap[op] = (countMap[op] || 0) + 1;
    });

    // Build sorted array with percentages
    const opTypeCounts = Object.entries(countMap)
      .map(([opType, count]) => ({
        opType,
        count,
        percentage: totalNodes > 0 ? parseFloat(((count / totalNodes) * 100).toFixed(1)) : 0,
      }))
      .sort((a, b) => b.count - a.count);

    this._stats = { opTypeCounts, totalNodes, totalEdges, totalInitializers };
    return this._stats;
  }

  /**
   * Render the statistics table into the container.
   * @param {LayerStatsData} stats
   */
  render(stats) {
    if (!this._container) return;

    if (!stats) {
      this._container.innerHTML = '<p class="text-muted">No statistics available.</p>';
      return;
    }

    const { opTypeCounts, totalNodes, totalEdges, totalInitializers } = stats;

    // Summary section
    let html = `
      <div class="mb-3">
        <div class="d-flex flex-wrap gap-3 small">
          <span><i class="fas fa-project-diagram me-1"></i><strong>${totalNodes}</strong> Nodes</span>
          <span><i class="fas fa-arrows-alt-h me-1"></i><strong>${totalEdges}</strong> Edges</span>
          <span><i class="fas fa-database me-1"></i><strong>${totalInitializers}</strong> Initializers</span>
        </div>
      </div>`;

    if (opTypeCounts.length === 0) {
      html += '<p class="text-muted">No operators found.</p>';
      this._container.innerHTML = html;
      return;
    }

    // Operator stats table
    html += `
      <table class="table table-sm table-hover mb-0" role="grid" aria-label="Operator statistics">
        <thead>
          <tr>
            <th scope="col">Operator</th>
            <th scope="col" class="text-end">Count</th>
            <th scope="col" class="text-end">%</th>
          </tr>
        </thead>
        <tbody>`;

    opTypeCounts.forEach((entry) => {
      html += `
          <tr class="layer-stats-row cursor-pointer"
              role="button"
              tabindex="0"
              data-op-type="${this._escapeHtml(entry.opType)}"
              title="Click to highlight all ${this._escapeHtml(entry.opType)} nodes"
              aria-label="${this._escapeHtml(entry.opType)}: ${entry.count} nodes, ${entry.percentage}%">
            <td>${this._escapeHtml(entry.opType)}</td>
            <td class="text-end">${entry.count}</td>
            <td class="text-end">${entry.percentage}%</td>
          </tr>`;
    });

    html += `
        </tbody>
      </table>`;

    this._container.innerHTML = html;
    this._attachRowHandlers();
  }

  /**
   * Clear the display.
   */
  clear() {
    this._stats = null;
    if (!this._container) return;
    this._container.innerHTML = '<p class="text-muted">Select a model to view layer statistics</p>';
  }

  /**
   * Get the last computed stats.
   * @returns {LayerStatsData|null}
   */
  getStats() {
    return this._stats;
  }
}

window.LayerStats = LayerStats;