frontPanel/src/api/statistic.ts

115 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-05-13 13:24:41 +00:00
import { makeRequest } from "@api/makeRequest";
2024-03-20 14:44:51 +00:00
2024-03-21 14:24:56 +00:00
import { parseAxiosError } from "@utils/parse-error";
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[];
2024-04-09 10:56:43 +00:00
FunnelData: number[];
2024-04-03 14:22:51 +00:00
Results: Record<string, number>;
Questions: Record<string, Record<string, number>>;
2024-03-21 14:24:56 +00:00
};
2024-06-24 19:58:06 +00:00
export type GraphicsResponse = unknown;
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
};
2024-05-15 11:44:10 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz/statistic`;
2024-05-13 13:24:41 +00:00
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-05-13 13:24:41 +00:00
url: `${API_URL}/${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",
2024-05-13 13:24:41 +00:00
url: `${API_URL}/${quizId}/general`,
2024-03-21 14:24:56 +00:00
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",
2024-05-13 13:24:41 +00:00
url: `${API_URL}/${quizId}/questions`,
2024-03-21 14:24:56 +00:00
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
}
};
2024-06-24 19:58:06 +00:00
export const getGraphics = async (
quizId: string,
to: number,
from: number,
): Promise<[GraphicsResponse | null, string?]> => {
try {
const questionsResponse = await makeRequest<TRequest, QuestionsResponse>({
method: "get",
url: `${API_URL}/${quizId}/pipelines?from=${from}&to=${to}`,
withCredentials: true,
});
console.log(questionsResponse)
return [questionsResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось получить статистику по результатам. ${error}`];
}
};