1515const fs = require ( "fs" ) ;
1616const fileUtils = require ( "./file-utils" ) ;
1717const request = require ( "request" ) ;
18- const puppeteer = require ( "puppeteer" ) ;
1918
2019// GitHub-served raw JSON file URLs from gh-pages branch
2120const overallURL =
@@ -25,6 +24,47 @@ const conciseURL =
2524const detailedURL =
2625 "https://tsitu.github.io/MH-Tools/data/json/sample-summary-detailed.json" ;
2726
27+ /**
28+ * Add a unique query string to bypass edge/browser caches for GitHub Pages JSON
29+ * @param {string } url
30+ * @returns {string }
31+ */
32+ function cacheBust ( url ) {
33+ return `${ url } ?t=${ Date . now ( ) } ` ;
34+ }
35+
36+ /**
37+ * Fetch and parse JSON with explicit no-cache headers
38+ * @param {string } url
39+ * @returns {Promise<object> }
40+ */
41+ function fetchJSON ( url ) {
42+ return new Promise ( ( resolve , reject ) => {
43+ request (
44+ {
45+ url : cacheBust ( url ) ,
46+ headers : {
47+ "Cache-Control" : "no-cache, no-store, must-revalidate" ,
48+ Pragma : "no-cache" ,
49+ Expires : "0"
50+ }
51+ } ,
52+ ( error , response , body ) => {
53+ if ( error ) {
54+ reject ( error ) ;
55+ return ;
56+ }
57+
58+ try {
59+ resolve ( JSON . parse ( body ) ) ;
60+ } catch ( parseErr ) {
61+ reject ( parseErr ) ;
62+ }
63+ }
64+ ) ;
65+ } ) ;
66+ }
67+
2868/**
2969 * Returns ideal sample size for 10% relative uncertainty at 95% level
3070 * Formula: n = 4 * z^2 * (1 - p) / (p * x^2)
@@ -253,127 +293,127 @@ function parseJSON() {
253293
254294function processOverall ( ) {
255295 return new Promise ( ( resolve , reject ) => {
256- request ( overallURL , ( error , response , body ) => {
257- if ( error ) throw error ;
258- const obj = JSON . parse ( body ) ;
259-
260- // Compare overall summary scores
261- const currentOverallSS = + obj [ "score" ] ;
262- const incomingOverallSS = + overallObj [ "score" ] ;
263-
264- console . log ( "----------------------------------------\n" ) ;
265- console . log (
266- `[ Overall Score Change ]\n\n ${ currentOverallSS } ( ${ scoreLabel (
267- currentOverallSS
268- ) } ) -> ${ incomingOverallSS } ( ${ scoreLabel ( incomingOverallSS ) } )\n`
269- ) ;
270- console . log ( "----------------------------------------\n" ) ;
271-
272- resolve ( ) ;
273- } ) ;
296+ fetchJSON ( overallURL )
297+ . then ( obj => {
298+
299+ // Compare overall summary scores
300+ const currentOverallSS = + obj [ "score" ] ;
301+ const incomingOverallSS = + overallObj [ "score" ] ;
302+
303+ console . log ( "----------------------------------------\n" ) ;
304+ console . log (
305+ `[ Overall Score Change ]\n\n ${ currentOverallSS } ( ${ scoreLabel (
306+ currentOverallSS
307+ ) } ) -> ${ incomingOverallSS } ( ${ scoreLabel ( incomingOverallSS ) } )\n`
308+ ) ;
309+ console . log ( "----------------------------------------\n" ) ;
310+
311+ resolve ( ) ;
312+ } )
313+ . catch ( reject ) ;
274314 } ) ;
275315}
276316
277317function processLocation ( ) {
278318 return new Promise ( ( resolve , reject ) => {
279- request ( conciseURL , ( error , response , body ) => {
280- if ( error ) throw error ;
281- const obj = JSON . parse ( body ) ;
282- console . log ( "[ Changes By Location ]\n" ) ;
283-
284- // Compare concise summaries
285- for ( let el in conciseObj ) {
286- if ( ! obj [ el ] ) {
287- console . log (
288- ` ${ el } (New Location)\n Average Score: ${
289- conciseObj [ el ] [ "Average Score" ]
290- } \n Location Rating: ${
291- conciseObj [ el ] [ "Location Rating" ]
292- } \n Average Sample Size: ${
293- conciseObj [ el ] [ "Average Sample Size" ]
294- } \n Average Mice Count: ${ conciseObj [ el ] [ "Average Mice Count" ] } \n`
295- ) ;
296- } else if (
297- conciseObj [ el ] [ "Average Score " ] != obj [ el ] [ "Average Score " ] ||
298- conciseObj [ el ] [ "Location Rating " ] != obj [ el ] [ "Location Rating" ] ||
299- conciseObj [ el ] [ "Average Sample Size" ] !=
300- obj [ el ] [ "Average Sample Size " ] ||
301- conciseObj [ el ] [ "Average Mice Count" ] != obj [ el ] [ "Average Mice Count" ]
302- ) {
303- console . log (
304- ` ${ el } \n Average Score: ${ obj [ el ] [ "Average Score" ] } -> ${
305- conciseObj [ el ] [ "Average Score" ]
306- } \n Location Rating: ${ obj [ el ] [ "Location Rating" ] } -> ${
307- conciseObj [ el ] [ "Location Rating" ]
308- } \n Average Sample Size: ${ obj [ el ] [ "Average Sample Size" ] } -> ${
309- conciseObj [ el ] [ "Average Sample Size" ]
310- } \n Average Mice Count: ${ obj [ el ] [ "Average Mice Count" ] } -> ${
311- conciseObj [ el ] [ "Average Mice Count" ]
312- } \n`
313- ) ;
319+ fetchJSON ( conciseURL )
320+ . then ( obj => {
321+ console . log ( "[ Changes By Location ]\n" ) ;
322+
323+ // Compare concise summaries
324+ for ( let el in conciseObj ) {
325+ if ( ! obj [ el ] ) {
326+ console . log (
327+ ` ${ el } (New Location)\n Average Score: ${
328+ conciseObj [ el ] [ " Average Score" ]
329+ } \n Location Rating: ${
330+ conciseObj [ el ] [ " Location Rating" ]
331+ } \n Average Sample Size: ${
332+ conciseObj [ el ] [ " Average Sample Size" ]
333+ } \n Average Mice Count: ${ conciseObj [ el ] [ "Average Mice Count" ] } \n`
334+ ) ;
335+ } else if (
336+ conciseObj [ el ] [ "Average Score" ] != obj [ el ] [ "Average Score" ] ||
337+ conciseObj [ el ] [ "Location Rating " ] != obj [ el ] [ "Location Rating " ] ||
338+ conciseObj [ el ] [ "Average Sample Size " ] !=
339+ obj [ el ] [ "Average Sample Size" ] ||
340+ conciseObj [ el ] [ "Average Mice Count " ] != obj [ el ] [ "Average Mice Count" ]
341+ ) {
342+ console . log (
343+ ` ${ el } \n Average Score: ${ obj [ el ] [ "Average Score" ] } -> ${
344+ conciseObj [ el ] [ "Average Score" ]
345+ } \n Location Rating: ${ obj [ el ] [ "Location Rating" ] } -> ${
346+ conciseObj [ el ] [ "Location Rating" ]
347+ } \n Average Sample Size: ${ obj [ el ] [ "Average Sample Size" ] } -> ${
348+ conciseObj [ el ] [ "Average Sample Size" ]
349+ } \n Average Mice Count: ${ obj [ el ] [ "Average Mice Count" ] } -> ${
350+ conciseObj [ el ] [ "Average Mice Count" ]
351+ } \n`
352+ ) ;
353+ }
314354 }
315- }
316- console . log ( "----------------------------------------\n" ) ;
355+ console . log ( "----------------------------------------\n" ) ;
317356
318- resolve ( ) ;
319- } ) ;
357+ resolve ( ) ;
358+ } )
359+ . catch ( reject ) ;
320360 } ) ;
321361}
322362
323363function processDetailed ( ) {
324364 return new Promise ( ( resolve , reject ) => {
325- request ( detailedURL , ( error , response , body ) => {
326- if ( error ) throw error ;
327- const obj = JSON . parse ( body ) ;
328- console . log ( "[ Changes By Phase/Cheese/Charm ]\n" ) ;
329-
330- // Compare detailed summaries
331- for ( let loc in detailedObj ) {
332- if ( ! obj [ loc ] ) {
333- console . log ( `${ loc } (New Location)` ) ;
334- for ( let sub in detailedObj [ loc ] ) {
335- console . log (
336- `${ sub } \n Score: ${
337- detailedObj [ loc ] [ sub ] [ "score" ]
338- } \n Sample Size: ${
339- detailedObj [ loc ] [ sub ] [ "sample" ]
340- } \n Mouse Count: ${ detailedObj [ loc ] [ sub ] [ "count" ] } `
341- ) ;
342- }
343- console . log ( "" ) ;
344- } else {
345- for ( let sub in detailedObj [ loc ] ) {
346- if ( ! obj [ loc ] [ sub ] ) {
347- // Log location every time?
365+ fetchJSON ( detailedURL )
366+ . then ( obj => {
367+ console . log ( "[ Changes By Phase/Cheese/Charm ]\n" ) ;
368+
369+ // Compare detailed summaries
370+ for ( let loc in detailedObj ) {
371+ if ( ! obj [ loc ] ) {
372+ console . log ( `${ loc } (New Location)` ) ;
373+ for ( let sub in detailedObj [ loc ] ) {
348374 console . log (
349- `${ loc } , ${ sub } (New PCC) \n Score: ${
375+ `${ sub } \n Score: ${
350376 detailedObj [ loc ] [ sub ] [ "score" ]
351377 } \n Sample Size: ${
352378 detailedObj [ loc ] [ sub ] [ "sample" ]
353- } \n Mouse Count: ${ detailedObj [ loc ] [ sub ] [ "count" ] } \n`
354- ) ;
355- } else if (
356- detailedObj [ loc ] [ sub ] [ "score" ] != obj [ loc ] [ sub ] [ "score" ] ||
357- detailedObj [ loc ] [ sub ] [ "sample" ] != obj [ loc ] [ sub ] [ "sample" ] ||
358- detailedObj [ loc ] [ sub ] [ "count" ] != obj [ loc ] [ sub ] [ "count" ]
359- ) {
360- console . log (
361- `${ loc } , ${ sub } \n Score: ${ obj [ loc ] [ sub ] [ "score" ] } -> ${
362- detailedObj [ loc ] [ sub ] [ "score" ]
363- } \n Sample Size: ${ obj [ loc ] [ sub ] [ "sample" ] } -> ${
364- detailedObj [ loc ] [ sub ] [ "sample" ]
365- } \n Mouse Count: ${ obj [ loc ] [ sub ] [ "count" ] } -> ${
366- detailedObj [ loc ] [ sub ] [ "count" ]
367- } \n`
379+ } \n Mouse Count: ${ detailedObj [ loc ] [ sub ] [ "count" ] } `
368380 ) ;
369381 }
382+ console . log ( "" ) ;
383+ } else {
384+ for ( let sub in detailedObj [ loc ] ) {
385+ if ( ! obj [ loc ] [ sub ] ) {
386+ // Log location every time?
387+ console . log (
388+ `${ loc } , ${ sub } (New PCC)\n Score: ${
389+ detailedObj [ loc ] [ sub ] [ "score" ]
390+ } \n Sample Size: ${
391+ detailedObj [ loc ] [ sub ] [ "sample" ]
392+ } \n Mouse Count: ${ detailedObj [ loc ] [ sub ] [ "count" ] } \n`
393+ ) ;
394+ } else if (
395+ detailedObj [ loc ] [ sub ] [ "score" ] != obj [ loc ] [ sub ] [ "score" ] ||
396+ detailedObj [ loc ] [ sub ] [ "sample" ] != obj [ loc ] [ sub ] [ "sample" ] ||
397+ detailedObj [ loc ] [ sub ] [ "count" ] != obj [ loc ] [ sub ] [ "count" ]
398+ ) {
399+ console . log (
400+ `${ loc } , ${ sub } \n Score: ${ obj [ loc ] [ sub ] [ "score" ] } -> ${
401+ detailedObj [ loc ] [ sub ] [ "score" ]
402+ } \n Sample Size: ${ obj [ loc ] [ sub ] [ "sample" ] } -> ${
403+ detailedObj [ loc ] [ sub ] [ "sample" ]
404+ } \n Mouse Count: ${ obj [ loc ] [ sub ] [ "count" ] } -> ${
405+ detailedObj [ loc ] [ sub ] [ "count" ]
406+ } \n`
407+ ) ;
408+ }
409+ }
370410 }
371411 }
372- }
373- console . log ( "----------------------------------------" ) ;
412+ console . log ( "----------------------------------------" ) ;
374413
375- resolve ( ) ;
376- } ) ;
414+ resolve ( ) ;
415+ } )
416+ . catch ( reject ) ;
377417 } ) ;
378418}
379419
@@ -382,20 +422,6 @@ function processDetailed() {
382422 * Consistent console.log ordering by using separate functions and chaining
383423 */
384424async function calculateDiffs ( ) {
385- // Force update raw JSON files on GitHub using Puppeteer
386- const browser = await puppeteer . launch ( {
387- args : [ "--no-sandbox" , "--disable-setuid-sandbox" ]
388- // executablePath:
389- // "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
390- } ) ;
391- const overallPage = await browser . newPage ( ) ;
392- const concisePage = await browser . newPage ( ) ;
393- const detailedPage = await browser . newPage ( ) ;
394- await overallPage . goto ( overallURL ) ;
395- await concisePage . goto ( conciseURL ) ;
396- await detailedPage . goto ( detailedURL ) ;
397- await browser . close ( ) ;
398-
399425 await processOverall ( ) ;
400426 await processLocation ( ) ;
401427 await processDetailed ( ) ;
0 commit comments