feat:iothome

pull/764/head
alwayssuper 2025-04-15 11:27:41 +08:00
parent feab54c11e
commit 89a8b25f17
4 changed files with 107 additions and 41 deletions

View File

@ -16,10 +16,15 @@ export interface IotStatisticsSummaryRespVO {
productCategoryDeviceCounts: Record<string, number>
}
/** 时间戳-数值的键值对类型 */
interface TimeValueItem {
[key: string]: number
}
/** IoT 消息统计数据类型 */
export interface IotStatisticsDeviceMessageSummaryRespVO {
upstreamCounts: Record<number, number>
downstreamCounts: Record<number, number>
upstreamCounts: TimeValueItem[]
downstreamCounts: TimeValueItem[]
}
// IoT 数据统计 API

View File

@ -330,3 +330,30 @@ export function getDateRange(
dayjs(endDate).endOf('d').format('YYYY-MM-DD HH:mm:ss')
]
}
/**
*
* @param hours
* @returns
*/
export function getHoursAgo(hours: number): number {
return dayjs().subtract(hours, 'hour').valueOf()
}
/**
*
* @param range '8h' | '24h' | '7d'
* @returns
*/
export function getTimeRangeStart(range: '8h' | '24h' | '7d'): number {
switch (range) {
case '8h':
return getHoursAgo(8)
case '24h':
return getHoursAgo(24)
case '7d':
return dayjs().subtract(7, 'day').valueOf()
default:
return dayjs().valueOf()
}
}

View File

@ -32,8 +32,9 @@ import { CanvasRenderer } from 'echarts/renderers'
import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import { UniversalTransition } from 'echarts/features'
import { IotStatisticsDeviceMessageSummaryRespVO } from '@/api/iot/statistics'
import { formatDate } from '@/utils/formatTime'
import { formatDate, getTimeRangeStart } from '@/utils/formatTime'
import type { PropType } from 'vue'
import dayjs from 'dayjs'
/** 消息趋势统计卡片 */
defineOptions({ name: 'MessageTrendCard' })
@ -48,27 +49,14 @@ const props = defineProps({
const emit = defineEmits(['timeRangeChange'])
const timeRange = ref('7d')
const dateRange = ref<[Date, Date] | null>(null)
const dateRange = ref<any>(null)
const messageChartRef = ref()
// TODO @super dayjs 1h24h7d utils/formatTime.ts
//
const handleTimeRangeChange = (range: string) => {
const now = Date.now()
let startTime: number
switch (range) {
case '8h':
startTime = now - 8 * 60 * 60 * 1000
break
case '24h':
startTime = now - 24 * 60 * 60 * 1000
break
case '7d':
startTime = now - 7 * 24 * 60 * 60 * 1000
break
default:
return
}
const now = dayjs().valueOf()
const startTime = getTimeRangeStart(range as '8h' | '24h' | '7d')
dateRange.value = null
emit('timeRangeChange', { startTime, endTime: now })
@ -96,31 +84,75 @@ const initChart = () => {
UniversalTransition
])
const timestamps = Array.from(
new Set([
...props.messageStats.upstreamCounts.map((item) => Number(Object.keys(item)[0])),
...props.messageStats.downstreamCounts.map((item) => Number(Object.keys(item)[0]))
])
).sort((a, b) => a - b) //
//
const xdata = timestamps.map((ts) => formatDate(ts, 'YYYY-MM-DD HH:mm'))
const upData = timestamps.map((ts) => {
const item = props.messageStats.upstreamCounts.find(
(count) => Number(Object.keys(count)[0]) === ts
)
return item ? Object.values(item)[0] : 0
})
const downData = timestamps.map((ts) => {
const item = props.messageStats.downstreamCounts.find(
(count) => Number(Object.keys(count)[0]) === ts
)
return item ? Object.values(item)[0] : 0
})
//
const upstreamCounts = Array.isArray(props.messageStats.upstreamCounts)
? props.messageStats.upstreamCounts
: Object.entries(props.messageStats.upstreamCounts || {}).map(([key, value]) => ({ [key]: value }))
const downstreamCounts = Array.isArray(props.messageStats.downstreamCounts)
? props.messageStats.downstreamCounts
: Object.entries(props.messageStats.downstreamCounts || {}).map(([key, value]) => ({ [key]: value }))
//
let timestamps: number[] = []
try {
//
if (Array.isArray(upstreamCounts) && upstreamCounts.length > 0) {
timestamps = Array.from(
new Set([
...upstreamCounts.map(item => Number(Object.keys(item)[0])),
...downstreamCounts.map(item => Number(Object.keys(item)[0]))
])
).sort((a, b) => a - b)
} else {
//
const upKeys = Object.keys(props.messageStats.upstreamCounts || {}).map(Number)
const downKeys = Object.keys(props.messageStats.downstreamCounts || {}).map(Number)
timestamps = Array.from(new Set([...upKeys, ...downKeys])).sort((a, b) => a - b)
}
} catch (error) {
console.error('提取时间戳出错:', error)
timestamps = []
}
//
const xdata = timestamps.map((ts) => formatDate(dayjs(ts).toDate(), 'YYYY-MM-DD HH:mm'))
let upData: number[] = []
let downData: number[] = []
try {
//
if (Array.isArray(upstreamCounts) && upstreamCounts.length > 0) {
upData = timestamps.map((ts) => {
const item = upstreamCounts.find(count =>
Number(Object.keys(count)[0]) === ts
)
return item ? Number(Object.values(item)[0]) : 0
})
downData = timestamps.map((ts) => {
const item = downstreamCounts.find(count =>
Number(Object.keys(count)[0]) === ts
)
return item ? Number(Object.values(item)[0]) : 0
})
} else {
//
const upstreamObj = props.messageStats.upstreamCounts || {}
const downstreamObj = props.messageStats.downstreamCounts || {}
upData = timestamps.map((ts) => Number(upstreamObj[ts as keyof typeof upstreamObj] || 0))
downData = timestamps.map((ts) => Number(downstreamObj[ts as keyof typeof downstreamObj] || 0))
}
} catch (error) {
console.error('提取数据出错:', error)
upData = []
downData = []
}
console.log(xdata, upData, downData)
//
const chart = echarts.init(messageChartRef.value)

View File

@ -124,6 +124,8 @@ const getStats = async () => {
statsData.value = await ProductCategoryApi.getIotStatisticsSummary()
//
messageStats.value = await ProductCategoryApi.getIotStatisticsDeviceMessageSummary(queryParams)
console.log('statsData', statsData.value)
console.log('messageStats', messageStats.value)
}
/** 初始化 */