@obzerver/server 0.18.1 → 0.18.2

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
@@ -736,11 +736,78 @@ export declare function outbound(db: Db, w: Window): {
736
736
  export declare function users(db: Db, w: Window, { groupBy }?: {
737
737
  groupBy?: string;
738
738
  }): Row[];
739
- /** The tenants behind a set of errors: the ones the app set, else the users' email domains. */
739
+ /** The tenants behind a set of errors: the ones the app set, else the users' email domains (`guessed`). */
740
740
  export declare function tenantsOfErrors(rows: Array<{
741
741
  user_id: string | null;
742
742
  tenant_id: string | null;
743
- }>): string[];
743
+ }>): Array<{
744
+ name: string;
745
+ guessed: boolean;
746
+ }>;
747
+ /**
748
+ * One tenant over the window: KPIs (with the previous window of the same length and all tenants for comparison),
749
+ * requests and failures over time, its operations, users and errors, and open incidents from those errors.
750
+ */
751
+ export declare function tenant(db: Db, w: Window, id: string, guessed?: boolean): {
752
+ tenant: string;
753
+ guessed: boolean;
754
+ lastSeen: number | null;
755
+ kpis: {
756
+ requests: number;
757
+ prevRequests: number;
758
+ failed: number;
759
+ failureRate: number | null;
760
+ prevFailureRate: number | null;
761
+ allFailureRate: number | null;
762
+ allFailed: number;
763
+ p95: number | null;
764
+ prevP95: number | null;
765
+ allP95: number | null;
766
+ users: number;
767
+ prevUsers: number;
768
+ errors: number;
769
+ incidents: number;
770
+ };
771
+ series: {
772
+ bucketMs: number;
773
+ points: {
774
+ t: number;
775
+ count: number;
776
+ failed: number;
777
+ }[];
778
+ };
779
+ operations: {
780
+ service: string;
781
+ name: string;
782
+ kind: string;
783
+ calls: number;
784
+ failed: number;
785
+ failureRate: number | null;
786
+ p95: number | null;
787
+ }[];
788
+ users: {
789
+ userId: string;
790
+ calls: number;
791
+ failed: number;
792
+ failureRate: number | null;
793
+ lastSeen: number;
794
+ }[];
795
+ errors: {
796
+ fingerprint: string;
797
+ class: string;
798
+ message: string;
799
+ n: number;
800
+ users: number;
801
+ last: number;
802
+ }[];
803
+ incidents: {
804
+ id: number;
805
+ title: string;
806
+ status: string;
807
+ severity: string;
808
+ updated: number;
809
+ }[];
810
+ };
744
811
  /** A gap longer than this between two actions starts a new session. */
745
812
  export declare const SESSION_GAP: number;
746
813
  /** Sessions the user page replays; the rest are on the sessions page. */
package/dist/queries.js CHANGED
@@ -959,9 +959,94 @@ function tenantsOf(db, w, id) {
959
959
  const d = domainOf(id);
960
960
  return { tenants: d ? [d] : [], tenantGuessed: !!d };
961
961
  }
962
- /** The tenants behind a set of errors: the ones the app set, else the users' email domains. */
962
+ /** The tenants behind a set of errors: the ones the app set, else the users' email domains (`guessed`). */
963
963
  export function tenantsOfErrors(rows) {
964
- return [...new Set(rows.map((r) => r.tenant_id ?? domainOf(r.user_id)).filter((t) => !!t))];
964
+ const out = new Map();
965
+ for (const r of rows) {
966
+ const name = r.tenant_id ?? domainOf(r.user_id);
967
+ if (!name)
968
+ continue;
969
+ const guessed = r.tenant_id == null;
970
+ out.set(`${guessed ? 1 : 0}|${name}`, { name, guessed });
971
+ }
972
+ return [...out.values()];
973
+ }
974
+ /** Which requests are a tenant's: the ones the app tagged with it, or (`guessed`) untagged ones whose user has that email domain. */
975
+ function tenantWhere(id, guessed) {
976
+ return guessed
977
+ ? { sql: "tenant_id IS NULL AND user_id LIKE ? ESCAPE '\\'", args: [`%@${id.replace(/[\\%_]/g, (c) => `\\${c}`)}`] }
978
+ : { sql: 'tenant_id = ?', args: [id] };
979
+ }
980
+ const p95Of = (d) => { const x = Float64Array.from(d).sort(); return percentile(x, 95); };
981
+ /**
982
+ * One tenant over the window: KPIs (with the previous window of the same length and all tenants for comparison),
983
+ * requests and failures over time, its operations, users and errors, and open incidents from those errors.
984
+ */
985
+ export function tenant(db, w, id, guessed = false) {
986
+ const t = tenantWhere(id, guessed);
987
+ const kinds = "kind IN ('http', 'job')";
988
+ const s = scope(w);
989
+ const len = w.to - w.from;
990
+ const prevScope = scope({ ...w, from: w.from - len, to: w.from });
991
+ const c = readColumns(db, `${kinds} AND ${t.sql} AND ${s.sql}`, [...t.args, ...s.args], { ops: true });
992
+ const prev = readColumns(db, `${kinds} AND ${t.sql} AND ${prevScope.sql}`, [...t.args, ...prevScope.args]);
993
+ const all = readColumns(db, `${kinds} AND (user_id IS NOT NULL OR tenant_id IS NOT NULL) AND ${s.sql}`, s.args);
994
+ const failedOf = (x) => { let f = 0; for (let i = 0; i < x.n; i++)
995
+ f += x.failed[i]; return f; };
996
+ const rate = (f, n) => (n ? round((f / n) * 100) : null);
997
+ const failed = failedOf(c), prevFailed = failedOf(prev), allFailed = failedOf(all);
998
+ // Requests and failures per bucket.
999
+ const b = buckets(w);
1000
+ const points = Array.from({ length: b.n }, (_, i) => ({ t: b.start + i * b.size, count: 0, failed: 0 }));
1001
+ for (let i = 0; i < c.n; i++) {
1002
+ const k = b.index(c.ts[i]);
1003
+ if (k >= 0) {
1004
+ points[k].count++;
1005
+ points[k].failed += c.failed[i];
1006
+ }
1007
+ }
1008
+ // Operations: calls, failures, p95.
1009
+ const byOp = new Map();
1010
+ const opFailed = new Map();
1011
+ for (let i = 0; i < c.n; i++) {
1012
+ const o = c.op[i];
1013
+ let arr = byOp.get(o);
1014
+ if (!arr)
1015
+ byOp.set(o, (arr = []));
1016
+ arr.push(c.d[i]);
1017
+ if (c.failed[i])
1018
+ opFailed.set(o, (opFailed.get(o) || 0) + 1);
1019
+ }
1020
+ const kindOf = new Map(db.all(`SELECT DISTINCT service, name, kind FROM traces INDEXED BY traces_window WHERE ${kinds} AND ${t.sql} AND ${s.sql}`, ...t.args, ...s.args)
1021
+ .map((r) => [`${r.service}\u0000${r.name}`, r.kind]));
1022
+ const operations = [...byOp.entries()].map(([o, d]) => {
1023
+ const f = opFailed.get(o) || 0;
1024
+ return { service: c.ops[o].service, name: c.ops[o].name, kind: kindOf.get(`${c.ops[o].service}\u0000${c.ops[o].name}`) ?? 'http', calls: d.length, failed: f, failureRate: rate(f, d.length), p95: round(p95Of(Float64Array.from(d))) };
1025
+ }).sort((a, b2) => b2.calls - a.calls);
1026
+ const users = db.all(`SELECT user_id, COUNT(*) calls, SUM(failed) failed, MAX(ts) last FROM traces INDEXED BY traces_window
1027
+ WHERE ${kinds} AND user_id IS NOT NULL AND ${t.sql} AND ${s.sql} GROUP BY user_id ORDER BY calls DESC LIMIT 500`, ...t.args, ...s.args)
1028
+ .map((r) => ({ userId: r.user_id, calls: r.calls, failed: r.failed, failureRate: rate(r.failed, r.calls), lastSeen: r.last }));
1029
+ const prevUsers = db.get(`SELECT COUNT(DISTINCT user_id) n FROM traces INDEXED BY traces_window WHERE ${kinds} AND user_id IS NOT NULL AND ${t.sql} AND ${prevScope.sql}`, ...t.args, ...prevScope.args).n;
1030
+ const errors = db.all(`SELECT fingerprint, class, MAX(message) message, COUNT(*) n, COUNT(DISTINCT user_id) users, MAX(ts) last FROM errors
1031
+ WHERE ${t.sql} AND ${s.sql} GROUP BY fingerprint ORDER BY n DESC LIMIT 100`, ...t.args, ...s.args);
1032
+ const fps = errors.map((e) => e.fingerprint);
1033
+ const incidents = fps.length ? db.all(`SELECT id, title, status, severity, updated FROM issues WHERE source_type = 'error' AND status NOT IN ('resolved', 'closed')
1034
+ AND source_ref IN (${fps.map(() => '?').join(',')}) ORDER BY updated DESC`, ...fps) : [];
1035
+ // Last seen at any time (not only in the window), within the chosen app and environment.
1036
+ const base = scopeBase(w);
1037
+ const last = db.get(`SELECT MAX(ts) ts FROM traces WHERE ${t.sql}${base.sql}`, ...t.args, ...base.args).ts;
1038
+ return {
1039
+ tenant: id, guessed, lastSeen: last,
1040
+ kpis: {
1041
+ requests: c.n, prevRequests: prev.n,
1042
+ failed, failureRate: rate(failed, c.n), prevFailureRate: rate(prevFailed, prev.n), allFailureRate: rate(allFailed, all.n), allFailed,
1043
+ p95: round(p95Of(c.d)), prevP95: round(p95Of(prev.d)), allP95: round(p95Of(all.d)),
1044
+ users: users.length, prevUsers,
1045
+ errors: errors.length, incidents: incidents.length,
1046
+ },
1047
+ series: { bucketMs: b.size, points },
1048
+ operations, users, errors, incidents,
1049
+ };
965
1050
  }
966
1051
  /** A gap longer than this between two actions starts a new session. */
967
1052
  export const SESSION_GAP = 30 * 60e3;