Unverified Commit 567984a6 authored by David Haynes's avatar David Haynes
Browse files

Fix for cache not refreshing (Closes #27)

- basically we weren't including a settings file
- that's ok, just make the value a variable
- also comment up the entierty of server/methods.js
parent f6ac6655
{
"weatherCacheExpireTime": "1800"
}
// /**
* At its core, a Method is an API endpoint for your server; you can define a
* Method on the server and its counterpart on the client, then call it with
* some data, write to the database, and get the return value in a callback.
*
* https://guide.meteor.com/methods.html
*/
Meteor.methods({ Meteor.methods({
getWeather: function(latitude, longitude) { getWeather: function(latitude, longitude) {
return getWeather(latitude, longitude); return getWeather(latitude, longitude);
...@@ -6,10 +12,14 @@ Meteor.methods({ ...@@ -6,10 +12,14 @@ Meteor.methods({
}); });
/** /**
* [getWeather description] * Gets the most recent wether data based on the given Lat. and Long. and then
* @param {[type]} latitude [description] * returns it. Only gets new data if we have left the acceptable cache time.
* @param {[type]} longitude [description] *
* @return {[type]} [description] * @param latitude - A long that represents the Lat. of the weather data we
* want.
* @param longitude - A long that represents the Long. of the weather data we
* want.
* @return weatherData - An object holding the API call data and retrieval time.
*/ */
function getWeather(latitude, longitude) { function getWeather(latitude, longitude) {
var curWeatherData = WeatherData.find({}, {sort: {retrievalTime: -1, limit: 1}}).fetch()[0]; var curWeatherData = WeatherData.find({}, {sort: {retrievalTime: -1, limit: 1}}).fetch()[0];
...@@ -22,66 +32,80 @@ function getWeather(latitude, longitude) { ...@@ -22,66 +32,80 @@ function getWeather(latitude, longitude) {
return curWeatherData; return curWeatherData;
} }
// Get current time
var date = new Date(); var date = new Date();
var timeDiff = (date.getTime()) - curWeatherData.retrievalTime; //Subtract the data timestamp form cur time to get age
if(timeDiff > (Meteor.settings.weatherCacheExpireTime*10)) { // Subtract the data timestamp form cur time to get age
var timeDiff = (date.getTime()) - curWeatherData.retrievalTime;
// The max time that we allow before refreshing the weather data
var weatherCacheExpireTime = 1800000;
// If the difference in time since the weather data was collected is greater
// than our allowed maximum time
if(timeDiff > weatherCacheExpireTime) {
// Data is expired, retrieve again. // Data is expired, retrieve again.
console.log("Data Age: "+timeDiff) console.log("Data Age: " + timeDiff)
console.log("Cache expired. Retrieving...") console.log("Cache expired. Retrieving...")
var weatherData = getWeatherFromAPI(latitude, longitude); var weatherData = getWeatherFromAPI(latitude, longitude);
// Check if there was an error in the API call.
if(weatherData === undefined) { if(weatherData === undefined) {
// There was an error. Mark the latest record as error state. // Mark the latest record as error state.
curWeatherData.error = true; curWeatherData.error = true;
// Return it // Return it
return curWeatherData; return curWeatherData;
} }
// Technically the else isn't needed but it looks nice.
else { else {
// Technically the else isn't needed but it looks nice. // Save this new data.
// Save this data.
WeatherData.insert(weatherData); WeatherData.insert(weatherData);
return curWeatherData; return curWeatherData;
} }
// The data is still valid, return it
} else { } else {
console.log("Returning cached data.") console.log("Returning cached data...")
// The data is still valid, return it
return curWeatherData; return curWeatherData;
} }
} }
/** /**
* [getWeatherFromAPI description] * Helper function to call the Dark Sky API for the latest data and return an
* @param {[type]} latitude [description] * object holding that data.
* @param {[type]} longitude [description] * @param latitude - A long that represents the Lat. of the weather data we
* @return {[type]} [description] * want.
* @param longitude - A long that represents the Long. of the weather data we
* want.
* @return weatherData - An object holding the API call data and retrieval time.
*/ */
function getWeatherFromAPI(latitude, longitude) { function getWeatherFromAPI(latitude, longitude) {
// Keep the key out of the keybase // Keep the key out of the keybase
var darkskyAPIKey = process.env.DARKSKY_API_KEY; var darkskyAPIKey = process.env.DARKSKY_API_KEY;
// API URL Call
var urlBase = "https://api.darksky.net/forecast/"+darkskyAPIKey+"/"+latitude+","+longitude var urlBase = "https://api.darksky.net/forecast/"+darkskyAPIKey+"/"+latitude+","+longitude
console.log(urlBase);
try { try {
// Make a blocking call // Make a blocking call
// I don't think we should add this.unblock() in case that comes up. // I don't think we should add this.unblock() in case that comes up.
var result = HTTP.call("GET", urlBase); var result = HTTP.call("GET", urlBase);
//Get current time // Get current time
var date = new Date(); var date = new Date();
var linuxTime = date.getTime(); //convert millis to seconds var linuxTime = date.getTime(); // convert millis to seconds
// Store the retrieval time with the data. We shouldn't get the same data every time someone goes to the page. // Store the retrieval time with the data. We shouldn't get the same data
// every time someone goes to the page.
var weatherData = { var weatherData = {
retrievalTime: linuxTime, retrievalTime: linuxTime,
data: result.data, data: result.data,
error: false error: false
}; };
// Return it // Return it
return weatherData; return weatherData;
} }
// Got a network error, time-out or HTTP error in the 400 or 500 range.
catch (e) { catch (e) {
// Got a network error, time-out or HTTP error in the 400 or 500 range.
console.log(e); console.log(e);
return undefined; return undefined;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment