Add DU network operations

This commit is contained in:
Alex Holliday committed 2025-02-05 13:59:41 -08:00
1 parent 85a5b83ab8
commit 68c772785b
1 file changed
+70
+70
View File
@@ -862,6 +862,76 @@ class NetworkService {
);
}
subscribeToDistributedUptimeMonitors(config) {
const {
authToken,
teamId,
onUpdate,
onError,
onOpen,
limit,
types,
page,
rowsPerPage,
filter,
field,
order,
} = config;
const params = new URLSearchParams();
if (limit) params.append("limit", limit);
if (types) {
types.forEach((type) => {
params.append("type", type);
});
}
if (page) params.append("page", page);
if (rowsPerPage) params.append("rowsPerPage", rowsPerPage);
if (filter) params.append("filter", filter);
if (field) params.append("field", field);
if (order) params.append("order", order);
if (this.eventSource) {
this.eventSource.close();
}
const url = `${this.axiosInstance.defaults.baseURL}/distributed-uptime/monitors/${teamId}?${params.toString()}`;
this.eventSource = new EventSource(url, {
headers: { Authorization: `Bearer ${authToken}` },
});
this.eventSource.onopen = () => {
onOpen?.();
};
this.eventSource.addEventListener("open", (e) => {
console.log("getDistributedUptimeMonitors connection opened:");
});
this.eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
onUpdate(data);
};
this.eventSource.onerror = (error) => {
console.error("Monitor stream error:", error);
onError?.();
this.eventSource.close();
};
// Returns a cleanup function
return () => {
if (this.eventSource) {
this.eventSource.close();
this.eventSource = null;
}
return () => {
console.log("Nothing to cleanup");
};
};
}
async getStatusPage(config) {
const { authToken } = config;
return this.axiosInstance.get(`/status-page`, {