2024-04-15 14:32:00 +00:00
|
|
|
|
import moment from "moment";
|
2024-03-21 14:24:56 +00:00
|
|
|
|
import { Box, Paper, Typography, useMediaQuery, useTheme } from "@mui/material";
|
|
|
|
|
import { LineChart } from "@mui/x-charts";
|
|
|
|
|
|
2024-04-15 14:32:00 +00:00
|
|
|
|
import type { FC } from "react";
|
2024-03-21 14:24:56 +00:00
|
|
|
|
import type { GeneralResponse } from "@api/statistic";
|
|
|
|
|
|
2024-04-01 18:09:26 +00:00
|
|
|
|
type GeneralItemsProps = {
|
2024-03-21 14:24:56 +00:00
|
|
|
|
title: string;
|
|
|
|
|
general: Record<string, number>;
|
|
|
|
|
color: string;
|
2024-04-05 18:23:08 +00:00
|
|
|
|
numberType: "sum" | "percent" | "time";
|
2024-04-08 14:22:19 +00:00
|
|
|
|
calculateTime?: boolean;
|
2024-04-10 22:29:02 +00:00
|
|
|
|
conversionValue?: number;
|
2024-04-11 15:58:23 +00:00
|
|
|
|
day: boolean;
|
2024-03-21 14:24:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-01 18:09:26 +00:00
|
|
|
|
type GeneralProps = {
|
|
|
|
|
data: GeneralResponse | null;
|
2024-04-11 15:58:23 +00:00
|
|
|
|
day: boolean;
|
2024-04-01 18:09:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-10 23:34:40 +00:00
|
|
|
|
const getCalculatedTime = (time: number) => {
|
|
|
|
|
const hours = String(Math.floor(time / 3600)).padStart(2, "0");
|
|
|
|
|
const minutes = String(Math.floor((time % 3600) / 60)).padStart(2, "0");
|
|
|
|
|
const seconds = String(Math.floor((time % 3600) % 60)).padStart(2, "0");
|
|
|
|
|
|
|
|
|
|
return `${hours}:${minutes}:${seconds}`;
|
|
|
|
|
};
|
2024-04-15 14:32:00 +00:00
|
|
|
|
|
2024-04-01 18:09:26 +00:00
|
|
|
|
const GeneralItem = ({
|
|
|
|
|
title,
|
|
|
|
|
general,
|
|
|
|
|
color,
|
|
|
|
|
numberType,
|
2024-04-08 14:22:19 +00:00
|
|
|
|
calculateTime = false,
|
2024-04-10 22:29:02 +00:00
|
|
|
|
conversionValue,
|
2024-04-12 21:05:15 +00:00
|
|
|
|
day,
|
2024-04-01 18:09:26 +00:00
|
|
|
|
}: GeneralItemsProps) => {
|
2024-03-21 14:24:56 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(700));
|
|
|
|
|
|
|
|
|
|
const numberValue =
|
|
|
|
|
numberType === "sum"
|
|
|
|
|
? Object.values(general).reduce((total, item) => total + item, 0)
|
2024-04-10 22:29:02 +00:00
|
|
|
|
: title === "Конверсия"
|
|
|
|
|
? conversionValue
|
|
|
|
|
: 0;
|
2024-04-05 18:23:08 +00:00
|
|
|
|
|
2024-04-10 22:29:02 +00:00
|
|
|
|
if (
|
|
|
|
|
Object.keys(general).length === 0 ||
|
|
|
|
|
Object.values(general).every((x) => x === 0)
|
|
|
|
|
) {
|
2024-04-07 16:19:49 +00:00
|
|
|
|
return (
|
|
|
|
|
<Typography textAlign="center">{`${title} - нет данных`}</Typography>
|
|
|
|
|
);
|
2024-04-08 14:22:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getCalculatedTime = (time: number) => {
|
|
|
|
|
const hours = String(Math.floor(time / 3600)).padStart(2, "0");
|
|
|
|
|
const minutes = String(Math.floor((time % 3600) / 60)).padStart(2, "0");
|
|
|
|
|
const seconds = String(Math.floor((time % 3600) % 60)).padStart(2, "0");
|
|
|
|
|
|
|
|
|
|
return `${hours}:${minutes}:${seconds}`;
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-21 14:24:56 +00:00
|
|
|
|
return (
|
|
|
|
|
<Paper
|
|
|
|
|
sx={{
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
borderRadius: "12px",
|
|
|
|
|
boxShadow: "0 0 20px rgba(0, 0, 0, 0.15)",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography sx={{ margin: "20px 20px 0" }}>{title}</Typography>
|
|
|
|
|
<Typography sx={{ margin: "10px 20px 0", fontWeight: "bold" }}>
|
2024-04-15 14:32:00 +00:00
|
|
|
|
{numberType === "percent" ? `${numberValue?.toFixed(2)}%` : numberValue}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
</Typography>
|
|
|
|
|
<LineChart
|
2024-04-05 16:29:20 +00:00
|
|
|
|
xAxis={[
|
|
|
|
|
{
|
|
|
|
|
data: Object.keys(general),
|
|
|
|
|
valueFormatter: (value) =>
|
2024-04-11 15:58:23 +00:00
|
|
|
|
moment.unix(Number(value)).format("DD/MM/YYYY HH") + "ч",
|
2024-04-05 16:29:20 +00:00
|
|
|
|
},
|
|
|
|
|
]}
|
2024-04-08 14:22:19 +00:00
|
|
|
|
series={[
|
|
|
|
|
{
|
|
|
|
|
data: Object.values(general),
|
|
|
|
|
valueFormatter: (value) =>
|
2024-04-12 21:05:15 +00:00
|
|
|
|
calculateTime
|
|
|
|
|
? getCalculatedTime(value)
|
|
|
|
|
: String(value.toFixed(2)),
|
2024-04-08 14:22:19 +00:00
|
|
|
|
},
|
|
|
|
|
]}
|
2024-04-10 23:34:40 +00:00
|
|
|
|
height={220}
|
|
|
|
|
colors={[color]}
|
|
|
|
|
sx={{
|
|
|
|
|
transform: isMobile ? "scale(1.1)" : "scale(1.2)",
|
|
|
|
|
"& .MuiChartsAxis-tickContainer": { display: "none" },
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Paper>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-04-15 14:32:00 +00:00
|
|
|
|
|
2024-04-10 23:34:40 +00:00
|
|
|
|
const GeneralItemTimeConv = ({
|
|
|
|
|
title,
|
|
|
|
|
general,
|
|
|
|
|
color,
|
|
|
|
|
numberType,
|
|
|
|
|
calculateTime = false,
|
|
|
|
|
conversionValue,
|
|
|
|
|
}: GeneralItemsProps) => {
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(700));
|
|
|
|
|
|
2024-04-25 06:09:35 +00:00
|
|
|
|
const data = Object.entries(general).sort((a, b) => a[0] - b[0]);
|
2024-04-17 16:45:06 +00:00
|
|
|
|
|
2024-04-25 06:09:35 +00:00
|
|
|
|
const days = [...data].map((e) => e[0]);
|
2024-04-17 16:45:06 +00:00
|
|
|
|
|
2024-04-25 06:09:35 +00:00
|
|
|
|
let buffer = 0;
|
2024-04-17 16:45:06 +00:00
|
|
|
|
|
2024-04-25 06:09:35 +00:00
|
|
|
|
const time = [...data].map((e) => {
|
2024-04-17 16:45:06 +00:00
|
|
|
|
if (e[1] > 0) {
|
2024-04-25 06:09:35 +00:00
|
|
|
|
buffer = e[1];
|
2024-04-17 16:45:06 +00:00
|
|
|
|
}
|
2024-04-25 06:09:35 +00:00
|
|
|
|
return buffer;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log("data", data);
|
|
|
|
|
console.log(
|
|
|
|
|
"time",
|
|
|
|
|
time.reduce((a, b) => Number(a) + Number(b), 0),
|
|
|
|
|
);
|
|
|
|
|
console.log(
|
|
|
|
|
"time",
|
|
|
|
|
getCalculatedTime(time.reduce((a, b) => Number(a) + Number(b), 0)),
|
|
|
|
|
);
|
|
|
|
|
console.log("days", days.length);
|
|
|
|
|
const numberValue = calculateTime
|
|
|
|
|
? time.reduce((a, b) => Number(a) + Number(b), 0) / days.length || 0
|
|
|
|
|
: conversionValue;
|
2024-04-10 23:34:40 +00:00
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
Object.keys(general).length === 0 ||
|
2024-04-17 16:45:06 +00:00
|
|
|
|
Object.values(general).every((x) => x === 0)
|
2024-04-10 23:34:40 +00:00
|
|
|
|
) {
|
|
|
|
|
return (
|
|
|
|
|
<Typography textAlign="center">{`${title} - нет данных`}</Typography>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Paper
|
|
|
|
|
sx={{
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
borderRadius: "12px",
|
|
|
|
|
boxShadow: "0 0 20px rgba(0, 0, 0, 0.15)",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography sx={{ margin: "20px 20px 0" }}>{title}</Typography>
|
|
|
|
|
<Typography sx={{ margin: "10px 20px 0", fontWeight: "bold" }}>
|
2024-04-25 06:09:35 +00:00
|
|
|
|
{calculateTime
|
|
|
|
|
? `${getCalculatedTime(numberValue)} с`
|
|
|
|
|
: `${numberValue.toFixed(2)}%`}
|
2024-04-10 23:34:40 +00:00
|
|
|
|
</Typography>
|
|
|
|
|
<LineChart
|
|
|
|
|
xAxis={[
|
|
|
|
|
{
|
|
|
|
|
data: days,
|
2024-04-12 21:05:15 +00:00
|
|
|
|
valueFormatter: (value) =>
|
2024-04-17 16:45:06 +00:00
|
|
|
|
moment.utc(Number(value) * 1000).format("DD/MM/YYYY"),
|
2024-04-10 23:34:40 +00:00
|
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
series={[
|
|
|
|
|
{
|
|
|
|
|
data: Object.values(time),
|
2024-04-17 16:45:06 +00:00
|
|
|
|
valueFormatter: (value) => {
|
2024-04-25 06:09:35 +00:00
|
|
|
|
console.log("log", value);
|
|
|
|
|
return calculateTime
|
|
|
|
|
? getCalculatedTime(value)
|
|
|
|
|
: String((value * 100).toFixed(2)) + "%";
|
|
|
|
|
},
|
2024-04-10 23:34:40 +00:00
|
|
|
|
},
|
|
|
|
|
]}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
height={220}
|
|
|
|
|
colors={[color]}
|
|
|
|
|
sx={{
|
|
|
|
|
transform: isMobile ? "scale(1.1)" : "scale(1.2)",
|
|
|
|
|
"& .MuiChartsAxis-tickContainer": { display: "none" },
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Paper>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-11 15:58:23 +00:00
|
|
|
|
export const General: FC<GeneralProps> = ({ data, day }) => {
|
2024-03-21 14:24:56 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
|
|
|
|
const isMobile = useMediaQuery(theme.breakpoints.down(700));
|
|
|
|
|
|
2024-04-01 18:09:26 +00:00
|
|
|
|
if (!data) {
|
2024-03-30 18:54:28 +00:00
|
|
|
|
return (
|
|
|
|
|
<Typography textAlign="center" m="10px 0">
|
|
|
|
|
нет данных о ключевых метриках
|
|
|
|
|
</Typography>
|
|
|
|
|
);
|
2024-04-01 18:09:26 +00:00
|
|
|
|
}
|
2024-04-11 15:58:23 +00:00
|
|
|
|
|
2024-04-12 21:05:15 +00:00
|
|
|
|
const currentDate = moment().unix();
|
|
|
|
|
|
2024-04-15 14:32:00 +00:00
|
|
|
|
const generalResponse = Object.entries(data).reduce(
|
|
|
|
|
(total, [fatherKey, values]) => {
|
|
|
|
|
const value = Object.keys(values).reduce((totalValue, key) => {
|
|
|
|
|
if (Number(key) - currentDate < 0) {
|
|
|
|
|
return { ...totalValue, [key]: values[key] };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return totalValue;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
return { ...total, [fatherKey]: value };
|
|
|
|
|
},
|
|
|
|
|
{} as GeneralResponse,
|
|
|
|
|
);
|
2024-04-11 15:58:23 +00:00
|
|
|
|
|
2024-04-15 14:32:00 +00:00
|
|
|
|
const resultSum = Object.values(generalResponse.Result).reduce(
|
|
|
|
|
(total, item) => total + item,
|
|
|
|
|
0,
|
|
|
|
|
);
|
|
|
|
|
const openSum = Object.values(generalResponse.Open).reduce(
|
|
|
|
|
(total, item) => total + item,
|
|
|
|
|
0,
|
|
|
|
|
);
|
|
|
|
|
const conversionValue = (resultSum / openSum) * 100;
|
2024-04-08 14:22:19 +00:00
|
|
|
|
|
2024-03-21 14:24:56 +00:00
|
|
|
|
return (
|
|
|
|
|
<Box sx={{ marginTop: "45px" }}>
|
|
|
|
|
<Typography
|
|
|
|
|
component="h3"
|
|
|
|
|
sx={{
|
|
|
|
|
fontSize: "24px",
|
|
|
|
|
fontWeight: "bold",
|
|
|
|
|
color: theme.palette.text.primary,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Ключевые метрики
|
|
|
|
|
</Typography>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "grid",
|
|
|
|
|
gridTemplateColumns: isTablet
|
|
|
|
|
? isMobile
|
|
|
|
|
? "1fr"
|
|
|
|
|
: "1fr 1fr"
|
|
|
|
|
: "1fr 1fr 1fr",
|
|
|
|
|
gap: "20px",
|
|
|
|
|
marginTop: "40px",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<GeneralItem
|
|
|
|
|
title="Открыли квиз"
|
|
|
|
|
numberType="sum"
|
2024-04-15 14:32:00 +00:00
|
|
|
|
general={generalResponse.Open}
|
|
|
|
|
color="#61BB1A"
|
2024-04-11 15:58:23 +00:00
|
|
|
|
day={day}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
/>
|
|
|
|
|
<GeneralItem
|
|
|
|
|
title="Получено заявок"
|
|
|
|
|
numberType="sum"
|
2024-04-15 14:32:00 +00:00
|
|
|
|
general={generalResponse.Result}
|
|
|
|
|
color="#7E2AEA"
|
2024-04-11 15:58:23 +00:00
|
|
|
|
day={day}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
/>
|
2024-04-10 23:34:40 +00:00
|
|
|
|
<GeneralItemTimeConv
|
2024-03-21 14:24:56 +00:00
|
|
|
|
title="Конверсия"
|
|
|
|
|
numberType="percent"
|
2024-04-10 22:29:02 +00:00
|
|
|
|
conversionValue={conversionValue}
|
2024-04-15 14:32:00 +00:00
|
|
|
|
general={generalResponse.Conversion}
|
|
|
|
|
color="#FB5607"
|
2024-04-11 15:58:23 +00:00
|
|
|
|
day={day}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
/>
|
2024-04-10 23:34:40 +00:00
|
|
|
|
<GeneralItemTimeConv
|
2024-03-21 14:24:56 +00:00
|
|
|
|
title="Среднее время прохождения квиза"
|
2024-04-05 18:23:08 +00:00
|
|
|
|
numberType="time"
|
2024-04-08 14:22:19 +00:00
|
|
|
|
calculateTime
|
2024-04-15 14:32:00 +00:00
|
|
|
|
general={generalResponse.AvTime}
|
|
|
|
|
color="#0886FB"
|
2024-04-11 15:58:23 +00:00
|
|
|
|
day={day}
|
2024-03-21 14:24:56 +00:00
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
};
|