-
Notifications
You must be signed in to change notification settings - Fork 13.5k
refactor: migrate statistics endpoints to new chained API pattern #38884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
smirk-dev
wants to merge
2
commits into
RocketChat:develop
from
smirk-dev:refactor/migrate-stats-api-chained-pattern
+138
−38
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Migrated `statistics`, `statistics.list`, and `statistics.telemetry` REST API endpoints from legacy `addRoute` pattern to the new chained `.get()`/`.post()` API pattern with typed response schemas and AJV query parameter validation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,62 +1,158 @@ | ||
| import type { TelemetryEvents, TelemetryMap } from '@rocket.chat/core-services'; | ||
| import type { IStats } from '@rocket.chat/core-typings'; | ||
| import { ajv, 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'; | ||
|
|
||
| API.v1.addRoute( | ||
| API.v1.get( | ||
| 'statistics', | ||
| { authRequired: true }, | ||
| { | ||
| async get() { | ||
| const { refresh = 'false' } = this.queryParams; | ||
|
|
||
| return API.v1.success( | ||
| await getLastStatistics({ | ||
| userId: this.userId, | ||
| refresh: refresh === 'true', | ||
| }), | ||
| ); | ||
| authRequired: true, | ||
| query: ajv.compile<{ refresh?: 'true' | 'false' }>({ | ||
| type: 'object', | ||
| properties: { | ||
| refresh: { | ||
| type: 'string', | ||
| nullable: true, | ||
| }, | ||
| }, | ||
| required: [], | ||
| additionalProperties: false, | ||
| }), | ||
| response: { | ||
| 200: ajv.compile<IStats>({ | ||
| type: 'object', | ||
| properties: { | ||
| success: { | ||
| type: 'boolean', | ||
| enum: [true], | ||
| }, | ||
| }, | ||
| required: ['success'], | ||
| }), | ||
smirk-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 401: validateUnauthorizedErrorResponse, | ||
| }, | ||
| }, | ||
| async function action() { | ||
| const { refresh = 'false' } = this.queryParams; | ||
|
|
||
| const stats = await getLastStatistics({ | ||
| userId: this.userId, | ||
| refresh: refresh === 'true', | ||
| }); | ||
|
|
||
| if (!stats) { | ||
| throw new Error('No statistics found'); | ||
| } | ||
smirk-dev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return API.v1.success(stats); | ||
| }, | ||
| ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| API.v1.addRoute( | ||
| API.v1.get( | ||
| '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, | ||
| authRequired: true, | ||
| query: ajv.compile<{ fields?: string; count?: number; offset?: number; sort?: string; query?: string }>({ | ||
| type: 'object', | ||
| properties: { | ||
| fields: { type: 'string', nullable: true }, | ||
| count: { type: 'number', nullable: true }, | ||
| offset: { type: 'number', nullable: true }, | ||
| sort: { type: 'string', nullable: true }, | ||
| query: { type: 'string', nullable: true }, | ||
| }, | ||
| required: [], | ||
| additionalProperties: false, | ||
| }), | ||
| response: { | ||
| 200: ajv.compile<{ | ||
| statistics: unknown[]; | ||
| count: number; | ||
| offset: number; | ||
| total: number; | ||
| }>({ | ||
| type: 'object', | ||
| properties: { | ||
| statistics: { type: 'array' }, | ||
| count: { type: 'number' }, | ||
| offset: { type: 'number' }, | ||
| total: { type: 'number' }, | ||
| success: { | ||
| type: 'boolean', | ||
| enum: [true], | ||
| }, | ||
| }), | ||
| ); | ||
| }, | ||
| required: ['statistics', 'count', 'offset', 'total', 'success'], | ||
| }), | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 401: validateUnauthorizedErrorResponse, | ||
| }, | ||
| }, | ||
| async function action() { | ||
| 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( | ||
| API.v1.post( | ||
| 'statistics.telemetry', | ||
| { authRequired: true }, | ||
| { | ||
| post() { | ||
| const events = this.bodyParams; | ||
| authRequired: true, | ||
| body: ajv.compile<{ params: { eventName: string; [key: string]: unknown }[] }>({ | ||
| type: 'object', | ||
| properties: { | ||
| params: { | ||
| type: 'array', | ||
| items: { | ||
| type: 'object', | ||
| properties: { | ||
| eventName: { type: 'string' }, | ||
| }, | ||
| required: ['eventName'], | ||
| }, | ||
| }, | ||
| }, | ||
| required: ['params'], | ||
| }), | ||
| response: { | ||
| 200: ajv.compile<void>({ | ||
smirk-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| type: 'object', | ||
| properties: { | ||
| success: { | ||
| type: 'boolean', | ||
| enum: [true], | ||
| }, | ||
| }, | ||
| required: ['success'], | ||
| additionalProperties: false, | ||
| }), | ||
| 401: validateUnauthorizedErrorResponse, | ||
| }, | ||
| }, | ||
| function action() { | ||
| const events = this.bodyParams; | ||
|
|
||
| events?.params?.forEach((event) => { | ||
| const { eventName, ...params } = event; | ||
| void telemetryEvent.call(eventName, params); | ||
| }); | ||
| events.params.forEach((event) => { | ||
| const { eventName, ...params } = event; | ||
| void telemetryEvent.call(eventName as TelemetryEvents, params as TelemetryMap[TelemetryEvents]); | ||
| }); | ||
|
|
||
| return API.v1.success(); | ||
| }, | ||
| return API.v1.success(); | ||
| }, | ||
| ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.