feat: 添加爬虫统计信息页面和功能
新增爬虫统计信息页面,展示各来源的爬取数量、最新更新时间、错误信息等统计指标 后端添加爬虫统计信息存储和查询接口,记录每次爬取的结果 支持按关键词过滤招标信息查询
This commit is contained in:
@@ -26,7 +26,13 @@ export class BidsController {
|
||||
}
|
||||
|
||||
@Get('by-date-range')
|
||||
getByDateRange(@Query('startDate') startDate: string, @Query('endDate') endDate: string) {
|
||||
return this.bidsService.getBidsByDateRange(startDate, endDate);
|
||||
getByDateRange(@Query('startDate') startDate: string, @Query('endDate') endDate?: string, @Query('keywords') keywords?: string) {
|
||||
const keywordsArray = keywords ? keywords.split(',') : undefined;
|
||||
return this.bidsService.getBidsByDateRange(startDate, endDate, keywordsArray);
|
||||
}
|
||||
|
||||
@Get('crawl-info-stats')
|
||||
getCrawlInfoStats() {
|
||||
return this.bidsService.getCrawlInfoAddStats();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ export class BidsService {
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async getBidsByDateRange(startDate?: string, endDate?: string) {
|
||||
async getBidsByDateRange(startDate?: string, endDate?: string, keywords?: string[]) {
|
||||
const qb = this.bidRepository.createQueryBuilder('bid');
|
||||
|
||||
if (startDate) {
|
||||
@@ -103,6 +103,49 @@ export class BidsService {
|
||||
qb.andWhere('bid.publishDate <= :endDate', { endDate: end });
|
||||
}
|
||||
|
||||
if (keywords && keywords.length > 0) {
|
||||
const keywordConditions = keywords.map((keyword, index) => {
|
||||
return `bid.title LIKE :keyword${index}`;
|
||||
}).join(' OR ');
|
||||
qb.andWhere(`(${keywordConditions})`, keywords.reduce((params, keyword, index) => {
|
||||
params[`keyword${index}`] = `%${keyword}%`;
|
||||
return params;
|
||||
}, {}));
|
||||
}
|
||||
|
||||
return qb.orderBy('bid.publishDate', 'DESC').getMany();
|
||||
}
|
||||
|
||||
async getCrawlInfoAddStats() {
|
||||
const { InjectRepository } = require('@nestjs/typeorm');
|
||||
const { Repository } = require('typeorm');
|
||||
const { CrawlInfoAdd } = require('../../crawler/entities/crawl-info-add.entity');
|
||||
|
||||
// 获取每个来源的最新一次爬虫记录
|
||||
const query = `
|
||||
SELECT
|
||||
source,
|
||||
count,
|
||||
latestPublishDate,
|
||||
error,
|
||||
createdAt as latestUpdate
|
||||
FROM crawl_info_add
|
||||
WHERE id IN (
|
||||
SELECT MAX(id)
|
||||
FROM crawl_info_add
|
||||
GROUP BY source
|
||||
)
|
||||
ORDER BY source ASC
|
||||
`;
|
||||
|
||||
const results = await this.bidRepository.query(query);
|
||||
|
||||
return results.map((item: any) => ({
|
||||
source: item.source,
|
||||
count: item.count,
|
||||
latestUpdate: item.latestUpdate,
|
||||
latestPublishDate: item.latestPublishDate,
|
||||
error: item.error,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user