@atproto/ozone 0.4.3 → 0.5.1
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/CHANGELOG.md +38 -0
- package/dist/api/inbox/appealActionedSubject.d.ts +4 -0
- package/dist/api/inbox/appealActionedSubject.d.ts.map +1 -0
- package/dist/api/inbox/appealActionedSubject.js +60 -0
- package/dist/api/inbox/appealActionedSubject.js.map +1 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +1 -1
- package/dist/api/queue/createQueue.d.ts.map +1 -1
- package/dist/api/queue/createQueue.js +22 -17
- package/dist/api/queue/createQueue.js.map +1 -1
- package/dist/api/queue/updateQueue.d.ts.map +1 -1
- package/dist/api/queue/updateQueue.js +33 -18
- package/dist/api/queue/updateQueue.js.map +1 -1
- package/dist/assignment/index.d.ts +1 -0
- package/dist/assignment/index.d.ts.map +1 -1
- package/dist/assignment/index.js +5 -0
- package/dist/assignment/index.js.map +1 -1
- package/dist/config/config.d.ts +11 -0
- package/dist/config/config.d.ts.map +1 -1
- package/dist/config/config.js +4 -0
- package/dist/config/config.js.map +1 -1
- package/dist/config/env.d.ts +1 -0
- package/dist/config/env.d.ts.map +1 -1
- package/dist/config/env.js +1 -0
- package/dist/config/env.js.map +1 -1
- package/dist/daemon/stats-computer.d.ts +2 -8
- package/dist/daemon/stats-computer.d.ts.map +1 -1
- package/dist/daemon/stats-computer.js +21 -9
- package/dist/daemon/stats-computer.js.map +1 -1
- package/dist/db/migrations/20260219T164523000Z-create-report-table.js +2 -2
- package/dist/db/migrations/20260219T164523000Z-create-report-table.js.map +1 -1
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.d.ts +4 -0
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.d.ts.map +1 -0
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.js +31 -0
- package/dist/db/migrations/20260810T000000000Z-add-report-stats-components.js.map +1 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.d.ts +4 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.d.ts.map +1 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.js +14 -0
- package/dist/db/migrations/20260922T153505234Z-add-report-closed-at-index.js.map +1 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.d.ts +4 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.d.ts.map +1 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.js +25 -0
- package/dist/db/migrations/20260928T000000000Z-inbox-appeal.js.map +1 -0
- package/dist/db/migrations/index.d.ts +3 -0
- package/dist/db/migrations/index.d.ts.map +1 -1
- package/dist/db/migrations/index.js +3 -0
- package/dist/db/migrations/index.js.map +1 -1
- package/dist/db/schema/report.js.map +1 -1
- package/dist/db/schema/report_queue.d.ts +1 -0
- package/dist/db/schema/report_queue.d.ts.map +1 -1
- package/dist/db/schema/report_queue.js.map +1 -1
- package/dist/db/schema/report_stat.d.ts +11 -1
- package/dist/db/schema/report_stat.d.ts.map +1 -1
- package/dist/db/schema/report_stat.js.map +1 -1
- package/dist/inbox/appeal.d.ts +187 -0
- package/dist/inbox/appeal.d.ts.map +1 -0
- package/dist/inbox/appeal.js +340 -0
- package/dist/inbox/appeal.js.map +1 -0
- package/dist/inbox/views.d.ts +92 -0
- package/dist/inbox/views.d.ts.map +1 -0
- package/dist/inbox/views.js +313 -0
- package/dist/inbox/views.js.map +1 -0
- package/dist/lexicons/app/bsky/authFullApp.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/authFullApp.defs.js +10 -0
- package/dist/lexicons/app/bsky/authFullApp.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/authManageNotifications.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/authManageNotifications.defs.js +1 -0
- package/dist/lexicons/app/bsky/authManageNotifications.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/feed/defs.defs.d.ts +8 -0
- package/dist/lexicons/app/bsky/feed/defs.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/feed/defs.defs.js +2 -0
- package/dist/lexicons/app/bsky/feed/defs.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/feed/getListFeed.defs.d.ts +4 -0
- package/dist/lexicons/app/bsky/feed/getListFeed.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/feed/getListFeed.defs.js +2 -0
- package/dist/lexicons/app/bsky/feed/getListFeed.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.d.ts +6 -0
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.js +2 -0
- package/dist/lexicons/app/bsky/feed/getQuotes.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/feed/getTimeline.defs.d.ts +4 -0
- package/dist/lexicons/app/bsky/feed/getTimeline.defs.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/feed/getTimeline.defs.js +2 -0
- package/dist/lexicons/app/bsky/feed/getTimeline.defs.js.map +1 -1
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.d.ts +3 -0
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.d.ts.map +1 -0
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.defs.d.ts +299 -0
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.defs.d.ts.map +1 -0
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.defs.js +231 -0
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.defs.js.map +1 -0
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.js +6 -0
- package/dist/lexicons/app/bsky/notification/getGroupedNotifications.js.map +1 -0
- package/dist/lexicons/app/bsky/notification.d.ts +1 -0
- package/dist/lexicons/app/bsky/notification.d.ts.map +1 -1
- package/dist/lexicons/app/bsky/notification.js +1 -0
- package/dist/lexicons/app/bsky/notification.js.map +1 -1
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.d.ts +3 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.d.ts +65 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.js +49 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.defs.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.js +6 -0
- package/dist/lexicons/tools/ozone/inbox/appealActionedSubject.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.d.ts +2 -0
- package/dist/lexicons/tools/ozone/inbox/defs.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.d.ts +89 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.js +73 -0
- package/dist/lexicons/tools/ozone/inbox/defs.defs.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox/defs.js +5 -0
- package/dist/lexicons/tools/ozone/inbox/defs.js.map +1 -0
- package/dist/lexicons/tools/ozone/inbox.d.ts +3 -0
- package/dist/lexicons/tools/ozone/inbox.d.ts.map +1 -0
- package/dist/lexicons/tools/ozone/inbox.js +6 -0
- package/dist/lexicons/tools/ozone/inbox.js.map +1 -0
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.d.ts +2 -0
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.js +2 -0
- package/dist/lexicons/tools/ozone/queue/createQueue.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/queue/defs.defs.d.ts +4 -0
- package/dist/lexicons/tools/ozone/queue/defs.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/queue/defs.defs.js +2 -0
- package/dist/lexicons/tools/ozone/queue/defs.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.d.ts +3 -1
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.js +3 -0
- package/dist/lexicons/tools/ozone/queue/updateQueue.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/report/defs.defs.d.ts +89 -9
- package/dist/lexicons/tools/ozone/report/defs.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/report/defs.defs.js +20 -0
- package/dist/lexicons/tools/ozone/report/defs.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.d.ts +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.js +1 -1
- package/dist/lexicons/tools/ozone/report/getLiveStats.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.d.ts +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.js +1 -1
- package/dist/lexicons/tools/ozone/report/refreshStats.defs.js.map +1 -1
- package/dist/lexicons/tools/ozone.d.ts +1 -0
- package/dist/lexicons/tools/ozone.d.ts.map +1 -1
- package/dist/lexicons/tools/ozone.js +1 -0
- package/dist/lexicons/tools/ozone.js.map +1 -1
- package/dist/mod-service/index.d.ts +2 -0
- package/dist/mod-service/index.d.ts.map +1 -1
- package/dist/mod-service/index.js +6 -3
- package/dist/mod-service/index.js.map +1 -1
- package/dist/queue/service.d.ts +16 -3
- package/dist/queue/service.d.ts.map +1 -1
- package/dist/queue/service.js +81 -27
- package/dist/queue/service.js.map +1 -1
- package/dist/report/stats.d.ts +25 -31
- package/dist/report/stats.d.ts.map +1 -1
- package/dist/report/stats.js +269 -175
- package/dist/report/stats.js.map +1 -1
- package/dist/report/views.d.ts.map +1 -1
- package/dist/report/views.js +20 -0
- package/dist/report/views.js.map +1 -1
- package/package.json +13 -13
package/dist/report/stats.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { sql } from 'kysely';
|
|
2
2
|
import { MINUTE } from '@atproto/common';
|
|
3
3
|
import { currentDatetimeString, toDatetimeString } from '@atproto/lex';
|
|
4
|
+
import { InvalidRequestError } from '@atproto/xrpc-server';
|
|
4
5
|
import { ComputedAtIdKeyset, paginate } from '../db/pagination.js';
|
|
5
6
|
import { jsonb } from '../db/types.js';
|
|
6
7
|
import { com, tools } from '../lexicons/index.js';
|
|
@@ -97,32 +98,37 @@ export class ReportStatsService {
|
|
|
97
98
|
async materializeAll(opts) {
|
|
98
99
|
try {
|
|
99
100
|
const start = Date.now();
|
|
100
|
-
const today = toDateString(new Date());
|
|
101
|
-
const yesterday = toDateString(new Date(
|
|
101
|
+
const today = toDateString(new Date(start));
|
|
102
|
+
const yesterday = toDateString(new Date(start - 24 * 60 * 60 * 1000));
|
|
102
103
|
// Always compute today's stats
|
|
103
|
-
await this.materializeDate(today, opts);
|
|
104
|
+
let rowsWritten = await this.materializeDate(today, opts);
|
|
104
105
|
// Finalize yesterday if its snapshot is missing or stale
|
|
105
106
|
if (!opts?.force) {
|
|
106
107
|
const yesterdayRow = await this.db.db
|
|
107
108
|
.selectFrom('report_stat')
|
|
108
109
|
.select('computedAt')
|
|
109
110
|
.where('date', '=', yesterday)
|
|
111
|
+
.where('queueId', 'is', null)
|
|
112
|
+
.where('moderatorDid', 'is', null)
|
|
113
|
+
.where('reportTypes', 'is', null)
|
|
110
114
|
.orderBy('computedAt', 'desc')
|
|
111
115
|
.executeTakeFirst();
|
|
112
|
-
const endOfYesterday = new Date(`${
|
|
116
|
+
const endOfYesterday = new Date(`${today}T00:00:00.000Z`).getTime();
|
|
113
117
|
if (!yesterdayRow ||
|
|
114
118
|
new Date(yesterdayRow.computedAt).getTime() < endOfYesterday) {
|
|
115
|
-
await this.materializeDate(yesterday, { force: true });
|
|
119
|
+
rowsWritten += await this.materializeDate(yesterday, { force: true });
|
|
116
120
|
}
|
|
117
121
|
}
|
|
118
122
|
else {
|
|
119
|
-
await this.materializeDate(yesterday, { force: true });
|
|
123
|
+
rowsWritten += await this.materializeDate(yesterday, { force: true });
|
|
120
124
|
}
|
|
121
125
|
const duration = Date.now() - start;
|
|
122
126
|
dbLogger.info({ duration }, 'report stats materialization completed');
|
|
127
|
+
return { rowsWritten };
|
|
123
128
|
}
|
|
124
129
|
catch (err) {
|
|
125
130
|
dbLogger.error({ err }, 'report stats materialization errored');
|
|
131
|
+
throw err;
|
|
126
132
|
}
|
|
127
133
|
}
|
|
128
134
|
/**
|
|
@@ -131,11 +137,17 @@ export class ReportStatsService {
|
|
|
131
137
|
async refreshDateRange(opts) {
|
|
132
138
|
const start = new Date(opts.startDate);
|
|
133
139
|
const end = new Date(opts.endDate);
|
|
140
|
+
const endOfToday = new Date();
|
|
141
|
+
endOfToday.setUTCHours(23, 59, 59, 999);
|
|
142
|
+
if (end > endOfToday) {
|
|
143
|
+
throw new InvalidRequestError('Cannot refresh statistics for future dates');
|
|
144
|
+
}
|
|
134
145
|
for (let d = new Date(start); d <= end; d.setUTCDate(d.getUTCDate() + 1)) {
|
|
135
146
|
const dateStr = toDateString(d);
|
|
136
147
|
if (opts.queueIds?.length) {
|
|
137
148
|
// Recompute only specific queue groups for this date
|
|
138
|
-
const
|
|
149
|
+
const computedAt = currentDatetimeString();
|
|
150
|
+
const batched = await this.computeBatchedStats(dateStr, computedAt);
|
|
139
151
|
const rows = [];
|
|
140
152
|
for (const queueId of opts.queueIds) {
|
|
141
153
|
const group = {
|
|
@@ -144,7 +156,7 @@ export class ReportStatsService {
|
|
|
144
156
|
reportTypes: null,
|
|
145
157
|
};
|
|
146
158
|
const stats = this.resolveGroupStats(group, batched);
|
|
147
|
-
rows.push(this.buildUpsertRow(dateStr, group, stats));
|
|
159
|
+
rows.push(this.buildUpsertRow(dateStr, group, stats, computedAt));
|
|
148
160
|
}
|
|
149
161
|
await this.bulkUpsert(rows);
|
|
150
162
|
}
|
|
@@ -155,9 +167,12 @@ export class ReportStatsService {
|
|
|
155
167
|
}
|
|
156
168
|
/** Compute and write all groups for a single date. */
|
|
157
169
|
async materializeDate(date, opts) {
|
|
170
|
+
// @NOTE Timestamp the start so a computation crossing midnight still needs
|
|
171
|
+
// historical finalization on the next cycle.
|
|
172
|
+
const computedAt = currentDatetimeString();
|
|
158
173
|
const groups = await this.enumerateGroups();
|
|
159
|
-
const batched = await this.computeBatchedStats(date);
|
|
160
|
-
const today =
|
|
174
|
+
const batched = await this.computeBatchedStats(date, computedAt);
|
|
175
|
+
const today = computedAt.slice(0, 10);
|
|
161
176
|
const isToday = date === today;
|
|
162
177
|
// Batch the cache check so we don't issue one SELECT per group.
|
|
163
178
|
const existingByKey = !opts?.force
|
|
@@ -178,13 +193,14 @@ export class ReportStatsService {
|
|
|
178
193
|
}
|
|
179
194
|
}
|
|
180
195
|
const stats = this.resolveGroupStats(group, batched);
|
|
181
|
-
rows.push(this.buildUpsertRow(date, group, stats));
|
|
196
|
+
rows.push(this.buildUpsertRow(date, group, stats, computedAt));
|
|
182
197
|
}
|
|
183
198
|
catch (err) {
|
|
184
199
|
dbLogger.error({ err, group, date }, 'error preparing report stats group');
|
|
185
200
|
}
|
|
186
201
|
}
|
|
187
202
|
await this.bulkUpsert(rows);
|
|
203
|
+
return rows.length;
|
|
188
204
|
}
|
|
189
205
|
/** Fetch all stat rows for a date, keyed by groupKey for O(1) lookup. */
|
|
190
206
|
async fetchExistingStatsByKey(date) {
|
|
@@ -249,184 +265,209 @@ export class ReportStatsService {
|
|
|
249
265
|
return groups;
|
|
250
266
|
}
|
|
251
267
|
/**
|
|
252
|
-
* Run batched
|
|
253
|
-
*
|
|
268
|
+
* Run batched queries for a calendar date.
|
|
269
|
+
*
|
|
270
|
+
* @description
|
|
271
|
+
* For each cohort, compute relevant groupings.
|
|
272
|
+
*
|
|
273
|
+
* Stat cohorts: inbound, pending, closed, escalated
|
|
274
|
+
*
|
|
275
|
+
* Stat groups: aggregate, per queue, per report type, and per moderator.
|
|
254
276
|
*/
|
|
255
|
-
async computeBatchedStats(date) {
|
|
277
|
+
async computeBatchedStats(date, computedAt) {
|
|
256
278
|
const dayStart = `${date}T00:00:00.000Z`;
|
|
257
279
|
const dayEnd = `${nextDate(date)}T00:00:00.000Z`;
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
280
|
+
// 1. create report grouping statements
|
|
281
|
+
const statGroups = sql `
|
|
282
|
+
case
|
|
283
|
+
when grouping(coalesce(r."queueId", -1)) = 0 then 'queue'
|
|
284
|
+
when grouping(r."reportType") = 0 then 'reportType'
|
|
285
|
+
when grouping(r."assignedTo") = 0 then 'moderator'
|
|
286
|
+
else 'aggregate'
|
|
287
|
+
end as "group",
|
|
288
|
+
case when grouping(coalesce(r."queueId", -1)) = 0 then coalesce(r."queueId", -1) end as "queueId",
|
|
289
|
+
case when grouping(r."reportType") = 0 then r."reportType" end as "reportType",
|
|
290
|
+
case when grouping(r."assignedTo") = 0 then r."assignedTo" end as "moderatorDid"`;
|
|
291
|
+
const statGroupsExceptModerator = sql `
|
|
292
|
+
case
|
|
293
|
+
when grouping(coalesce(r."queueId", -1)) = 0 then 'queue'
|
|
294
|
+
when grouping(r."reportType") = 0 then 'reportType'
|
|
295
|
+
else 'aggregate'
|
|
296
|
+
end as "group",
|
|
297
|
+
case when grouping(coalesce(r."queueId", -1)) = 0 then coalesce(r."queueId", -1) end as "queueId",
|
|
298
|
+
case when grouping(r."reportType") = 0 then r."reportType" end as "reportType",
|
|
299
|
+
null as "moderatorDid"`;
|
|
300
|
+
// 2. inbound stats
|
|
301
|
+
const inboundStats = () => sql `
|
|
302
|
+
select ${statGroups}, count(*) as "inboundCount"
|
|
303
|
+
from report r
|
|
304
|
+
where r."createdAt" >= ${dayStart} and r."createdAt" < ${dayEnd}
|
|
305
|
+
group by grouping sets ((), (coalesce(r."queueId", -1)), (r."reportType"), (r."assignedTo"))
|
|
306
|
+
`.execute(this.db.db);
|
|
307
|
+
// 3. pending stats
|
|
308
|
+
const pendingReports = date < computedAt.slice(0, 10)
|
|
309
|
+
? sql `
|
|
310
|
+
with subsequent_transitions as (
|
|
311
|
+
select distinct on ("reportId") "reportId", "activityType"
|
|
312
|
+
from report_activity
|
|
313
|
+
where "createdAt" >= ${dayEnd}
|
|
314
|
+
and "activityType" in ('closeActivity', 'reopenActivity')
|
|
315
|
+
order by "reportId", "createdAt", id
|
|
316
|
+
), candidates as (
|
|
317
|
+
select id, "queueId", "reportType" from report
|
|
318
|
+
where status != 'closed' and "createdAt" < ${dayEnd}
|
|
319
|
+
union all
|
|
320
|
+
select id, "queueId", "reportType" from report
|
|
321
|
+
where status = 'closed' and "closedAt" >= ${dayEnd}
|
|
322
|
+
and "createdAt" < ${dayEnd}
|
|
323
|
+
)
|
|
324
|
+
select r."queueId", r."reportType"
|
|
325
|
+
from candidates r
|
|
326
|
+
left join subsequent_transitions t on t."reportId" = r.id
|
|
327
|
+
where t."activityType" is distinct from 'reopenActivity'
|
|
328
|
+
`
|
|
329
|
+
: sql `
|
|
330
|
+
select "queueId", "reportType" from report where status != 'closed'
|
|
331
|
+
`;
|
|
332
|
+
const pendingStats = () => sql `
|
|
333
|
+
select ${statGroupsExceptModerator}, count(*) as "pendingCount"
|
|
334
|
+
from (${pendingReports}) r
|
|
335
|
+
group by grouping sets ((), (coalesce(r."queueId", -1)), (r."reportType"))
|
|
336
|
+
`.execute(this.db.db);
|
|
337
|
+
// 4. closure stats
|
|
338
|
+
const closureStats = () => sql `
|
|
339
|
+
with closed_reports as (
|
|
340
|
+
select
|
|
341
|
+
r."queueId", r."reportType", r."assignedTo",
|
|
342
|
+
r."createdAt", r."assignedAt", r."closedAt",
|
|
343
|
+
-- @NOTE The final linked event determines the current closure outcome.
|
|
344
|
+
(r."actionEventIds" ->> -1)::integer as "actionEventId"
|
|
345
|
+
from report r
|
|
346
|
+
where r."closedAt" >= ${dayStart} and r."closedAt" < ${dayEnd}
|
|
347
|
+
), closure_outcomes as (
|
|
348
|
+
select r.*,
|
|
349
|
+
case me.action
|
|
350
|
+
when ${tools.ozone.moderation.defs.modEventLabel.$type} then 'label'
|
|
351
|
+
when ${tools.ozone.moderation.defs.modEventTag.$type} then 'tag'
|
|
352
|
+
when ${tools.ozone.moderation.defs.modEventTakedown.$type} then 'takedown'
|
|
353
|
+
else 'acknowledged'
|
|
354
|
+
end as outcome
|
|
355
|
+
from closed_reports r
|
|
356
|
+
left join moderation_event me on me.id = r."actionEventId"
|
|
357
|
+
), closure_durations as (
|
|
358
|
+
select r.*,
|
|
359
|
+
case when r."assignedAt" is not null then
|
|
360
|
+
greatest(0, extract(epoch from (r."closedAt"::timestamp - r."assignedAt"::timestamp)))
|
|
361
|
+
end as "handlingTimeSec",
|
|
362
|
+
greatest(0, extract(epoch from (r."closedAt"::timestamp - r."createdAt"::timestamp))) as "resolutionTimeSec"
|
|
363
|
+
from closure_outcomes r
|
|
364
|
+
)
|
|
365
|
+
select ${statGroups},
|
|
366
|
+
count(*) as "closedCount",
|
|
367
|
+
count(*) filter (where r.outcome != 'acknowledged') as "actionedCount",
|
|
368
|
+
count(*) filter (where r.outcome = 'acknowledged') as "acknowledgedCount",
|
|
369
|
+
count(*) filter (where r.outcome = 'label') as "labelActionCount",
|
|
370
|
+
count(*) filter (where r.outcome = 'tag') as "tagActionCount",
|
|
371
|
+
count(*) filter (where r.outcome = 'takedown') as "takedownActionCount",
|
|
372
|
+
coalesce(sum(r."handlingTimeSec"), 0) as "ahtDurationSec",
|
|
373
|
+
count(r."handlingTimeSec") as "ahtSampleCount",
|
|
374
|
+
coalesce(sum(r."resolutionTimeSec"), 0) as "resolutionDurationSec",
|
|
375
|
+
count(*) as "resolutionSampleCount"
|
|
376
|
+
from closure_durations r
|
|
377
|
+
group by grouping sets ((), (coalesce(r."queueId", -1)), (r."reportType"), (r."assignedTo"))
|
|
378
|
+
`.execute(this.db.db);
|
|
379
|
+
// 5. escalation stats
|
|
380
|
+
const escalationStats = () => sql `
|
|
381
|
+
select ${statGroups}, count(*) as "escalatedCount"
|
|
382
|
+
from report_activity ra
|
|
383
|
+
join report r on r.id = ra."reportId"
|
|
384
|
+
where ra."activityType" = 'escalationActivity'
|
|
385
|
+
and ra."createdAt" >= ${dayStart} and ra."createdAt" < ${dayEnd}
|
|
386
|
+
group by grouping sets ((), (coalesce(r."queueId", -1)), (r."reportType"), (r."assignedTo"))
|
|
387
|
+
`.execute(this.db.db);
|
|
388
|
+
// 6. execute all
|
|
389
|
+
const [inbound, pending, closures, escalations] = await Promise.all([
|
|
390
|
+
inboundStats(),
|
|
391
|
+
pendingStats(),
|
|
392
|
+
closureStats(),
|
|
393
|
+
escalationStats(),
|
|
394
|
+
]);
|
|
395
|
+
return mergeStats([
|
|
396
|
+
...inbound.rows,
|
|
397
|
+
...pending.rows,
|
|
398
|
+
...closures.rows,
|
|
399
|
+
...escalations.rows,
|
|
273
400
|
]);
|
|
274
|
-
const queueWindow = await this.db.db
|
|
275
|
-
.selectFrom('report')
|
|
276
|
-
.select([
|
|
277
|
-
'queueId',
|
|
278
|
-
sql `count(*)`.as('inboundCount'),
|
|
279
|
-
sql `count(*) filter (where "status" = 'closed' and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('actionedCount'),
|
|
280
|
-
sql `count(*) filter (where "status" = 'escalated')`.as('escalatedCount'),
|
|
281
|
-
sql `sum(extract(epoch from ("closedAt"::timestamp - "createdAt"::timestamp))) filter (where "status" = 'closed' and "closedAt" is not null and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('handlingTimeSum'),
|
|
282
|
-
sql `count(*) filter (where "status" = 'closed' and "closedAt" is not null and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('handlingTimeCount'),
|
|
283
|
-
])
|
|
284
|
-
.where('createdAt', '>=', dayStart)
|
|
285
|
-
.where('createdAt', '<', dayEnd)
|
|
286
|
-
.where('queueId', 'is not', null)
|
|
287
|
-
.groupBy('queueId')
|
|
288
|
-
.execute();
|
|
289
|
-
// Aggregate windowed (includes all reports)
|
|
290
|
-
const aggregateWindow = await this.db.db
|
|
291
|
-
.selectFrom('report')
|
|
292
|
-
.select([
|
|
293
|
-
sql `count(*)`.as('inboundCount'),
|
|
294
|
-
sql `count(*) filter (where "status" = 'closed' and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('actionedCount'),
|
|
295
|
-
sql `count(*) filter (where "status" = 'escalated')`.as('escalatedCount'),
|
|
296
|
-
sql `sum(extract(epoch from ("closedAt"::timestamp - "createdAt"::timestamp))) filter (where "status" = 'closed' and "closedAt" is not null and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('handlingTimeSum'),
|
|
297
|
-
sql `count(*) filter (where "status" = 'closed' and "closedAt" is not null and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('handlingTimeCount'),
|
|
298
|
-
])
|
|
299
|
-
.where('createdAt', '>=', dayStart)
|
|
300
|
-
.where('createdAt', '<', dayEnd)
|
|
301
|
-
.executeTakeFirst();
|
|
302
|
-
const typePending = await this.db.db
|
|
303
|
-
.selectFrom('report')
|
|
304
|
-
.select(['reportType', sql `count(*)`.as('count')])
|
|
305
|
-
.where('status', '!=', 'closed')
|
|
306
|
-
.groupBy('reportType')
|
|
307
|
-
.execute();
|
|
308
|
-
const typeWindow = await this.db.db
|
|
309
|
-
.selectFrom('report')
|
|
310
|
-
.select([
|
|
311
|
-
'reportType',
|
|
312
|
-
sql `count(*)`.as('inboundCount'),
|
|
313
|
-
sql `count(*) filter (where "status" = 'closed' and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('actionedCount'),
|
|
314
|
-
sql `count(*) filter (where "status" = 'escalated')`.as('escalatedCount'),
|
|
315
|
-
sql `sum(extract(epoch from ("closedAt"::timestamp - "createdAt"::timestamp))) filter (where "status" = 'closed' and "closedAt" is not null and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('handlingTimeSum'),
|
|
316
|
-
sql `count(*) filter (where "status" = 'closed' and "closedAt" is not null and "closedAt" >= ${dayStart} and "closedAt" < ${dayEnd})`.as('handlingTimeCount'),
|
|
317
|
-
])
|
|
318
|
-
.where('createdAt', '>=', dayStart)
|
|
319
|
-
.where('createdAt', '<', dayEnd)
|
|
320
|
-
.groupBy('reportType')
|
|
321
|
-
.execute();
|
|
322
|
-
const moderator = await this.db.db
|
|
323
|
-
.selectFrom('report as r')
|
|
324
|
-
.innerJoin('moderator_assignment as ma', (join) => join.onRef('ma.reportId', '=', 'r.id').on('ma.endAt', 'is', null))
|
|
325
|
-
.select([
|
|
326
|
-
'ma.did',
|
|
327
|
-
sql `count(*)`.as('inboundCount'),
|
|
328
|
-
sql `count(*) filter (where r."status" = 'closed')`.as('actionedCount'),
|
|
329
|
-
sql `sum(extract(epoch from (r."closedAt"::timestamp - ma."startAt"::timestamp))) filter (where r."status" = 'closed' and r."closedAt" is not null)`.as('handlingTimeSum'),
|
|
330
|
-
sql `count(*) filter (where r."status" = 'closed' and r."closedAt" is not null)`.as('handlingTimeCount'),
|
|
331
|
-
])
|
|
332
|
-
.where('r.createdAt', '>=', dayStart)
|
|
333
|
-
.where('r.createdAt', '<', dayEnd)
|
|
334
|
-
.groupBy('ma.did')
|
|
335
|
-
.execute();
|
|
336
|
-
// Inject aggregate as a synthetic row with queueId=null so resolveQueueStats can find it
|
|
337
|
-
const allQueuePending = [
|
|
338
|
-
...queuePending,
|
|
339
|
-
{ queueId: null, count: aggregatePending?.count ?? '0' },
|
|
340
|
-
];
|
|
341
|
-
const allQueueWindow = aggregateWindow
|
|
342
|
-
? [
|
|
343
|
-
...queueWindow,
|
|
344
|
-
{
|
|
345
|
-
queueId: null,
|
|
346
|
-
inboundCount: aggregateWindow.inboundCount,
|
|
347
|
-
actionedCount: aggregateWindow.actionedCount,
|
|
348
|
-
escalatedCount: aggregateWindow.escalatedCount,
|
|
349
|
-
handlingTimeSum: aggregateWindow.handlingTimeSum,
|
|
350
|
-
handlingTimeCount: aggregateWindow.handlingTimeCount,
|
|
351
|
-
},
|
|
352
|
-
]
|
|
353
|
-
: queueWindow;
|
|
354
|
-
return {
|
|
355
|
-
queuePending: allQueuePending,
|
|
356
|
-
queueWindow: allQueueWindow,
|
|
357
|
-
typePending,
|
|
358
|
-
typeWindow,
|
|
359
|
-
moderator,
|
|
360
|
-
};
|
|
361
401
|
}
|
|
362
402
|
/** Resolve a single group's stats from batched query results (pure in-memory). */
|
|
363
403
|
resolveGroupStats(group, batched) {
|
|
364
404
|
if (group.moderatorDid) {
|
|
365
|
-
|
|
405
|
+
const row = batched.get(statKey({
|
|
406
|
+
group: 'moderator',
|
|
407
|
+
queueId: null,
|
|
408
|
+
reportType: null,
|
|
409
|
+
moderatorDid: group.moderatorDid,
|
|
410
|
+
}));
|
|
411
|
+
const { pendingCount: _, ...stats } = this.resolveRows(row ? [row] : []);
|
|
412
|
+
return stats;
|
|
366
413
|
}
|
|
367
414
|
if (group.reportTypes !== null) {
|
|
368
|
-
|
|
415
|
+
const rows = group.reportTypes.flatMap((reportType) => {
|
|
416
|
+
const row = batched.get(statKey({
|
|
417
|
+
group: 'reportType',
|
|
418
|
+
queueId: null,
|
|
419
|
+
reportType,
|
|
420
|
+
moderatorDid: null,
|
|
421
|
+
}));
|
|
422
|
+
return row ? [row] : [];
|
|
423
|
+
});
|
|
424
|
+
return this.resolveRows(rows);
|
|
369
425
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
const inboundCount = num(window?.inboundCount);
|
|
378
|
-
const actionedCount = num(window?.actionedCount);
|
|
379
|
-
const escalatedCount = num(window?.escalatedCount);
|
|
380
|
-
const handlingTimeSum = Number(window?.handlingTimeSum ?? 0);
|
|
381
|
-
const handlingTimeCount = num(window?.handlingTimeCount);
|
|
382
|
-
const actionRate = inboundCount > 0 ? Math.round((actionedCount / inboundCount) * 100) : 0;
|
|
383
|
-
const avgHandlingTimeSec = handlingTimeCount > 0
|
|
384
|
-
? Math.round(handlingTimeSum / handlingTimeCount)
|
|
385
|
-
: undefined;
|
|
386
|
-
return {
|
|
387
|
-
inboundCount,
|
|
388
|
-
pendingCount,
|
|
389
|
-
actionedCount,
|
|
390
|
-
escalatedCount,
|
|
391
|
-
actionRate,
|
|
392
|
-
avgHandlingTimeSec,
|
|
393
|
-
};
|
|
426
|
+
const row = batched.get(statKey({
|
|
427
|
+
group: group.queueId === null ? 'aggregate' : 'queue',
|
|
428
|
+
queueId: group.queueId,
|
|
429
|
+
reportType: null,
|
|
430
|
+
moderatorDid: null,
|
|
431
|
+
}));
|
|
432
|
+
return this.resolveRows(row ? [row] : []);
|
|
394
433
|
}
|
|
395
|
-
|
|
396
|
-
const
|
|
397
|
-
const
|
|
398
|
-
const
|
|
399
|
-
const
|
|
400
|
-
const
|
|
401
|
-
const
|
|
402
|
-
const
|
|
403
|
-
const
|
|
404
|
-
const
|
|
405
|
-
const actionRate = inboundCount > 0 ? Math.round((actionedCount / inboundCount) * 100) : 0;
|
|
406
|
-
const avgHandlingTimeSec = handlingTimeCount > 0
|
|
407
|
-
? Math.round(handlingTimeSum / handlingTimeCount)
|
|
408
|
-
: undefined;
|
|
434
|
+
resolveRows(rows) {
|
|
435
|
+
const sum = (field) => sumNum(rows, field);
|
|
436
|
+
const inboundCount = sum('inboundCount');
|
|
437
|
+
const pendingCount = sum('pendingCount');
|
|
438
|
+
const closedCount = sum('closedCount');
|
|
439
|
+
const actionedCount = sum('actionedCount');
|
|
440
|
+
const ahtDurationSec = Math.round(sum('ahtDurationSec'));
|
|
441
|
+
const ahtSampleCount = sum('ahtSampleCount');
|
|
442
|
+
const resolutionDurationSec = Math.round(sum('resolutionDurationSec'));
|
|
443
|
+
const resolutionSampleCount = sum('resolutionSampleCount');
|
|
409
444
|
return {
|
|
410
445
|
inboundCount,
|
|
411
446
|
pendingCount,
|
|
447
|
+
closedCount,
|
|
412
448
|
actionedCount,
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
449
|
+
acknowledgedCount: sum('acknowledgedCount'),
|
|
450
|
+
escalatedCount: sum('escalatedCount'),
|
|
451
|
+
labelActionCount: sum('labelActionCount'),
|
|
452
|
+
tagActionCount: sum('tagActionCount'),
|
|
453
|
+
takedownActionCount: sum('takedownActionCount'),
|
|
454
|
+
ahtDurationSec,
|
|
455
|
+
ahtSampleCount,
|
|
456
|
+
resolutionDurationSec,
|
|
457
|
+
resolutionSampleCount,
|
|
458
|
+
actionRate: closedCount > 0
|
|
459
|
+
? Math.round((actionedCount / closedCount) * 100)
|
|
460
|
+
: undefined,
|
|
461
|
+
avgHandlingTimeSec: ahtSampleCount > 0
|
|
462
|
+
? Math.round(ahtDurationSec / ahtSampleCount)
|
|
463
|
+
: undefined,
|
|
464
|
+
avgResolutionTimeSec: resolutionSampleCount > 0
|
|
465
|
+
? Math.round(resolutionDurationSec / resolutionSampleCount)
|
|
466
|
+
: undefined,
|
|
416
467
|
};
|
|
417
468
|
}
|
|
418
|
-
resolveModeratorStats(moderatorDid, rows) {
|
|
419
|
-
const row = rows.find((r) => r.did === moderatorDid);
|
|
420
|
-
const inboundCount = num(row?.inboundCount);
|
|
421
|
-
const actionedCount = num(row?.actionedCount);
|
|
422
|
-
const handlingTimeCount = num(row?.handlingTimeCount);
|
|
423
|
-
const avgHandlingTimeSec = handlingTimeCount > 0 && row?.handlingTimeSum
|
|
424
|
-
? Math.round(Number(row.handlingTimeSum) / handlingTimeCount)
|
|
425
|
-
: undefined;
|
|
426
|
-
return { inboundCount, actionedCount, avgHandlingTimeSec };
|
|
427
|
-
}
|
|
428
469
|
/** Build an upsert row from (date, group, stats). */
|
|
429
|
-
buildUpsertRow(date, group, stats) {
|
|
470
|
+
buildUpsertRow(date, group, stats, computedAt) {
|
|
430
471
|
const pendingCount = 'pendingCount' in stats ? (stats.pendingCount ?? null) : null;
|
|
431
472
|
const escalatedCount = 'escalatedCount' in stats ? (stats.escalatedCount ?? null) : null;
|
|
432
473
|
const actionRate = 'actionRate' in stats ? (stats.actionRate ?? null) : null;
|
|
@@ -437,11 +478,21 @@ export class ReportStatsService {
|
|
|
437
478
|
reportTypes: group.reportTypes,
|
|
438
479
|
inboundCount: stats.inboundCount ?? null,
|
|
439
480
|
pendingCount,
|
|
481
|
+
closedCount: stats.closedCount,
|
|
440
482
|
actionedCount: stats.actionedCount ?? null,
|
|
483
|
+
acknowledgedCount: stats.acknowledgedCount,
|
|
441
484
|
escalatedCount,
|
|
485
|
+
labelActionCount: stats.labelActionCount,
|
|
486
|
+
tagActionCount: stats.tagActionCount,
|
|
487
|
+
takedownActionCount: stats.takedownActionCount,
|
|
488
|
+
ahtDurationSec: stats.ahtDurationSec,
|
|
489
|
+
ahtSampleCount: stats.ahtSampleCount,
|
|
490
|
+
resolutionDurationSec: stats.resolutionDurationSec,
|
|
491
|
+
resolutionSampleCount: stats.resolutionSampleCount,
|
|
442
492
|
actionRate,
|
|
443
493
|
avgHandlingTimeSec: stats.avgHandlingTimeSec ?? null,
|
|
444
|
-
|
|
494
|
+
avgResolutionTimeSec: stats.avgResolutionTimeSec ?? null,
|
|
495
|
+
computedAt,
|
|
445
496
|
};
|
|
446
497
|
}
|
|
447
498
|
/**
|
|
@@ -477,10 +528,20 @@ export class ReportStatsService {
|
|
|
477
528
|
reportTypes: r.reportTypes !== null ? jsonb(r.reportTypes) : null,
|
|
478
529
|
inboundCount: r.inboundCount,
|
|
479
530
|
pendingCount: r.pendingCount,
|
|
531
|
+
closedCount: r.closedCount,
|
|
480
532
|
actionedCount: r.actionedCount,
|
|
533
|
+
acknowledgedCount: r.acknowledgedCount,
|
|
481
534
|
escalatedCount: r.escalatedCount,
|
|
535
|
+
labelActionCount: r.labelActionCount,
|
|
536
|
+
tagActionCount: r.tagActionCount,
|
|
537
|
+
takedownActionCount: r.takedownActionCount,
|
|
538
|
+
ahtDurationSec: r.ahtDurationSec,
|
|
539
|
+
ahtSampleCount: r.ahtSampleCount,
|
|
540
|
+
resolutionDurationSec: r.resolutionDurationSec,
|
|
541
|
+
resolutionSampleCount: r.resolutionSampleCount,
|
|
482
542
|
actionRate: r.actionRate,
|
|
483
543
|
avgHandlingTimeSec: r.avgHandlingTimeSec,
|
|
544
|
+
avgResolutionTimeSec: r.avgResolutionTimeSec,
|
|
484
545
|
computedAt: r.computedAt,
|
|
485
546
|
})
|
|
486
547
|
.execute();
|
|
@@ -583,14 +644,47 @@ export class ReportStatsService {
|
|
|
583
644
|
}
|
|
584
645
|
}
|
|
585
646
|
// ─── Helpers ───
|
|
586
|
-
/** Parse a pg bigint string to number, defaulting to 0. */
|
|
587
|
-
function num(val) {
|
|
588
|
-
return val ? Number(val) : 0;
|
|
589
|
-
}
|
|
590
647
|
/** Sum a numeric string field across rows. */
|
|
591
648
|
function sumNum(rows, field) {
|
|
592
649
|
return rows.reduce((sum, r) => sum + Number(r[field] ?? 0), 0);
|
|
593
650
|
}
|
|
651
|
+
function emptyStats(group) {
|
|
652
|
+
return {
|
|
653
|
+
...group,
|
|
654
|
+
inboundCount: '0',
|
|
655
|
+
pendingCount: '0',
|
|
656
|
+
closedCount: '0',
|
|
657
|
+
actionedCount: '0',
|
|
658
|
+
acknowledgedCount: '0',
|
|
659
|
+
escalatedCount: '0',
|
|
660
|
+
labelActionCount: '0',
|
|
661
|
+
tagActionCount: '0',
|
|
662
|
+
takedownActionCount: '0',
|
|
663
|
+
ahtDurationSec: '0',
|
|
664
|
+
ahtSampleCount: '0',
|
|
665
|
+
resolutionDurationSec: '0',
|
|
666
|
+
resolutionSampleCount: '0',
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
function statKey(group) {
|
|
670
|
+
return [
|
|
671
|
+
group.group,
|
|
672
|
+
group.queueId,
|
|
673
|
+
group.reportType,
|
|
674
|
+
group.moderatorDid,
|
|
675
|
+
].join('|');
|
|
676
|
+
}
|
|
677
|
+
function mergeStats(statsRows) {
|
|
678
|
+
const stats = new Map();
|
|
679
|
+
for (const row of statsRows) {
|
|
680
|
+
const key = statKey(row);
|
|
681
|
+
stats.set(key, {
|
|
682
|
+
...(stats.get(key) ?? emptyStats(row)),
|
|
683
|
+
...row,
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
return stats;
|
|
687
|
+
}
|
|
594
688
|
/**
|
|
595
689
|
* Stable cache-key for a stat group. Used to look up an existing row in the
|
|
596
690
|
* batched cache map without issuing per-group SELECTs. Report types are
|