Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/serious-apricots-compare.md
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
---

Adds an endpoint to export apps logs as files
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const registerAppGeneralLogsHandler = ({ api, _orch }: AppsRestApi) =>
return api.failure({ error: error instanceof Error ? error.message : 'Unknown error' });
}

const result = await _orch.getLogStorage().find(query, options);
const result = await _orch.getLogStorage().findPaginated(query, options);

return api.success({ offset, logs: result.logs, count: result.logs.length, total: result.total });
},
Expand Down
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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const registerAppLogsHandler = ({ api, _manager, _orch }: AppsRestApi) =>
return api.failure({ error: error instanceof Error ? error.message : 'Unknown error' });
}

const result = await _orch.getLogStorage().find(query, options);
const result = await _orch.getLogStorage().findPaginated(query, options);

return api.success({ offset, logs: result.logs, count: result.logs.length, total: result.total });
},
Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/ee/server/apps/communication/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ZodError } from 'zod';

import { registerActionButtonsHandler } from './endpoints/actionButtonsHandler';
import { registerAppGeneralLogsHandler } from './endpoints/appGeneralLogsHandler';
import { registerAppLogsExportHandler } from './endpoints/appLogsExportHandler';
import { registerAppLogsHandler } from './endpoints/appLogsHandler';
import { registerAppsCountHandler } from './endpoints/appsCountHandler';
import { API } from '../../../../app/api/server';
Expand Down Expand Up @@ -108,6 +109,7 @@ export class AppsRestApi {
registerAppsCountHandler(this);

registerAppLogsHandler(this);
registerAppLogsExportHandler(this);
registerAppGeneralLogsHandler(this);

this.api.addRoute(
Expand Down
9 changes: 9 additions & 0 deletions apps/meteor/ee/server/apps/storage/AppRealLogStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export class AppRealLogStorage extends AppLogStorage {
[field: string]: any;
},
options: IAppLogStorageFindOptions,
) {
return this.db.find<ILoggerStorageEntry>(query, options).toArray();
}

async findPaginated(
query: {
[field: string]: any;
},
options: IAppLogStorageFindOptions,
) {
const { cursor, totalCount } = this.db.findPaginated<ILoggerStorageEntry>(query, options);

Expand Down
1 change: 1 addition & 0 deletions apps/meteor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@
"isolated-vm": "5.0.4",
"jschardet": "^3.1.4",
"jsdom": "^26.1.0",
"json-2-csv": "^5.5.9",
"jsrsasign": "^11.1.0",
"juice": "^8.1.0",
"katex": "~0.16.22",
Expand Down
Loading
Loading