|
| 1 | +// Copyright (C) 2024-2025 Intel Corporation |
| 2 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +// See LICENSE.TXT |
| 4 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + |
| 6 | +/** |
| 7 | + * Find version changes in data points to create annotations |
| 8 | + * @param {Array} points - Data points to analyze |
| 9 | + * @param {string} versionKey - Key to track version changes |
| 10 | + * @returns {Array} - List of change points |
| 11 | + */ |
| 12 | +function findVersionChanges(points, versionKey) { |
| 13 | + if (!points || points.length < 2) return []; |
| 14 | + |
| 15 | + const changes = []; |
| 16 | + // Sort points by date |
| 17 | + const sortedPoints = [...points].sort((a, b) => a.x - b.x); |
| 18 | + let lastVersion = sortedPoints[0][versionKey]; |
| 19 | + |
| 20 | + for (let i = 1; i < sortedPoints.length; i++) { |
| 21 | + const currentPoint = sortedPoints[i]; |
| 22 | + |
| 23 | + const currentVersion = currentPoint[versionKey]; |
| 24 | + if (currentVersion && currentVersion !== lastVersion) { |
| 25 | + changes.push({ |
| 26 | + date: currentPoint.x, |
| 27 | + newVersion: currentVersion, |
| 28 | + }); |
| 29 | + lastVersion = currentVersion; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return changes; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Add version change annotations to chart options |
| 38 | + * @param {Object} data - Chart data |
| 39 | + * @param {Object} options - Chart.js options object |
| 40 | + */ |
| 41 | +function addVersionChangeAnnotations(data, options) { |
| 42 | + const changeTrackers = [ |
| 43 | + { |
| 44 | + // Benchmark repos updates |
| 45 | + versionKey: 'gitBenchHash', |
| 46 | + sources: [ |
| 47 | + { |
| 48 | + name: "Compute Benchmarks", |
| 49 | + url: "https://github.com/intel/compute-benchmarks.git", |
| 50 | + color: { |
| 51 | + border: 'rgba(220, 53, 69, 0.8)', |
| 52 | + background: 'rgba(220, 53, 69, 0.9)' |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "SYCL-Bench", |
| 57 | + url: "https://github.com/unisa-hpc/sycl-bench.git", |
| 58 | + color: { |
| 59 | + border: 'rgba(32, 156, 238, 0.8)', |
| 60 | + background: 'rgba(32, 156, 238, 0.9)' |
| 61 | + } |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Velocity-Bench", |
| 65 | + url: "https://github.com/oneapi-src/Velocity-Bench/", |
| 66 | + color: { |
| 67 | + border: 'rgba(255, 153, 0, 0.8)', |
| 68 | + background: 'rgba(255, 153, 0, 0.9)' |
| 69 | + } |
| 70 | + } |
| 71 | + ], |
| 72 | + pointsFilter: (points, url) => points.filter(p => p.gitBenchUrl === url), |
| 73 | + formatLabel: (sourceName, version) => `${sourceName}: ${version.substring(0, 7)}` |
| 74 | + }, |
| 75 | + { |
| 76 | + // Compute Runtime updates |
| 77 | + versionKey: 'compute_runtime', |
| 78 | + sources: [ |
| 79 | + { |
| 80 | + name: "Compute Runtime", |
| 81 | + url: "https://github.com/intel/compute-runtime.git", |
| 82 | + color: { |
| 83 | + border: 'rgba(40, 167, 69, 0.8)', |
| 84 | + background: 'rgba(40, 167, 69, 0.9)' |
| 85 | + } |
| 86 | + } |
| 87 | + ], |
| 88 | + } |
| 89 | + ]; |
| 90 | + |
| 91 | + changeTrackers.forEach(tracker => { |
| 92 | + tracker.sources.forEach((source) => { |
| 93 | + const changes = {}; |
| 94 | + |
| 95 | + // Find changes across all runs |
| 96 | + Object.values(data.runs).flatMap(runData => |
| 97 | + findVersionChanges( |
| 98 | + tracker.pointsFilter ? tracker.pointsFilter(runData.data, source.url) : runData.data, |
| 99 | + tracker.versionKey |
| 100 | + ) |
| 101 | + ).forEach(change => { |
| 102 | + const changeKey = `${source.name}-${change.newVersion}`; |
| 103 | + if (!changes[changeKey] || change.date < changes[changeKey].date) { |
| 104 | + changes[changeKey] = change; |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + // Create annotation for each unique change |
| 109 | + Object.values(changes).forEach(change => { |
| 110 | + const annotationId = `${change.date}`; |
| 111 | + // If annotation at a given date already exists, update it |
| 112 | + if (options.plugins.annotation.annotations[annotationId]) { |
| 113 | + options.plugins.annotation.annotations[annotationId].label.content.push(`${tracker.formatLabel ? |
| 114 | + `tracker.formatLabel(source.name, change.newVersion)` : |
| 115 | + `${source.name}: ${change.newVersion}`}`); |
| 116 | + options.plugins.annotation.annotations[annotationId].borderColor = 'rgba(128, 128, 128, 0.8)'; |
| 117 | + options.plugins.annotation.annotations[annotationId].borderWidth += 1; |
| 118 | + options.plugins.annotation.annotations[annotationId].label.backgroundColor = 'rgba(128, 128, 128, 0.9)'; |
| 119 | + } else { |
| 120 | + options.plugins.annotation.annotations[annotationId] = { |
| 121 | + type: 'line', |
| 122 | + xMin: change.date, |
| 123 | + xMax: change.date, |
| 124 | + borderColor: source.color.border, |
| 125 | + borderWidth: 2, |
| 126 | + borderDash: [3, 3], |
| 127 | + label: { |
| 128 | + content: [ tracker.formatLabel ? |
| 129 | + tracker.formatLabel(source.name, change.newVersion) : |
| 130 | + `${source.name}: ${change.newVersion}` ], |
| 131 | + display: false, |
| 132 | + position: 'start', |
| 133 | + backgroundColor: source.color.background, |
| 134 | + z: 1, |
| 135 | + } |
| 136 | + } |
| 137 | + }; |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Set up event listeners for annotation interactions |
| 145 | + * @param {Chart} chart - Chart.js instance |
| 146 | + * @param {CanvasRenderingContext2D} ctx - Canvas context |
| 147 | + * @param {Object} options - Chart.js options object |
| 148 | + */ |
| 149 | +function setupAnnotationListeners(chart, ctx, options) { |
| 150 | + // Add event listener for annotation clicks - display/hide label |
| 151 | + ctx.canvas.addEventListener('click', function(e) { |
| 152 | + const activeElements = chart.getElementsAtEventForMode(e, 'nearest', { intersect: true }, false); |
| 153 | + |
| 154 | + // If no data point is clicked, check if an annotation was clicked |
| 155 | + if (activeElements.length === 0) { |
| 156 | + const rect = chart.canvas.getBoundingClientRect(); |
| 157 | + const x = e.clientX - rect.left; |
| 158 | + |
| 159 | + // Check if click is near any annotation line |
| 160 | + const annotations = options.plugins.annotation.annotations; |
| 161 | + Object.values(annotations).some(annotation => { |
| 162 | + // Get the position of the annotation line |
| 163 | + const xPos = chart.scales.x.getPixelForValue(annotation.xMin); |
| 164 | + |
| 165 | + // Display label if click is near the annotation line (within 5 pixels) |
| 166 | + if (Math.abs(x - xPos) < 5) { |
| 167 | + annotation.label.display = !annotation.label.display; |
| 168 | + chart.update(); |
| 169 | + return true; // equivalent to break in a for loop |
| 170 | + } |
| 171 | + return false; |
| 172 | + }); |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + // Add mouse move handler to change cursor when hovering over annotations |
| 177 | + ctx.canvas.addEventListener('mousemove', function(e) { |
| 178 | + const rect = chart.canvas.getBoundingClientRect(); |
| 179 | + const x = e.clientX - rect.left; |
| 180 | + |
| 181 | + // Check if mouse is near any annotation line |
| 182 | + const annotations = options.plugins.annotation.annotations; |
| 183 | + const isNearAnnotation = Object.values(annotations).some(annotation => { |
| 184 | + const xPos = chart.scales.x.getPixelForValue(annotation.xMin); |
| 185 | + |
| 186 | + if (Math.abs(x - xPos) < 5) { |
| 187 | + return true; |
| 188 | + } |
| 189 | + return false; |
| 190 | + }); |
| 191 | + |
| 192 | + // Change cursor style based on proximity to annotation |
| 193 | + ctx.canvas.style.cursor = isNearAnnotation ? 'pointer' : ''; |
| 194 | + }); |
| 195 | + |
| 196 | + // Reset cursor when mouse leaves the chart area |
| 197 | + ctx.canvas.addEventListener('mouseleave', function() { |
| 198 | + ctx.canvas.style.cursor = ''; |
| 199 | + }); |
| 200 | +} |
| 201 | + |
| 202 | +// Export functions to make them available to other modules |
| 203 | +window.ChartAnnotations = { |
| 204 | + findVersionChanges, |
| 205 | + addVersionChangeAnnotations, |
| 206 | + setupAnnotationListeners |
| 207 | +}; |
0 commit comments