summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/admin/logs.ts
blob: 1ec732039965ed38e866596466da18f51542e48b (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import $ from 'cafy';
import define from '../../define';
import { Logs } from '../../../../models';
import { Brackets } from 'typeorm';

export const meta = {
	tags: ['admin'],

	requireCredential: true as const,
	requireModerator: true,

	params: {
		limit: {
			validator: $.optional.num.range(1, 100),
			default: 30
		},

		level: {
			validator: $.optional.nullable.str,
			default: null
		},

		domain: {
			validator: $.optional.nullable.str,
			default: null
		}
	},

	res: {
		type: 'array' as const,
		optional: false as const, nullable: false as const,
		items: {
			type: 'object' as const,
			optional: false as const, nullable: false as const,
			properties: {
				id: {
					type: 'string' as const,
					optional: false as const, nullable: false as const,
					format: 'id',
					example: 'xxxxxxxxxx',
				},
				createdAt: {
					type: 'string' as const,
					optional: false as const, nullable: false as const,
					format: 'date-time',
				},
				domain: {
					type: 'array' as const,
					optional: false as const, nullable: false as const,
					items: {
						type: 'string' as const,
						optional: true as const, nullable: false as const
					}
				},
				level: {
					type: 'string' as const,
					optional: false as const, nullable: false as const
				},
				worker: {
					type: 'string' as const,
					optional: false as const, nullable: false as const
				},
				machine: {
					type: 'string' as const,
					optional: false as const, nullable: false as const,
				},
				message: {
					type: 'string' as const,
					optional: false as const, nullable: false as const,
				},
				data: {
					type: 'object' as const,
					optional: false as const, nullable: false as const
				}
			}
		}
	}
};

export default define(meta, async (ps) => {
	const query = Logs.createQueryBuilder('log');

	if (ps.level) query.andWhere('log.level = :level', { level: ps.level });

	if (ps.domain) {
		const whiteDomains = ps.domain.split(' ').filter(x => !x.startsWith('-'));
		const blackDomains = ps.domain.split(' ').filter(x => x.startsWith('-')).map(x => x.substr(1));

		if (whiteDomains.length > 0) {
			query.andWhere(new Brackets(qb => {
				for (const whiteDomain of whiteDomains) {
					let i = 0;
					for (const subDomain of whiteDomain.split('.')) {
						const p = `whiteSubDomain_${subDomain}_${i}`;
						// SQL is 1 based, so we need '+ 1'
						qb.orWhere(`log.domain[${i + 1}] = :${p}`, { [p]: subDomain });
						i++;
					}
				}
			}));
		}

		if (blackDomains.length > 0) {
			query.andWhere(new Brackets(qb => {
				for (const blackDomain of blackDomains) {
					qb.andWhere(new Brackets(qb => {
						const subDomains = blackDomain.split('.');
						let i = 0;
						for (const subDomain of subDomains) {
							const p = `blackSubDomain_${subDomain}_${i}`;
							// 全体で否定できないのでド・モルガンの法則で
							// !(P && Q) を !P || !Q で表す
							// SQL is 1 based, so we need '+ 1'
							qb.orWhere(`log.domain[${i + 1}] != :${p}`, { [p]: subDomain });
							i++;
						}
					}));
				}
			}));
		}
	}

	const logs = await query.orderBy('log.createdAt', 'DESC').take(ps.limit!).getMany();

	return logs;
});