-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathstats.ts
More file actions
90 lines (79 loc) · 1.97 KB
/
stats.ts
File metadata and controls
90 lines (79 loc) · 1.97 KB
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
import { ajv, ajvQuery, validateBadRequestErrorResponse, validateUnauthorizedErrorResponse } from '@rocket.chat/rest-typings';
import { getStatistics, getLastStatistics } from '../../../statistics/server';
import telemetryEvent from '../../../statistics/server/lib/telemetryEvents';
import { API } from '../api';
import { getPaginationItems } from '../helpers/getPaginationItems';
const StatisticsQuerySchema = {
type: 'object',
properties: {
refresh: { type: 'string', enum: ['true', 'false'] },
},
required: [],
additionalProperties: false,
} as const;
const isStatisticsProps = ajvQuery.compile<{ refresh?: 'true' | 'false' }>(StatisticsQuerySchema);
const statisticsResponseSchema = ajv.compile<Record<string, unknown>>({
type: 'object',
properties: {
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
additionalProperties: true,
});
API.v1.get(
'statistics',
{
authRequired: true,
query: isStatisticsProps,
response: {
200: statisticsResponseSchema,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { refresh = 'false' } = this.queryParams;
return API.v1.success(
await getLastStatistics({
userId: this.userId,
refresh: refresh === 'true',
}),
);
},
);
API.v1.addRoute(
'statistics.list',
{ authRequired: true },
{
async get() {
const { offset, count } = await getPaginationItems(this.queryParams);
const { sort, fields, query } = await this.parseJsonQuery();
return API.v1.success(
await getStatistics({
userId: this.userId,
query,
pagination: {
offset,
count,
sort,
fields,
},
}),
);
},
},
);
API.v1.addRoute(
'statistics.telemetry',
{ authRequired: true },
{
post() {
const events = this.bodyParams;
events?.params?.forEach((event) => {
const { eventName, ...params } = event;
void telemetryEvent.call(eventName, params);
});
return API.v1.success();
},
},
);