Skip to content
Open
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
65 changes: 64 additions & 1 deletion client/www/pages/intern/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ type AppSessions = {
};
type MachineSessions = Record<AppId, AppSessions>;
type SessionReports = Record<MachineId, MachineSessions>;
type MinuteOverview = { 'session-reports': SessionReports };
type ProxiedConnection = { count: number; target: string | null };
type ProxiedConnections = Record<MachineId, Record<AppId, ProxiedConnection>>;
type MinuteOverview = {
'session-reports': SessionReports;
'proxied-connections': ProxiedConnections | null;
};

async function fetchMinuteOverview(token: string): Promise<MinuteOverview> {
return jsonFetch(`${config.apiURI}/dash/overview/minute`, {
Expand Down Expand Up @@ -171,6 +176,29 @@ function flattenedSessionReports(machineToReport: SessionReports) {
return items;
}

type FlatProxiedConnection = {
'app-id': AppId;
target: string | null;
count: number;
};

function flattenedProxiedConnections(
machineToConnections: ProxiedConnections | null,
): FlatProxiedConnection[] {
const res: Record<AppId, FlatProxiedConnection> = {};
for (const machineId in machineToConnections) {
const machineConnections = machineToConnections[machineId];
for (const appId in machineConnections) {
const curr = machineConnections[appId];
const prev = res[appId];
res[appId] = prev
? { ...prev, count: prev.count + curr.count }
: { 'app-id': appId, target: curr.target, count: curr.count };
}
}
return Object.values(res).toSorted((a, b) => b.count - a.count);
}

function makeMachineSummary(
machineToReport: SessionReports,
): Record<string, number> {
Expand Down Expand Up @@ -390,6 +418,14 @@ const MinuteStatsSection = ({
);
const totalApps = Object.keys(sessions).length;

const proxiedConnections = flattenedProxiedConnections(
minute.data['proxied-connections'],
);
const totalProxied = proxiedConnections.reduce(
(acc: number, x) => acc + x.count,
0,
);

return (
<div className={wrapperClass}>
<button
Expand Down Expand Up @@ -428,6 +464,33 @@ const MinuteStatsSection = ({
<div>Active Apps</div>
</div>
</div>
{proxiedConnections.length > 0 && (
<div className="flex flex-col">
<div className="flex items-baseline space-x-2">
<h3 className="font-bold">Proxied Connections</h3>
<span className="text-gray-500">
{Intl.NumberFormat().format(totalProxied)} across{' '}
{proxiedConnections.length} app
{proxiedConnections.length > 1 ? 's' : ''}
</span>
</div>
<div className="mt-1 max-h-48 overflow-y-scroll border">
<table className="w-full">
<tbody>
{proxiedConnections.map((conn) => (
<tr key={conn['app-id']}>
<td className="px-4 py-2 text-right">
{Intl.NumberFormat().format(conn.count)}
</td>
<td className="px-4 py-2">{conn['app-id']}</td>
<td className="px-4 py-2">{conn.target || '-'}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
<div className="mt-4 overflow-y-scroll border">
<table className="w-full">
<tbody>
Expand Down
13 changes: 13 additions & 0 deletions server/src/instant/app_proxy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@
(into [] cat (vals @proxied-websockets))
opts))

(defn local-proxied-connections
"Summarizes the WebSocket connections this instance is currently proxying to
another backend, keyed by app id. The target comes from the live routing
table so it reflects the current config."
[]
(let [table (flags/app-proxy-targets)]
(reduce-kv
(fn [acc app-id connections]
(assoc acc app-id {:count (count connections)
:target (some-> ^URI (get table app-id) str)}))
{}
@proxied-websockets)))

(defn- changed-app-ids [old-targets new-targets]
;; Includes apps that were added or removed as well as apps whose target
;; origin changed.
Expand Down
10 changes: 8 additions & 2 deletions server/src/instant/dash/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,15 @@
(defn admin-overview-minute-get [req]
(let [{:keys [email]} (req->auth-user! req)
_ (assert-admin-email! email)
session-reports (machine-summaries/get-session-reports-cached)]
session-reports (machine-summaries/get-session-reports-cached)
;; Gate the cross-machine task behind a flag: older machines that
;; predate proxied-connections-task can't run it, so only fan out once
;; every machine has been updated and the flag is flipped on.
proxied-connections (when (flags/flag :proxied-connections-overview-enabled false)
(machine-summaries/get-proxied-connections-cached))]
(response/ok
{:session-reports session-reports})))
{:session-reports session-reports
:proxied-connections proxied-connections})))

(defn app-stats-get [req]
(let [{{app-id :id} :app} (req->app-and-user! :collaborator req)
Expand Down
27 changes: 27 additions & 0 deletions server/src/instant/machine_summaries.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns instant.machine-summaries
(:require
[instant.app-proxy :as app-proxy]
[instant.flags :as flags]
[instant.reactive.ephemeral :as eph]
[instant.reactive.store :as rs]
Expand Down Expand Up @@ -59,6 +60,32 @@
(fn [_]
(get-session-reports (eph/get-hz))))))

;; proxied connections

(defn proxied-connections-task
[]
(app-proxy/local-proxied-connections))

(defn get-proxied-connections [hz]
(let [executor (HazelcastInstance/.getExecutorService hz "proxied-connections-executor")
futures (IExecutorService/.submitToAllMembers executor (hz/->Task #'proxied-connections-task))]
(into {} (for [[member fut] futures]
[(str (or (Member/.getAttribute member "instance-id")
(Member/.getAddress member)))
@fut]))))

(comment
(get-proxied-connections (eph/get-hz)))

(def proxied-connections-cache
(cache/make
{:ttl 5000
:value-fn (fn [_]
(get-proxied-connections (eph/get-hz)))}))

(defn get-proxied-connections-cached []
(cache/get proxied-connections-cache :proxied-connections))

;; num sessions

(defn num-sessions-task
Expand Down
Loading