-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
204 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { WeatherData, WeatherSource, WeatherService, WeatherUnits } from '../core/weather-data/weather-data.interface'; | ||
import { OpenWeatherMapService } from '../weather-sources/open-weather-map/open-weather-map.service'; | ||
import { AlwaysSunnyService } from '../weather-sources/always-sunny/always-sunny.service'; | ||
import { WeatherSourcesRegistryService } from '../weather-sources/weather-sources-registry.service'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
private readonly weatherServices: Map<WeatherSource, WeatherService> = new Map<WeatherSource, WeatherService>(); | ||
|
||
constructor(openWeatherMapService: OpenWeatherMapService, alwaysSunnyService: AlwaysSunnyService) { | ||
this.weatherServices.set(WeatherSource.OpenWeatherMap, openWeatherMapService) | ||
this.weatherServices.set(WeatherSource.AlwaysSunny, alwaysSunnyService) | ||
constructor(private readonly weatherSourcesRegistryService: WeatherSourcesRegistryService) { | ||
} | ||
|
||
async getWeatherForCity(city: string, units: WeatherUnits = WeatherUnits.Metric, source: WeatherSource = WeatherSource.OpenWeatherMap): Promise<WeatherData> { | ||
return this.getWeatherService(source).fetchWeatherForCity(city, units) | ||
return this.weatherSourcesRegistryService.getWeatherService(source).fetchWeatherForCity(city, units) | ||
} | ||
|
||
async getWeatherForZipCode(zip: string, units: WeatherUnits = WeatherUnits.Metric, source: WeatherSource = WeatherSource.OpenWeatherMap): Promise<WeatherData> { | ||
return this.getWeatherService(source).fetchWeatherForZipCode(zip, units) | ||
return this.weatherSourcesRegistryService.getWeatherService(source).fetchWeatherForZipCode(zip, units) | ||
} | ||
|
||
async getWeatherForLatLong(latitude: string, longitude: string, units: WeatherUnits = WeatherUnits.Metric, source: WeatherSource = WeatherSource.OpenWeatherMap): Promise<WeatherData> { | ||
return this.getWeatherService(source).fetchWeatherForLatLong(latitude, longitude, units) | ||
} | ||
|
||
private getWeatherService(source: WeatherSource): WeatherService { | ||
if (!this.weatherServices.has(source)) throw new Error('unrecognized weather source') | ||
return this.weatherServices.get(source) | ||
return this.weatherSourcesRegistryService.getWeatherService(source).fetchWeatherForLatLong(latitude, longitude, units) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export enum ServiceHealth { | ||
Normal = 'normal', | ||
Degraded = 'degraded' | ||
} | ||
|
||
export interface Service { | ||
getName(): string | ||
getHealth(): Promise<ServiceHealth> | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { HealthCheckController } from './health-check.controller'; | ||
import { WeatherSourcesRegistryModule } from '../weather-sources/weather-sources-registry.module'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
|
||
describe('HealthCheckController', () => { | ||
let controller: HealthCheckController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ConfigModule.forRoot(), WeatherSourcesRegistryModule], | ||
controllers: [HealthCheckController], | ||
}).compile(); | ||
|
||
controller = module.get<HealthCheckController>(HealthCheckController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
|
||
it('should return "up" from basic health check route', () => { | ||
expect(controller.getBasicHealth()).toEqual('up') | ||
}) | ||
|
||
it('should return status for each weather source', async () => { | ||
const statuses = await controller.getDetailedHealth() | ||
expect(statuses).toHaveProperty('status') | ||
expect(statuses).toHaveProperty('sources') | ||
expect(statuses.sources).toHaveProperty('alwayssunny') | ||
expect(statuses.sources).toHaveProperty('openweathermap') | ||
expect(statuses.sources.alwayssunny.status).toEqual('normal') | ||
expect(statuses.sources.openweathermap.status).toEqual('normal') | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { WeatherSourcesRegistryService } from '../weather-sources/weather-sources-registry.service'; | ||
import { ServiceHealth } from '../core/service/service.interface'; | ||
|
||
type SourceHealth = { | ||
[source: string]: { status: ServiceHealth } | ||
} | ||
|
||
type DetailedHealthResponse = { | ||
status: ServiceHealth | ||
sources: SourceHealth | ||
} | ||
|
||
@Controller('v1/health-check') | ||
export class HealthCheckController { | ||
constructor(private readonly weatherSourcesRegistryService: WeatherSourcesRegistryService) {} | ||
|
||
@Get() | ||
getBasicHealth() { | ||
return 'up' | ||
} | ||
|
||
@Get('detailed') | ||
async getDetailedHealth(): Promise<DetailedHealthResponse> { | ||
const services = this.weatherSourcesRegistryService.getWeatherServices() | ||
let health = { status: ServiceHealth.Normal, sources: {} } | ||
for (let i = 0; i < services.length; i++) { | ||
const service = services[i] | ||
const status = await service.getHealth() | ||
health.sources[service.getName()] = { status } | ||
if (status !== ServiceHealth.Normal.toString()) health.status = status as ServiceHealth | ||
} | ||
return health | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { HealthCheckController } from './health-check.controller'; | ||
import { WeatherSourcesRegistryModule } from 'src/weather-sources/weather-sources-registry.module'; | ||
|
||
@Module({ | ||
imports: [WeatherSourcesRegistryModule], | ||
controllers: [HealthCheckController] | ||
}) | ||
export class HealthCheckModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { OpenWeatherMapModule } from './open-weather-map/open-weather-map.module'; | ||
import { AlwaysSunnyModule } from './always-sunny/always-sunny.module'; | ||
import { WeatherSourcesRegistryService } from './weather-sources-registry.service'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
OpenWeatherMapModule, | ||
AlwaysSunnyModule | ||
], | ||
providers: [ | ||
WeatherSourcesRegistryService | ||
], | ||
exports: [ | ||
WeatherSourcesRegistryService | ||
] | ||
}) | ||
export class WeatherSourcesRegistryModule {} |
32 changes: 32 additions & 0 deletions
32
src/weather-sources/weather-sources-registry.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { WeatherSourcesRegistryService } from './weather-sources-registry.service'; | ||
import { AlwaysSunnyModule } from './always-sunny/always-sunny.module'; | ||
import { OpenWeatherMapModule } from './open-weather-map/open-weather-map.module'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { WeatherSource } from 'src/core/weather-data/weather-data.interface'; | ||
|
||
describe('WeatherSourcesRegistryService', () => { | ||
let registry: WeatherSourcesRegistryService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ConfigModule.forRoot(), AlwaysSunnyModule, OpenWeatherMapModule], | ||
providers: [WeatherSourcesRegistryService], | ||
}).compile(); | ||
|
||
registry = module.get<WeatherSourcesRegistryService>(WeatherSourcesRegistryService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(registry).toBeDefined(); | ||
}); | ||
|
||
it('should return the list of services', () => { | ||
const services = registry.getWeatherServices() | ||
expect(services.length).toBeGreaterThan(1) | ||
services.forEach((service) => { | ||
expect(service.getName()).toBeDefined() | ||
expect(registry.getWeatherService(service.getName() as WeatherSource).getName()).toEqual(service.getName()) | ||
}) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { WeatherSource, WeatherService } from '../core/weather-data/weather-data.interface'; | ||
import { OpenWeatherMapService } from './open-weather-map/open-weather-map.service'; | ||
import { AlwaysSunnyService } from './always-sunny/always-sunny.service'; | ||
|
||
@Injectable() | ||
export class WeatherSourcesRegistryService { | ||
private readonly weatherServices: Map<WeatherSource, WeatherService> = new Map<WeatherSource, WeatherService>(); | ||
|
||
constructor(openWeatherMapService: OpenWeatherMapService, alwaysSunnyService: AlwaysSunnyService) { | ||
this.weatherServices.set(WeatherSource.OpenWeatherMap, openWeatherMapService) | ||
this.weatherServices.set(WeatherSource.AlwaysSunny, alwaysSunnyService) | ||
} | ||
|
||
getWeatherServices(): readonly WeatherService[] { | ||
return Array.from(this.weatherServices.values()) | ||
} | ||
|
||
getWeatherService(source: WeatherSource): WeatherService { | ||
if (!this.weatherServices.has(source)) throw new Error('unrecognized weather source') | ||
return this.weatherServices.get(source) | ||
} | ||
|
||
} |