logging.controller.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. Controller,
  3. Get,
  4. Delete,
  5. Query,
  6. HttpException,
  7. HttpStatus,
  8. } from '@nestjs/common';
  9. import { ApiQuery, ApiResponse } from '@nestjs/swagger';
  10. import { LogEntryDto } from './dto/logentry.dto';
  11. import { LoggingService } from './logging.service';
  12. import { LogEntry } from './models/logentry.model';
  13. @Controller('logging')
  14. export class LoggingController {
  15. constructor(private readonly loggingService: LoggingService) {}
  16. private static logEntryToDto(entry: LogEntry): LogEntryDto {
  17. return {
  18. component: entry.component,
  19. level: entry.level,
  20. timestamp: entry.timestamp,
  21. message: entry.message,
  22. };
  23. }
  24. @Get('/all')
  25. @ApiResponse({
  26. status: 201,
  27. description: 'All log entries',
  28. type: [LogEntryDto],
  29. })
  30. @ApiResponse({
  31. status: 404,
  32. description: 'No messages found',
  33. })
  34. async allMessages(): Promise<LogEntryDto[]> {
  35. return this.loggingService.allMessages().then((response) => {
  36. if (response === null) {
  37. throw new HttpException(
  38. 'Unable to find the log messages',
  39. HttpStatus.NOT_FOUND,
  40. );
  41. }
  42. const messages: LogEntryDto[] = [];
  43. response.forEach((entry) =>
  44. messages.push(LoggingController.logEntryToDto(entry)),
  45. );
  46. return messages;
  47. });
  48. }
  49. @Get('/component')
  50. @ApiQuery({
  51. name: 'component',
  52. description: 'The component filter for the messages',
  53. type: String,
  54. })
  55. @ApiResponse({
  56. status: 201,
  57. description: 'All log entries',
  58. type: [LogEntryDto],
  59. })
  60. @ApiResponse({
  61. status: 404,
  62. description: 'No messages found',
  63. })
  64. async componentMessages(
  65. @Query('component') component: string,
  66. ): Promise<LogEntryDto[]> {
  67. return this.loggingService.componentMessages(component).then((response) => {
  68. if (response === null) {
  69. throw new HttpException(
  70. 'Unable to find the log messages',
  71. HttpStatus.NOT_FOUND,
  72. );
  73. }
  74. const messages: LogEntryDto[] = [];
  75. response.forEach((entry) =>
  76. messages.push(LoggingController.logEntryToDto(entry)),
  77. );
  78. return messages;
  79. });
  80. }
  81. @Delete('/deleteAll')
  82. @ApiResponse({
  83. status: 200,
  84. description: 'All log entries are deleted',
  85. })
  86. async deleteAll(): Promise<void> {
  87. return this.loggingService.deleteAllMessages();
  88. }
  89. @Delete('/deleteComponent')
  90. @ApiQuery({
  91. name: 'component',
  92. description: 'The component filter for the messages',
  93. type: String,
  94. })
  95. @ApiResponse({
  96. status: 200,
  97. description: 'All log entries are deleted',
  98. })
  99. async deleteComponent(@Query('component') component: string): Promise<void> {
  100. return this.loggingService.deleteComponentMessages(component);
  101. }
  102. }