history query

This commit is contained in:
Evert Prants 2023-09-17 10:05:52 +03:00
parent 53d7efc20a
commit ded3f0fd83
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 38 additions and 2 deletions

View File

@ -1,6 +1,7 @@
import { Controller, Get, UseInterceptors } from '@nestjs/common'; import { Controller, Get, Query, UseInterceptors } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { CacheInterceptor, CacheTTL } from '@nestjs/cache-manager'; import { CacheInterceptor, CacheTTL } from '@nestjs/cache-manager';
import { HistoryQueryDto } from './dtos/history-query.dto';
@Controller() @Controller()
export class AppController { export class AppController {
@ -12,4 +13,9 @@ export class AppController {
getWeather() { getWeather() {
return this.appService.getWeather(); return this.appService.getWeather();
} }
@Get('history')
getWeatherHistory(@Query() query: HistoryQueryDto) {
return this.appService.getWeatherHistory(query);
}
} }

View File

@ -5,10 +5,11 @@ import {
OnApplicationShutdown, OnApplicationShutdown,
} from '@nestjs/common'; } from '@nestjs/common';
import WS1080 from './module/ws1080'; import WS1080 from './module/ws1080';
import { Repository } from 'typeorm'; import { MoreThan, Repository } from 'typeorm';
import { WeatherEntity } from './entities/weather.entity'; import { WeatherEntity } from './entities/weather.entity';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Cron } from '@nestjs/schedule'; import { Cron } from '@nestjs/schedule';
import { HistoryQueryDto } from './dtos/history-query.dto';
@Injectable() @Injectable()
export class AppService implements OnApplicationShutdown { export class AppService implements OnApplicationShutdown {
@ -60,6 +61,30 @@ export class AppService implements OnApplicationShutdown {
} }
} }
async getWeatherHistory(query: HistoryQueryDto) {
const pageSize = Number(query.pageSize) || 100;
const page = Number(query.page) || 1;
const [list, rowCount] = await this.weatherRepository.findAndCount({
where: query.since
? { date: MoreThan(new Date(query.since)) }
: undefined,
order: { date: -1 },
take: pageSize,
skip: (page - 1) * pageSize,
});
const pageCount = Math.ceil(rowCount / pageSize);
return {
list,
pagination: {
page,
pageSize,
pageCount,
rowCount,
},
};
}
@Cron('0 * * * *') @Cron('0 * * * *')
scheduledPulls() { scheduledPulls() {
this.getWeather().catch(() => { this.getWeather().catch(() => {

View File

@ -0,0 +1,5 @@
export class HistoryQueryDto {
since?: string;
page?: string;
pageSize?: string;
}