@@ -15,8 +15,29 @@ document.addEventListener('DOMContentLoaded', () => {
15
15
} ) ) ;
16
16
} ;
17
17
18
- // Function to handle clicking of labels on the x-axis
19
- const clickableScales = ( chart , event ) => {
18
+ // Function to create a graph container dynamically
19
+ const createGraphContainer = ( benchmarkName ) => {
20
+ const container = document . createElement ( 'div' ) ;
21
+ container . className = 'graph_container' ;
22
+
23
+ const descriptionDiv = document . createElement ( 'div' ) ;
24
+ descriptionDiv . className = 'description' ;
25
+ descriptionDiv . textContent = `Performance test comparison for ${ benchmarkName } ` ;
26
+
27
+ const chartContainerDiv = document . createElement ( 'div' ) ;
28
+ chartContainerDiv . className = 'chart-container' ;
29
+ const canvas = document . createElement ( 'canvas' ) ;
30
+ canvas . id = `chart_${ benchmarkName . replace ( / \s + / g, '_' ) } ` ;
31
+
32
+ chartContainerDiv . appendChild ( canvas ) ;
33
+ container . appendChild ( descriptionDiv ) ;
34
+ container . appendChild ( chartContainerDiv ) ;
35
+
36
+ return container ;
37
+ } ;
38
+
39
+ // Function to handle clickable x-axis labels
40
+ const clickableScales = ( chart , event , prId ) => {
20
41
const { top, bottom, left, width : chartWidth } = chart . scales . x ;
21
42
const tickWidth = chartWidth / chart . scales . x . ticks . length ;
22
43
const { clientX, clientY } = event ;
@@ -32,9 +53,9 @@ document.addEventListener('DOMContentLoaded', () => {
32
53
const label = chart . data . labels [ i ] ;
33
54
let url ;
34
55
35
- if ( label . startsWith ( 'PR_' ) ) {
36
- // Open pull request URL if the label starts with "PR_"
37
- url = `https://github.com/esrlabs/chipmunk/pull/${ label . split ( "_" ) [ 1 ] } ` ;
56
+ if ( label . startsWith ( 'PR_' ) && prId ) {
57
+ // Open pull request URL if the label starts with "PR_" and pr_id exists
58
+ url = `https://github.com/esrlabs/chipmunk/pull/${ prId } ` ;
38
59
} else {
39
60
// Open release URL if the label does not start with "PR_"
40
61
url = `https://github.com/esrlabs/chipmunk/releases/tag/${ label } ` ;
@@ -46,9 +67,8 @@ document.addEventListener('DOMContentLoaded', () => {
46
67
}
47
68
} ;
48
69
49
-
50
70
// Function to render a chart
51
- const renderChart = ( canvasId , labels , datasets ) => {
71
+ const renderChart = ( canvasId , labels , datasets , prId ) => {
52
72
const ctx = document . getElementById ( canvasId ) . getContext ( '2d' ) ;
53
73
const chart = new Chart ( ctx , {
54
74
type : 'line' ,
@@ -85,52 +105,49 @@ document.addEventListener('DOMContentLoaded', () => {
85
105
} ) ;
86
106
87
107
document . getElementById ( canvasId ) . addEventListener ( 'click' , ( e ) => {
88
- clickableScales ( chart , e ) ;
108
+ clickableScales ( chart , e , prId ) ;
89
109
} ) ;
90
110
91
111
return chart ;
92
112
} ;
93
113
94
114
// Function to fetch and combine data
95
115
const fetchAndCombineData = ( prId ) => {
116
+ // Fetch the main data
96
117
const fetchMainData = fetch ( 'data/data.json' ) . then ( response => response . json ( ) ) ;
118
+
119
+ // Fetch the PR data if prId is provided
97
120
const fetchPrData = prId ? fetch ( `data/pull_request/Benchmark_PR_${ prId } .json` ) . then ( response => response . json ( ) ) : Promise . resolve ( { } ) ;
98
121
99
- return Promise . all ( [ fetchMainData , fetchPrData ] )
100
- . then ( ( [ mainData , prData ] ) => {
101
- // Combine data
102
- const combinedData = { ...mainData } ;
122
+ // Combine the main data with PR data
123
+ return Promise . all ( [ fetchMainData , fetchPrData ] ) . then ( ( [ mainData , prData ] ) => {
124
+ // Merge the PR data into the main data
125
+ Object . entries ( prData ) . forEach ( ( [ benchmark , entries ] ) => {
126
+ if ( ! mainData [ benchmark ] ) {
127
+ mainData [ benchmark ] = [ ] ;
128
+ }
129
+ mainData [ benchmark ] = mainData [ benchmark ] . concat ( entries ) ;
130
+ } ) ;
103
131
104
- // Merge pull request data into combined data
105
- Object . entries ( prData ) . forEach ( ( [ benchmark , entries ] ) => {
106
- if ( ! combinedData [ benchmark ] ) {
107
- combinedData [ benchmark ] = [ ] ;
108
- }
109
- combinedData [ benchmark ] = combinedData [ benchmark ] . concat ( entries ) ;
110
- } ) ;
132
+ return mainData ;
133
+ } ) ;
134
+ } ;
111
135
112
- // Generate datasets
136
+ // Function to fetch and render all benchmarks as individual charts
137
+ const fetchAndRenderBenchmarks = ( prId ) => {
138
+ fetchAndCombineData ( prId )
139
+ . then ( ( combinedData ) => {
113
140
const allFileNames = [ ...new Set ( Object . values ( combinedData ) . flat ( ) . map ( entry => entry . release ) ) ] ;
114
- const below500Data = { } ;
115
- const above500Data = { } ;
116
141
117
- Object . entries ( combinedData ) . forEach ( ( [ benchmark , entries ] ) => {
118
- const maxValue = Math . max ( ...entries . map ( entry => entry . actual_value ) ) ;
119
- if ( maxValue < 500 ) {
120
- below500Data [ benchmark ] = entries ;
121
- } else {
122
- above500Data [ benchmark ] = entries ;
123
- }
124
- } ) ;
142
+ Object . keys ( combinedData ) . forEach ( benchmarkName => {
143
+ const entries = combinedData [ benchmarkName ] ;
144
+ const container = createGraphContainer ( benchmarkName ) ;
125
145
126
- const datasets = createDatasets ( combinedData ) ;
127
- const below500Datasets = createDatasets ( below500Data ) ;
128
- const above500Datasets = createDatasets ( above500Data ) ;
146
+ document . getElementById ( 'dynamic_graphs_container' ) . appendChild ( container ) ;
129
147
130
- // Render charts
131
- renderChart ( 'chart_full' , allFileNames , datasets ) ;
132
- renderChart ( 'chart_below500' , allFileNames , below500Datasets ) ;
133
- renderChart ( 'chart_above500' , allFileNames , above500Datasets ) ;
148
+ const datasets = createDatasets ( { [ benchmarkName ] : entries } ) ;
149
+ renderChart ( `chart_${ benchmarkName . replace ( / \s + / g, '_' ) } ` , allFileNames , datasets , prId ) ;
150
+ } ) ;
134
151
} )
135
152
. catch ( error => console . error ( 'Error fetching data:' , error ) ) ;
136
153
} ;
@@ -147,7 +164,7 @@ document.addEventListener('DOMContentLoaded', () => {
147
164
148
165
// Fetch benchmark data and render charts
149
166
const params = getQueryParams ( ) ;
150
- var prId = params [ 'pr_id' ] || null ;
167
+ const prId = params [ 'pr_id' ] || null ;
151
168
152
- fetchAndCombineData ( prId ) ;
169
+ fetchAndRenderBenchmarks ( prId ) ;
153
170
} ) ;
0 commit comments