@@ -23,54 +23,24 @@ export default {
2323
2424 let logData = document . body . innerText . split ( "\n" ) ;
2525
26- // ======================== Per hours ========================
27-
2826 // Function to extract time from log data
2927 function extractTime ( log ) {
3028 let lastColon = log . lastIndexOf ( ":" ) ;
3129 let time = log . substring ( 0 , lastColon ) ;
3230 return new Date ( time ) ;
3331 }
3432
33+ function randColor ( ) {
34+ return "#" + Math . floor ( Math . random ( ) * 16777215 ) . toString ( 16 ) ;
35+ }
36+
3537 // Count logs per hour
3638 const logsPerHour = Array ( 24 ) . fill ( 0 ) ;
3739 logData . forEach ( ( log ) => {
3840 const hour = extractTime ( log ) . getHours ( ) ;
3941 logsPerHour [ hour ] ++ ;
4042 } ) ;
4143
42- // Chart.js code to draw the graph
43- const canvas = document . createElement ( "canvas" ) ;
44- canvas . style . cssText = "max-width: 900px; max-height: 300px;" ;
45- document . body . prepend ( canvas ) ;
46- const ctx = canvas . getContext ( "2d" ) ;
47- const logTimelineChart = new Chart ( ctx , {
48- type : "line" ,
49- data : {
50- labels : Array . from ( { length : 24 } , ( _ , i ) => `${ i } :00` ) ,
51- datasets : [
52- {
53- label : "Number of Logs" ,
54- data : logsPerHour ,
55- borderColor : "rgb(75, 192, 192)" ,
56- tension : 0.1 ,
57- fill : false ,
58- } ,
59- ] ,
60- } ,
61- options : {
62- responsive : true ,
63- interaction : {
64- intersect : false ,
65- } ,
66- scales : {
67- y : {
68- beginAtZero : true ,
69- } ,
70- } ,
71- } ,
72- } ) ;
73-
7444 // ======================== Per script name ========================
7545 const scriptNameCount = new Map ( ) ;
7646 logData . forEach ( ( log ) => {
@@ -89,8 +59,7 @@ export default {
8959 ) ;
9060
9161 const canvas2 = document . createElement ( "canvas" ) ;
92- canvas2 . style . cssText = "max-width: 300px; max-height: 300px;" ;
93- document . body . prepend ( canvas2 ) ;
62+ canvas2 . style . cssText = "max-height: 500px;" ;
9463 const ctx2 = canvas2 . getContext ( "2d" ) ;
9564 const scriptNameChart = new Chart ( ctx2 , {
9665 type : "doughnut" ,
@@ -144,37 +113,34 @@ export default {
144113 return {
145114 label : scriptName ,
146115 data,
147- backgroundColor : `rgb(${ Math . floor (
148- Math . random ( ) * 255
149- ) } ,${ Math . floor ( Math . random ( ) * 255 ) } ,${ Math . floor (
150- Math . random ( ) * 255
151- ) } )`,
116+ backgroundColor : randColor ( ) ,
117+ stack : "combined" ,
118+ type : "bar" ,
152119 } ;
153120 } ) ;
154121
155- const canvas3 = document . createElement ( "canvas" ) ;
156- canvas3 . style . cssText = "max-width: 900px; max-height: 300px;" ;
157- document . body . prepend ( canvas3 ) ;
158- const ctx3 = canvas3 . getContext ( "2d" ) ;
122+ const canvas = document . createElement ( "canvas" ) ;
123+ canvas . style . cssText = "max-height: 500px;" ;
124+ const ctx3 = canvas . getContext ( "2d" ) ;
159125 const scriptNamePerHourChart = new Chart ( ctx3 , {
160- type : "bar " ,
126+ type : "line " ,
161127 data : {
162128 labels : Array . from ( { length : 24 } , ( _ , i ) => `${ i } :00` ) ,
163- datasets : scriptNamePerHour_dataset ,
129+ datasets : scriptNamePerHour_dataset . concat ( {
130+ label : "Total" ,
131+ data : logsPerHour ,
132+ borderColor : "rgb(75, 192, 192)" ,
133+ type : "line" ,
134+ fill : false ,
135+ tension : 0.5 ,
136+ } ) ,
164137 } ,
165138 options : {
166- interaction : {
167- intersect : false ,
139+ datalabels : {
140+ display : true ,
141+ formatter : ( value ) => value > 0 ,
168142 } ,
169143 responsive : true ,
170- scales : {
171- x : {
172- stacked : true ,
173- } ,
174- y : {
175- stacked : true ,
176- } ,
177- } ,
178144 } ,
179145 } ) ;
180146
@@ -183,7 +149,9 @@ export default {
183149 h1 . textContent = `${ logData . length } logs,
184150 ${ scriptNameCount . size } scripts used
185151 (~${ ~ ~ ( logData . length / new Date ( ) . getHours ( ) ) } scripts/hour)` ;
186- document . body . prepend ( h1 ) ;
152+
153+ // ======================== Append Charts ========================
154+ document . body . prepend ( h1 , canvas , canvas2 ) ;
187155 }
188156 ) ;
189157 } ) ( ) ;
0 commit comments