Skip to content

Commit

Permalink
Merge branch 'main' into 2494-provider-servicenow-should-support-oaut…
Browse files Browse the repository at this point in the history
…h-to-obtain-token
  • Loading branch information
talboren authored Nov 16, 2024
2 parents f426708 + 4f6b0b4 commit af9cad7
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 25 deletions.
3 changes: 3 additions & 0 deletions docs/api-ref/dashboard/get-metric-widgets.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: get /dashboard/metric-widgets
---
3 changes: 3 additions & 0 deletions docs/api-ref/incidents/commit-with-ai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: post /incidents/ai/{suggestion_id}/commit
---
3 changes: 3 additions & 0 deletions docs/api-ref/incidents/create-incident.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: post /incidents
---
3 changes: 3 additions & 0 deletions docs/api-ref/incidents/create-with-ai.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: post /incidents/ai/suggest
---
3 changes: 3 additions & 0 deletions docs/api-ref/incidents/receive-event.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: post /incidents/event/{provider_type}
---
3 changes: 3 additions & 0 deletions docs/api-ref/root.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: get /
---
3 changes: 3 additions & 0 deletions docs/api-ref/workflows/get-workflow-by-id-1.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
openapi: get /workflows/{workflow_id}/runs
---
11 changes: 9 additions & 2 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
{
"group": "Keep API",
"pages": [
"api-ref/root",
{
"group": "providers",
"pages": [
Expand Down Expand Up @@ -376,7 +377,11 @@
"api-ref/incidents/get-future-incidents-for-an-incident",
"api-ref/incidents/get-incident-workflows",
"api-ref/incidents/get-incidents-meta",
"api-ref/incidents/merge-incidents"
"api-ref/incidents/merge-incidents",
"api-ref/incidents/commit-with-ai",
"api-ref/incidents/create-incident",
"api-ref/incidents/create-with-ai",
"api-ref/incidents/receive-event"
]
},
{
Expand Down Expand Up @@ -408,6 +413,7 @@
"api-ref/workflows/create-workflow-from-body",
"api-ref/workflows/get-random-workflow-templates",
"api-ref/workflows/get-workflow-by-id",
"api-ref/workflows/get-workflow-by-id-1",
"api-ref/workflows/update-workflow-by-id",
"api-ref/workflows/delete-workflow-by-id",
"api-ref/workflows/get-raw-workflow-by-id",
Expand Down Expand Up @@ -513,7 +519,8 @@
"api-ref/dashboard/read-dashboards",
"api-ref/dashboard/create-dashboard",
"api-ref/dashboard/update-dashboard",
"api-ref/dashboard/delete-dashboard"
"api-ref/dashboard/delete-dashboard",
"api-ref/dashboard/get-metric-widgets"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi.json

Large diffs are not rendered by default.

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
3 changes: 3 additions & 0 deletions keep/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def get_app(

@app.get("/")
async def root():
"""
App description and version.
"""
return {"message": app.description, "version": KEEP_VERSION}

app.add_middleware(RawContextMiddleware, plugins=(plugins.RequestIdPlugin(),))
Expand Down
12 changes: 12 additions & 0 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,18 @@ def get_last_workflow_executions(tenant_id: str, limit=20):
)

return execution_with_logs


def get_workflow_executions_count(tenant_id: str):
with Session(engine) as session:
query = session.query(WorkflowExecution).filter(
WorkflowExecution.tenant_id == tenant_id,
)

return {
"success": query.filter(WorkflowExecution.status == "success").count(),
"other": query.filter(WorkflowExecution.status != "success").count(),
}


def add_audit(
Expand Down
15 changes: 13 additions & 2 deletions keep/api/routes/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi import APIRouter, Depends, Response

from keep.api.models.alert import AlertDto
from keep.api.core.db import get_last_incidents, get_last_alerts_for_incidents
from keep.api.core.db import get_last_incidents, get_last_alerts_for_incidents, get_workflow_executions_count
from keep.identitymanager.authenticatedentity import AuthenticatedEntity
from keep.identitymanager.identitymanagerfactory import IdentityManagerFactory

Expand All @@ -24,7 +24,8 @@ def get_metrics(
"""
This endpoint is used by Prometheus to scrape such metrics from the application:
- alerts_total {incident_name, incident_id} - The total number of alerts per incident.
- open_incidents_total - The total number of open incidents
- open_incidents_total - The total number of open incidents.
- workflows_executions_total {status} - The total number of workflow executions.
Please note that those metrics are per-tenant and are not designed to be used for the monitoring of the application itself.
Expand Down Expand Up @@ -92,4 +93,14 @@ def get_metrics(
export += "# TYPE open_incidents_total counter\n"
export += f"open_incidents_total {incidents_total}\n"

workflow_execution_counts = get_workflow_executions_count(
tenant_id=tenant_id,
)

export += "\n\n"
export += "# HELP workflows_executions_total The total number of workflows.\r\n"
export += "# TYPE workflows_executions_total counter\n"
export += f"workflows_executions_total {{status=\"success\"}} {workflow_execution_counts['success']}\n"
export += f"workflows_executions_total {{status=\"other\"}} {workflow_execution_counts['other']}\n"

return Response(content=export, media_type=CONTENT_TYPE_LATEST)
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

0 comments on commit af9cad7

Please sign in to comment.