@obzerver/server 0.17.2 → 0.17.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/queries.d.ts CHANGED
@@ -731,18 +731,41 @@ export declare function outbound(db: Db, w: Window): {
731
731
  export declare function users(db: Db, w: Window, { groupBy }?: {
732
732
  groupBy?: string;
733
733
  }): Row[];
734
+ /** A gap longer than this between two actions starts a new session. */
735
+ export declare const SESSION_GAP: number;
736
+ /** Sessions the user page replays; the rest are on the sessions page. */
737
+ export declare const REPLAY_SESSIONS = 4;
738
+ export interface UserSession {
739
+ n: number;
740
+ start: number;
741
+ end: number;
742
+ actions: number;
743
+ failed: number;
744
+ }
745
+ type Action = {
746
+ id: number;
747
+ ts: number;
748
+ name: string;
749
+ kind: string;
750
+ status: number | null;
751
+ duration: number;
752
+ failed: number;
753
+ error_message: string | null;
754
+ trace_id: string;
755
+ };
734
756
  export declare function user(db: Db, w: Window, id: string): {
735
757
  userId: string;
736
- actions: {
737
- id: number;
738
- ts: number;
739
- name: string;
740
- kind: string;
741
- status: number | null;
742
- duration: number;
758
+ lastSeen: number | null;
759
+ sessionCount: number;
760
+ actionCount: number;
761
+ failedCount: number;
762
+ sessions: {
763
+ items: Action[];
764
+ n: number;
765
+ start: number;
766
+ end: number;
767
+ actions: number;
743
768
  failed: number;
744
- error_message: string | null;
745
- trace_id: string;
746
769
  }[];
747
770
  operations: {
748
771
  avg: number | null;
@@ -756,6 +779,19 @@ export declare function user(db: Db, w: Window, id: string): {
756
779
  n: number;
757
780
  }[];
758
781
  };
782
+ /** All of a user's sessions in the window, newest first; with `n`, also the actions of session `n`. */
783
+ export declare function userSessions(db: Db, w: Window, id: string, n?: number): {
784
+ userId: string;
785
+ sessions: UserSession[];
786
+ session: {
787
+ items: Action[];
788
+ n: number;
789
+ start: number;
790
+ end: number;
791
+ actions: number;
792
+ failed: number;
793
+ } | null;
794
+ };
759
795
  export declare function runtime(db: Db, w: Window): {
760
796
  service: string;
761
797
  instance: string;
package/dist/queries.js CHANGED
@@ -938,25 +938,65 @@ export function users(db, w, { groupBy } = {}) {
938
938
  return { customer, users: us.length, calls, failed, lastSeen: maxOf(us.map((u) => u.lastSeen)), failureRate: round((failed / calls) * 100) };
939
939
  }).sort((a, b) => b.calls - a.calls);
940
940
  }
941
+ /** A gap longer than this between two actions starts a new session. */
942
+ export const SESSION_GAP = 30 * 60e3;
943
+ /** Sessions the user page replays; the rest are on the sessions page. */
944
+ export const REPLAY_SESSIONS = 4;
945
+ /** Every session of a user in the window, oldest first, numbered from 1. */
946
+ function sessionsOf(db, w, id) {
947
+ const s = scope(w);
948
+ const rows = db.all(`SELECT ts, failed FROM traces WHERE user_id = ? AND ${s.sql} ORDER BY ts`, id, ...s.args);
949
+ const out = [];
950
+ for (const r of rows) {
951
+ const cur = out[out.length - 1];
952
+ if (cur && r.ts - cur.end <= SESSION_GAP) {
953
+ cur.end = r.ts;
954
+ cur.actions++;
955
+ cur.failed += r.failed ? 1 : 0;
956
+ }
957
+ else
958
+ out.push({ n: out.length + 1, start: r.ts, end: r.ts, actions: 1, failed: r.failed ? 1 : 0 });
959
+ }
960
+ return out;
961
+ }
962
+ function actionsBetween(db, w, id, from, to, limit = 1000) {
963
+ const s = scope({ ...w, from: Math.max(w.from, from), to: Math.min(w.to, to) });
964
+ return db.all(`SELECT id, ts, name, kind, status, duration, failed, error_message, trace_id FROM traces WHERE user_id = ? AND ${s.sql} ORDER BY ts DESC LIMIT ${limit}`, id, ...s.args);
965
+ }
966
+ /** Newest session first, each with its actions newest first. */
967
+ function withActions(db, w, id, list) {
968
+ return list.map((x) => ({ ...x, items: actionsBetween(db, w, id, x.start, x.end) }));
969
+ }
941
970
  export function user(db, w, id) {
942
971
  const s = scope(w);
943
- const actions = db.all(`SELECT id, ts, name, kind, status, duration, failed, error_message, trace_id FROM traces WHERE user_id = ? AND ${s.sql} ORDER BY ts DESC LIMIT 300`, id, ...s.args);
972
+ const all = sessionsOf(db, w, id);
944
973
  const ops = db.all(`SELECT name, COUNT(*) calls, SUM(failed) failed, AVG(duration) avg FROM traces WHERE user_id = ? AND ${s.sql} GROUP BY name ORDER BY calls DESC`, id, ...s.args);
945
974
  const errors = db.all(`SELECT fingerprint, class, MAX(message) message, COUNT(*) n FROM errors WHERE user_id = ? AND ${s.sql} GROUP BY fingerprint ORDER BY n DESC`, id, ...s.args);
946
- // Paths: consecutive action transitions within 30 minutes.
947
- const chron = [...actions].reverse();
975
+ // Last seen anywhere, not only in this window.
976
+ const last = db.get(`SELECT MAX(ts) ts FROM traces WHERE user_id = ?${scopeBase(w).sql}`, id, ...scopeBase(w).args).ts;
977
+ // Paths: consecutive action transitions within a session, over the newest 300 actions.
978
+ const chron = actionsBetween(db, w, id, w.from, w.to, 300).reverse();
948
979
  const paths = new Map();
949
980
  for (let i = 1; i < chron.length; i++) {
950
- if (chron[i].ts - chron[i - 1].ts > 30 * 60e3)
981
+ if (chron[i].ts - chron[i - 1].ts > SESSION_GAP)
951
982
  continue;
952
983
  const k = `${chron[i - 1].name} → ${chron[i].name}`;
953
984
  paths.set(k, (paths.get(k) || 0) + 1);
954
985
  }
955
986
  return {
956
- userId: id, actions, operations: ops.map((o) => ({ ...o, avg: round(o.avg) })), errors,
987
+ userId: id, lastSeen: last, sessionCount: all.length,
988
+ actionCount: sum(all.map((x) => x.actions)), failedCount: sum(all.map((x) => x.failed)),
989
+ sessions: withActions(db, w, id, all.slice(-REPLAY_SESSIONS).reverse()),
990
+ operations: ops.map((o) => ({ ...o, avg: round(o.avg) })), errors,
957
991
  paths: [...paths.entries()].map(([path, n]) => ({ path, n })).sort((a, b) => b.n - a.n).slice(0, 15),
958
992
  };
959
993
  }
994
+ /** All of a user's sessions in the window, newest first; with `n`, also the actions of session `n`. */
995
+ export function userSessions(db, w, id, n) {
996
+ const all = sessionsOf(db, w, id);
997
+ const pick = n ? all.find((x) => x.n === n) : undefined;
998
+ return { userId: id, sessions: [...all].reverse(), session: pick ? withActions(db, w, id, [pick])[0] : null };
999
+ }
960
1000
  // ------------------------------------------------------------------ runtime & metrics
961
1001
  export function runtime(db, w) {
962
1002
  const s = scope(w);