@dads-framework/audit 0.0.0-stage → 0.1.0

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.
Files changed (185) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +69 -2
  3. package/dist/audit.tsbuildinfo +1 -0
  4. package/dist/index.d.ts +25 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +24 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/lib/actor/compose-subject-to-audit-actor.d.ts +32 -0
  9. package/dist/lib/actor/compose-subject-to-audit-actor.d.ts.map +1 -0
  10. package/dist/lib/actor/compose-subject-to-audit-actor.js +66 -0
  11. package/dist/lib/actor/compose-subject-to-audit-actor.js.map +1 -0
  12. package/dist/lib/audit-plugin.d.ts +21 -0
  13. package/dist/lib/audit-plugin.d.ts.map +1 -0
  14. package/dist/lib/audit-plugin.js +61 -0
  15. package/dist/lib/audit-plugin.js.map +1 -0
  16. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.d.ts +5 -0
  17. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.d.ts.map +1 -0
  18. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.js +4 -0
  19. package/dist/lib/client-diagnostics/client-diagnostic-event.ids.js.map +1 -0
  20. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.d.ts +23 -0
  21. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.d.ts.map +1 -0
  22. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.js +27 -0
  23. package/dist/lib/client-diagnostics/client-diagnostics.route-handlers.js.map +1 -0
  24. package/dist/lib/client-diagnostics/client-diagnostics.routes.d.ts +18 -0
  25. package/dist/lib/client-diagnostics/client-diagnostics.routes.d.ts.map +1 -0
  26. package/dist/lib/client-diagnostics/client-diagnostics.routes.js +41 -0
  27. package/dist/lib/client-diagnostics/client-diagnostics.routes.js.map +1 -0
  28. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.d.ts +30 -0
  29. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.d.ts.map +1 -0
  30. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.js +39 -0
  31. package/dist/lib/client-diagnostics/logic/client-diagnostic-row.js.map +1 -0
  32. package/dist/lib/client-diagnostics/record-client-diagnostics.d.ts +29 -0
  33. package/dist/lib/client-diagnostics/record-client-diagnostics.d.ts.map +1 -0
  34. package/dist/lib/client-diagnostics/record-client-diagnostics.js +40 -0
  35. package/dist/lib/client-diagnostics/record-client-diagnostics.js.map +1 -0
  36. package/dist/lib/compose-audit.d.ts +27 -0
  37. package/dist/lib/compose-audit.d.ts.map +1 -0
  38. package/dist/lib/compose-audit.js +38 -0
  39. package/dist/lib/compose-audit.js.map +1 -0
  40. package/dist/lib/database/attach-audit-trigger.d.ts +34 -0
  41. package/dist/lib/database/attach-audit-trigger.d.ts.map +1 -0
  42. package/dist/lib/database/attach-audit-trigger.js +40 -0
  43. package/dist/lib/database/attach-audit-trigger.js.map +1 -0
  44. package/dist/lib/database/audit-log-position.ids.d.ts +13 -0
  45. package/dist/lib/database/audit-log-position.ids.d.ts.map +1 -0
  46. package/dist/lib/database/audit-log-position.ids.js +11 -0
  47. package/dist/lib/database/audit-log-position.ids.js.map +1 -0
  48. package/dist/lib/database/audit-row-json.d.ts +10 -0
  49. package/dist/lib/database/audit-row-json.d.ts.map +1 -0
  50. package/dist/lib/database/audit-row-json.js +2 -0
  51. package/dist/lib/database/audit-row-json.js.map +1 -0
  52. package/dist/lib/database/audit-trigger-allowlist.d.ts +4 -0
  53. package/dist/lib/database/audit-trigger-allowlist.d.ts.map +1 -0
  54. package/dist/lib/database/audit-trigger-allowlist.js +8 -0
  55. package/dist/lib/database/audit-trigger-allowlist.js.map +1 -0
  56. package/dist/lib/database/configure-audit-triggers.d.ts +8 -0
  57. package/dist/lib/database/configure-audit-triggers.d.ts.map +1 -0
  58. package/dist/lib/database/configure-audit-triggers.js +27 -0
  59. package/dist/lib/database/configure-audit-triggers.js.map +1 -0
  60. package/dist/lib/database/configure-plugin-audit-triggers.d.ts +11 -0
  61. package/dist/lib/database/configure-plugin-audit-triggers.d.ts.map +1 -0
  62. package/dist/lib/database/configure-plugin-audit-triggers.js +23 -0
  63. package/dist/lib/database/configure-plugin-audit-triggers.js.map +1 -0
  64. package/dist/lib/database/generated-tables/audit-log.tables.d.ts +363 -0
  65. package/dist/lib/database/generated-tables/audit-log.tables.d.ts.map +1 -0
  66. package/dist/lib/database/generated-tables/audit-log.tables.js +53 -0
  67. package/dist/lib/database/generated-tables/audit-log.tables.js.map +1 -0
  68. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.d.ts +376 -0
  69. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.d.ts.map +1 -0
  70. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.js +45 -0
  71. package/dist/lib/database/generated-tables/client-diagnostic-events.tables.js.map +1 -0
  72. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.d.ts +76 -0
  73. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.d.ts.map +1 -0
  74. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.js +32 -0
  75. package/dist/lib/database/generated-tables/known-fault-fingerprints.tables.js.map +1 -0
  76. package/dist/lib/database/generated-tables/request-log.tables.d.ts +678 -0
  77. package/dist/lib/database/generated-tables/request-log.tables.d.ts.map +1 -0
  78. package/dist/lib/database/generated-tables/request-log.tables.js +59 -0
  79. package/dist/lib/database/generated-tables/request-log.tables.js.map +1 -0
  80. package/dist/lib/database/migrations/20260910233455_audit.migration.d.ts +5 -0
  81. package/dist/lib/database/migrations/20260910233455_audit.migration.d.ts.map +1 -0
  82. package/dist/lib/database/migrations/20260910233455_audit.migration.js +304 -0
  83. package/dist/lib/database/migrations/20260910233455_audit.migration.js.map +1 -0
  84. package/dist/lib/database/readers/audit-log-reader.interface.d.ts +82 -0
  85. package/dist/lib/database/readers/audit-log-reader.interface.d.ts.map +1 -0
  86. package/dist/lib/database/readers/audit-log-reader.interface.js +10 -0
  87. package/dist/lib/database/readers/audit-log-reader.interface.js.map +1 -0
  88. package/dist/lib/database/readers/drizzle-audit-log-reader.d.ts +16 -0
  89. package/dist/lib/database/readers/drizzle-audit-log-reader.d.ts.map +1 -0
  90. package/dist/lib/database/readers/drizzle-audit-log-reader.js +197 -0
  91. package/dist/lib/database/readers/drizzle-audit-log-reader.js.map +1 -0
  92. package/dist/lib/database/readers/drizzle-fault-alert-reader.d.ts +36 -0
  93. package/dist/lib/database/readers/drizzle-fault-alert-reader.d.ts.map +1 -0
  94. package/dist/lib/database/readers/drizzle-fault-alert-reader.js +113 -0
  95. package/dist/lib/database/readers/drizzle-fault-alert-reader.js.map +1 -0
  96. package/dist/lib/database/readers/drizzle-request-log-reader.d.ts +16 -0
  97. package/dist/lib/database/readers/drizzle-request-log-reader.d.ts.map +1 -0
  98. package/dist/lib/database/readers/drizzle-request-log-reader.js +88 -0
  99. package/dist/lib/database/readers/drizzle-request-log-reader.js.map +1 -0
  100. package/dist/lib/database/readers/fault-alert-reader.interface.d.ts +73 -0
  101. package/dist/lib/database/readers/fault-alert-reader.interface.d.ts.map +1 -0
  102. package/dist/lib/database/readers/fault-alert-reader.interface.js +13 -0
  103. package/dist/lib/database/readers/fault-alert-reader.interface.js.map +1 -0
  104. package/dist/lib/database/readers/request-log-reader.interface.d.ts +64 -0
  105. package/dist/lib/database/readers/request-log-reader.interface.d.ts.map +1 -0
  106. package/dist/lib/database/readers/request-log-reader.interface.js +9 -0
  107. package/dist/lib/database/readers/request-log-reader.interface.js.map +1 -0
  108. package/dist/lib/database/testing/audited-fixture-tables.d.ts +14 -0
  109. package/dist/lib/database/testing/audited-fixture-tables.d.ts.map +1 -0
  110. package/dist/lib/database/testing/audited-fixture-tables.js +46 -0
  111. package/dist/lib/database/testing/audited-fixture-tables.js.map +1 -0
  112. package/dist/lib/database/writers/client-diagnostic-writer.interface.d.ts +20 -0
  113. package/dist/lib/database/writers/client-diagnostic-writer.interface.d.ts.map +1 -0
  114. package/dist/lib/database/writers/client-diagnostic-writer.interface.js +3 -0
  115. package/dist/lib/database/writers/client-diagnostic-writer.interface.js.map +1 -0
  116. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.d.ts +11 -0
  117. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.d.ts.map +1 -0
  118. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.js +46 -0
  119. package/dist/lib/database/writers/drizzle-client-diagnostic-writer.js.map +1 -0
  120. package/dist/lib/database/writers/drizzle-request-log-writer.d.ts +9 -0
  121. package/dist/lib/database/writers/drizzle-request-log-writer.d.ts.map +1 -0
  122. package/dist/lib/database/writers/drizzle-request-log-writer.js +17 -0
  123. package/dist/lib/database/writers/drizzle-request-log-writer.js.map +1 -0
  124. package/dist/lib/database/writers/request-log-writer.interface.d.ts +17 -0
  125. package/dist/lib/database/writers/request-log-writer.interface.d.ts.map +1 -0
  126. package/dist/lib/database/writers/request-log-writer.interface.js +10 -0
  127. package/dist/lib/database/writers/request-log-writer.interface.js.map +1 -0
  128. package/dist/lib/fault-alerts/alert-new-faults.job.d.ts +48 -0
  129. package/dist/lib/fault-alerts/alert-new-faults.job.d.ts.map +1 -0
  130. package/dist/lib/fault-alerts/alert-new-faults.job.js +105 -0
  131. package/dist/lib/fault-alerts/alert-new-faults.job.js.map +1 -0
  132. package/dist/lib/fault-alerts/logic/describe-fault-line.d.ts +16 -0
  133. package/dist/lib/fault-alerts/logic/describe-fault-line.d.ts.map +1 -0
  134. package/dist/lib/fault-alerts/logic/describe-fault-line.js +21 -0
  135. package/dist/lib/fault-alerts/logic/describe-fault-line.js.map +1 -0
  136. package/dist/lib/fault-alerts/logic/summarize-new-faults.d.ts +51 -0
  137. package/dist/lib/fault-alerts/logic/summarize-new-faults.d.ts.map +1 -0
  138. package/dist/lib/fault-alerts/logic/summarize-new-faults.js +56 -0
  139. package/dist/lib/fault-alerts/logic/summarize-new-faults.js.map +1 -0
  140. package/dist/lib/fault-alerts/new-faults.email-template.d.ts +8 -0
  141. package/dist/lib/fault-alerts/new-faults.email-template.d.ts.map +1 -0
  142. package/dist/lib/fault-alerts/new-faults.email-template.js +66 -0
  143. package/dist/lib/fault-alerts/new-faults.email-template.js.map +1 -0
  144. package/dist/lib/jobs/prune-client-diagnostics.job.d.ts +25 -0
  145. package/dist/lib/jobs/prune-client-diagnostics.job.d.ts.map +1 -0
  146. package/dist/lib/jobs/prune-client-diagnostics.job.js +38 -0
  147. package/dist/lib/jobs/prune-client-diagnostics.job.js.map +1 -0
  148. package/dist/lib/jobs/prune-request-log.job.d.ts +23 -0
  149. package/dist/lib/jobs/prune-request-log.job.d.ts.map +1 -0
  150. package/dist/lib/jobs/prune-request-log.job.js +36 -0
  151. package/dist/lib/jobs/prune-request-log.job.js.map +1 -0
  152. package/dist/lib/jobs/retention-window.d.ts +3 -0
  153. package/dist/lib/jobs/retention-window.d.ts.map +1 -0
  154. package/dist/lib/jobs/retention-window.js +10 -0
  155. package/dist/lib/jobs/retention-window.js.map +1 -0
  156. package/dist/lib/request-log/insert-foreign-rows.d.ts +21 -0
  157. package/dist/lib/request-log/insert-foreign-rows.d.ts.map +1 -0
  158. package/dist/lib/request-log/insert-foreign-rows.js +86 -0
  159. package/dist/lib/request-log/insert-foreign-rows.js.map +1 -0
  160. package/dist/lib/request-log/postgres-request-log-sink.d.ts +64 -0
  161. package/dist/lib/request-log/postgres-request-log-sink.d.ts.map +1 -0
  162. package/dist/lib/request-log/postgres-request-log-sink.js +168 -0
  163. package/dist/lib/request-log/postgres-request-log-sink.js.map +1 -0
  164. package/dist/schema.d.ts +6 -0
  165. package/dist/schema.d.ts.map +1 -0
  166. package/dist/schema.js +5 -0
  167. package/dist/schema.js.map +1 -0
  168. package/dist/testing/audit-log-record.factory.d.ts +13 -0
  169. package/dist/testing/audit-log-record.factory.d.ts.map +1 -0
  170. package/dist/testing/audit-log-record.factory.js +46 -0
  171. package/dist/testing/audit-log-record.factory.js.map +1 -0
  172. package/dist/testing/audit-log.factory.d.ts +14 -0
  173. package/dist/testing/audit-log.factory.d.ts.map +1 -0
  174. package/dist/testing/audit-log.factory.js +30 -0
  175. package/dist/testing/audit-log.factory.js.map +1 -0
  176. package/dist/testing/index.d.ts +4 -0
  177. package/dist/testing/index.d.ts.map +1 -0
  178. package/dist/testing/index.js +5 -0
  179. package/dist/testing/index.js.map +1 -0
  180. package/dist/testing/request-log.factory.d.ts +13 -0
  181. package/dist/testing/request-log.factory.d.ts.map +1 -0
  182. package/dist/testing/request-log.factory.js +40 -0
  183. package/dist/testing/request-log.factory.js.map +1 -0
  184. package/package.json +67 -4
  185. package/src/lib/database/migrations/20260910233455_audit.migration.ts +345 -0
@@ -0,0 +1,197 @@
1
+ import { and, asc, desc, eq, gte, inArray, lt, ne, notInArray, sql, } from "drizzle-orm";
2
+ import { alias } from "drizzle-orm/pg-core";
3
+ import { auditLog } from "../generated-tables/audit-log.tables.js";
4
+ import {} from "./audit-log-reader.interface.js";
5
+ /**
6
+ * `recorded_at` to the microsecond as UTC text. A `Date` keeps milliseconds,
7
+ * which is not enough to resume a keyset page exactly (see `AuditLogCursor`).
8
+ */
9
+ const RECORDED_AT_EXACT = sql `to_char(${auditLog.recordedAt} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')`;
10
+ const ROW_COLUMNS = {
11
+ scopeValues: auditLog.scopeValues,
12
+ position: auditLog.position,
13
+ recordedAt: auditLog.recordedAt,
14
+ recordedAtExact: RECORDED_AT_EXACT,
15
+ actorKind: auditLog.actorKind,
16
+ actorId: auditLog.actorId,
17
+ clientId: auditLog.clientId,
18
+ clientName: auditLog.clientName,
19
+ requestId: auditLog.requestId,
20
+ reason: auditLog.reason,
21
+ userId: auditLog.userId,
22
+ tableName: auditLog.tableName,
23
+ rowKey: auditLog.rowKey,
24
+ operation: auditLog.operation,
25
+ changedColumns: auditLog.changedColumns,
26
+ rowBefore: auditLog.rowBefore,
27
+ rowAfter: auditLog.rowAfter,
28
+ };
29
+ /** Keyset reads on the audit trail with exact stored cursor timestamps. */
30
+ export class DrizzleAuditLogReader {
31
+ db;
32
+ constructor(db) {
33
+ this.db = db;
34
+ }
35
+ async listRows(scope, filter, olderThan, limit) {
36
+ const shownTables = filter.tableNames ?? [];
37
+ const rows = await this.db
38
+ .select(ROW_COLUMNS)
39
+ .from(auditLog)
40
+ .where(scope.kind === "application" && shownTables.length > 0
41
+ ? inArray(auditLog.position, this.newestPerShownTable(scope, filter, shownTables, olderThan, limit))
42
+ : and(...scopeConditions(scope, auditLog), ...filterConditions(filter, auditLog), olderThanAuditCursor(olderThan, auditLog)))
43
+ .orderBy(desc(auditLog.recordedAt), desc(auditLog.position))
44
+ .limit(limit);
45
+ return rows.map(toAuditLogRecord);
46
+ }
47
+ /** Match scope and table through the combined GIN index before paging. */
48
+ newestPerShownTable(scope, filter, shownTables, olderThan, limit) {
49
+ const candidate = alias(auditLog, "candidate");
50
+ const newestOfOneTable = this.db
51
+ .select({
52
+ position: candidate.position,
53
+ recordedAt: candidate.recordedAt,
54
+ })
55
+ .from(candidate)
56
+ .where(and(...scopeConditions(scope, candidate), sql `ARRAY[${candidate.tableName}] @> ARRAY[shown.table_name]`, ...filterConditions({ ...filter, tableNames: [] }, candidate), olderThanAuditCursor(olderThan, candidate)))
57
+ .orderBy(desc(candidate.recordedAt), desc(candidate.position))
58
+ .limit(limit)
59
+ .as("picked");
60
+ const tables = sql.join(shownTables.map((table) => sql `${table}`), sql `, `);
61
+ return this.db
62
+ .select({ position: newestOfOneTable.position })
63
+ .from(sql `unnest(ARRAY[${tables}]::text[]) AS shown(table_name)`)
64
+ .crossJoinLateral(newestOfOneTable)
65
+ .orderBy(desc(newestOfOneTable.recordedAt), desc(newestOfOneTable.position))
66
+ .limit(limit);
67
+ }
68
+ async rowsForRequest(requestId) {
69
+ const rows = await this.db
70
+ .select(ROW_COLUMNS)
71
+ .from(auditLog)
72
+ .where(eq(auditLog.requestId, requestId))
73
+ .orderBy(asc(auditLog.position));
74
+ return rows.map(toAuditLogRecord);
75
+ }
76
+ async countRecordsForRequest(scope, requestId, filter) {
77
+ const [counted] = await this.db
78
+ .select({
79
+ total: sql `count(distinct (${auditLog.tableName}, ${auditLog.rowKey}))::int`,
80
+ })
81
+ .from(auditLog)
82
+ .where(and(eq(auditLog.requestId, requestId), ...scopeConditions(scope, auditLog), ...shownUpdateCondition(filter, auditLog)));
83
+ return counted?.total ?? 0;
84
+ }
85
+ async rowAt(position) {
86
+ const [row] = await this.db
87
+ .select(ROW_COLUMNS)
88
+ .from(auditLog)
89
+ .where(eq(auditLog.position, position))
90
+ .limit(1);
91
+ return row === undefined ? null : toAuditLogRecord(row);
92
+ }
93
+ }
94
+ function scopeConditions(scope, table) {
95
+ if (scope.kind === "platform")
96
+ return [];
97
+ return Object.keys(scope.values).length === 0
98
+ ? [sql `false`]
99
+ : [sql `${table.scopeValues} @> ${JSON.stringify(scope.values)}::jsonb`];
100
+ }
101
+ function filterConditions(filter, table) {
102
+ return [
103
+ ...whoAndWhenConditions(filter, table),
104
+ ...whatConditions(filter, table),
105
+ ...shownUpdateCondition(filter, table),
106
+ ];
107
+ }
108
+ /** The date range and the person. */
109
+ function whoAndWhenConditions(filter, table) {
110
+ return [
111
+ ...(filter.from === undefined ? [] : [gte(table.recordedAt, filter.from)]),
112
+ ...(filter.to === undefined ? [] : [lt(table.recordedAt, filter.to)]),
113
+ ...(filter.userId === undefined ? [] : [eq(table.userId, filter.userId)]),
114
+ ...(filter.excludeRequestId === undefined
115
+ ? []
116
+ : [ne(table.requestId, filter.excludeRequestId)]),
117
+ ];
118
+ }
119
+ /**
120
+ * An update to a named table that changed none of its shown columns is left
121
+ * out. The map travels as one jsonb parameter, so the condition is the same
122
+ * size whatever the number of tables.
123
+ */
124
+ function shownUpdateCondition(filter, table) {
125
+ const shown = filter.shownUpdateColumns;
126
+ if (shown === undefined || Object.keys(shown).length === 0)
127
+ return [];
128
+ const columnsOfTable = sql `(${JSON.stringify(shown)}::jsonb -> ${table.tableName})`;
129
+ return [
130
+ sql `NOT (
131
+ ${table.operation} = 'update'
132
+ AND ${columnsOfTable} IS NOT NULL
133
+ AND NOT coalesce(
134
+ ${table.changedColumns} && ARRAY(SELECT jsonb_array_elements_text(${columnsOfTable})),
135
+ false
136
+ )
137
+ )`,
138
+ ];
139
+ }
140
+ /** The kind of actor, the tables and the one record. */
141
+ function whatConditions(filter, table) {
142
+ const actorKinds = filter.actorKinds ?? [];
143
+ const tableNames = filter.tableNames ?? [];
144
+ const excluded = filter.excludeTableNames ?? [];
145
+ return [
146
+ ...(actorKinds.length === 0
147
+ ? []
148
+ : [inArray(table.actorKind, [...actorKinds])]),
149
+ ...(tableNames.length === 0
150
+ ? []
151
+ : [inArray(table.tableName, [...tableNames])]),
152
+ ...(excluded.length === 0
153
+ ? []
154
+ : [notInArray(table.tableName, [...excluded])]),
155
+ ...(filter.record === undefined
156
+ ? []
157
+ : [
158
+ eq(table.tableName, filter.record.tableName),
159
+ sql `${table.rowKey} = ${JSON.stringify(filter.record.rowKey)}::jsonb`,
160
+ ]),
161
+ ];
162
+ }
163
+ /** Strictly after the cursor in `(recorded_at DESC, position DESC)` order. */
164
+ function olderThanAuditCursor(olderThan, table) {
165
+ if (olderThan === null)
166
+ return undefined;
167
+ return sql `(${table.recordedAt}, ${table.position}) < (${olderThan.recordedAt}::timestamptz, ${olderThan.position})`;
168
+ }
169
+ function toAuditLogRecord(row) {
170
+ return {
171
+ scopeValues: row.scopeValues,
172
+ position: row.position,
173
+ recordedAt: row.recordedAt,
174
+ actorKind: row.actorKind,
175
+ actorId: row.actorId,
176
+ clientId: row.clientId,
177
+ clientName: row.clientName,
178
+ requestId: row.requestId,
179
+ reason: row.reason,
180
+ userId: row.userId,
181
+ tableName: row.tableName,
182
+ rowKey: row.rowKey,
183
+ operation: toOperation(row.operation),
184
+ changedColumns: row.changedColumns,
185
+ rowBefore: row.rowBefore,
186
+ rowAfter: row.rowAfter,
187
+ cursor: { recordedAt: row.recordedAtExact, position: row.position },
188
+ };
189
+ }
190
+ /** The table's check constraint admits exactly these three. */
191
+ function toOperation(stored) {
192
+ if (stored === "insert" || stored === "update" || stored === "delete") {
193
+ return stored;
194
+ }
195
+ throw new Error(`audit_log.operation holds an unknown value: ${stored}`);
196
+ }
197
+ //# sourceMappingURL=drizzle-audit-log-reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle-audit-log-reader.js","sourceRoot":"","sources":["../../../../src/lib/database/readers/drizzle-audit-log-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,GAAG,EACH,IAAI,EACJ,EAAE,EACF,GAAG,EACH,OAAO,EACP,EAAE,EACF,EAAE,EACF,UAAU,EACV,GAAG,GACJ,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,KAAK,EAAiB,MAAM,qBAAqB,CAAC;AAK3D,OAAO,EAAE,QAAQ,EAAE,MAAM,yCAAyC,CAAC;AAEnE,OAAO,EAKN,MAAM,iCAAiC,CAAC;AAEzC;;;GAGG;AACH,MAAM,iBAAiB,GAAG,GAAG,CAAQ,WAAW,QAAQ,CAAC,UAAU,uDAAuD,CAAC;AAE3H,MAAM,WAAW,GAAG;IAClB,WAAW,EAAE,QAAQ,CAAC,WAAW;IACjC,QAAQ,EAAE,QAAQ,CAAC,QAAQ;IAC3B,UAAU,EAAE,QAAQ,CAAC,UAAU;IAC/B,eAAe,EAAE,iBAAiB;IAClC,SAAS,EAAE,QAAQ,CAAC,SAAS;IAC7B,OAAO,EAAE,QAAQ,CAAC,OAAO;IACzB,QAAQ,EAAE,QAAQ,CAAC,QAAQ;IAC3B,UAAU,EAAE,QAAQ,CAAC,UAAU;IAC/B,SAAS,EAAE,QAAQ,CAAC,SAAS;IAC7B,MAAM,EAAE,QAAQ,CAAC,MAAM;IACvB,MAAM,EAAE,QAAQ,CAAC,MAAM;IACvB,SAAS,EAAE,QAAQ,CAAC,SAAS;IAC7B,MAAM,EAAE,QAAQ,CAAC,M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AClB,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,SAAS,EAAE,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC;QACrC,cAAc,EAAE,GAAG,CAAC,cAAc;QAClC,SAAS,EAAE,GAAG,CAAC,SAAS;QACxB,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,MAAM,EAAE,EAAE,UAAU,EAAE,GAAG,CAAC,eAAe,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE;KACpE,CAAC;AACJ,CAAC;AAED,+DAA+D;AAC/D,SAAS,WAAW,CAAC,MAAc;IACjC,IAAI,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,QAAQ,EAAE,CAAC;QACtE,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,+CAA+C,MAAM,EAAE,CAAC,CAAC;AAC3E,CAAC"}
@@ -0,0 +1,36 @@
1
+ import type { DrizzleHandle } from "@dads-framework/postgres";
2
+ import type { FaultAlertReader } from "./fault-alert-reader.interface.js";
3
+ import { type AlertedFault, type FaultGroup } from "./fault-alert-reader.interface.js";
4
+ /**
5
+ * Drizzle adapter for "what is newly broken".
6
+ *
7
+ * Both reads are the same shape: everything inside the window, grouped by
8
+ * fingerprint, minus what `known_fault_fingerprints` says has already been
9
+ * reported. The window bounds the work — it is a range scan on `recorded_at`
10
+ * rather than a group over the whole table — and the memory bounds the noise.
11
+ * Neither alone is enough: the window cannot tell a returning fault from a new
12
+ * one once the old rows are pruned, and the memory cannot tell a fault that is
13
+ * happening now from one that stopped last spring.
14
+ *
15
+ * `max(...)` picks a representative route or release rather than listing every
16
+ * one: the alert's job is to say something is newly broken and roughly where,
17
+ * and the fingerprint is the handle for looking properly.
18
+ */
19
+ export declare class DrizzleFaultAlertReader implements FaultAlertReader {
20
+ private readonly db;
21
+ constructor(db: DrizzleHandle);
22
+ unalertedServerFaultsSince(since: Date): Promise<readonly FaultGroup[]>;
23
+ unalertedClientFaultsSince(since: Date): Promise<readonly FaultGroup[]>;
24
+ hasRememberedAnyFault(): Promise<boolean>;
25
+ rememberFaultsSeenBefore(before: Date): Promise<number>;
26
+ rememberAlertedFaults(faults: readonly AlertedFault[]): Promise<void>;
27
+ /**
28
+ * The half of "new" that survives the fault tables being pruned.
29
+ *
30
+ * `NOT EXISTS` rather than a join so a fingerprint with a memory row costs
31
+ * one probe of the composite primary key and stops, and so the grouped count
32
+ * stays the fault's own however many memory rows exist.
33
+ */
34
+ private notRemembered;
35
+ }
36
+ //# sourceMappingURL=drizzle-fault-alert-reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle-fault-alert-reader.d.ts","sourceRoot":"","sources":["../../../../src/lib/database/readers/drizzle-fault-alert-reader.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAK9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,UAAU,EAEhB,MAAM,mCAAmC,CAAC;AAE3C;;;;;;;;;;;;;;GAcG;AACH,qBAAa,uBAAwB,YAAW,gBAAgB;IAClD,OAAO,CAAC,QAAQ,CAAC,EAAE;IAA/B,YAA6B,EAAE,EAAE,aAAa,EAAI;IAE5C,0BAA0B,CAC9B,KAAK,EAAE,IAAI,GACV,OAAO,CAAC,SAAS,UAAU,EAAE,CAAC,CAkBhC;IAEK,0BAA0B,CAC9B,KAAK,EAAE,IAAI,GACV,OAAO,CAAC,SAAS,UAAU,EAAE,CAAC,CAmBhC;IAEK,qBAAqB,IAAI,OAAO,CAAC,OAAO,CAAC,CAM9C;IAEK,wBAAwB,CAAC,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAgB5D;IAEK,qBAAqB,CAAC,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAY1E;IAED;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;CAatB"}
@@ -0,0 +1,113 @@
1
+ import { and, count, eq, gte, isNotNull, max, ne, notExists, sql, } from "drizzle-orm";
2
+ import { clientDiagnosticEvents } from "../generated-tables/client-diagnostic-events.tables.js";
3
+ import { knownFaultFingerprints } from "../generated-tables/known-fault-fingerprints.tables.js";
4
+ import { requestLog } from "../generated-tables/request-log.tables.js";
5
+ import {} from "./fault-alert-reader.interface.js";
6
+ /**
7
+ * Drizzle adapter for "what is newly broken".
8
+ *
9
+ * Both reads are the same shape: everything inside the window, grouped by
10
+ * fingerprint, minus what `known_fault_fingerprints` says has already been
11
+ * reported. The window bounds the work — it is a range scan on `recorded_at`
12
+ * rather than a group over the whole table — and the memory bounds the noise.
13
+ * Neither alone is enough: the window cannot tell a returning fault from a new
14
+ * one once the old rows are pruned, and the memory cannot tell a fault that is
15
+ * happening now from one that stopped last spring.
16
+ *
17
+ * `max(...)` picks a representative route or release rather than listing every
18
+ * one: the alert's job is to say something is newly broken and roughly where,
19
+ * and the fingerprint is the handle for looking properly.
20
+ */
21
+ export class DrizzleFaultAlertReader {
22
+ db;
23
+ constructor(db) {
24
+ this.db = db;
25
+ }
26
+ async unalertedServerFaultsSince(since) {
27
+ const rows = await this.db
28
+ .select({
29
+ fingerprint: requestLog.exceptionFingerprint,
30
+ type: max(requestLog.exceptionType),
31
+ occurrences: count(),
32
+ where: max(requestLog.routeId),
33
+ })
34
+ .from(requestLog)
35
+ .where(and(isNotNull(requestLog.exceptionFingerprint), gte(requestLog.recordedAt, since), this.notRemembered("server", requestLog.exceptionFingerprint)))
36
+ .groupBy(requestLog.exceptionFingerprint);
37
+ return rows.map(toFaultGroup);
38
+ }
39
+ async unalertedClientFaultsSince(since) {
40
+ const rows = await this.db
41
+ .select({
42
+ fingerprint: clientDiagnosticEvents.fingerprint,
43
+ type: max(clientDiagnosticEvents.kind),
44
+ occurrences: count(),
45
+ where: max(clientDiagnosticEvents.release),
46
+ })
47
+ .from(clientDiagnosticEvents)
48
+ .where(and(gte(clientDiagnosticEvents.recordedAt, since),
49
+ // An info row is a measurement (the first-screen time), not a fault.
50
+ ne(clientDiagnosticEvents.severity, "info"), this.notRemembered("client", clientDiagnosticEvents.fingerprint)))
51
+ .groupBy(clientDiagnosticEvents.fingerprint);
52
+ return rows.map(toFaultGroup);
53
+ }
54
+ async hasRememberedAnyFault() {
55
+ const rows = await this.db
56
+ .select({ fingerprint: knownFaultFingerprints.fingerprint })
57
+ .from(knownFaultFingerprints)
58
+ .limit(1);
59
+ return rows.length > 0;
60
+ }
61
+ async rememberFaultsSeenBefore(before) {
62
+ const seeded = await this.db.execute(sql `
63
+ INSERT INTO ${knownFaultFingerprints} (origin, fingerprint)
64
+ SELECT 'server', ${requestLog.exceptionFingerprint}
65
+ FROM ${requestLog}
66
+ WHERE ${requestLog.exceptionFingerprint} IS NOT NULL
67
+ AND ${requestLog.recordedAt} < ${before}
68
+ GROUP BY ${requestLog.exceptionFingerprint}
69
+ UNION
70
+ SELECT 'client', ${clientDiagnosticEvents.fingerprint}
71
+ FROM ${clientDiagnosticEvents}
72
+ WHERE ${clientDiagnosticEvents.recordedAt} < ${before}
73
+ GROUP BY ${clientDiagnosticEvents.fingerprint}
74
+ ON CONFLICT DO NOTHING
75
+ `);
76
+ return seeded.rowCount ?? 0;
77
+ }
78
+ async rememberAlertedFaults(faults) {
79
+ if (faults.length === 0)
80
+ return;
81
+ await this.db
82
+ .insert(knownFaultFingerprints)
83
+ .values(faults.map((fault) => ({
84
+ origin: fault.origin,
85
+ fingerprint: fault.fingerprint,
86
+ })))
87
+ // Two workers can sweep the same hour; the second one has nothing to add.
88
+ .onConflictDoNothing();
89
+ }
90
+ /**
91
+ * The half of "new" that survives the fault tables being pruned.
92
+ *
93
+ * `NOT EXISTS` rather than a join so a fingerprint with a memory row costs
94
+ * one probe of the composite primary key and stops, and so the grouped count
95
+ * stays the fault's own however many memory rows exist.
96
+ */
97
+ notRemembered(origin, fingerprint) {
98
+ return notExists(this.db
99
+ .select({ remembered: sql `1` })
100
+ .from(knownFaultFingerprints)
101
+ .where(and(eq(knownFaultFingerprints.origin, origin), eq(knownFaultFingerprints.fingerprint, fingerprint))));
102
+ }
103
+ }
104
+ /** Aggregates are nullable to SQL; a grouped row always has them. */
105
+ function toFaultGroup(row) {
106
+ return {
107
+ fingerprint: row.fingerprint ?? "unknown",
108
+ type: row.type ?? "unknown",
109
+ occurrences: row.occurrences,
110
+ where: row.where,
111
+ };
112
+ }
113
+ //# sourceMappingURL=drizzle-fault-alert-reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle-fault-alert-reader.js","sourceRoot":"","sources":["../../../../src/lib/database/readers/drizzle-fault-alert-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,KAAK,EACL,EAAE,EACF,GAAG,EACH,SAAS,EACT,GAAG,EACH,EAAE,EACF,SAAS,EACT,GAAG,GACJ,MAAM,aAAa,CAAC;AAKrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,wDAAwD,CAAC;AAChG,OAAO,EAAE,sBAAsB,EAAE,MAAM,wDAAwD,CAAC;AAChG,OAAO,EAAE,UAAU,EAAE,MAAM,2CAA2C,CAAC;AAEvE,OAAO,EAIN,MAAM,mCAAmC,CAAC;AAE3C;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,uBAAuB;IACL,EAAE;IAA/B,YAA6B,EAAiB;kBAAjB,EAAE;IAAkB,CAAC;IAElD,KAAK,CAAC,0BAA0B,CAC9B,KAAW;QAEX,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,EAAE;aACvB,MAAM,CAAC;YACN,WAAW,EAAE,UAAU,CAAC,oBAAoB;YAC5C,IAAI,EAAE,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC;YACnC,WAAW,EAAE,KAAK,EAAE;YACpB,KAAK,EAAE,GAAG,CAAC,UAAU,CAAC,OAA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AAsB,CAAC,WAAW,EAAE,WAAW,CAAC,CACpD,CACF,CACJ,CAAC;IACJ,CAAC;CACF;AAED,qEAAqE;AACrE,SAAS,YAAY,CAAC,GAKrB;IACC,OAAO;QACL,WAAW,EAAE,GAAG,CAAC,WAAW,IAAI,SAAS;QACzC,IAAI,EAAE,GAAG,CAAC,IAAI,IAAI,SAAS;QAC3B,WAAW,EAAE,GAAG,CAAC,WAAW;QAC5B,KAAK,EAAE,GAAG,CAAC,KAAK;KACjB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { DrizzleHandle } from "@dads-framework/postgres";
2
+ import type { RequestLogReader } from "./request-log-reader.interface.js";
3
+ import { type RefusedRequest, type RefusedRequestFilter, type RequestLogCursor, type RequestLogEntry } from "./request-log-reader.interface.js";
4
+ /**
5
+ * Drizzle adapter for reading `request_log`: one request by its primary key
6
+ * for an audit entry's detail, and the refusals in a bounded window. The
7
+ * window keeps the refusal read on `request_log_recorded_at_idx`; the table is
8
+ * pruned to 90 days, so it never reaches further back than that.
9
+ */
10
+ export declare class DrizzleRequestLogReader implements RequestLogReader {
11
+ private readonly db;
12
+ constructor(db: DrizzleHandle);
13
+ findRequest(requestId: string): Promise<RequestLogEntry | null>;
14
+ listRefusals(filter: RefusedRequestFilter, olderThan: RequestLogCursor | null, limit: number): Promise<readonly RefusedRequest[]>;
15
+ }
16
+ //# sourceMappingURL=drizzle-request-log-reader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle-request-log-reader.d.ts","sourceRoot":"","sources":["../../../../src/lib/database/readers/drizzle-request-log-reader.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAG9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACrB,MAAM,mCAAmC,CAAC;AAoB3C;;;;;GAKG;AACH,qBAAa,uBAAwB,YAAW,gBAAgB;IAClD,OAAO,CAAC,QAAQ,CAAC,EAAE;IAA/B,YAA6B,EAAE,EAAE,aAAa,EAAI;IAE5C,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAmBpE;IAEK,YAAY,CAChB,MAAM,EAAE,oBAAoB,EAC5B,SAAS,EAAE,gBAAgB,GAAG,IAAI,EAClC,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,SAAS,cAAc,EAAE,CAAC,CA8BpC;CACF"}
@@ -0,0 +1,88 @@
1
+ import { and, desc, eq, gte, inArray, ne, sql } from "drizzle-orm";
2
+ import { requestIdSchema } from "@dads-framework/http";
3
+ import { requestLog } from "../generated-tables/request-log.tables.js";
4
+ import {} from "./request-log-reader.interface.js";
5
+ /** The decisions that mean the request was refused rather than served or broken. */
6
+ const REFUSAL_DECISIONS = ["denied", "not-found", "unauthenticated"];
7
+ /**
8
+ * A refusal worth reading has someone behind it. Requests before sign-in
9
+ * (`anonymous`, no actor id) are mostly sign-in pages and probes, and would
10
+ * bury the refusals of people who were signed in.
11
+ */
12
+ const SIGNED_IN_ACTOR = [
13
+ ne(requestLog.actorKind, "anonymous"),
14
+ ne(requestLog.actorId, ""),
15
+ ];
16
+ /** A refusal must come from this application's enforcement. */
17
+ const LOCAL_ENFORCEMENT = eq(requestLog.imported, false);
18
+ const RECORDED_AT_EXACT = sql `to_char(${requestLog.recordedAt} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"Z"')`;
19
+ /**
20
+ * Drizzle adapter for reading `request_log`: one request by its primary key
21
+ * for an audit entry's detail, and the refusals in a bounded window. The
22
+ * window keeps the refusal read on `request_log_recorded_at_idx`; the table is
23
+ * pruned to 90 days, so it never reaches further back than that.
24
+ */
25
+ export class DrizzleRequestLogReader {
26
+ db;
27
+ constructor(db) {
28
+ this.db = db;
29
+ }
30
+ async findRequest(requestId) {
31
+ // A value that is not a request id names no request.
32
+ const parsed = requestIdSchema.safeParse(requestId);
33
+ if (!parsed.success)
34
+ return null;
35
+ const [row] = await this.db
36
+ .select({
37
+ requestId: requestLog.requestId,
38
+ recordedAt: requestLog.recordedAt,
39
+ method: requestLog.method,
40
+ routeId: requestLog.routeId,
41
+ status: requestLog.status,
42
+ ip: requestLog.ip,
43
+ userAgent: requestLog.userAgent,
44
+ clientContext: requestLog.clientContext,
45
+ })
46
+ .from(requestLog)
47
+ .where(eq(requestLog.requestId, parsed.data))
48
+ .limit(1);
49
+ return row ?? null;
50
+ }
51
+ async listRefusals(filter, olderThan, limit) {
52
+ const rows = await this.db
53
+ .select({
54
+ requestId: requestLog.requestId,
55
+ recordedAt: requestLog.recordedAt,
56
+ recordedAtExact: RECORDED_AT_EXACT,
57
+ actorKind: requestLog.actorKind,
58
+ actorId: requestLog.actorId,
59
+ decision: requestLog.decision,
60
+ permission: requestLog.permission,
61
+ routeId: requestLog.routeId,
62
+ method: requestLog.method,
63
+ })
64
+ .from(requestLog)
65
+ .where(and(gte(requestLog.recordedAt, filter.since), inArray(requestLog.decision, REFUSAL_DECISIONS), ...SIGNED_IN_ACTOR, LOCAL_ENFORCEMENT, userCondition(filter.userId), olderThanRequestCursor(olderThan)))
66
+ .orderBy(desc(requestLog.recordedAt), desc(requestLog.requestId))
67
+ .limit(limit);
68
+ return rows.map(({ recordedAtExact, ...row }) => ({
69
+ ...row,
70
+ cursor: { recordedAt: recordedAtExact, requestId: row.requestId },
71
+ }));
72
+ }
73
+ }
74
+ /** Restrict refusals to an authenticated user. */
75
+ function userCondition(userId) {
76
+ if (userId === undefined)
77
+ return undefined;
78
+ return and(eq(requestLog.actorKind, "user"), eq(requestLog.actorId, userId));
79
+ }
80
+ function olderThanRequestCursor(olderThan) {
81
+ if (olderThan === null)
82
+ return undefined;
83
+ if (olderThan.requestId === null) {
84
+ return sql `${requestLog.recordedAt} < ${olderThan.recordedAt}::timestamptz`;
85
+ }
86
+ return sql `(${requestLog.recordedAt}, ${requestLog.requestId}) < (${olderThan.recordedAt}::timestamptz, ${olderThan.requestId})`;
87
+ }
88
+ //# sourceMappingURL=drizzle-request-log-reader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle-request-log-reader.js","sourceRoot":"","sources":["../../../../src/lib/database/readers/drizzle-request-log-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAGnE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAGvD,OAAO,EAAE,UAAU,EAAE,MAAM,2CAA2C,CAAC;AAEvE,OAAO,EAKN,MAAM,mCAAmC,CAAC;AAE3C,oFAAoF;AACpF,MAAM,iBAAiB,GAAG,CAAC,QAAQ,EAAE,WAAW,EAAE,iBAAiB,CAAC,CAAC;AAErE;;;;GAIG;AACH,MAAM,eAAe,GAAG;IACtB,EAAE,CAAC,UAAU,CAAC,SAAS,EAAE,WAAW,CAAC;IACrC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC;CAC3B,CAAC;AAEF,+DAA+D;AAC/D,MAAM,iBAAiB,GAAG,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;AAEzD,MAAM,iBAAiB,GAAG,GAAG,CAAQ,WAAW,UAAU,CAAC,UAAU,uDAAuD,CAAC;AAE7H;;;;;GAKG;AACH,MAAM,OAAO,uBAAuB;IACL,EAAE;IAA/B,YAA6B,EAAiB;kBAAjB,EAAE;IAAkB,CAAC;IAElD,KAAK,CAAC,WAAW,CAAC,SAAiB;QACjC,qDAAqD;QACrD,MAAM,MAAM,GAAG,eAAe,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;QACpD,IAAI,CAAC,MAAM,CAAC,OAAO;YAAE,OAAO,IAAI,CAA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AG,eAAe,EAClB,iBAAiB,EACjB,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,EAC5B,sBAAsB,CAAC,SAAS,CAAC,CAClC,CACF;aACA,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;aAChE,KAAK,CAAC,KAAK,CAAC,CAAC;QAChB,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,eAAe,EAAE,GAAG,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC;YAChD,GAAG,GAAG;YACN,MAAM,EAAE,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE;SAClE,CAAC,CAAC,CAAC;IACN,CAAC;CACF;AAED,kDAAkD;AAClD,SAAS,aAAa,CAAC,MAA0B;IAC/C,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC3C,OAAO,GAAG,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;AAC/E,CAAC;AAED,SAAS,sBAAsB,CAC7B,SAAkC;IAElC,IAAI,SAAS,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IACzC,IAAI,SAAS,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;QACjC,OAAO,GAAG,CAAA,GAAG,UAAU,CAAC,UAAU,MAAM,SAAS,CAAC,UAAU,eAAe,CAAC;IAC9E,CAAC;IACD,OAAO,GAAG,CAAA,IAAI,UAAU,CAAC,UAAU,KAAK,UAAU,CAAC,SAAS,QAAQ,SAAS,CAAC,UAAU,kBAAkB,SAAS,CAAC,SAAS,GAAG,CAAC;AACnI,CAAC"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * No behavior lives in this file. The port the fault-alert job holds, and the
3
+ * injection token composition binds. `DrizzleFaultAlertReader` implements it;
4
+ * a fake stands in for the job's unit tests, which is what lets the "what is
5
+ * worth waking someone for" rules be proved without a database.
6
+ *
7
+ * The port reads faults AND remembers which ones have been alerted, because
8
+ * those are one decision: "new" is defined by the memory, and a port that
9
+ * could read without remembering would let a caller alert twice.
10
+ */
11
+ /** Which fault table a fingerprint came from. Part of its identity. */
12
+ export type FaultOrigin = "server" | "client";
13
+ /** One fault type, as the alert needs to describe it. */
14
+ export type FaultGroup = {
15
+ /** The stable grouping key — the thing that makes this ONE fault, not many. */
16
+ readonly fingerprint: string;
17
+ /** The exception class, or the client fault kind. */
18
+ readonly type: string;
19
+ /** How many times it happened inside the window being swept. */
20
+ readonly occurrences: number;
21
+ /** Where it happened — a route pattern, or a client release. */
22
+ readonly where: string | null;
23
+ };
24
+ /** A fault type the alert has now told somebody about. */
25
+ export type AlertedFault = {
26
+ readonly origin: FaultOrigin;
27
+ readonly fingerprint: string;
28
+ };
29
+ export declare abstract class FaultAlertReader {
30
+ /**
31
+ * Server faults that happened at or after `since` and have never been
32
+ * alerted on.
33
+ *
34
+ * "Never been alerted" is a fact in `known_fault_fingerprints`, not an
35
+ * inference from the fault tables. Those are pruned — 90 days for
36
+ * `request_log`, 30 for client diagnostics — so a fault that goes quiet for
37
+ * a month and comes back has no old rows left to prove it is not new.
38
+ * Asking the memory instead is what makes "new" survive retention, and it
39
+ * is also what makes the sweep idempotent: the window can be any length
40
+ * without a fingerprint being reported twice.
41
+ */
42
+ abstract unalertedServerFaultsSince(since: Date): Promise<readonly FaultGroup[]>;
43
+ /** Client faults since `since` that have never been alerted on. */
44
+ abstract unalertedClientFaultsSince(since: Date): Promise<readonly FaultGroup[]>;
45
+ /**
46
+ * Whether anything has ever been remembered.
47
+ *
48
+ * False means alerting has never run here, and every fault still on record
49
+ * would read as new — see `rememberFaultsSeenBefore`.
50
+ */
51
+ abstract hasRememberedAnyFault(): Promise<boolean>;
52
+ /**
53
+ * Remember every fault type that was already happening before `before`,
54
+ * alerting on none of them, and answer how many were recorded.
55
+ *
56
+ * Runs while the memory is empty. Without it, switching alerting on would
57
+ * report months of known problems in one email — the exact thing that
58
+ * teaches people to filter the channel on day one. It deliberately does
59
+ * *not* swallow faults that only started inside the window: on a system
60
+ * where nothing has broken yet this records nothing, and the first fault to
61
+ * ever happen still gets an alert.
62
+ */
63
+ abstract rememberFaultsSeenBefore(before: Date): Promise<number>;
64
+ /**
65
+ * Remember the faults an alert just went out about.
66
+ *
67
+ * Called after the mail is enqueued, never before: an alert that is
68
+ * remembered and then not sent is one nobody ever hears about, while the
69
+ * reverse costs a duplicate email on a retry.
70
+ */
71
+ abstract rememberAlertedFaults(faults: readonly AlertedFault[]): Promise<void>;
72
+ }
73
+ //# sourceMappingURL=fault-alert-reader.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fault-alert-reader.interface.d.ts","sourceRoot":"","sources":["../../../../src/lib/database/readers/fault-alert-reader.interface.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,uEAAuE;AACvE,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE9C,yDAAyD;AACzD,MAAM,MAAM,UAAU,GAAG;IACvB,+EAA+E;IAC/E,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,qDAAqD;IACrD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,gEAAgE;IAChE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,gEAAgE;IAChE,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B,CAAC;AAEF,0DAA0D;AAC1D,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,8BAAsB,gBAAgB;IACpC;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,0BAA0B,CACjC,KAAK,EAAE,IAAI,GACV,OAAO,CAAC,SAAS,UAAU,EAAE,CAAC,CAAC;IAElC,mEAAmE;IACnE,QAAQ,CAAC,0BAA0B,CACjC,KAAK,EAAE,IAAI,GACV,OAAO,CAAC,SAAS,UAAU,EAAE,CAAC,CAAC;IAElC;;;;;OAKG;IACH,QAAQ,CAAC,qBAAqB,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAEnD;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,wBAAwB,CAAC,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAEjE;;;;;;OAMG;IACH,QAAQ,CAAC,qBAAqB,CAC5B,MAAM,EAAE,SAAS,YAAY,EAAE,GAC9B,OAAO,CAAC,IAAI,CAAC,CAAC;CAClB"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * No behavior lives in this file. The port the fault-alert job holds, and the
3
+ * injection token composition binds. `DrizzleFaultAlertReader` implements it;
4
+ * a fake stands in for the job's unit tests, which is what lets the "what is
5
+ * worth waking someone for" rules be proved without a database.
6
+ *
7
+ * The port reads faults AND remembers which ones have been alerted, because
8
+ * those are one decision: "new" is defined by the memory, and a port that
9
+ * could read without remembering would let a caller alert twice.
10
+ */
11
+ export class FaultAlertReader {
12
+ }
13
+ //# sourceMappingURL=fault-alert-reader.interface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fault-alert-reader.interface.js","sourceRoot":"","sources":["../../../../src/lib/database/readers/fault-alert-reader.interface.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAuBH,MAAM,OAAgB,gBAAgB;CAqDrC"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * One request's environment, as `request_log` keeps it (90 days): how it
3
+ * arrived, where from, and what it was told.
4
+ */
5
+ export type RequestLogEntry = {
6
+ readonly requestId: string;
7
+ readonly recordedAt: Date;
8
+ readonly method: string;
9
+ /** The matched route template, never the raw path; null when none matched. */
10
+ readonly routeId: string | null;
11
+ readonly status: number;
12
+ readonly ip: string | null;
13
+ readonly userAgent: string | null;
14
+ /** The decoded `x-client-context` header, as stored. */
15
+ readonly clientContext: Readonly<Record<string, unknown>> | null;
16
+ };
17
+ /**
18
+ * Where the previous page of refusals stopped. `recordedAt` is the stored
19
+ * instant to the microsecond as UTC text; `requestId` breaks ties within one
20
+ * microsecond and is null when the caller could not carry it.
21
+ */
22
+ export type RequestLogCursor = {
23
+ readonly recordedAt: string;
24
+ readonly requestId: string | null;
25
+ };
26
+ /** A signed-in actor's request that was refused: denied, not found, or unauthenticated. */
27
+ export type RefusedRequest = {
28
+ readonly requestId: string;
29
+ readonly recordedAt: Date;
30
+ readonly actorKind: string;
31
+ readonly actorId: string;
32
+ /** The staff sign-in-as session the request was made under; null otherwise. */
33
+ /** `denied`, `not-found` or `unauthenticated`. */
34
+ readonly decision: string;
35
+ readonly permission: string | null;
36
+ readonly routeId: string | null;
37
+ readonly method: string;
38
+ /** This row's own keyset position, to resume a page after it. */
39
+ readonly cursor: RequestLogCursor;
40
+ };
41
+ /** Narrowing for refusals; they combine with AND. */
42
+ export type RefusedRequestFilter = {
43
+ /** Inclusive lower bound on `recorded_at`; the log keeps 90 days anyway. */
44
+ readonly since: Date;
45
+ /** The person behind the request, directly or for a company. */
46
+ readonly userId?: string;
47
+ };
48
+ /**
49
+ * No behavior lives in this file — it is not a wrapper or a pass-through.
50
+ * This abstract class is the port for reading `request_log`: the type a
51
+ * caller holds AND the injection token composition binds.
52
+ * `DrizzleRequestLogReader` (database/readers/) implements it.
53
+ */
54
+ export declare abstract class RequestLogReader {
55
+ /** One request by its id; null once pruned or when it was never logged. */
56
+ abstract findRequest(requestId: string): Promise<RequestLogEntry | null>;
57
+ /**
58
+ * Refused requests of a signed-in actor (never `anonymous`, never one with
59
+ * no actor id), newest first by `(recorded_at, request_id)`, strictly
60
+ * older than `olderThan` when given, at most `limit` of them.
61
+ */
62
+ abstract listRefusals(filter: RefusedRequestFilter, olderThan: RequestLogCursor | null, limit: number): Promise<readonly RefusedRequest[]>;
63
+ }
64
+ //# sourceMappingURL=request-log-reader.interface.d.ts.map