Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Fix missing weather status
Browse files Browse the repository at this point in the history
  • Loading branch information
anduong96 committed Nov 27, 2023
1 parent 622732f commit ee569dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
17 changes: 2 additions & 15 deletions src/services/airport.weather/get.airport.weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,13 @@ export async function populateAirportWeather(airportIata: string) {
const month = time.month();
const year = time.year();
const current = entry.data.instant;
const nextHour = entry.data.next_1_hours;
const status = nextHour?.summary.symbol_code;
const nextHour = entry.data.next_1_hours!;
const status = nextHour.summary.symbol_code;
const iconURL = format(
'https://raw.githubusercontent.com/anduong96/weathericons/main/weather/png/%s.png',
status,
);

if (!status) {
Logger.warn(
'Missing weather data for airport=%s date=%s hour=%s month=%s year=%s',
airportIata,
date,
hour,
month,
year,
);

continue;
}

const airTemperatureCelsius = current.details.air_temperature;
const windFromDirectionDegrees = current.details.wind_from_direction;
const windSpeedMeterPerSecond = current.details.wind_speed;
Expand Down
30 changes: 7 additions & 23 deletions src/vendors/weather/meteorologisk/__tests__/get.forecast.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import moment from 'moment';
import { last } from 'lodash';
import { describe, expect, it } from 'bun:test';

import { Logger } from '@app/lib/logger';
import { Coordinates } from '@app/types/coordinates';

import { MetNoApi } from '..';
Expand All @@ -22,8 +22,11 @@ describe('Vendor::Weather::Meteorologisk', () => {
expect(latitude).toBe(location.latitude);
const updatedAt = response.properties.meta.updated_at;
const firstSeriesTime = response.properties.timeseries[0].time;
const lastSeriesTime = last(response.properties.timeseries)?.time;
const daysDiff = moment(firstSeriesTime).diff(lastSeriesTime, 'days');
expect(now.diff(updatedAt, 'hours')).toBeWithin(0, 24);
expect(now.diff(firstSeriesTime, 'hours')).toBeWithin(0, 24);
expect(Math.floor(daysDiff)).toBeLessThan(MetNoApi.MAX_FORECAST_DAYS);

for (const entry of response.properties.timeseries) {
const time = moment(entry.time);
Expand All @@ -36,28 +39,9 @@ describe('Vendor::Weather::Meteorologisk', () => {
expect(hour).toBeWithin(0, 24);
expect(month).toBeWithin(0, 12);
expect(year).toBeWithin(now.year(), now.year() + 2);

const summary =
entry.data.next_1_hours ??
entry.data.next_6_hours ??
entry.data.next_12_hours;

if (!summary) {
Logger.error(
'Missing weather data for location=%s date=%s hour=%s month=%s year=%s entry=%o previous=%o',
name,
date,
hour,
month,
year,
entry,
response.properties.timeseries[
response.properties.timeseries.indexOf(entry)
].data,
);
}

expect(summary?.summary.symbol_code).toBeTruthy();
const summary = entry.data.next_1_hours;
expect(summary).toBeTruthy();
expect(summary?.summary?.symbol_code).toBeTypeOf('string');
}
});
}
Expand Down
7 changes: 6 additions & 1 deletion src/vendors/weather/meteorologisk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Coordinates } from '@app/types/coordinates';
import { MetNoWeatherForecastResponse } from './types';

export class MetNoApi {
static readonly MAX_FORECAST_DAYS = 5;
static readonly MAX_FORECAST_DAYS = 9;
private static readonly baseURL = 'https://api.met.no/weatherapi';
private static client = ky.create({ prefixUrl: this.baseURL });

Expand All @@ -20,6 +20,11 @@ export class MetNoApi {
})
.json<MetNoWeatherForecastResponse>();

data.properties.timeseries = data.properties.timeseries.slice(
0,
MetNoApi.MAX_FORECAST_DAYS,
);

return data;
}
}

0 comments on commit ee569dc

Please sign in to comment.