Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Local pusher & alert feed #2484

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions keep-ui/utils/hooks/usePusher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Pusher from "pusher-js";
import Pusher, { Options as PusherOptions } from "pusher-js";
import { useConfig } from "./useConfig";
import { useSession } from "next-auth/react";
import { useApiUrl } from "./useConfig";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";

let PUSHER: Pusher | null = null;
const POLLING_INTERVAL = 3000;
const POLLING_INTERVAL = 1000 * 10; // Once per 10 seconds.

export const useWebsocket = () => {
const apiUrl = useApiUrl();
Expand All @@ -26,15 +26,19 @@ export const useWebsocket = () => {
channelName = `private-${session?.tenantId}`;
console.log("useWebsocket: Creating new Pusher instance");
try {
const isRelativeHost =
configData.PUSHER_HOST && !configData.PUSHER_HOST.includes("://");
console.log("useWebsocket: isRelativeHost:", isRelativeHost);
PUSHER = new Pusher(configData.PUSHER_APP_KEY, {
wsHost: isRelativeHost
const isRelativeHostAndNotLocal =
configData.PUSHER_HOST &&
!configData.PUSHER_HOST.includes("://") &&
!["localhost", "127.0.0.1"].includes(configData.PUSHER_HOST);

console.log("useWebsocket: isRelativeHostAndNotLocal:", isRelativeHostAndNotLocal);

var pusherOptions: PusherOptions = {
wsHost: isRelativeHostAndNotLocal
? window.location.hostname
: configData.PUSHER_HOST,
wsPath: isRelativeHost ? configData.PUSHER_HOST : "",
wsPort: isRelativeHost
wsPath: isRelativeHostAndNotLocal ? configData.PUSHER_HOST : "",
wsPort: isRelativeHostAndNotLocal
? window.location.protocol === "https:"
? 443
: 80
Expand All @@ -50,8 +54,10 @@ export const useWebsocket = () => {
Authorization: `Bearer ${session?.accessToken!}`,
},
},
});
console.log("useWebsocket: Pusher instance created successfully");
}
PUSHER = new Pusher(configData.PUSHER_APP_KEY, pusherOptions);

console.log("useWebsocket: Pusher instance created successfully. Options:", pusherOptions);

PUSHER.connection.bind("connected", () => {
console.log("useWebsocket: Pusher connected successfully");
Expand All @@ -61,6 +67,10 @@ export const useWebsocket = () => {
console.error("useWebsocket: Pusher connection error:", err);
});

PUSHER.connection.bind('state_change', function(states:any) {
console.log("useWebsocket: Connection state changed from", states.previous, "to", states.current);
});

PUSHER.subscribe(channelName)
.bind("pusher:subscription_succeeded", () => {
console.log(
Expand Down Expand Up @@ -148,13 +158,14 @@ export const useAlertPolling = () => {
`useAlertPolling: Time since last poll: ${timeSinceLastPoll}ms`
);

const newPollValue = Math.floor(Math.random() * 10000);

if (timeSinceLastPoll < POLLING_INTERVAL) {
console.log("useAlertPolling: Ignoring poll due to short interval");
setPollAlerts(0);
} else {
console.log("useAlertPolling: Updating poll alerts");
lastPollTimeRef.current = currentTime;
const newPollValue = Math.floor(Math.random() * 10000);
console.log(`useAlertPolling: New poll value: ${newPollValue}`);
setPollAlerts(newPollValue);
}
Expand Down
27 changes: 19 additions & 8 deletions keep/api/tasks/process_event_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,26 +411,37 @@ def __handle_formatted_events(
},
)


pusher_client = get_pusher_client() if notify_client else None

# Tell the client to poll alerts
if notify_client and incidents:
pusher_client = get_pusher_client()
if not pusher_client:
if pusher_client:
try:
pusher_client.trigger(
f"private-{tenant_id}",
"poll-alerts",
"{}",
)
logger.info("Told client to poll alerts")
except Exception:
logger.exception("Failed to tell client to poll alerts")
pass

if incidents and pusher_client:
try:
pusher_client.trigger(
f"private-{tenant_id}",
"incident-change",
{},
)
except Exception:
logger.exception("Failed to push alert to the client")
logger.exception("Failed to tell the client to pull incidents")

# Now we need to update the presets
# send with pusher
if notify_client:
pusher_client = get_pusher_client()
if not pusher_client:
return
if not pusher_client:
return

try:
presets = get_all_presets(tenant_id)
rules_engine = RulesEngine(tenant_id=tenant_id)
Expand Down
Loading