This code snippet defines a function loadLocations
that parses a JSON file containing location data, groups locations by date, and stores them in a cache for efficient retrieval. It uses a custom importer
module to stream the JSON data and process it asynchronously.
npm run import -- "Load location export data"
var importer = require('../Core');
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function loadLocations(file) {
var locationCache = {};
var offset = (new Date()).getTimezoneOffset() * 60 * 1000;
importer.streamJson(file, [true, {emitPath: true}], (match) => {
if (match.path[0] === 'locations' && match.path.length === 3) {
var currDate = new Date(parseInt(match.value.timestampMs));
var newKey = currDate.getDate() + months[currDate.getMonth()]
+ (currDate.getFullYear() + '').substr(2, 2);
var newRow = Object.assign({
time: currDate,
type: 'location',
location: newKey
}, match.value);
var cache = locationCache[newRow.location];
if (typeof cache === 'undefined') {
cache = locationCache[newRow.location] = [];
}
cache[cache.length] = newRow;
}
})
return locationCache;
};
module.exports = loadLocations;
const importer = require('../Core');
// Define months array for easier access
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/**
* Loads locations from a JSON file and caches them by date.
*
* @param {string} file Path to the JSON file.
* @returns {object} An object containing location caches.
*/
function loadLocations(file) {
// Create a cache object to store locations
const locationCache = {};
// Get the current timezone offset
const timezoneOffset = (new Date()).getTimezoneOffset() * 60 * 1000;
// Use the importer to stream the JSON file
importer.streamJson(file, [true, { emitPath: true }], (match) => {
// Check if the path is locations and has a length of 3
if (match.path[0] === 'locations' && match.path.length === 3) {
// Parse the timestampMs value to a Date object
const currDate = new Date(parseInt(match.value.timestampMs));
// Create a new key for the location cache
const newKey = `${currDate.getDate()}${MONTHS[currDate.getMonth()]}${currDate.getFullYear().toString().substr(-2)}`;
// Create a new row object
const newRow = {
time: currDate,
type: 'location',
location: newKey,
...match.value,
};
// Get the cache for the new location
const cache = locationCache[newRow.location];
// If the cache is undefined, create it
if (typeof cache === 'undefined') {
locationCache[newRow.location] = cache = [];
}
// Add the new row to the cache
cache.push(newRow);
// Log a debug message to check the cache
// TODO: Remove this after debugging
// console.log(locationCache);
}
});
// Return the location cache
return locationCache;
}
module.exports = loadLocations;
This code snippet defines a function loadLocations
that reads a JSON file containing location data and processes it to create a cache of locations grouped by date.
Here's a breakdown:
Dependencies:
importer
: A custom module likely containing a function for streaming JSON data.months
Array:
loadLocations
Function:
locationCache
to store the processed location data.importer.streamJson
to read the JSON file asynchronously.
match
) as it's processed.locations
and has a length of 3.newRow
containing the location data, timestamp, type, and date key.locationCache
.newRow
to the corresponding cache entry.locationCache
object.Module Export:
loadLocations
function as the main module export.