Files
esg/src/logging/inquiry-log.service.ts
T

481 lines
14 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { FilterQuery, Model } from 'mongoose';
import { AttemptSummaryDto } from '../common/dto/attempt-summary.dto';
import {
InquiryStatus,
NO_MATCH_ERROR_CODES,
} from '../common/enums/inquiry-status.enum';
import { InquiryType } from '../common/enums/inquiry-type.enum';
import { ProviderName } from '../common/enums/provider-name.enum';
import { maskPayload } from '../common/helpers/mask-payload.helper';
import {
InquiryLogQueryDto,
InquiryLogTimelineQueryDto,
LogTimelineInterval,
LogTimeRangePreset,
} from './dto/inquiry-log-query.dto';
import { InquiryLog, InquiryLogDocument } from './schemas/inquiry-log.schema';
import type { OutboundRequestTrace } from '../common/helpers/http-client.helper';
export interface CreateInquiryLogInput {
inquiryType: InquiryType;
provider: ProviderName;
requestPayload: Record<string, unknown>;
responsePayload?: Record<string, unknown>;
status: InquiryStatus;
duration: number;
requestId: string;
trackingCode: string;
error?: Record<string, unknown>;
attemptSummary?: AttemptSummaryDto;
outboundRequests?: OutboundRequestTrace[];
}
export interface InquiryLogListItem {
id: string;
inquiryType: InquiryType;
provider: ProviderName;
status: InquiryStatus;
duration: number;
requestId: string;
trackingCode: string;
maskedRequest: Record<string, unknown>;
responsePayload?: Record<string, unknown>;
error?: Record<string, unknown>;
outboundRequests?: Record<string, unknown>[];
createdAt?: Date;
}
export interface InquiryLogSummary {
total: number;
evaluated: number;
success: number;
failure: number;
noMatch: number;
partial: number;
successRate: number;
failureRate: number;
averageDuration: number;
minDuration: number;
maxDuration: number;
}
export interface CommonErrorItem {
code: string;
message: string;
count: number;
lastSeenAt: Date;
}
export interface DurationExtremeItem {
id: string;
inquiryType: InquiryType;
provider: ProviderName;
status: InquiryStatus;
duration: number;
requestId: string;
trackingCode: string;
error?: Record<string, unknown>;
createdAt?: Date;
}
export interface TimelineBucket {
bucket: string;
total: number;
success: number;
failure: number;
partial: number;
averageDuration: number;
maxDuration: number;
commonErrorCode?: string;
}
@Injectable()
export class InquiryLogService {
constructor(
@InjectModel(InquiryLog.name)
private readonly inquiryLogModel: Model<InquiryLogDocument>,
) {}
async create(input: CreateInquiryLogInput): Promise<InquiryLogDocument> {
return this.inquiryLogModel.create({
...input,
maskedRequest: maskPayload(input.requestPayload),
});
}
async purgeAll(): Promise<{ deletedCount: number }> {
const result = await this.inquiryLogModel.deleteMany({}).exec();
return { deletedCount: result.deletedCount ?? 0 };
}
async findLogs(query: InquiryLogQueryDto): Promise<{
data: InquiryLogListItem[];
page: number;
limit: number;
total: number;
}> {
const filter = this.buildFilter(query);
const limit = query.limit ?? 50;
const page = query.page ?? 1;
const skip = (page - 1) * limit;
const [logs, total] = await Promise.all([
this.inquiryLogModel
.find(filter)
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.select('inquiryType provider status duration requestId trackingCode maskedRequest responsePayload error outboundRequests createdAt')
.lean()
.exec(),
this.inquiryLogModel.countDocuments(filter),
]);
return {
data: logs.map((log) => ({
id: log._id.toString(),
inquiryType: log.inquiryType,
provider: log.provider,
status: log.status,
duration: log.duration,
requestId: log.requestId,
trackingCode: log.trackingCode,
maskedRequest: log.maskedRequest,
responsePayload: log.responsePayload,
error: log.error,
outboundRequests: log.outboundRequests,
createdAt: log.createdAt,
})),
page,
limit,
total,
};
}
async getSummary(query: InquiryLogQueryDto): Promise<InquiryLogSummary> {
const [summary] = await this.inquiryLogModel.aggregate<InquiryLogSummary>([
{ $match: this.buildFilter(query) },
{
$group: {
_id: null,
total: { $sum: 1 },
success: {
$sum: { $cond: [{ $eq: ['$status', InquiryStatus.SUCCESS] }, 1, 0] },
},
failure: {
$sum: { $cond: [{ $eq: ['$status', InquiryStatus.FAILURE] }, 1, 0] },
},
noMatch: {
$sum: {
$cond: [
{
$or: [
{ $eq: ['$status', InquiryStatus.NO_MATCH] },
{
$and: [
{ $eq: ['$status', InquiryStatus.FAILURE] },
{ $in: ['$error.code', NO_MATCH_ERROR_CODES] },
],
},
],
},
1,
0,
],
},
},
noMatchFailure: {
$sum: {
$cond: [
{
$and: [
{ $eq: ['$status', InquiryStatus.FAILURE] },
{ $in: ['$error.code', NO_MATCH_ERROR_CODES] },
],
},
1,
0,
],
},
},
partial: {
$sum: { $cond: [{ $eq: ['$status', InquiryStatus.PARTIAL] }, 1, 0] },
},
averageDuration: { $avg: '$duration' },
minDuration: { $min: '$duration' },
maxDuration: { $max: '$duration' },
},
},
{
$project: {
_id: 0,
total: 1,
success: 1,
failure: { $subtract: ['$failure', '$noMatchFailure'] },
evaluated: {
$add: ['$success', { $subtract: ['$failure', '$noMatchFailure'] }],
},
noMatch: 1,
partial: 1,
successRate: {
$cond: [
{ $eq: [{ $add: ['$success', { $subtract: ['$failure', '$noMatchFailure'] }] }, 0] },
0,
{ $divide: ['$success', { $add: ['$success', { $subtract: ['$failure', '$noMatchFailure'] }] }] },
],
},
failureRate: {
$cond: [
{ $eq: [{ $add: ['$success', { $subtract: ['$failure', '$noMatchFailure'] }] }, 0] },
0,
{ $divide: [{ $subtract: ['$failure', '$noMatchFailure'] }, { $add: ['$success', { $subtract: ['$failure', '$noMatchFailure'] }] }] },
],
},
averageDuration: { $ifNull: ['$averageDuration', 0] },
minDuration: { $ifNull: ['$minDuration', 0] },
maxDuration: { $ifNull: ['$maxDuration', 0] },
},
},
]);
return summary ?? this.emptySummary();
}
async getCommonErrors(query: InquiryLogQueryDto): Promise<CommonErrorItem[]> {
const limit = query.limit ?? 10;
return this.inquiryLogModel.aggregate<CommonErrorItem>([
{
$match: {
...this.buildFilter(query),
error: { $exists: true, $ne: null },
},
},
{
$group: {
_id: {
code: { $ifNull: ['$error.code', 'UNKNOWN_ERROR'] },
message: { $ifNull: ['$error.message', 'Unknown error'] },
},
count: { $sum: 1 },
lastSeenAt: { $max: '$createdAt' },
},
},
{ $sort: { count: -1, lastSeenAt: -1 } },
{ $limit: limit },
{
$project: {
_id: 0,
code: '$_id.code',
message: '$_id.message',
count: 1,
lastSeenAt: 1,
},
},
]);
}
async getDurationExtremes(query: InquiryLogQueryDto): Promise<{
fastest: DurationExtremeItem[];
slowest: DurationExtremeItem[];
}> {
const filter = this.buildFilter(query);
const limit = query.limit ?? 10;
const projection =
'inquiryType provider status duration requestId trackingCode error createdAt';
const [fastest, slowest] = await Promise.all([
this.inquiryLogModel.find(filter).sort({ duration: 1 }).limit(limit).select(projection).lean(),
this.inquiryLogModel.find(filter).sort({ duration: -1 }).limit(limit).select(projection).lean(),
]);
return {
fastest: fastest.map((log) => this.toDurationExtremeItem(log)),
slowest: slowest.map((log) => this.toDurationExtremeItem(log)),
};
}
async getTimeline(query: InquiryLogTimelineQueryDto): Promise<TimelineBucket[]> {
const interval = query.interval ?? LogTimelineInterval.HOUR;
return this.inquiryLogModel.aggregate<TimelineBucket>([
{ $match: this.buildFilter(query) },
{
$group: {
_id: {
bucket: {
$dateToString: {
format: this.timelineFormat(interval),
date: '$createdAt',
timezone: 'Asia/Tehran',
},
},
errorCode: { $ifNull: ['$error.code', null] },
},
total: { $sum: 1 },
success: {
$sum: { $cond: [{ $eq: ['$status', InquiryStatus.SUCCESS] }, 1, 0] },
},
failure: {
$sum: { $cond: [{ $eq: ['$status', InquiryStatus.FAILURE] }, 1, 0] },
},
partial: {
$sum: { $cond: [{ $eq: ['$status', InquiryStatus.PARTIAL] }, 1, 0] },
},
durationSum: { $sum: '$duration' },
maxDuration: { $max: '$duration' },
},
},
{
$addFields: {
errorCount: {
$cond: [{ $ne: ['$_id.errorCode', null] }, '$total', 0],
},
},
},
{ $sort: { '_id.bucket': 1, errorCount: -1 } },
{
$group: {
_id: '$_id.bucket',
total: { $sum: '$total' },
success: { $sum: '$success' },
failure: { $sum: '$failure' },
partial: { $sum: '$partial' },
durationSum: { $sum: '$durationSum' },
maxDuration: { $max: '$maxDuration' },
commonErrorCode: { $first: '$_id.errorCode' },
},
},
{ $sort: { _id: 1 } },
{
$project: {
_id: 0,
bucket: '$_id',
total: 1,
success: 1,
failure: 1,
partial: 1,
averageDuration: {
$cond: [{ $eq: ['$total', 0] }, 0, { $divide: ['$durationSum', '$total'] }],
},
maxDuration: { $ifNull: ['$maxDuration', 0] },
commonErrorCode: 1,
},
},
]);
}
private buildFilter(query: InquiryLogQueryDto): FilterQuery<InquiryLogDocument> {
const filter: FilterQuery<InquiryLogDocument> = {};
const createdAt = this.buildCreatedAtFilter(query);
if (createdAt) {
filter.createdAt = createdAt;
}
if (query.inquiryType) {
filter.inquiryType = query.inquiryType;
}
if (query.provider) {
filter.provider = query.provider;
}
if (query.status) {
filter.status = query.status;
}
if (query.errorCode) {
filter['error.code'] = query.errorCode;
}
if (query.errorMessage) {
filter['error.message'] = { $regex: query.errorMessage, $options: 'i' };
}
if (query.trackingCode) {
filter.trackingCode = query.trackingCode;
}
return filter;
}
private buildCreatedAtFilter(query: InquiryLogQueryDto): { $gte?: Date; $lte?: Date } | undefined {
if (!query.from && !query.to && !query.preset) {
return undefined;
}
const to = query.to ? new Date(query.to) : undefined;
const from = query.from ? new Date(query.from) : this.defaultFromDate(query.preset);
return {
...(from && { $gte: from }),
...(to && { $lte: to }),
};
}
private defaultFromDate(preset?: LogTimeRangePreset): Date {
const now = new Date();
const selectedPreset = preset ?? LogTimeRangePreset.LAST_24_HOURS;
switch (selectedPreset) {
case LogTimeRangePreset.LAST_30_DAYS:
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
case LogTimeRangePreset.LAST_7_DAYS:
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
case LogTimeRangePreset.LAST_24_HOURS:
default:
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
}
}
private timelineFormat(interval: LogTimelineInterval): string {
switch (interval) {
case LogTimelineInterval.MONTH:
return '%Y-%m';
case LogTimelineInterval.DAY:
return '%Y-%m-%d';
case LogTimelineInterval.HOUR:
default:
return '%Y-%m-%d %H:00';
}
}
private emptySummary(): InquiryLogSummary {
return {
total: 0,
evaluated: 0,
success: 0,
failure: 0,
noMatch: 0,
partial: 0,
successRate: 0,
failureRate: 0,
averageDuration: 0,
minDuration: 0,
maxDuration: 0,
};
}
private toDurationExtremeItem(log: {
_id: unknown;
inquiryType: InquiryType;
provider: ProviderName;
status: InquiryStatus;
duration: number;
requestId: string;
trackingCode: string;
error?: Record<string, unknown>;
createdAt?: Date;
}): DurationExtremeItem {
return {
id: String(log._id),
inquiryType: log.inquiryType,
provider: log.provider,
status: log.status,
duration: log.duration,
requestId: log.requestId,
trackingCode: log.trackingCode,
error: log.error,
createdAt: log.createdAt,
};
}
}