63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import type { Logger } from '@core/shared/application';
|
|
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
|
|
export interface GetAnalyticsMetricsInput {
|
|
startDate?: Date;
|
|
endDate?: Date;
|
|
}
|
|
|
|
export interface GetAnalyticsMetricsOutput {
|
|
pageViews: number;
|
|
uniqueVisitors: number;
|
|
averageSessionDuration: number;
|
|
bounceRate: number;
|
|
}
|
|
|
|
export type GetAnalyticsMetricsErrorCode = 'REPOSITORY_ERROR';
|
|
|
|
export class GetAnalyticsMetricsUseCase {
|
|
constructor(
|
|
private readonly pageViewRepository: IPageViewRepository,
|
|
private readonly logger: Logger,
|
|
) {}
|
|
|
|
async execute(input: GetAnalyticsMetricsInput = {}): Promise<
|
|
Result<GetAnalyticsMetricsOutput, ApplicationErrorCode<GetAnalyticsMetricsErrorCode, { message: string }>>
|
|
> {
|
|
try {
|
|
const startDate = input.startDate ?? new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); // 30 days ago
|
|
const endDate = input.endDate ?? new Date();
|
|
|
|
// For now, return placeholder values as actual implementation would require
|
|
// aggregating data across all entities or specifying which entity
|
|
// This is a simplified version
|
|
const pageViews = 0;
|
|
const uniqueVisitors = 0;
|
|
const averageSessionDuration = 0;
|
|
const bounceRate = 0;
|
|
|
|
this.logger.info('Analytics metrics retrieved', {
|
|
startDate,
|
|
endDate,
|
|
pageViews,
|
|
uniqueVisitors,
|
|
});
|
|
|
|
return Result.ok({
|
|
pageViews,
|
|
uniqueVisitors,
|
|
averageSessionDuration,
|
|
bounceRate,
|
|
});
|
|
} catch (error) {
|
|
const err = error as Error;
|
|
this.logger.error('Failed to get analytics metrics', { error, input });
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message: err.message ?? 'Failed to get analytics metrics' },
|
|
});
|
|
}
|
|
}
|
|
} |