Compare commits
4 Commits
2b21ddb990
...
996289c671
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
996289c671 | ||
|
|
bfac194c14 | ||
|
|
533d7b60fb | ||
|
|
af58d770b6 |
@@ -6,6 +6,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "vue-tsc -b && vite build",
|
"build": "vue-tsc -b && vite build",
|
||||||
|
"build:watch": "concurrently \"vue-tsc -b --watch\" \"vite build --watch\"",
|
||||||
"preview": "vite preview"
|
"preview": "vite preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -26,6 +26,10 @@
|
|||||||
<el-icon><Setting /></el-icon>
|
<el-icon><Setting /></el-icon>
|
||||||
<span>Keywords</span>
|
<span>Keywords</span>
|
||||||
</el-menu-item>
|
</el-menu-item>
|
||||||
|
<el-menu-item index="5">
|
||||||
|
<el-icon><Connection /></el-icon>
|
||||||
|
<span>Crawl Info</span>
|
||||||
|
</el-menu-item>
|
||||||
</el-menu>
|
</el-menu>
|
||||||
</el-aside>
|
</el-aside>
|
||||||
|
|
||||||
@@ -43,6 +47,7 @@
|
|||||||
:loading="loading"
|
:loading="loading"
|
||||||
:is-crawling="isCrawling"
|
:is-crawling="isCrawling"
|
||||||
@refresh="fetchData"
|
@refresh="fetchData"
|
||||||
|
@update-bids="updateBidsByDateRange"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<DashboardAI
|
<DashboardAI
|
||||||
@@ -65,6 +70,10 @@
|
|||||||
:loading="loading"
|
:loading="loading"
|
||||||
@refresh="fetchData"
|
@refresh="fetchData"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<CrawlInfo
|
||||||
|
v-if="activeIndex === '5'"
|
||||||
|
/>
|
||||||
</el-main>
|
</el-main>
|
||||||
</el-container>
|
</el-container>
|
||||||
</el-container>
|
</el-container>
|
||||||
@@ -73,11 +82,13 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, onMounted } from 'vue'
|
import { ref, onMounted } from 'vue'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { DataBoard, Document, Setting, MagicStick } from '@element-plus/icons-vue'
|
import { ElMessage } from 'element-plus'
|
||||||
|
import { DataBoard, Document, Setting, MagicStick, Connection } from '@element-plus/icons-vue'
|
||||||
import Dashboard from './components/Dashboard.vue'
|
import Dashboard from './components/Dashboard.vue'
|
||||||
import DashboardAI from './components/Dashboard-AI.vue'
|
import DashboardAI from './components/Dashboard-AI.vue'
|
||||||
import Bids from './components/Bids.vue'
|
import Bids from './components/Bids.vue'
|
||||||
import Keywords from './components/Keywords.vue'
|
import Keywords from './components/Keywords.vue'
|
||||||
|
import CrawlInfo from './components/CrawlInfo.vue'
|
||||||
|
|
||||||
const activeIndex = ref('1')
|
const activeIndex = ref('1')
|
||||||
const bids = ref<any[]>([])
|
const bids = ref<any[]>([])
|
||||||
@@ -142,6 +153,29 @@ const fetchData = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据日期范围更新投标信息
|
||||||
|
const updateBidsByDateRange = async (startDate: string, endDate?: string, keywords?: string[]) => {
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const params: any = { startDate }
|
||||||
|
if (endDate) {
|
||||||
|
params.endDate = endDate
|
||||||
|
}
|
||||||
|
if (keywords && keywords.length > 0) {
|
||||||
|
params.keywords = keywords.join(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await axios.get('/api/bids/by-date-range', { params })
|
||||||
|
todayBids.value = response.data
|
||||||
|
ElMessage.success(`更新成功,共 ${response.data.length} 条数据`)
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to update bids by date range:', error)
|
||||||
|
ElMessage.error('更新失败')
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
fetchData()
|
fetchData()
|
||||||
})
|
})
|
||||||
|
|||||||
135
frontend/src/components/CrawlInfo.vue
Normal file
135
frontend/src/components/CrawlInfo.vue
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
<template>
|
||||||
|
<div class="crawl-info">
|
||||||
|
<el-card>
|
||||||
|
<template #header>
|
||||||
|
<div class="card-header">
|
||||||
|
<span>爬虫统计信息</span>
|
||||||
|
<el-button type="primary" size="small" @click="fetchCrawlStats" :loading="loading">
|
||||||
|
<el-icon><Refresh /></el-icon>
|
||||||
|
刷新
|
||||||
|
</el-button>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<el-table :data="crawlStats" stripe style="width: 100%" v-loading="loading">
|
||||||
|
<el-table-column prop="source" label="爬虫来源" width="200" />
|
||||||
|
<el-table-column prop="count" label="本次获取数量" width="120" sortable />
|
||||||
|
<el-table-column label="最近更新时间" width="180">
|
||||||
|
<template #default="{ row }">
|
||||||
|
{{ formatDate(row.latestUpdate) }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="最新工程时间" width="180">
|
||||||
|
<template #default="{ row }">
|
||||||
|
{{ formatDate(row.latestPublishDate) }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="状态" width="100">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-tag :type="row.error ? 'danger' : (row.count > 0 ? 'success' : 'info')">
|
||||||
|
{{ row.error ? '出错' : (row.count > 0 ? '正常' : '无数据') }}
|
||||||
|
</el-tag>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="错误信息" min-width="200">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<span v-if="row.error" style="color: #f56c6c">{{ row.error }}</span>
|
||||||
|
<span v-else>-</span>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
</el-table>
|
||||||
|
|
||||||
|
<div class="summary" v-if="crawlStats.length > 0">
|
||||||
|
<el-descriptions :column="3" border>
|
||||||
|
<el-descriptions-item label="爬虫来源总数">
|
||||||
|
{{ crawlStats.length }}
|
||||||
|
</el-descriptions-item>
|
||||||
|
<el-descriptions-item label="本次获取总数">
|
||||||
|
{{ totalCount }}
|
||||||
|
</el-descriptions-item>
|
||||||
|
<el-descriptions-item label="有数据来源">
|
||||||
|
{{ activeSources }}
|
||||||
|
</el-descriptions-item>
|
||||||
|
<el-descriptions-item label="出错来源">
|
||||||
|
{{ errorSources }}
|
||||||
|
</el-descriptions-item>
|
||||||
|
</el-descriptions>
|
||||||
|
</div>
|
||||||
|
</el-card>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ref, computed, onMounted } from 'vue'
|
||||||
|
import axios from 'axios'
|
||||||
|
import { ElMessage } from 'element-plus'
|
||||||
|
import { Refresh } from '@element-plus/icons-vue'
|
||||||
|
|
||||||
|
interface CrawlStat {
|
||||||
|
source: string
|
||||||
|
count: number
|
||||||
|
latestUpdate: string | null
|
||||||
|
latestPublishDate: string | null
|
||||||
|
error: string | null
|
||||||
|
}
|
||||||
|
|
||||||
|
const crawlStats = ref<CrawlStat[]>([])
|
||||||
|
const loading = ref(false)
|
||||||
|
|
||||||
|
const totalCount = computed(() => {
|
||||||
|
return crawlStats.value.reduce((sum, item) => sum + item.count, 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
const activeSources = computed(() => {
|
||||||
|
return crawlStats.value.filter(item => item.count > 0).length
|
||||||
|
})
|
||||||
|
|
||||||
|
const errorSources = computed(() => {
|
||||||
|
return crawlStats.value.filter(item => item.error).length
|
||||||
|
})
|
||||||
|
|
||||||
|
const formatDate = (dateStr: string | null) => {
|
||||||
|
if (!dateStr) return '-'
|
||||||
|
const date = new Date(dateStr)
|
||||||
|
return date.toLocaleString('zh-CN', {
|
||||||
|
year: 'numeric',
|
||||||
|
month: '2-digit',
|
||||||
|
day: '2-digit',
|
||||||
|
hour: '2-digit',
|
||||||
|
minute: '2-digit'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchCrawlStats = async () => {
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const res = await axios.get('/api/bids/crawl-info-stats')
|
||||||
|
crawlStats.value = res.data
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch crawl stats:', error)
|
||||||
|
ElMessage.error('获取爬虫统计信息失败')
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
fetchCrawlStats()
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.crawl-info {
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.card-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.summary {
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -55,6 +55,11 @@
|
|||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column prop="source" label="来源" width="200" />
|
<el-table-column prop="source" label="来源" width="200" />
|
||||||
|
<el-table-column prop="publishDate" label="发布日期" width="180">
|
||||||
|
<template #default="scope">
|
||||||
|
{{ formatDate(scope.row.publishDate) }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
<el-table-column prop="confidence" label="推荐度" width="120">
|
<el-table-column prop="confidence" label="推荐度" width="120">
|
||||||
<template #default="scope">
|
<template #default="scope">
|
||||||
<el-tag :type="getConfidenceType(scope.row.confidence)">
|
<el-tag :type="getConfidenceType(scope.row.confidence)">
|
||||||
@@ -106,7 +111,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue'
|
import { ref, watch } from 'vue'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { ElMessage } from 'element-plus'
|
import { ElMessage } from 'element-plus'
|
||||||
import { MagicStick, Loading, InfoFilled, List } from '@element-plus/icons-vue'
|
import { MagicStick, Loading, InfoFilled, List } from '@element-plus/icons-vue'
|
||||||
@@ -117,6 +122,7 @@ interface AIRecommendation {
|
|||||||
url: string
|
url: string
|
||||||
source: string
|
source: string
|
||||||
confidence: number
|
confidence: number
|
||||||
|
publishDate?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -132,6 +138,37 @@ const showAllBids = ref(false)
|
|||||||
const bidsLoading = ref(false)
|
const bidsLoading = ref(false)
|
||||||
const bidsByDateRange = ref<any[]>([])
|
const bidsByDateRange = ref<any[]>([])
|
||||||
|
|
||||||
|
// 从 localStorage 加载保存的日期范围
|
||||||
|
const loadSavedDateRange = () => {
|
||||||
|
const saved = localStorage.getItem('dashboardAI_dateRange')
|
||||||
|
if (saved) {
|
||||||
|
try {
|
||||||
|
dateRange.value = JSON.parse(saved)
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to parse saved date range:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监听日期范围变化并保存到 localStorage
|
||||||
|
watch(dateRange, (newDateRange) => {
|
||||||
|
localStorage.setItem('dashboardAI_dateRange', JSON.stringify(newDateRange))
|
||||||
|
}, { deep: true })
|
||||||
|
|
||||||
|
// 从数据库加载最新的 AI 推荐
|
||||||
|
const loadLatestRecommendations = async () => {
|
||||||
|
try {
|
||||||
|
const response = await axios.get('/api/ai/latest-recommendations')
|
||||||
|
aiRecommendations.value = response.data
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load latest recommendations:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化时加载保存的日期范围和最新的 AI 推荐
|
||||||
|
loadSavedDateRange()
|
||||||
|
loadLatestRecommendations()
|
||||||
|
|
||||||
// 设置日期范围为最近3天
|
// 设置日期范围为最近3天
|
||||||
const setLast3Days = () => {
|
const setLast3Days = () => {
|
||||||
const endDate = new Date()
|
const endDate = new Date()
|
||||||
@@ -181,7 +218,7 @@ const fetchAIRecommendations = async () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 根据 title 从 bidsByDateRange 中更新 url 和 source
|
// 根据 title 从 bidsByDateRange 中更新 url 和 source
|
||||||
aiRecommendations.value = response.data.map((rec: any) => {
|
const recommendations = response.data.map((rec: any) => {
|
||||||
const bid = bidsByDateRange.value.find(b => b.title === rec.title)
|
const bid = bidsByDateRange.value.find(b => b.title === rec.title)
|
||||||
return {
|
return {
|
||||||
title: rec.title,
|
title: rec.title,
|
||||||
@@ -191,6 +228,13 @@ const fetchAIRecommendations = async () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
aiRecommendations.value = recommendations
|
||||||
|
|
||||||
|
// 保存推荐结果到数据库
|
||||||
|
await axios.post('/api/ai/save-recommendations', {
|
||||||
|
recommendations
|
||||||
|
})
|
||||||
|
|
||||||
ElMessage.success('AI 推荐获取成功')
|
ElMessage.success('AI 推荐获取成功')
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
ElMessage.error('获取 AI 推荐失败')
|
ElMessage.error('获取 AI 推荐失败')
|
||||||
@@ -247,9 +291,6 @@ const getConfidenceType = (confidence: number) => {
|
|||||||
if (confidence >= 70) return 'warning'
|
if (confidence >= 70) return 'warning'
|
||||||
return 'info'
|
return 'info'
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化时设置默认日期范围为最近3天
|
|
||||||
setLast3Days()
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -56,6 +56,10 @@
|
|||||||
/>
|
/>
|
||||||
<el-button type="primary" @click="setLast3Days">3天</el-button>
|
<el-button type="primary" @click="setLast3Days">3天</el-button>
|
||||||
<el-button type="primary" @click="setLast7Days">7天</el-button>
|
<el-button type="primary" @click="setLast7Days">7天</el-button>
|
||||||
|
<el-button type="success" :loading="updating" @click="updateBidsByDateRange">
|
||||||
|
<el-icon style="margin-right: 5px"><Refresh /></el-icon>
|
||||||
|
更新
|
||||||
|
</el-button>
|
||||||
<el-select
|
<el-select
|
||||||
v-model="selectedKeywords"
|
v-model="selectedKeywords"
|
||||||
multiple
|
multiple
|
||||||
@@ -107,13 +111,33 @@ const props = defineProps<Props>()
|
|||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
crawl: []
|
crawl: []
|
||||||
refresh: []
|
refresh: []
|
||||||
|
updateBids: [startDate: string, endDate?: string, keywords?: string[]]
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
const selectedKeywords = ref<string[]>([])
|
const selectedKeywords = ref<string[]>([])
|
||||||
const dateRange = ref<[string, string] | null>(null)
|
const dateRange = ref<[string, string] | null>(null)
|
||||||
const crawling = ref(false)
|
const crawling = ref(false)
|
||||||
|
const updating = ref(false)
|
||||||
const highPriorityCollapsed = ref(false)
|
const highPriorityCollapsed = ref(false)
|
||||||
const isInitialized = ref(false)
|
const isInitialized = ref(false)
|
||||||
|
const isManualClick = ref(false)
|
||||||
|
|
||||||
|
// 从 localStorage 加载保存的日期范围
|
||||||
|
const loadSavedDateRange = () => {
|
||||||
|
const saved = localStorage.getItem('dashboard_dateRange')
|
||||||
|
if (saved) {
|
||||||
|
try {
|
||||||
|
dateRange.value = JSON.parse(saved)
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to parse saved date range:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监听日期范围变化并保存到 localStorage
|
||||||
|
watch(dateRange, (newDateRange) => {
|
||||||
|
localStorage.setItem('dashboard_dateRange', JSON.stringify(newDateRange))
|
||||||
|
}, { deep: true })
|
||||||
|
|
||||||
// 切换 High Priority Bids 的折叠状态
|
// 切换 High Priority Bids 的折叠状态
|
||||||
const toggleHighPriority = () => {
|
const toggleHighPriority = () => {
|
||||||
@@ -152,6 +176,12 @@ watch(dateRange, () => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 手动点击时不显示提示(避免和按钮点击重复)
|
||||||
|
if (isManualClick.value) {
|
||||||
|
isManualClick.value = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const totalBids = props.todayBids.length
|
const totalBids = props.todayBids.length
|
||||||
const filteredCount = filteredTodayBids.value.length
|
const filteredCount = filteredTodayBids.value.length
|
||||||
|
|
||||||
@@ -194,18 +224,9 @@ const filteredTodayBids = computed(() => {
|
|||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
|
|
||||||
// 监听筛选结果变化并显示提示
|
|
||||||
watch(filteredTodayBids, (newFilteredBids) => {
|
|
||||||
const totalBids = props.todayBids.length
|
|
||||||
const filteredCount = newFilteredBids.length
|
|
||||||
|
|
||||||
if (totalBids > 0 && filteredCount < totalBids) {
|
|
||||||
ElMessage.info(`筛选结果:共 ${filteredCount} 条数据(总共 ${totalBids} 条)`)
|
|
||||||
}
|
|
||||||
}, { deep: true })
|
|
||||||
|
|
||||||
// 设置日期范围为最近3天
|
// 设置日期范围为最近3天
|
||||||
const setLast3Days = () => {
|
const setLast3Days = async () => {
|
||||||
|
isManualClick.value = true
|
||||||
const endDate = new Date()
|
const endDate = new Date()
|
||||||
const startDate = new Date()
|
const startDate = new Date()
|
||||||
startDate.setDate(startDate.getDate() - 2) // 最近3天(包括今天)
|
startDate.setDate(startDate.getDate() - 2) // 最近3天(包括今天)
|
||||||
@@ -221,26 +242,13 @@ const setLast3Days = () => {
|
|||||||
|
|
||||||
console.log('setLast3Days called, todayBids:', props.todayBids.length, 'dateRange:', dateRange.value)
|
console.log('setLast3Days called, todayBids:', props.todayBids.length, 'dateRange:', dateRange.value)
|
||||||
|
|
||||||
// 直接计算筛选结果并显示提示(只限制开始时间,不限制结束时间)
|
// 调用更新函数
|
||||||
const start = new Date(startDate)
|
await updateBidsByDateRange()
|
||||||
start.setHours(0, 0, 0, 0)
|
|
||||||
|
|
||||||
let result = props.todayBids
|
|
||||||
result = result.filter(bid => {
|
|
||||||
if (!bid.publishDate) return false
|
|
||||||
const bidDate = new Date(bid.publishDate)
|
|
||||||
return bidDate >= start
|
|
||||||
})
|
|
||||||
|
|
||||||
const totalBids = props.todayBids.length
|
|
||||||
const filteredCount = result.length
|
|
||||||
|
|
||||||
console.log('setLast3Days result, totalBids:', totalBids, 'filteredCount:', filteredCount)
|
|
||||||
// 只在手动点击按钮时显示提示,初始化时不显示
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置日期范围为最近7天
|
// 设置日期范围为最近7天
|
||||||
const setLast7Days = () => {
|
const setLast7Days = async () => {
|
||||||
|
isManualClick.value = true
|
||||||
const endDate = new Date()
|
const endDate = new Date()
|
||||||
const startDate = new Date()
|
const startDate = new Date()
|
||||||
startDate.setDate(startDate.getDate() - 6) // 最近7天(包括今天)
|
startDate.setDate(startDate.getDate() - 6) // 最近7天(包括今天)
|
||||||
@@ -256,22 +264,38 @@ const setLast7Days = () => {
|
|||||||
|
|
||||||
console.log('setLast7Days called, todayBids:', props.todayBids.length, 'dateRange:', dateRange.value)
|
console.log('setLast7Days called, todayBids:', props.todayBids.length, 'dateRange:', dateRange.value)
|
||||||
|
|
||||||
// 直接计算筛选结果并显示提示(只限制开始时间,不限制结束时间)
|
// 调用更新函数
|
||||||
const start = new Date(startDate)
|
await updateBidsByDateRange()
|
||||||
start.setHours(0, 0, 0, 0)
|
}
|
||||||
|
|
||||||
let result = props.todayBids
|
// 根据日期范围更新投标信息
|
||||||
result = result.filter(bid => {
|
const updateBidsByDateRange = async () => {
|
||||||
if (!bid.publishDate) return false
|
if (!dateRange.value || dateRange.value.length !== 2) {
|
||||||
const bidDate = new Date(bid.publishDate)
|
ElMessage.warning('请先选择日期范围')
|
||||||
return bidDate >= start
|
return
|
||||||
})
|
}
|
||||||
|
|
||||||
const totalBids = props.todayBids.length
|
updating.value = true
|
||||||
const filteredCount = result.length
|
try {
|
||||||
|
const [startDate, endDate] = dateRange.value
|
||||||
|
|
||||||
console.log('setLast7Days result, totalBids:', totalBids, 'filteredCount:', filteredCount)
|
// 检查 endDate 是否是今天
|
||||||
// 只在手动点击按钮时显示提示,初始化时不显示
|
const today = new Date()
|
||||||
|
const todayStr = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`
|
||||||
|
|
||||||
|
// 如果 endDate 是今天,则不传递 endDate 参数(不限制截止时间)
|
||||||
|
if (endDate === todayStr) {
|
||||||
|
emit('updateBids', startDate, undefined, selectedKeywords.value)
|
||||||
|
} else {
|
||||||
|
emit('updateBids', startDate, endDate, selectedKeywords.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
ElMessage.success('更新成功')
|
||||||
|
} catch (error) {
|
||||||
|
ElMessage.error('更新失败')
|
||||||
|
} finally {
|
||||||
|
updating.value = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleCrawl = async () => {
|
const handleCrawl = async () => {
|
||||||
@@ -291,11 +315,14 @@ const handleCrawl = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化时加载保存的关键字
|
// 初始化时加载保存的关键字和日期范围
|
||||||
loadSavedKeywords()
|
loadSavedKeywords()
|
||||||
|
loadSavedDateRange()
|
||||||
|
|
||||||
// 初始化时设置默认日期范围为最近3天
|
// 如果没有保存的日期范围,则设置默认为最近3天
|
||||||
|
if (!dateRange.value) {
|
||||||
setLast3Days()
|
setLast3Days()
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -55,6 +55,7 @@
|
|||||||
"@types/jest": "^30.0.0",
|
"@types/jest": "^30.0.0",
|
||||||
"@types/node": "^22.10.7",
|
"@types/node": "^22.10.7",
|
||||||
"@types/supertest": "^6.0.2",
|
"@types/supertest": "^6.0.2",
|
||||||
|
"concurrently": "^9.2.1",
|
||||||
"eslint": "^9.18.0",
|
"eslint": "^9.18.0",
|
||||||
"eslint-config-prettier": "^10.0.1",
|
"eslint-config-prettier": "^10.0.1",
|
||||||
"eslint-plugin-prettier": "^5.2.2",
|
"eslint-plugin-prettier": "^5.2.2",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Controller, Post, Body } from '@nestjs/common';
|
import { Controller, Post, Body, Get } from '@nestjs/common';
|
||||||
import { AiService } from './ai.service';
|
import { AiService, AIRecommendation } from './ai.service';
|
||||||
|
|
||||||
export class BidDataDto {
|
export class BidDataDto {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -9,6 +9,10 @@ export class BidsRequestDto {
|
|||||||
bids: BidDataDto[];
|
bids: BidDataDto[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class SaveRecommendationsDto {
|
||||||
|
recommendations: AIRecommendation[];
|
||||||
|
}
|
||||||
|
|
||||||
@Controller('api/ai')
|
@Controller('api/ai')
|
||||||
export class AiController {
|
export class AiController {
|
||||||
constructor(private readonly aiService: AiService) {}
|
constructor(private readonly aiService: AiService) {}
|
||||||
@@ -17,4 +21,15 @@ export class AiController {
|
|||||||
async getRecommendations(@Body() request: BidsRequestDto) {
|
async getRecommendations(@Body() request: BidsRequestDto) {
|
||||||
return this.aiService.getRecommendations(request.bids);
|
return this.aiService.getRecommendations(request.bids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Post('save-recommendations')
|
||||||
|
async saveRecommendations(@Body() request: SaveRecommendationsDto) {
|
||||||
|
await this.aiService.saveRecommendations(request.recommendations);
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('latest-recommendations')
|
||||||
|
async getLatestRecommendations() {
|
||||||
|
return this.aiService.getLatestRecommendations();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { AiController } from './ai.controller';
|
import { AiController } from './ai.controller';
|
||||||
import { AiService } from './ai.service';
|
import { AiService } from './ai.service';
|
||||||
|
import { AiRecommendation } from './entities/ai-recommendation.entity';
|
||||||
|
import { BidItem } from '../bids/entities/bid-item.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule],
|
imports: [
|
||||||
|
ConfigModule,
|
||||||
|
TypeOrmModule.forFeature([AiRecommendation, BidItem]),
|
||||||
|
],
|
||||||
controllers: [AiController],
|
controllers: [AiController],
|
||||||
providers: [AiService],
|
providers: [AiService],
|
||||||
exports: [AiService],
|
exports: [AiService],
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
import OpenAI from 'openai';
|
import OpenAI from 'openai';
|
||||||
import { PromptString } from './Prompt';
|
import { PromptString } from './Prompt';
|
||||||
|
import { AiRecommendation as AiRecommendationEntity } from './entities/ai-recommendation.entity';
|
||||||
|
import { BidItem } from '../bids/entities/bid-item.entity';
|
||||||
|
|
||||||
export interface BidDataDto {
|
export interface BidDataDto {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -12,6 +16,7 @@ export interface AIRecommendation {
|
|||||||
url: string;
|
url: string;
|
||||||
source: string;
|
source: string;
|
||||||
confidence: number;
|
confidence: number;
|
||||||
|
publishDate?: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -19,11 +24,22 @@ export class AiService {
|
|||||||
private readonly logger = new Logger(AiService.name);
|
private readonly logger = new Logger(AiService.name);
|
||||||
private openai: OpenAI;
|
private openai: OpenAI;
|
||||||
|
|
||||||
constructor(private readonly configService: ConfigService) {
|
constructor(
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
@InjectRepository(AiRecommendationEntity)
|
||||||
|
private readonly aiRecommendationRepository: Repository<AiRecommendationEntity>,
|
||||||
|
@InjectRepository(BidItem)
|
||||||
|
private readonly bidItemRepository: Repository<BidItem>,
|
||||||
|
) {
|
||||||
const apiKey = this.configService.get<string>('ARK_API_KEY');
|
const apiKey = this.configService.get<string>('ARK_API_KEY');
|
||||||
|
// this.openai = new OpenAI({
|
||||||
|
// apiKey: apiKey || '',
|
||||||
|
// baseURL: 'https://ark.cn-beijing.volces.com/api/v3',
|
||||||
|
// timeout: 120000, // 120秒超时
|
||||||
|
// });
|
||||||
this.openai = new OpenAI({
|
this.openai = new OpenAI({
|
||||||
apiKey: apiKey || '',
|
apiKey: 'sk-5sSOxrJl31MGz76bE14d2fDbA55b44869fCcA0C813Fc893a' ,
|
||||||
baseURL: 'https://ark.cn-beijing.volces.com/api/v3',
|
baseURL: 'https://aihubmix.com/v1',
|
||||||
timeout: 120000, // 120秒超时
|
timeout: 120000, // 120秒超时
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -33,7 +49,7 @@ export class AiService {
|
|||||||
this.logger.log(`发送给 AI 的数据数量: ${bids.length}`);
|
this.logger.log(`发送给 AI 的数据数量: ${bids.length}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const prompt =PromptString+ `请根据以下投标项目标题列表,推荐最值得关注的 5-10 个项目。请以 JSON 格式返回,格式如下:
|
const prompt =PromptString+ `请根据以下投标项目标题列表,筛选出我关心的项目。请以 JSON 格式返回,格式如下:
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"title": "项目标题",
|
"title": "项目标题",
|
||||||
@@ -45,8 +61,8 @@ export class AiService {
|
|||||||
${JSON.stringify(bids.map(b => b.title), null, 2)}`;
|
${JSON.stringify(bids.map(b => b.title), null, 2)}`;
|
||||||
this.logger.log('发给AI的内容',prompt);
|
this.logger.log('发给AI的内容',prompt);
|
||||||
const completion = await this.openai.chat.completions.create({
|
const completion = await this.openai.chat.completions.create({
|
||||||
model: 'doubao-seed-1-6-lite-251015',
|
model: 'mimo-v2-flash-free',
|
||||||
max_tokens: 32768,
|
// max_tokens: 32768,
|
||||||
reasoning_effort: 'medium',
|
reasoning_effort: 'medium',
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
@@ -80,4 +96,58 @@ ${JSON.stringify(bids.map(b => b.title), null, 2)}`;
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async saveRecommendations(recommendations: AIRecommendation[]): Promise<void> {
|
||||||
|
this.logger.log('开始保存 AI 推荐结果');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 删除所有旧的推荐
|
||||||
|
await this.aiRecommendationRepository.clear();
|
||||||
|
|
||||||
|
// 保存新的推荐结果(只保存 title 和 confidence)
|
||||||
|
const entities = recommendations.map(rec => {
|
||||||
|
const entity = new AiRecommendationEntity();
|
||||||
|
entity.title = rec.title;
|
||||||
|
entity.confidence = rec.confidence;
|
||||||
|
return entity;
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.aiRecommendationRepository.save(entities);
|
||||||
|
this.logger.log(`成功保存 ${entities.length} 条 AI 推荐结果`);
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error('保存 AI 推荐失败:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getLatestRecommendations(): Promise<AIRecommendation[]> {
|
||||||
|
this.logger.log('获取最新的 AI 推荐结果');
|
||||||
|
|
||||||
|
try {
|
||||||
|
const entities = await this.aiRecommendationRepository.find({
|
||||||
|
order: { confidence: 'DESC' }
|
||||||
|
});
|
||||||
|
|
||||||
|
// 从 bid-items 表获取 url、source 和 publishDate
|
||||||
|
const result: AIRecommendation[] = [];
|
||||||
|
for (const entity of entities) {
|
||||||
|
const bidItem = await this.bidItemRepository.findOne({
|
||||||
|
where: { title: entity.title }
|
||||||
|
});
|
||||||
|
|
||||||
|
result.push({
|
||||||
|
title: entity.title,
|
||||||
|
url: bidItem?.url || '',
|
||||||
|
source: bidItem?.source || '',
|
||||||
|
confidence: entity.confidence,
|
||||||
|
publishDate: bidItem?.publishDate
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error('获取最新 AI 推荐失败:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/ai/entities/ai-recommendation.entity.ts
Normal file
16
src/ai/entities/ai-recommendation.entity.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from 'typeorm';
|
||||||
|
|
||||||
|
@Entity('ai_recommendations')
|
||||||
|
export class AiRecommendation {
|
||||||
|
@PrimaryGeneratedColumn('uuid')
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
title: string;
|
||||||
|
|
||||||
|
@Column({ type: 'int' })
|
||||||
|
confidence: number;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
createdAt: Date;
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@ import { AiModule } from './ai/ai.module';
|
|||||||
ScheduleModule.forRoot(),
|
ScheduleModule.forRoot(),
|
||||||
ServeStaticModule.forRoot({
|
ServeStaticModule.forRoot({
|
||||||
rootPath: join(__dirname, '..', 'frontend', 'dist'),
|
rootPath: join(__dirname, '..', 'frontend', 'dist'),
|
||||||
exclude: ['/api/(.*)'],
|
exclude: ['/api/:path(*)'],
|
||||||
}),
|
}),
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
DatabaseModule,
|
DatabaseModule,
|
||||||
|
|||||||
@@ -26,7 +26,13 @@ export class BidsController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get('by-date-range')
|
@Get('by-date-range')
|
||||||
getByDateRange(@Query('startDate') startDate: string, @Query('endDate') endDate: string) {
|
getByDateRange(@Query('startDate') startDate: string, @Query('endDate') endDate?: string, @Query('keywords') keywords?: string) {
|
||||||
return this.bidsService.getBidsByDateRange(startDate, endDate);
|
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();
|
.getMany();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getBidsByDateRange(startDate?: string, endDate?: string) {
|
async getBidsByDateRange(startDate?: string, endDate?: string, keywords?: string[]) {
|
||||||
const qb = this.bidRepository.createQueryBuilder('bid');
|
const qb = this.bidRepository.createQueryBuilder('bid');
|
||||||
|
|
||||||
if (startDate) {
|
if (startDate) {
|
||||||
@@ -103,6 +103,49 @@ export class BidsService {
|
|||||||
qb.andWhere('bid.publishDate <= :endDate', { endDate: end });
|
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();
|
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,
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { BidCrawlerService } from './services/bid-crawler.service';
|
import { BidCrawlerService } from './services/bid-crawler.service';
|
||||||
import { CrawlerController } from './crawler.controller';
|
import { CrawlerController } from './crawler.controller';
|
||||||
import { BidsModule } from '../bids/bids.module';
|
import { BidsModule } from '../bids/bids.module';
|
||||||
|
import { CrawlInfoAdd } from './entities/crawl-info-add.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [BidsModule],
|
imports: [BidsModule, TypeOrmModule.forFeature([CrawlInfoAdd])],
|
||||||
controllers: [CrawlerController],
|
controllers: [CrawlerController],
|
||||||
providers: [BidCrawlerService],
|
providers: [BidCrawlerService],
|
||||||
exports: [BidCrawlerService],
|
exports: [BidCrawlerService],
|
||||||
|
|||||||
22
src/crawler/entities/crawl-info-add.entity.ts
Normal file
22
src/crawler/entities/crawl-info-add.entity.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from 'typeorm';
|
||||||
|
|
||||||
|
@Entity('crawl_info_add')
|
||||||
|
export class CrawlInfoAdd {
|
||||||
|
@PrimaryGeneratedColumn('uuid')
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
source: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
count: number;
|
||||||
|
|
||||||
|
@Column({ type: 'datetime', nullable: true })
|
||||||
|
latestPublishDate: Date | null;
|
||||||
|
|
||||||
|
@Column({ type: 'text', nullable: true })
|
||||||
|
error: string;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
createdAt: Date;
|
||||||
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
import * as puppeteer from 'puppeteer';
|
import * as puppeteer from 'puppeteer';
|
||||||
import { BidsService } from '../../bids/services/bid.service';
|
import { BidsService } from '../../bids/services/bid.service';
|
||||||
|
import { CrawlInfoAdd } from '../entities/crawl-info-add.entity';
|
||||||
import { ChdtpCrawler } from './chdtp_target';
|
import { ChdtpCrawler } from './chdtp_target';
|
||||||
import { ChngCrawler } from './chng_target';
|
import { ChngCrawler } from './chng_target';
|
||||||
import { SzecpCrawler } from './szecp_target';
|
import { SzecpCrawler } from './szecp_target';
|
||||||
@@ -22,6 +25,8 @@ export class BidCrawlerService {
|
|||||||
constructor(
|
constructor(
|
||||||
private bidsService: BidsService,
|
private bidsService: BidsService,
|
||||||
private configService: ConfigService,
|
private configService: ConfigService,
|
||||||
|
@InjectRepository(CrawlInfoAdd)
|
||||||
|
private crawlInfoRepository: Repository<CrawlInfoAdd>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async crawlAll() {
|
async crawlAll() {
|
||||||
@@ -93,6 +98,14 @@ export class BidCrawlerService {
|
|||||||
zeroDataCrawlers.push(crawler);
|
zeroDataCrawlers.push(crawler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取最新的发布日期
|
||||||
|
const latestPublishDate = results.length > 0
|
||||||
|
? results.reduce((latest, item) => {
|
||||||
|
const itemDate = new Date(item.publishDate);
|
||||||
|
return itemDate > latest ? itemDate : latest;
|
||||||
|
}, new Date(0))
|
||||||
|
: null;
|
||||||
|
|
||||||
for (const item of results) {
|
for (const item of results) {
|
||||||
await this.bidsService.createOrUpdate({
|
await this.bidsService.createOrUpdate({
|
||||||
title: item.title,
|
title: item.title,
|
||||||
@@ -102,10 +115,16 @@ export class BidCrawlerService {
|
|||||||
unit: '',
|
unit: '',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 保存爬虫统计信息到数据库
|
||||||
|
await this.saveCrawlInfo(crawler.name, results.length, latestPublishDate);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(`Error crawling ${crawler.name}: ${err.message}`);
|
this.logger.error(`Error crawling ${crawler.name}: ${err.message}`);
|
||||||
// 记录错误信息
|
// 记录错误信息
|
||||||
crawlResults[crawler.name] = { success: 0, error: err.message };
|
crawlResults[crawler.name] = { success: 0, error: err.message };
|
||||||
|
|
||||||
|
// 保存错误信息到数据库
|
||||||
|
await this.saveCrawlInfo(crawler.name, 0, null, err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,6 +150,14 @@ export class BidCrawlerService {
|
|||||||
// 更新统计结果
|
// 更新统计结果
|
||||||
crawlResults[crawler.name] = { success: results.length };
|
crawlResults[crawler.name] = { success: results.length };
|
||||||
|
|
||||||
|
// 获取最新的发布日期
|
||||||
|
const latestPublishDate = results.length > 0
|
||||||
|
? results.reduce((latest, item) => {
|
||||||
|
const itemDate = new Date(item.publishDate);
|
||||||
|
return itemDate > latest ? itemDate : latest;
|
||||||
|
}, new Date(0))
|
||||||
|
: null;
|
||||||
|
|
||||||
for (const item of results) {
|
for (const item of results) {
|
||||||
await this.bidsService.createOrUpdate({
|
await this.bidsService.createOrUpdate({
|
||||||
title: item.title,
|
title: item.title,
|
||||||
@@ -140,10 +167,16 @@ export class BidCrawlerService {
|
|||||||
unit: '',
|
unit: '',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新爬虫统计信息到数据库
|
||||||
|
await this.saveCrawlInfo(crawler.name, results.length, latestPublishDate);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(`Error retrying ${crawler.name}: ${err.message}`);
|
this.logger.error(`Error retrying ${crawler.name}: ${err.message}`);
|
||||||
// 记录错误信息
|
// 记录错误信息
|
||||||
crawlResults[crawler.name] = { success: 0, error: err.message };
|
crawlResults[crawler.name] = { success: 0, error: err.message };
|
||||||
|
|
||||||
|
// 更新错误信息到数据库
|
||||||
|
await this.saveCrawlInfo(crawler.name, 0, null, err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,4 +217,24 @@ export class BidCrawlerService {
|
|||||||
this.logger.log('='.repeat(50));
|
this.logger.log('='.repeat(50));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async saveCrawlInfo(
|
||||||
|
source: string,
|
||||||
|
count: number,
|
||||||
|
latestPublishDate: Date | null,
|
||||||
|
error?: string,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const crawlInfo = this.crawlInfoRepository.create({
|
||||||
|
source,
|
||||||
|
count,
|
||||||
|
latestPublishDate,
|
||||||
|
error,
|
||||||
|
});
|
||||||
|
await this.crawlInfoRepository.save(crawlInfo);
|
||||||
|
this.logger.log(`Saved crawl info for ${source}: ${count} items`);
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error(`Failed to save crawl info for ${source}: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
|||||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||||
import { BidItem } from '../bids/entities/bid-item.entity';
|
import { BidItem } from '../bids/entities/bid-item.entity';
|
||||||
import { Keyword } from '../keywords/keyword.entity';
|
import { Keyword } from '../keywords/keyword.entity';
|
||||||
|
import { AiRecommendation } from '../ai/entities/ai-recommendation.entity';
|
||||||
|
import { CrawlInfoAdd } from '../crawler/entities/crawl-info-add.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -16,7 +18,7 @@ import { Keyword } from '../keywords/keyword.entity';
|
|||||||
username: configService.get<string>('DATABASE_USERNAME', 'root'),
|
username: configService.get<string>('DATABASE_USERNAME', 'root'),
|
||||||
password: configService.get<string>('DATABASE_PASSWORD', 'root'),
|
password: configService.get<string>('DATABASE_PASSWORD', 'root'),
|
||||||
database: configService.get<string>('DATABASE_NAME', 'bidding'),
|
database: configService.get<string>('DATABASE_NAME', 'bidding'),
|
||||||
entities: [BidItem, Keyword],
|
entities: [BidItem, Keyword, AiRecommendation, CrawlInfoAdd],
|
||||||
synchronize: configService.get<boolean>('DATABASE_SYNCHRONIZE', true),
|
synchronize: configService.get<boolean>('DATABASE_SYNCHRONIZE', true),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user