From 1ec89c72c1050462d9479490a982fbabecae7634 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 17 Sep 2023 20:49:17 +0300 Subject: [PATCH] rainfall last 24h --- src/app.service.ts | 18 ++++++++++++++++++ src/entities/weather.entity.ts | 1 + 2 files changed, 19 insertions(+) diff --git a/src/app.service.ts b/src/app.service.ts index 2f11b6f..73b2253 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -35,6 +35,7 @@ export class AppService implements OnApplicationShutdown { await this.weatherRepository.save(entity); entity.fresh = true; + entity.rain24h = await this.rainFall24h(); return entity; } catch (error) { @@ -56,6 +57,7 @@ export class AppService implements OnApplicationShutdown { if (!previous) throw new InternalServerErrorException(); + previous.rain24h = await this.rainFall24h(previous.date); previous.fresh = false; return previous; } @@ -85,6 +87,22 @@ export class AppService implements OnApplicationShutdown { }; } + /** + * Get rainfall in the last 24h since `since` start point. + * @param since Time start point + * @returns Rainfall in mm + */ + async rainFall24h(since = new Date()) { + const { rainfall } = await this.weatherRepository + .createQueryBuilder('weather') + .select('SUM(rainDiff)', 'rainfall') + .where('date >= :date', { + date: new Date(since.getTime() - 24 * 60 * 60 * 1000), + }) + .getRawOne(); + return Number(rainfall) || 0; + } + @Cron('0 * * * *') scheduledPulls() { this.getWeather().catch(() => { diff --git a/src/entities/weather.entity.ts b/src/entities/weather.entity.ts index 62073f9..9b0d5e0 100644 --- a/src/entities/weather.entity.ts +++ b/src/entities/weather.entity.ts @@ -44,5 +44,6 @@ export class WeatherEntity { @Column({ nullable: true, type: 'decimal', precision: 6, scale: 2 }) absPressure: number; + rain24h?: number; fresh?: boolean; }