Skip to content

Commit

Permalink
fix: 近三天访问 统计数据修正
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq committed Oct 31, 2022
1 parent 565c1f2 commit c8daddc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
25 changes: 13 additions & 12 deletions server/src/controller/stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import logger from '../logger';
import uaparser from 'ua-parser-js';
import { stat } from '@prisma/client';
import { uniqBy } from 'lodash';
import dayjs from 'dayjs';
import { getISOString } from '../utils/helper';
import { wrapDayjs } from '../utils/helper';

const IP_PREFIX = 'ip_';

Expand All @@ -35,8 +34,8 @@ export class StatController {
const list = await prisma.stat.findMany({
where: {
createdAt: {
lte: getISOString(endAt),
gte: getISOString(startAt),
lte: wrapDayjs(endAt).toISOString(),
gte: wrapDayjs(startAt).toISOString(),
},
},
orderBy: [
Expand Down Expand Up @@ -69,12 +68,11 @@ export class StatController {
}
return acc;
}, {});

const list7 = await prisma.stat.findMany({
where: {
createdAt: {
lte: getISOString(endAt),
gte: getISOString(dayjs(endAt).subtract(3, 'd')),
lte: wrapDayjs(endAt).toISOString(),
gte: wrapDayjs(endAt).subtract(3, 'd').add(1, 's').toISOString(),
},
},
distinct: ['ip'],
Expand All @@ -89,22 +87,25 @@ export class StatController {
date: string;
value: number;
}

const uvStats: UvStat[] = [
{ date: dayjs(endAt).format('YYYY-MM-DD'), value: 0 },
{ date: wrapDayjs(endAt).format('YYYY-MM-DD'), value: 0 },

{
date: dayjs(endAt).subtract(1, 'd').format('YYYY-MM-DD'),
date: wrapDayjs(endAt).subtract(1, 'd').format('YYYY-MM-DD'),
value: 0,
},
{
date: dayjs(endAt).subtract(2, 'd').format('YYYY-MM-DD'),
date: wrapDayjs(endAt).subtract(2, 'd').format('YYYY-MM-DD'),
value: 0,
},
];

list7.forEach((stat) => {
const { createdAt } = stat;
const diff = dayjs(endAt).diff(createdAt, 'd');
const diff = wrapDayjs(endAt).diff(createdAt, 'd');
console.log(diff, createdAt);

uvStats[diff].value++;
});

Expand Down Expand Up @@ -169,7 +170,7 @@ export class StatController {
province,
isp,
userAgent,
createdAt: getISOString(createdAt),
createdAt: wrapDayjs(createdAt).toISOString(),
},
});
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions server/src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import dayjs from 'dayjs';
import dayjs, { Dayjs } from 'dayjs';

type rawType = 'Object' | 'Array' | 'Number' | 'String';

Expand All @@ -16,6 +16,6 @@ export function isType(...args: rawType[]) {
* @param {Parameters<typeof dayjs>[0]} date
* @return {*}
*/
export function getISOString(date: Parameters<typeof dayjs>[0]) {
return dayjs(date).add(8, 'h').toISOString();
export function wrapDayjs(date: Parameters<typeof dayjs>[0]): Dayjs {
return dayjs(date).add(8, 'h');
}

0 comments on commit c8daddc

Please sign in to comment.