77 lines
1.8 KiB
JavaScript
77 lines
1.8 KiB
JavaScript
import analysisDao from '../dao/analysis-dao';
|
|
import orm from '../entity/orm';
|
|
import email from '../entity/email';
|
|
import { desc, count, eq, and, ne, isNotNull } from 'drizzle-orm';
|
|
import { emailConst } from '../const/entity-const';
|
|
import kvConst from '../const/kv-const';
|
|
import dayjs from 'dayjs';
|
|
import { toUtc } from '../utils/date-uitil';
|
|
const analysisService = {
|
|
|
|
async echarts(c) {
|
|
|
|
|
|
const [
|
|
numberCount,
|
|
nameRatio,
|
|
userDayCountRaw,
|
|
receiveDayCountRaw,
|
|
sendDayCountRaw,
|
|
daySendTotalRaw
|
|
] = await Promise.all([
|
|
analysisDao.numberCount(c),
|
|
|
|
orm(c)
|
|
.select({ name: email.name, total: count() })
|
|
.from(email)
|
|
.where(and(eq(email.type, emailConst.type.RECEIVE), isNotNull(email.name), ne(email.name,'')))
|
|
.groupBy(email.name)
|
|
.orderBy(desc(count()))
|
|
.limit(6),
|
|
|
|
|
|
analysisDao.userDayCount(c),
|
|
analysisDao.receiveDayCount(c),
|
|
analysisDao.sendDayCount(c),
|
|
|
|
c.env.kv.get(kvConst.SEND_DAY_COUNT + dayjs().format('YYYY-MM-DD')),
|
|
]);
|
|
|
|
|
|
const userDayCount = this.filterEmptyDay(userDayCountRaw);
|
|
const receiveDayCount = this.filterEmptyDay(receiveDayCountRaw);
|
|
const sendDayCount = this.filterEmptyDay(sendDayCountRaw);
|
|
|
|
const daySendTotal = daySendTotalRaw || 0;
|
|
|
|
return {
|
|
numberCount,
|
|
userDayCount,
|
|
receiveRatio: {
|
|
nameRatio
|
|
},
|
|
emailDayCount: {
|
|
receiveDayCount,
|
|
sendDayCount
|
|
},
|
|
daySendTotal: Number(daySendTotal)
|
|
};
|
|
},
|
|
|
|
filterEmptyDay(data) {
|
|
const today = toUtc().tz('Asia/Shanghai').subtract(1, 'day');
|
|
const previousDays = Array.from({ length: 15 }, (_, i) => {
|
|
return today.subtract(i, 'day').format('YYYY-MM-DD');
|
|
}).reverse();
|
|
|
|
return previousDays.map(day => {
|
|
const index = data.findIndex(item => item.date === day)
|
|
const total = index > - 1 ? data[index].total : 0
|
|
return {date: day,total}
|
|
})
|
|
|
|
}
|
|
}
|
|
|
|
export default analysisService
|