Files
HellreigN/frontend/src/modules/agent/api/agent.api.service.ts
T
nikita 915aa7018a
ci-front / build (push) Successful in 3m39s
feat: graph 2
2026-04-05 09:19:39 +03:00

182 lines
5.1 KiB
TypeScript

import { apiClient } from "@/shared/api/axios.instance";
import type {
AgentInfo,
TokenCreate,
TokenUser,
LogEntry,
LogFilters,
InsertLogRequest,
InsertLogsRequest,
TokenUpdate,
TokenUpdatePermissions,
TokenPasswordReset,
RegistrationRequest,
DeployAgentsRequest,
DeployResponse,
SystemMetrics,
} from "../types/agent.types";
import type { GraphApiResponse } from "@/modules/graph/types";
class AgentApiService {
private readonly basePath = "/agents";
private readonly authBasePath = "/auth";
private readonly logsBasePath = "/logs";
async getAgents(): Promise<AgentInfo[]> {
const response = await apiClient.get<AgentInfo[]>(this.basePath);
return Array.isArray(response.data) ? response.data : [];
}
async getUsers(): Promise<TokenUser[]> {
const response = await apiClient.get<TokenUser[]>(
`${this.authBasePath}/tokens`,
);
return Array.isArray(response.data) ? response.data : [];
}
async createUser(data: TokenCreate): Promise<void> {
await apiClient.post(`${this.authBasePath}/token`, data);
}
async deleteUser(login: string): Promise<void> {
await apiClient.delete(`${this.authBasePath}/tokens/${login}`);
}
async deleteMyAccount(): Promise<void> {
await apiClient.delete(`${this.authBasePath}/token`);
}
async searchLogs(filters?: LogFilters): Promise<LogEntry[]> {
const response = await apiClient.get<LogEntry[]>(this.logsBasePath, {
params: {
level: filters?.level || undefined,
service: filters?.service || undefined,
agent: filters?.agent || undefined,
date_from: filters?.date_from || undefined,
date_to: filters?.date_to || undefined,
limit: filters?.limit ?? 100,
offset: filters?.offset ?? 0,
},
});
if (!Array.isArray(response.data)) {
console.error(
"[Logs] Unexpected response format:",
typeof response.data,
response.data,
);
return [];
}
return response.data;
}
async insertLog(entry: InsertLogRequest): Promise<void> {
await apiClient.post(this.logsBasePath, entry);
}
async insertLogsBatch(data: InsertLogsRequest): Promise<void> {
await apiClient.post(`${this.logsBasePath}/batch`, data);
}
async getDistinctAgents(): Promise<string[]> {
const response = await apiClient.get<string[]>(
`${this.logsBasePath}/agents`,
);
return Array.isArray(response.data) ? response.data : [];
}
async getDistinctLevels(): Promise<string[]> {
const response = await apiClient.get<string[]>(
`${this.logsBasePath}/levels`,
);
return Array.isArray(response.data) ? response.data : [];
}
async getDistinctServices(): Promise<string[]> {
const response = await apiClient.get<string[]>(
`${this.logsBasePath}/services`,
);
return Array.isArray(response.data) ? response.data : [];
}
// User management methods
async getUserByLogin(login: string): Promise<TokenUser> {
const response = await apiClient.get<TokenUser>(
`${this.authBasePath}/users/${login}`,
);
if (!response.data || typeof response.data !== "object") {
throw new Error(`User not found: ${login}`);
}
return response.data;
}
async getInactiveUsers(): Promise<TokenUser[]> {
const response = await apiClient.get<TokenUser[]>(
`${this.authBasePath}/users/inactive`,
);
return Array.isArray(response.data) ? response.data : [];
}
async updateUser(login: string, data: TokenUpdate): Promise<void> {
await apiClient.put(`${this.authBasePath}/users/${login}`, data);
}
async updateUserPermissions(
login: string,
data: TokenUpdatePermissions,
): Promise<void> {
await apiClient.put(
`${this.authBasePath}/users/${login}/permissions`,
data,
);
}
async resetUserPassword(
login: string,
data: TokenPasswordReset,
): Promise<void> {
await apiClient.put(`${this.authBasePath}/users/${login}/password`, data);
}
async activateUser(login: string): Promise<void> {
await apiClient.post(`${this.authBasePath}/users/${login}/activate`);
}
async deactivateUser(login: string): Promise<void> {
await apiClient.post(`${this.authBasePath}/users/${login}/deactivate`);
}
async createRegistrationToken(
data: RegistrationRequest,
): Promise<Record<string, string>> {
const response = await apiClient.post<Record<string, string>>(
`${this.basePath}/register-token`,
data,
);
return response.data;
}
async deployAgents(data: DeployAgentsRequest): Promise<DeployResponse> {
const response = await apiClient.post<DeployResponse>(
`${this.basePath}/deploy`,
data,
);
return response.data;
}
async getSystemMetrics(): Promise<SystemMetrics[]> {
const response = await apiClient.get<SystemMetrics[]>(
`${this.basePath}/system-metrics`,
);
return Array.isArray(response.data) ? response.data : [];
}
async getGraph(): Promise<GraphApiResponse> {
const response = await apiClient.get<GraphApiResponse>("/graph");
return response.data;
}
}
export const agentApiService = new AgentApiService();