feat:visitors analytics
This commit is contained in:
parent
39cc916259
commit
33986fe7e4
|
|
@ -2,7 +2,10 @@
|
||||||
import React, { Component, useEffect, useState } from "react";
|
import React, { Component, useEffect, useState } from "react";
|
||||||
import ReactApexChart from "react-apexcharts";
|
import ReactApexChart from "react-apexcharts";
|
||||||
import dummyData from "../../../../const/dummy.json";
|
import dummyData from "../../../../const/dummy.json";
|
||||||
import { getStatisticMonthly } from "@/services/article";
|
import {
|
||||||
|
getStatisticMonthly,
|
||||||
|
getStatisticVisitorsMonthly,
|
||||||
|
} from "@/services/article";
|
||||||
|
|
||||||
type WeekData = {
|
type WeekData = {
|
||||||
week: number;
|
week: number;
|
||||||
|
|
@ -50,14 +53,13 @@ function getRangeAcrossMonths(
|
||||||
endMonth: number,
|
endMonth: number,
|
||||||
endDay: number
|
endDay: number
|
||||||
) {
|
) {
|
||||||
const view: number[] = [];
|
const visitors: number[] = [];
|
||||||
const comment: number[] = [];
|
|
||||||
const share: number[] = [];
|
|
||||||
const labels: string[] = [];
|
const labels: string[] = [];
|
||||||
|
|
||||||
const sortedData = data.sort((a, b) => a.month - b.month);
|
const sortedData = data.sort((a, b) => a.month - b.month);
|
||||||
for (const monthData of sortedData) {
|
for (const monthData of sortedData) {
|
||||||
const { month, view: v, comment: c, share: s } = monthData;
|
const { month, data: v } = monthData;
|
||||||
|
|
||||||
if (month < startMonth || month > endMonth) continue;
|
if (month < startMonth || month > endMonth) continue;
|
||||||
|
|
||||||
|
|
@ -68,9 +70,7 @@ function getRangeAcrossMonths(
|
||||||
if (month === endMonth) endIndex = endDay - 1;
|
if (month === endMonth) endIndex = endDay - 1;
|
||||||
|
|
||||||
for (let i = startIndex; i <= endIndex; i++) {
|
for (let i = startIndex; i <= endIndex; i++) {
|
||||||
view.push(v[i]);
|
visitors.push(v[i]);
|
||||||
comment.push(c[i]);
|
|
||||||
share.push(s[i]);
|
|
||||||
|
|
||||||
const label = `${(i + 1).toString().padStart(2, "0")} - ${month
|
const label = `${(i + 1).toString().padStart(2, "0")} - ${month
|
||||||
.toString()
|
.toString()
|
||||||
|
|
@ -79,27 +79,25 @@ function getRangeAcrossMonths(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { view, comment, share, labels };
|
return { visitors, labels };
|
||||||
}
|
}
|
||||||
|
|
||||||
const ApexChartColumnVisitors = (props: {
|
const ApexChartColumnVisitors = (props: {
|
||||||
type: string;
|
type: string;
|
||||||
date: string;
|
date: string;
|
||||||
view: string[];
|
|
||||||
range: { start: any; end: any };
|
range: { start: any; end: any };
|
||||||
}) => {
|
}) => {
|
||||||
const { date, type, view, range } = props;
|
const { date, type, range } = props;
|
||||||
const [categories, setCategories] = useState<string[]>([]);
|
const [categories, setCategories] = useState<string[]>([]);
|
||||||
const [series, setSeries] = useState<{ name: string; data: number[] }[]>([]);
|
const [series, setSeries] = useState<{ name: string; data: number[] }[]>([]);
|
||||||
const [seriesComment, setSeriesComment] = useState<number[]>([]);
|
const [seriesVisitors, setSeriesVisitors] = useState<number[]>([]);
|
||||||
const [seriesView, setSeriesView] = useState<number[]>([]);
|
|
||||||
const [seriesShare, setSeriesShare] = useState<number[]>([]);
|
|
||||||
const [years, setYear] = useState("");
|
const [years, setYear] = useState("");
|
||||||
const [datas, setDatas] = useState<any>([]);
|
const [datas, setDatas] = useState<any>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
initFetch();
|
initFetch();
|
||||||
}, [date, type, view, range]);
|
}, [date, type, range]);
|
||||||
|
|
||||||
const initFetch = async () => {
|
const initFetch = async () => {
|
||||||
const splitDate = date.split(" ");
|
const splitDate = date.split(" ");
|
||||||
|
|
@ -112,10 +110,11 @@ const ApexChartColumnVisitors = (props: {
|
||||||
) {
|
) {
|
||||||
data = datas;
|
data = datas;
|
||||||
} else {
|
} else {
|
||||||
const res = await getStatisticMonthly(
|
const res = await getStatisticVisitorsMonthly(
|
||||||
type === "monthly" ? splitDate[1] : splitDateDaily
|
type === "monthly" ? splitDate[1] : splitDateDaily
|
||||||
);
|
);
|
||||||
data = res?.data?.data;
|
data = res?.data?.data;
|
||||||
|
console.log("daaaa", data);
|
||||||
setDatas(data);
|
setDatas(data);
|
||||||
}
|
}
|
||||||
const getDatas = data?.find(
|
const getDatas = data?.find(
|
||||||
|
|
@ -123,30 +122,8 @@ const ApexChartColumnVisitors = (props: {
|
||||||
a.month == Number(splitDate[0]) && a.year === Number(splitDate[1])
|
a.month == Number(splitDate[0]) && a.year === Number(splitDate[1])
|
||||||
);
|
);
|
||||||
if (getDatas) {
|
if (getDatas) {
|
||||||
const temp1 = processMonthlyData(getDatas?.comment);
|
if (type == "monthly") {
|
||||||
const temp2 = processMonthlyData(getDatas?.view);
|
setSeriesVisitors(getDatas.data);
|
||||||
const temp3 = processMonthlyData(getDatas?.share);
|
|
||||||
|
|
||||||
if (type == "weekly") {
|
|
||||||
setSeriesComment(
|
|
||||||
temp1.weeks.map((list) => {
|
|
||||||
return list.total;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
setSeriesView(
|
|
||||||
temp2.weeks.map((list) => {
|
|
||||||
return list.total;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
setSeriesShare(
|
|
||||||
temp3.weeks.map((list) => {
|
|
||||||
return list.total;
|
|
||||||
})
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
setSeriesComment(getDatas.comment);
|
|
||||||
setSeriesView(getDatas.view);
|
|
||||||
setSeriesShare(getDatas.share);
|
|
||||||
}
|
}
|
||||||
if (type === "daily") {
|
if (type === "daily") {
|
||||||
const mappedData = getRangeAcrossMonths(
|
const mappedData = getRangeAcrossMonths(
|
||||||
|
|
@ -157,9 +134,7 @@ const ApexChartColumnVisitors = (props: {
|
||||||
range.end.day
|
range.end.day
|
||||||
);
|
);
|
||||||
|
|
||||||
setSeriesComment(mappedData.comment);
|
setSeriesVisitors(mappedData.visitors);
|
||||||
setSeriesView(mappedData.view);
|
|
||||||
setSeriesShare(mappedData.share);
|
|
||||||
setCategories(mappedData.labels);
|
setCategories(mappedData.labels);
|
||||||
}
|
}
|
||||||
// if (type === "weekly") {
|
// if (type === "weekly") {
|
||||||
|
|
@ -170,7 +145,7 @@ const ApexChartColumnVisitors = (props: {
|
||||||
// setCategories(category);
|
// setCategories(category);
|
||||||
// }
|
// }
|
||||||
} else {
|
} else {
|
||||||
setSeriesComment([]);
|
setSeriesVisitors([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
setYear(type === "monthly" ? splitDate[1] : splitDateDaily);
|
setYear(type === "monthly" ? splitDate[1] : splitDateDaily);
|
||||||
|
|
@ -180,12 +155,12 @@ const ApexChartColumnVisitors = (props: {
|
||||||
const temp = [
|
const temp = [
|
||||||
{
|
{
|
||||||
name: "Visitors",
|
name: "Visitors",
|
||||||
data: view.includes("visitor") ? seriesView : [],
|
data: seriesVisitors || [],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
setSeries(temp);
|
setSeries(temp);
|
||||||
}, [view, seriesShare, seriesView, seriesComment]);
|
}, [seriesVisitors]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full">
|
<div className="h-full">
|
||||||
|
|
|
||||||
|
|
@ -1122,7 +1122,6 @@ export default function DashboardContainer() {
|
||||||
String(visitorSelectedMonth)
|
String(visitorSelectedMonth)
|
||||||
).split("-")[0]
|
).split("-")[0]
|
||||||
}`}
|
}`}
|
||||||
view={["visitor"]}
|
|
||||||
range={visitorDailyDate}
|
range={visitorDailyDate}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -233,6 +233,16 @@ export async function getStatisticMonthly(year: string) {
|
||||||
};
|
};
|
||||||
return await httpGet(`/articles/statistic/monthly?year=${year}`, headers);
|
return await httpGet(`/articles/statistic/monthly?year=${year}`, headers);
|
||||||
}
|
}
|
||||||
|
export async function getStatisticVisitorsMonthly(year: string) {
|
||||||
|
const headers = {
|
||||||
|
"content-type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
};
|
||||||
|
return await httpGet(
|
||||||
|
`/activity-logs/visitors-monthly-stats?year=${year}`,
|
||||||
|
headers
|
||||||
|
);
|
||||||
|
}
|
||||||
export async function getStatisticUsersMonthly(year: string) {
|
export async function getStatisticUsersMonthly(year: string) {
|
||||||
const headers = {
|
const headers = {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue