@@ -12,44 +12,59 @@ function formatBytes(numBytes) {
1212 return `${ numBytes . toFixed ( 0 ) } ${ units [ unitIndex ] } ` ;
1313}
1414
15- async function fetchGistData ( ) {
16- // Determine the data source based on the hostname and local file existence
17- const isLocal = window . location . hostname === "localhost" || window . location . hostname === "127.0.0.1" ;
18- const gistURL = 'https://gist.githubusercontent.com/beverleyy/c9112c25c5acd400b90741efa81aa411/raw/kaggle_stats.json' ;
19- const localURL = './kaggle_stats.json' ; // Path to the local JSON file
20-
21- let dataURL = gistURL ;
22-
23- if ( isLocal ) {
24- try {
25- // Check if the local JSON file exists
26- const response = await fetch ( localURL , { method : 'HEAD' } ) ;
27- if ( response . ok ) {
28- dataURL = localURL ;
29- } else {
30- console . log ( "Local JSON file not found. Falling back to Gist." ) ;
31- }
32- } catch ( error ) {
33- console . log ( "Error checking local JSON file. Falling back to Gist:" , error ) ;
34- }
15+ async function fetchFirebaseData ( ) {
16+ const firebaseURL = "https://blastnet-backend-default-rtdb.firebaseio.com/kaggle_stats.json" ;
17+
18+ // Try to get cached ETag and JSON from localStorage
19+ let etag = localStorage . getItem ( "kaggle_etag" ) ;
20+ let cachedJSON = localStorage . getItem ( "kaggle_stats_json" ) ;
21+
22+ let headers = { } ;
23+ if ( etag ) {
24+ headers [ "If-None-Match" ] = etag ;
25+ } else {
26+ headers [ "X-Firebase-ETag" ] = "true" ; // ask Firebase to return an ETag
3527 }
3628
3729 try {
38- // Fetch data from the appropriate source
39- console . log ( `Fetching data from: ${ dataURL } ` ) ;
40- const response = await fetch ( dataURL ) ;
41- const data = await response . json ( ) ;
30+ console . log ( `Fetching data from Firebase REST API: ${ firebaseURL } ` ) ;
31+
32+ const response = await fetch ( firebaseURL , { method : "GET" , headers } ) ;
33+ let data ;
34+
35+ if ( response . status === 304 ) {
36+ // Data hasn't changed — use cached version
37+ data = JSON . parse ( cachedJSON ) ;
38+ console . log ( "Using cached data (not modified)." ) ;
39+ } else if ( response . ok ) {
40+ // New data — store ETag and JSON string
41+ const newETag = response . headers . get ( "ETag" ) ;
42+ const jsonString = await response . text ( ) ;
43+
44+ localStorage . setItem ( "kaggle_etag" , newETag ) ;
45+ localStorage . setItem ( "kaggle_stats_json" , jsonString ) ;
4246
43- // Parse JSON data
47+ data = JSON . parse ( jsonString ) ;
48+ console . log ( "Loaded fresh data from Firebase." ) ;
49+ } else {
50+ throw new Error ( `HTTP error! Status: ${ response . status } ` ) ;
51+ }
52+
53+ // Parse the data again
54+ data = JSON . parse ( data )
55+
56+ // Access your data
4457 const total_size = parseInt ( data . total_size ) ;
4558 const total_bytes = parseInt ( data . total_bytes ) ;
4659
47- // Print total bytes downloaded
60+ // Format total bytes downloaded
4861 const formattedNumber = total_bytes . toLocaleString ( ) ;
49- const wrappedNumber = Array . from ( formattedNumber ) . map ( char => ( / \d / . test ( char ) ? `<span>${ char } </span>` : `<em>${ char } </em>` ) ) . join ( '' ) ;
62+ const wrappedNumber = Array . from ( formattedNumber )
63+ . map ( char => ( / \d / . test ( char ) ? `<span>${ char } </span>` : `<em>${ char } </em>` ) )
64+ . join ( '' ) ;
5065 document . getElementById ( "kaggle_stat" ) . innerHTML = wrappedNumber + " bytes downloaded" ;
5166
52- // Print total dataset size
67+ // Format total dataset size
5368 const formattedSize = formatBytes ( total_size ) ;
5469 const wrappedSize = formattedSize . split ( / ( \s + ) / ) . map ( chunk => {
5570 if ( / ^ \d + $ / . test ( chunk ) )
@@ -62,7 +77,7 @@ async function fetchGistData() {
6277 document . getElementById ( "kaggle_size" ) . innerHTML = wrappedSize ;
6378 document . getElementById ( "data_size" ) . innerHTML = formattedSize ;
6479
65- // Extra formatting thanks matthias
80+ // Extra formatting
6681 let children = Array . from ( document . getElementById ( "kaggle_stat" ) . children ) ;
6782 let firstEmIndex = children . findIndex ( child => child . tagName . toLowerCase ( ) === 'em' ) ;
6883 let byteCount = firstEmIndex === - 1
@@ -78,7 +93,9 @@ async function fetchGistData() {
7893 }
7994
8095 } catch ( error ) {
81- console . log ( "Failed to fetch data:" , error ) ;
96+ console . error ( "Failed to fetch Firebase data:" , error ) ;
8297 }
8398}
84- fetchGistData ( ) ;
99+
100+ fetchFirebaseData ( ) ;
101+
0 commit comments