From 8d9fb96ea992d9500761967724dae9532ca9b86e Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Fri, 21 Jun 2019 17:25:18 -0400 Subject: [PATCH 1/5] Error if WeatherProvider API key is not provided --- routes/weatherProviders/DarkSky.ts | 18 +++++++++++++----- routes/weatherProviders/OWM.ts | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/routes/weatherProviders/DarkSky.ts b/routes/weatherProviders/DarkSky.ts index 7098f39..0e69d55 100644 --- a/routes/weatherProviders/DarkSky.ts +++ b/routes/weatherProviders/DarkSky.ts @@ -6,14 +6,23 @@ import { WeatherProvider } from "./WeatherProvider"; export default class DarkSkyWeatherProvider extends WeatherProvider { + private readonly API_KEY: string; + + public constructor() { + super(); + this.API_KEY = process.env.DARKSKY_API_KEY; + if (!this.API_KEY) { + throw "DARKSKY_API_KEY environment variable is not defined."; + } + } + public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > { // The Unix timestamp of 24 hours ago. const yesterdayTimestamp: number = moment().subtract( 1, "day" ).unix(); const todayTimestamp: number = moment().unix(); - const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY, - yesterdayUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`, - todayUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ todayTimestamp }?exclude=currently,minutely,daily,alerts,flags`; + const yesterdayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`, + todayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ todayTimestamp }?exclude=currently,minutely,daily,alerts,flags`; let yesterdayData, todayData; try { @@ -62,8 +71,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider { } public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > { - const DARKSKY_API_KEY = process.env.DARKSKY_API_KEY, - forecastUrl = `https://api.darksky.net/forecast/${ DARKSKY_API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] }?exclude=minutely,alerts,flags`; + const forecastUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] }?exclude=minutely,alerts,flags`; let forecast; try { diff --git a/routes/weatherProviders/OWM.ts b/routes/weatherProviders/OWM.ts index fdb4023..0d32d8f 100644 --- a/routes/weatherProviders/OWM.ts +++ b/routes/weatherProviders/OWM.ts @@ -4,9 +4,18 @@ import { WeatherProvider } from "./WeatherProvider"; export default class OWMWeatherProvider extends WeatherProvider { + private readonly API_KEY: string; + + public constructor() { + super(); + this.API_KEY = process.env.OWM_API_KEY; + if (!this.API_KEY) { + throw "OWM_API_KEY environment variable is not defined."; + } + } + public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > { - const OWM_API_KEY = process.env.OWM_API_KEY, - forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ]; + const forecastUrl = `http://api.openweathermap.org/data/2.5/forecast?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`; // Perform the HTTP request to retrieve the weather data let forecast; @@ -43,9 +52,8 @@ export default class OWMWeatherProvider extends WeatherProvider { } public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > { - const OWM_API_KEY = process.env.OWM_API_KEY, - currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ], - forecastDailyUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?appid=" + OWM_API_KEY + "&units=imperial&lat=" + coordinates[ 0 ] + "&lon=" + coordinates[ 1 ]; + const currentUrl = `http://api.openweathermap.org/data/2.5/weather?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`, + forecastDailyUrl = `http://api.openweathermap.org/data/2.5/forecast/daily?appid=${ this.API_KEY }&units=imperial&lat=${ coordinates[ 0 ] }&lon=${ coordinates[ 1 ] }`; let current, forecast; try { From 930a0026def072aa6bbb6b1d4468055c5e409b66 Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Fri, 21 Jun 2019 19:53:28 -0400 Subject: [PATCH 2/5] Improve handling of missing data in Dark Sky --- routes/weatherProviders/DarkSky.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/routes/weatherProviders/DarkSky.ts b/routes/weatherProviders/DarkSky.ts index 0e69d55..843678c 100644 --- a/routes/weatherProviders/DarkSky.ts +++ b/routes/weatherProviders/DarkSky.ts @@ -56,9 +56,16 @@ export default class DarkSkyWeatherProvider extends WeatherProvider { const totals = { temp: 0, humidity: 0, precip: 0 }; for ( const sample of samples ) { + /* + * If temperature or humidity is missing from a sample, the total will become NaN. This is intended since + * calculateWateringScale will treat NaN as a missing value and temperature/humidity can't be accurately + * calculated when data is missing from some samples (since they follow diurnal cycles and will be + * significantly skewed if data is missing for several consecutive hours). + */ totals.temp += sample.temperature; totals.humidity += sample.humidity; - totals.precip += sample.precipIntensity + // This field may be missing from the response if it is snowing. + totals.precip += sample.precipIntensity || 0; } return { From 375cda9e11d08ca09274117d9610d579c332f1ca Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Tue, 25 Jun 2019 22:13:15 -0400 Subject: [PATCH 3/5] Fix tests --- routes/weather.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routes/weather.spec.ts b/routes/weather.spec.ts index 36e92e5..a979773 100644 --- a/routes/weather.spec.ts +++ b/routes/weather.spec.ts @@ -4,6 +4,10 @@ import * as MockExpressRequest from 'mock-express-request'; import * as MockExpressResponse from 'mock-express-response'; import * as MockDate from 'mockdate'; +// The tests don't use OWM, but the WeatherProvider API key must be set to prevent an error from being thrown on startup. +process.env.WEATHER_PROVIDER = "OWM"; +process.env.OWM_API_KEY = "NO_KEY"; + import { getWateringData } from './weather'; import { GeoCoordinates, WateringData, WeatherData } from "../types"; import { WeatherProvider } from "./weatherProviders/WeatherProvider"; From dd92b8ecf878382cebed57df4b4f6cbff7302bc5 Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Sun, 30 Jun 2019 18:24:58 -0400 Subject: [PATCH 4/5] Make both Dark Sky API calls simultaneously --- routes/weatherProviders/DarkSky.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/routes/weatherProviders/DarkSky.ts b/routes/weatherProviders/DarkSky.ts index a5c3746..66a0692 100644 --- a/routes/weatherProviders/DarkSky.ts +++ b/routes/weatherProviders/DarkSky.ts @@ -26,8 +26,7 @@ export default class DarkSkyWeatherProvider extends WeatherProvider { let yesterdayData, todayData; try { - yesterdayData = await httpJSONRequest( yesterdayUrl ); - todayData = await httpJSONRequest( todayUrl ); + [ yesterdayData, todayData ] = await Promise.all( [ httpJSONRequest( yesterdayUrl ), httpJSONRequest( todayUrl ) ] ); } catch ( err ) { console.error( "Error retrieving weather information from Dark Sky:", err ); throw "An error occurred while retrieving weather information from Dark Sky." From a92f488ec335ad8f70a46126c163fbe507960c99 Mon Sep 17 00:00:00 2001 From: Matthew Oslan Date: Tue, 2 Jul 2019 17:15:09 -0400 Subject: [PATCH 5/5] Use fixed calendar day window in Dark Sky WeatherProvider --- routes/weatherProviders/DarkSky.ts | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/routes/weatherProviders/DarkSky.ts b/routes/weatherProviders/DarkSky.ts index 66a0692..a7228aa 100644 --- a/routes/weatherProviders/DarkSky.ts +++ b/routes/weatherProviders/DarkSky.ts @@ -19,33 +19,23 @@ export default class DarkSkyWeatherProvider extends WeatherProvider { public async getWateringData( coordinates: GeoCoordinates ): Promise< ZimmermanWateringData > { // The Unix timestamp of 24 hours ago. const yesterdayTimestamp: number = moment().subtract( 1, "day" ).unix(); - const todayTimestamp: number = moment().unix(); - const yesterdayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`, - todayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ todayTimestamp }?exclude=currently,minutely,daily,alerts,flags`; + const yesterdayUrl = `https://api.darksky.net/forecast/${ this.API_KEY }/${ coordinates[ 0 ] },${ coordinates[ 1 ] },${ yesterdayTimestamp }?exclude=currently,minutely,daily,alerts,flags`; - let yesterdayData, todayData; + let yesterdayData; try { - [ yesterdayData, todayData ] = await Promise.all( [ httpJSONRequest( yesterdayUrl ), httpJSONRequest( todayUrl ) ] ); + yesterdayData = await httpJSONRequest( yesterdayUrl ); } catch ( err ) { console.error( "Error retrieving weather information from Dark Sky:", err ); throw "An error occurred while retrieving weather information from Dark Sky." } - if ( !todayData.hourly || !todayData.hourly.data || !yesterdayData.hourly || !yesterdayData.hourly.data ) { + if ( !yesterdayData.hourly || !yesterdayData.hourly.data ) { throw "Necessary field(s) were missing from weather information returned by Dark Sky."; } - /* The number of hourly forecasts to use from today's data. This will only include elements that contain historic - data (not forecast data). */ - // Find the first element that contains forecast data. - const todayElements = Math.min( 24, todayData.hourly.data.findIndex( ( data ) => data.time > todayTimestamp - 60 * 60 ) ); - - /* Take as much data as possible from the first elements of today's data and take the remaining required data from - the remaining data from the last elements of yesterday's data. */ const samples = [ - ...yesterdayData.hourly.data.slice( todayElements - 24 ), - ...todayData.hourly.data.slice( 0, todayElements ) + ...yesterdayData.hourly.data ]; // Fail if not enough data is available.