-
Notifications
You must be signed in to change notification settings - Fork 13.7k
feat: export logs endpoint #36186
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
Closed
feat: export logs endpoint #36186
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1190fe3
wip
d-gubert 29e9f2d
Separate find and findPaginated in the AppRealLogStorage
d-gubert 180ae5d
Export content generation logic
d-gubert fac934e
Fix app logs export validation function
d-gubert 027e6c5
Refactor endpoint registration to allow for authentication via cookie
d-gubert 0510118
Fix engine tests
d-gubert 3df3934
Add response validator to 401
d-gubert 3c45d0c
Add tests for endpoint
d-gubert fc4be16
Add changeset
d-gubert 0b6dc05
Merge branch 'develop' into feat/apps-logs-export
d-gubert e5cd8e1
Merge remote-tracking branch 'origin' into feat/apps-logs-export
d-gubert 8270b8f
Merge branch 'develop' into feat/apps-logs-export
d-gubert 17671c8
Improve endpoint typing
d-gubert e4cf013
Merge remote-tracking branch 'origin' into feat/apps-logs-export
d-gubert e23781d
Update .changeset/serious-apricots-compare.md
d-gubert f683021
Merge branch 'develop' into feat/apps-logs-export
d-gubert 74e59c8
Merge branch 'develop' into feat/apps-logs-export
d-gubert 6bbb7bd
Merge branch 'develop' into feat/apps-logs-export
d-gubert 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,7 @@ | ||
| --- | ||
| '@rocket.chat/rest-typings': minor | ||
| '@rocket.chat/apps-engine': minor | ||
| '@rocket.chat/meteor': minor | ||
| --- | ||
|
|
||
| Added endpoint to export apps logs as files | ||
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
126 changes: 126 additions & 0 deletions
126
apps/meteor/ee/server/apps/communication/endpoints/appLogsExportHandler.ts
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,126 @@ | ||
| import type { IUser } from '@rocket.chat/core-typings'; | ||
| import { isAppLogsExportProps } from '@rocket.chat/rest-typings'; | ||
| import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv'; | ||
| import { parse } from 'cookie'; | ||
| import { json2csv } from 'json-2-csv'; | ||
|
|
||
| import { getPaginationItems } from '../../../../../app/api/server/helpers/getPaginationItems'; | ||
| import type { AppsRestApi } from '../rest'; | ||
| import { makeAppLogsQuery } from './lib/makeAppLogsQuery'; | ||
| import { APIClass } from '../../../../../app/api/server/ApiClass'; | ||
|
|
||
| const isErrorResponse = ajv.compile({ | ||
| type: 'object', | ||
| properties: { | ||
| success: { | ||
| type: 'boolean', | ||
| enum: [false], | ||
| }, | ||
| error: { | ||
| type: 'string', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| class ExportHandlerAPI extends APIClass { | ||
| protected async authenticatedRoute(req: Request): Promise<IUser | null> { | ||
| const { rc_uid, rc_token } = parse(req.headers.get('cookie') || ''); | ||
|
|
||
| if (rc_uid) { | ||
| req.headers.set('x-user-id', rc_uid); | ||
| } | ||
|
|
||
| if (rc_token) { | ||
| req.headers.set('x-auth-token', rc_token); | ||
| } | ||
|
|
||
| return super.authenticatedRoute(req); | ||
| } | ||
| } | ||
|
|
||
| const adhocApi = new ExportHandlerAPI({ | ||
| useDefaultAuth: false, | ||
| prettyJson: process.env.NODE_ENV !== 'development', | ||
| }); | ||
|
|
||
| export const registerAppLogsExportHandler = ({ api, _manager, _orch }: AppsRestApi) => { | ||
| adhocApi.get( | ||
| ':id/export-logs', | ||
| { | ||
| authRequired: true, | ||
| permissionsRequired: ['manage-apps'], | ||
| query: isAppLogsExportProps, | ||
| response: { | ||
| 200: ajv.compile({ | ||
| type: 'object', | ||
| properties: { | ||
| body: { | ||
| type: 'string', | ||
| format: 'binary', | ||
| description: 'The content of the exported logs file, either in JSON or CSV format.', | ||
| }, | ||
| }, | ||
| }), | ||
| 400: isErrorResponse, | ||
| 401: isErrorResponse, | ||
| 404: isErrorResponse, | ||
| }, | ||
| }, | ||
|
|
||
| async function () { | ||
| const proxiedApp = _manager.getOneById(this.urlParams.id); | ||
|
|
||
| if (!proxiedApp) { | ||
| return api.notFound(`No App found by the id of: ${this.urlParams.id}`); | ||
| } | ||
|
|
||
| const { count } = await getPaginationItems(this.queryParams); | ||
| const { sort } = await this.parseJsonQuery(); | ||
|
|
||
| const options = { | ||
| sort: sort || { _updatedAt: -1 }, | ||
| skip: 0, | ||
| limit: Math.min(count || 100, 2000), | ||
| }; | ||
|
|
||
| let query: ReturnType<typeof makeAppLogsQuery>; | ||
|
|
||
| try { | ||
| query = makeAppLogsQuery({ appId: this.urlParams.id, ...this.queryParams }); | ||
| } catch (error) { | ||
| return api.failure({ error: error instanceof Error ? error.message : 'Unknown error' }); | ||
| } | ||
|
|
||
| const result = await _orch.getLogStorage().find(query, options); | ||
|
|
||
| if (!result.length) { | ||
| return api.failure({ error: 'No logs found for the specified criteria' }); | ||
| } | ||
|
|
||
| let fileContent: Buffer; | ||
| let filename: string; | ||
| const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); | ||
|
|
||
| if (this.queryParams.type === 'json') { | ||
| fileContent = Buffer.from(JSON.stringify(result, null, 2), 'utf8'); | ||
| filename = `app-logs-${this.urlParams.id}-${timestamp}.json`; | ||
| } else { | ||
| fileContent = Buffer.from(json2csv(result, { expandArrayObjects: true }), 'utf8'); | ||
| filename = `app-logs-${this.urlParams.id}-${timestamp}.csv`; | ||
| } | ||
|
|
||
| return { | ||
| body: fileContent, | ||
| statusCode: 200, | ||
| headers: { | ||
| // 'application/json' here creates problems down the line with the router | ||
| 'Content-Type': 'text/plain', | ||
| 'Content-Disposition': `attachment; filename="${filename}"`, | ||
| 'Content-Length': fileContent.length.toString(), | ||
| }, | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| api.router.use(adhocApi.router); | ||
| }; |
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
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
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
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
Oops, something went wrong.
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.