rainfall last 24h

This commit is contained in:
Evert Prants 2023-09-17 20:49:17 +03:00
parent a9aaf45bf3
commit 1ec89c72c1
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 19 additions and 0 deletions

View File

@ -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(() => {

View File

@ -44,5 +44,6 @@ export class WeatherEntity {
@Column({ nullable: true, type: 'decimal', precision: 6, scale: 2 })
absPressure: number;
rain24h?: number;
fresh?: boolean;
}