frontPanel/src/api/statistic.ts

93 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-03-20 14:44:51 +00:00
import { makeRequest } from "@frontend/kitui";
2024-03-21 14:24:56 +00:00
import { parseAxiosError } from "@utils/parse-error";
2024-03-20 14:44:51 +00:00
2024-03-21 14:24:56 +00:00
const apiUrl = process.env.REACT_APP_DOMAIN + "/squiz/statistic";
2024-03-20 14:44:51 +00:00
export type DevicesResponse = {
2024-04-03 14:22:51 +00:00
Device: Record<string, number>;
OS: Record<string, number>;
Browser: Record<string, number>;
2024-03-20 14:44:51 +00:00
};
2024-03-21 14:24:56 +00:00
export type GeneralResponse = {
2024-04-03 14:22:51 +00:00
Open: Record<string, number>;
Result: Record<string, number>;
AvTime: Record<string, number>;
Conversion: Record<string, number>;
2024-03-21 14:24:56 +00:00
};
export type QuestionsResponse = {
2024-04-03 14:22:51 +00:00
Funnel: number[];
Results: Record<string, number>;
Questions: Record<string, Record<string, number>>;
2024-03-21 14:24:56 +00:00
};
2024-04-01 18:09:26 +00:00
type TRequest = {
to: number;
from: number;
2024-03-21 14:24:56 +00:00
};
export const getDevices = async (
2024-03-20 14:44:51 +00:00
quizId: string,
2024-03-28 09:18:24 +00:00
to: number,
from: number,
2024-03-21 14:24:56 +00:00
): Promise<[DevicesResponse | null, string?]> => {
2024-03-20 14:44:51 +00:00
try {
2024-04-01 18:09:26 +00:00
const devicesResponse = await makeRequest<TRequest, DevicesResponse>({
2024-03-20 14:44:51 +00:00
method: "POST",
2024-03-21 14:24:56 +00:00
url: `${apiUrl}/${quizId}/devices`,
2024-03-20 14:44:51 +00:00
withCredentials: true,
body: { to, from },
2024-03-20 14:44:51 +00:00
});
return [devicesResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2024-03-21 14:24:56 +00:00
return [null, `Не удалось получить статистику о девайсах. ${error}`];
}
};
export const getGeneral = async (
quizId: string,
2024-03-28 09:18:24 +00:00
to: number,
from: number,
2024-03-21 14:24:56 +00:00
): Promise<[GeneralResponse | null, string?]> => {
try {
2024-04-01 18:09:26 +00:00
const generalResponse = await makeRequest<TRequest, GeneralResponse>({
2024-03-21 14:24:56 +00:00
method: "POST",
url: `${apiUrl}/${quizId}/general`,
withCredentials: true,
body: { to, from },
2024-03-21 14:24:56 +00:00
});
return [generalResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось получить ключевые метрики. ${error}`];
}
};
export const getQuestions = async (
quizId: string,
2024-03-28 09:18:24 +00:00
to: number,
from: number,
2024-03-21 14:24:56 +00:00
): Promise<[QuestionsResponse | null, string?]> => {
try {
2024-04-01 18:09:26 +00:00
const questionsResponse = await makeRequest<TRequest, QuestionsResponse>({
2024-03-21 14:24:56 +00:00
method: "POST",
url: `${apiUrl}/${quizId}/questions`,
withCredentials: true,
body: { to, from },
2024-03-21 14:24:56 +00:00
});
return [questionsResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось получить статистику по результатам. ${error}`];
2024-03-20 14:44:51 +00:00
}
};