Skip to content

Commit

Permalink
improve ws logging
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 22, 2024
1 parent 7dfefb2 commit 0fbd7f9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 66 deletions.
55 changes: 32 additions & 23 deletions lib/Logging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AnyObject, ClientSchema } from "prostgles-types";
import { LocalParams } from "./DboBuilder/DboBuilder";
import { TableHandler } from "./DboBuilder/TableHandler/TableHandler";
import { NOTIF_TYPE, NotifTypeName } from "./PubSubManager/PubSubManager";

type ClientInfo = {
socketId: string | undefined;
Expand All @@ -23,7 +24,7 @@ export namespace EventTypes {
localParams: LocalParams | undefined;
};

type SyncOneClient = ClientInfo &
export type Sync = ClientInfo &
DebugInfo & {
type: "sync";
tableName: string;
Expand All @@ -41,13 +42,6 @@ export namespace EventTypes {
rows: number;
socketId: string;
}
| {
command: "addTrigger";
state: "ok" | "fail";
/** If no socket id then it's a local subscribe */
socketId: string | undefined;
condition: string;
}
| {
command: "addSync" | "unsync";
socketId: string;
Expand All @@ -61,21 +55,36 @@ export namespace EventTypes {
connectedSocketIds: string[];
}
);
export type SyncMultiClient = {
type: "sync";
tableName: string;
localParams?: LocalParams;
connectedSocketIds: string[];
} & {
command: "notifListener";
op_name: string | undefined;
condition_ids_str: string | undefined;
tableTriggers: string[] | undefined;
tableSyncs: string;
state: "ok" | "error" | "no-triggers" | "invalid_condition_ids";
};

export type Sync = SyncOneClient | SyncMultiClient;
export type SyncOrSub = ClientInfo &
DebugInfo & {
type: "syncOrSub";
tableName: string;
localParams?: LocalParams;
connectedSocketIds: string[];
triggers: Record<string, string[]> | undefined;
} & (
| {
command: "addTrigger";
state: "ok" | "fail";
/** If no socket id then it's a local subscribe */
socketId: string | undefined;
condition: string;
}
| {
command: "notifListener";
notifType: NotifTypeName;
dataArr: any[];
}
| {
command: "notifListener.Finished";
op_name: string | undefined;
condition_ids_str: string | undefined;
tableTriggers: string[] | undefined;
tableSyncs: string;
state: "ok" | "error" | "no-triggers" | "invalid_condition_ids";
}
);

export type Connection =
| (ClientInfo & {
Expand Down Expand Up @@ -134,7 +143,7 @@ export type EventInfo =
| EventTypes.Table
| EventTypes.Method
| EventTypes.Sync
| EventTypes.SyncMultiClient
| EventTypes.SyncOrSub
| EventTypes.Connection
| EventTypes.Debug;

Expand Down
19 changes: 11 additions & 8 deletions lib/PubSubManager/PubSubManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
WAL,
} from "prostgles-types";

import { find, pickKeys, tryCatch } from "prostgles-types/dist/util";
import { find, pickKeys, tryCatch, tryCatchV2 } from "prostgles-types/dist/util";
import { LocalFuncs, getOnDataFunc, matchesLocalFuncs } from "../DboBuilder/ViewHandler/subscribe";
import { EVENT_TRIGGER_TAGS } from "../Event_Trigger_Tags";
import { EventTypes } from "../Logging";
Expand Down Expand Up @@ -183,7 +183,7 @@ export class PubSubManager {
}

dboBuilder: DboBuilder;
_triggers?: Record<string, string[]>;
_triggers: Record<string, string[]> | undefined;
sockets: AnyObject = {};

subs: Subscription[] = [];
Expand Down Expand Up @@ -408,7 +408,7 @@ export class PubSubManager {
get connectedSocketIds() {
return this.dboBuilder.prostgles.connectedSockets.map((s) => s.id);
}
_log = (params: EventTypes.Sync) => {
_log = (params: EventTypes.Sync | EventTypes.SyncOrSub) => {
return this.dboBuilder.prostgles.opts.onLog?.({ ...params });
};

Expand Down Expand Up @@ -473,7 +473,7 @@ export class PubSubManager {
viewOptions: ViewSubscriptionOptions | undefined,
socket: PRGLIOSocket | undefined
) {
const addedTrigger = await tryCatch(async () => {
const addedTrigger = await tryCatchV2(async () => {
const { table_name } = { ...params };
let { condition } = { ...params };
if (!table_name) throw "MISSING table_name";
Expand Down Expand Up @@ -538,17 +538,18 @@ export class PubSubManager {
});

await this._log({
type: "sync",
type: "syncOrSub",
command: "addTrigger",
condition: addedTrigger.cond ?? params.condition,
condition: addedTrigger.data?.cond ?? params.condition,
duration: addedTrigger.duration,
socketId: socket?.id,
state: !addedTrigger.tbl ? "fail" : "ok",
state: !addedTrigger.data?.tbl ? "fail" : "ok",
error: addedTrigger.error,
sid: socket && this.dboBuilder.prostgles.authHandler?.getSIDNoError({ socket }),
tableName: addedTrigger.tbl ?? params.table_name,
tableName: addedTrigger.data?.tbl ?? params.table_name,
connectedSocketIds: this.dboBuilder.prostgles.connectedSockets.map((s) => s.id),
localParams: socket && { clientReq: { socket } },
triggers: this._triggers,
});

if (addedTrigger.error) throw addedTrigger.error;
Expand Down Expand Up @@ -624,6 +625,8 @@ export const NOTIF_TYPE = {
data_trigger_change: "data_watch_triggers_have_changed",
schema: "schema_has_changed",
} as const;

export type NotifTypeName = (typeof NOTIF_TYPE)[keyof typeof NOTIF_TYPE];
export const NOTIF_CHANNEL = {
preffix: "prostgles_" as const,
getFull: (appID: string | undefined) => {
Expand Down
69 changes: 38 additions & 31 deletions lib/PubSubManager/notifListener.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { log, NOTIF_TYPE, pickKeys, PubSubManager } from "./PubSubManager";
import { log, NOTIF_TYPE, NotifTypeName, pickKeys, PubSubManager } from "./PubSubManager";

/* Relay relevant data to relevant subscriptions */
export async function notifListener(
this: PubSubManager,
data: { payload: string },
) {
export async function notifListener(this: PubSubManager, data: { payload: string }) {
const str = data.payload;

if (!str) {
Expand All @@ -13,10 +10,27 @@ export async function notifListener(
}

const dataArr = str.split(PubSubManager.DELIMITER);
const notifType = dataArr[0];
const notifType = dataArr[0] as NotifTypeName;

log(str);

const commonLog = {
triggers: this._triggers,
sid: undefined,
connectedSocketIds: this.connectedSocketIds,
socketId: undefined,
duration: 0,
tableName: dataArr[1] ?? "",
dataArr,
notifType,
};

await this._log({
type: "syncOrSub",
command: "notifListener",
...commonLog,
});

if (notifType === NOTIF_TYPE.schema) {
if (this.dboBuilder.prostgles.schemaWatch?.onSchemaChange) {
const [_, command, _event_type, query] = dataArr;
Expand All @@ -41,6 +55,7 @@ export async function notifListener(
return;
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (notifType !== NOTIF_TYPE.data) {
console.error("Unexpected notif type: ", notifType);
return;
Expand All @@ -57,14 +72,12 @@ export async function notifListener(
throw "table_name undef";
}

const tableTriggerConditions = this._triggers?.[table_name]?.map(
(condition, idx) => ({
idx,
condition,
subs: this.getTriggerSubs(table_name, condition),
syncs: this.getSyncs(table_name, condition),
}),
);
const tableTriggerConditions = this._triggers?.[table_name]?.map((condition, idx) => ({
idx,
condition,
subs: this.getTriggerSubs(table_name, condition),
syncs: this.getSyncs(table_name, condition),
}));
let state: "error" | "no-triggers" | "ok" | "invalid_condition_ids" = "ok";

// const triggers = await this.db.any("SELECT * FROM prostgles.triggers WHERE table_name = $1 AND id IN ($2:csv)", [table_name, condition_ids_str.split(",").map(v => +v)]);
Expand All @@ -81,18 +94,15 @@ export async function notifListener(
tableTriggerConditions.map(({ condition }) => {
const subs = this.getTriggerSubs(table_name, condition);
subs.map((s) => {
this.pushSubData(
s,
pref + ". Check server logs. Schema might have changed",
);
this.pushSubData(s, pref + ". Check server logs. Schema might have changed");
});
});

/* Trigger ok */
} else if (condition_ids?.every((id) => Number.isInteger(id))) {
state = "ok";
const firedTableConditions = tableTriggerConditions.filter(({ idx }) =>
condition_ids.includes(idx),
condition_ids.includes(idx)
);
const orphanedTableConditions = condition_ids.filter((condId) => {
const tc = tableTriggerConditions.at(condId);
Expand All @@ -116,7 +126,7 @@ export async function notifListener(
AND at.condition = t.condition
)
`,
[table_name, orphanedTableConditions, this.appId],
[table_name, orphanedTableConditions, this.appId]
)
.then(() => {
this.refreshTriggers();
Expand All @@ -130,7 +140,7 @@ export async function notifListener(
log(
"notifListener",
subs.map((s) => s.channel_name),
syncs.map((s) => s.channel_name),
syncs.map((s) => s.channel_name)
);

syncs.map((s) => {
Expand All @@ -143,15 +153,12 @@ export async function notifListener(
(trg) =>
this.dbo[trg.table_name] &&
sub.is_ready &&
((sub.socket_id && this.sockets[sub.socket_id]) || sub.localFuncs),
),
((sub.socket_id && this.sockets[sub.socket_id]) || sub.localFuncs)
)
);
activeAndReadySubs.forEach((sub) => {
const { throttle = 0, throttleOpts } = sub;
if (
!throttleOpts?.skipFirst &&
sub.last_throttled <= Date.now() - throttle
) {
if (!throttleOpts?.skipFirst && sub.last_throttled <= Date.now() - throttle) {
sub.last_throttled = Date.now();

/* It is assumed the policy was checked before this point */
Expand All @@ -174,17 +181,17 @@ export async function notifListener(
}

await this._log({
type: "sync",
command: "notifListener",
...commonLog,
type: "syncOrSub",
command: "notifListener.Finished",
state,
tableName: table_name,
op_name,
condition_ids_str,
tableTriggers: this._triggers?.[table_name],
tableSyncs: JSON.stringify(
this.syncs
.filter((s) => s.table_name === table_name)
.map((s) => pickKeys(s, ["condition", "socket_id"])),
.map((s) => pickKeys(s, ["condition", "socket_id"]))
),
connectedSocketIds: this.connectedSocketIds,
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.197",
"version": "4.2.198",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0fbd7f9

Please sign in to comment.