rainfall last 24h
This commit is contained in:
parent
a9aaf45bf3
commit
1ec89c72c1
@ -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(() => {
|
||||
|
@ -44,5 +44,6 @@ export class WeatherEntity {
|
||||
@Column({ nullable: true, type: 'decimal', precision: 6, scale: 2 })
|
||||
absPressure: number;
|
||||
|
||||
rain24h?: number;
|
||||
fresh?: boolean;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user