forked from laurent22/joplin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_report.ts
73 lines (54 loc) · 1.82 KB
/
debug_report.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { SubPath } from '../../utils/routeUtils';
import Router from '../../utils/Router';
import { RouteType } from '../../utils/types';
import { AppContext } from '../../utils/types';
import { ErrorMethodNotAllowed } from '../../utils/errors';
import { contextSessionId } from '../../utils/requestUtils';
const router = new Router(RouteType.Web);
router.get('debug_report', async (_path: SubPath, ctx: AppContext) => {
contextSessionId(ctx);
if (ctx.method !== 'GET') {
throw new ErrorMethodNotAllowed();
}
const outputLines = [];
const startTime = Date.now();
const exceededTimeLimit = () => {
return Date.now() - startTime > 250;
};
const itemModel = ctx.joplin.models.item();
const userId = ctx.joplin.owner.id;
const formatItemId = (id: string) => id;
// Start by logging all changes
outputLines.push('# Changes:', '');
const changeModel = ctx.joplin.models.change();
const changes = await changeModel.delta(userId, { limit: 10_000 });
for (const change of changes.items) {
outputLines.push(`\t${formatItemId(change.item_name)}:\tupdated @ ${change.jop_updated_time}, type: ${change.type}`);
}
if (changes.has_more) {
outputLines.push('...', '');
}
let cursor;
let allItems;
let page = 1;
// Log all items
outputLines.push('');
outputLines.push('# All items:');
outputLines.push('');
while (!allItems || allItems.has_more) {
allItems = await itemModel.children(userId, '', { page, cursor });
cursor = allItems.cursor;
page ++;
const newLines = allItems.items.map(item =>
`\t${formatItemId(item.name)}:\t(update @ ${item.updated_time})`,
);
outputLines.push(...newLines);
if (exceededTimeLimit()) {
outputLines.push('Exceeded time limit.');
break;
}
}
outputLines.push('', `Done in ${(Date.now() - startTime) / 1000}s`);
return outputLines.join('\n');
});
export default router;