Skip to content

Commit

Permalink
fix: apply filters on logging API
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-rocket committed Oct 14, 2024
1 parent daee85f commit 908b63d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
39 changes: 18 additions & 21 deletions packages/core/src/api-call-log/api-call-log.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ export class ApiCallLogService extends TenantAwareCrudService<ApiCallLog> {
// Ensure that filters are properly defined
const queryOptions: FindManyOptions<ApiCallLog> = {
where: {},
...filters
take: filters.take ?? 100, // Default to 100 if not provided
skip: filters.skip ? filters.take * (filters.skip - 1) : 0 // Calculate offset
};

// Apply sorting options (if provided)
if (filters.order) {
queryOptions.order = filters.order; // Order, in which entities should be ordered. Default to ASC if no order is provided.
}

// Check if `filters.where` is an array or an object, then apply individual filters
if (!Array.isArray(filters)) {
if (filters.organizationId) {
queryOptions.where['organizationId'] = filters.organizationId;
}
if (filters.correlationId) {
queryOptions.where['correlationId'] = filters.correlationId;
}
Expand All @@ -41,41 +50,29 @@ export class ApiCallLogService extends TenantAwareCrudService<ApiCallLog> {
if (filters.ipAddress) {
queryOptions.where['ipAddress'] = filters.ipAddress;
}
if (filters.method) {
queryOptions.where['method'] = filters.method;
}
if (filters.userId) {
queryOptions.where['userId'] = filters.userId;
}

// Apply date range filters for requestTime
if (filters.startRequestTime || filters.endRequestTime) {
// The start date for filtering, defaults to the start of today.
const startRequestTime = filters.startRequestTime
const start = filters.startRequestTime
? moment(filters.startRequestTime).toDate()
: moment().startOf('day').toDate();

// The end date for filtering, defaults to the end of today.
const endRequestTime = filters.endRequestTime
const end = filters.endRequestTime
? moment(filters.endRequestTime).toDate()
: moment().endOf('day').toDate();
: moment().endOf('day').toDate(); // Default to end of today if no end date is provided

// Retrieves a date range filter using the `startRequestTime` and `endRequestTime` values.
queryOptions.where['requestTime'] = Between(
startRequestTime, // Default to start of today if no start date is provided
endRequestTime // Default to end of today if no end date is provided
);
// Retrieves a date range filter using the `start` and `end` values.
queryOptions.where['requestTime'] = Between(start, end);
}
}

// Apply pagination and sorting options (if provided)
if (filters.take) {
queryOptions.take = filters.take;
}
if (filters.skip) {
queryOptions.skip = filters.skip;
}
if (filters.order) {
queryOptions.order = filters.order;
}

// Perform the query with filters, sorting, and pagination applied
return await super.findAll(queryOptions);
}
Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/api-call-log/dto/api-call-log-filter.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiPropertyOptional, IntersectionType, PickType } from '@nestjs/swagger';
import { IsDateString, IsNumber, IsOptional, IsUUID } from 'class-validator';
import { ID } from '@gauzy/contracts';
import { PaginationParams } from '../../core/crud/pagination-params';
import { TenantOrganizationBaseDTO } from '../../core/dto/tenant-organization-base.dto';
import { ApiCallLog } from '../api-call-log.entity';
Expand All @@ -10,27 +11,27 @@ import { ApiCallLog } from '../api-call-log.entity';
export class ApiCallLogFilterDTO extends IntersectionType(
TenantOrganizationBaseDTO,
PickType(PaginationParams, ['skip', 'take', 'order']),
PickType(ApiCallLog, ['userId', 'ipAddress'] as const)
PickType(ApiCallLog, ['userId', 'ipAddress', 'method'] as const)
) {
// Correlation ID to filter the request against.
@ApiPropertyOptional()
@ApiPropertyOptional({ type: () => String })
@IsOptional()
@IsUUID()
correlationId?: string;
correlationId?: ID;

// Status Code to filter the request against.
@ApiPropertyOptional()
@ApiPropertyOptional({ type: () => Number })
@IsOptional()
@IsNumber()
statusCode?: number;

// Date range filters for requestTime and responseTime
@ApiPropertyOptional()
@ApiPropertyOptional({ type: () => Date })
@IsOptional()
@IsDateString()
startRequestTime?: Date;

@ApiPropertyOptional()
@ApiPropertyOptional({ type: () => Date })
@IsOptional()
@IsDateString()
endRequestTime?: Date;
Expand Down

0 comments on commit 908b63d

Please sign in to comment.