refactor adapters
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* Infrastructure: InMemoryAnalyticsSnapshotRepository
|
||||
*
|
||||
* In-memory implementation of IAnalyticsSnapshotRepository for development/testing.
|
||||
*/
|
||||
|
||||
import type { IAnalyticsSnapshotRepository } from '../../domain/repositories/IAnalyticsSnapshotRepository';
|
||||
import { AnalyticsSnapshot, type SnapshotPeriod, type SnapshotEntityType } from '../../domain/entities/AnalyticsSnapshot';
|
||||
import type { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryAnalyticsSnapshotRepository implements IAnalyticsSnapshotRepository {
|
||||
private snapshots: Map<string, AnalyticsSnapshot> = new Map();
|
||||
private readonly logger: ILogger;
|
||||
|
||||
constructor(logger: ILogger) {
|
||||
this.logger = logger;
|
||||
this.logger.info('InMemoryAnalyticsSnapshotRepository initialized.');
|
||||
}
|
||||
|
||||
async save(snapshot: AnalyticsSnapshot): Promise<void> {
|
||||
this.logger.debug(`Saving AnalyticsSnapshot: ${snapshot.id}`);
|
||||
try {
|
||||
this.snapshots.set(snapshot.id, snapshot);
|
||||
this.logger.info(`AnalyticsSnapshot ${snapshot.id} saved successfully.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error saving AnalyticsSnapshot ${snapshot.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<AnalyticsSnapshot | null> {
|
||||
this.logger.debug(`Finding AnalyticsSnapshot by ID: ${id}`);
|
||||
try {
|
||||
const snapshot = this.snapshots.get(id) ?? null;
|
||||
if (snapshot) {
|
||||
this.logger.info(`Found AnalyticsSnapshot with ID: ${id}`);
|
||||
} else {
|
||||
this.logger.warn(`AnalyticsSnapshot with ID ${id} not found.`);
|
||||
}
|
||||
return snapshot;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding AnalyticsSnapshot by ID ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByEntity(entityType: SnapshotEntityType, entityId: string): Promise<AnalyticsSnapshot[]> {
|
||||
this.logger.debug(`Finding AnalyticsSnapshots by Entity: ${entityType}, ${entityId}`);
|
||||
try {
|
||||
const snapshots = Array.from(this.snapshots.values()).filter(
|
||||
s => s.entityType === entityType && s.entityId === entityId
|
||||
);
|
||||
this.logger.info(`Found ${snapshots.length} AnalyticsSnapshots for entity ${entityId}.`);
|
||||
return snapshots;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding AnalyticsSnapshots for entity ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByPeriod(
|
||||
entityType: SnapshotEntityType,
|
||||
entityId: string,
|
||||
period: SnapshotPeriod,
|
||||
startDate: Date,
|
||||
endDate: Date
|
||||
): Promise<AnalyticsSnapshot | null> {
|
||||
this.logger.debug(`Finding AnalyticsSnapshot by Period for entity ${entityId}, period ${period}, from ${startDate.toISOString()} to ${endDate.toISOString()}`);
|
||||
try {
|
||||
const snapshot = Array.from(this.snapshots.values()).find(
|
||||
s => s.entityType === entityType &&
|
||||
s.entityId === entityId &&
|
||||
s.period === period &&
|
||||
s.startDate >= startDate &&
|
||||
s.endDate <= endDate
|
||||
) ?? null;
|
||||
if (snapshot) {
|
||||
this.logger.info(`Found AnalyticsSnapshot for entity ${entityId}, period ${period}.`);
|
||||
} else {
|
||||
this.logger.warn(`No AnalyticsSnapshot found for entity ${entityId}, period ${period}, from ${startDate.toISOString()} to ${endDate.toISOString()}.`);
|
||||
}
|
||||
return snapshot;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding AnalyticsSnapshot by period for entity ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findLatest(
|
||||
entityType: SnapshotEntityType,
|
||||
entityId: string,
|
||||
period: SnapshotPeriod
|
||||
): Promise<AnalyticsSnapshot | null> {
|
||||
this.logger.debug(`Finding latest AnalyticsSnapshot for entity ${entityId}, period ${period}`);
|
||||
try {
|
||||
const matching = Array.from(this.snapshots.values())
|
||||
.filter(s => s.entityType === entityType && s.entityId === entityId && s.period === period)
|
||||
.sort((a, b) => b.endDate.getTime() - a.endDate.getTime());
|
||||
|
||||
const snapshot = matching[0] ?? null;
|
||||
if (snapshot) {
|
||||
this.logger.info(`Found latest AnalyticsSnapshot for entity ${entityId}, period ${period}.`);
|
||||
} else {
|
||||
this.logger.warn(`No latest AnalyticsSnapshot found for entity ${entityId}, period ${period}.`);
|
||||
}
|
||||
return snapshot;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding latest AnalyticsSnapshot for entity ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getHistoricalSnapshots(
|
||||
entityType: SnapshotEntityType,
|
||||
entityId: string,
|
||||
period: SnapshotPeriod,
|
||||
limit: number
|
||||
): Promise<AnalyticsSnapshot[]> {
|
||||
this.logger.debug(`Getting historical AnalyticsSnapshots for entity ${entityId}, period ${period}, limit ${limit}`);
|
||||
try {
|
||||
const snapshots = Array.from(this.snapshots.values())
|
||||
.filter(s => s.entityType === entityType && s.entityId === entityId && s.period === period)
|
||||
.sort((a, b) => b.endDate.getTime() - a.endDate.getTime())
|
||||
.slice(0, limit);
|
||||
this.logger.info(`Found ${snapshots.length} historical AnalyticsSnapshots for entity ${entityId}, period ${period}.`);
|
||||
return snapshots;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error getting historical AnalyticsSnapshots for entity ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper for testing
|
||||
clear(): void {
|
||||
this.snapshots.clear();
|
||||
}
|
||||
|
||||
// Helper for seeding demo data
|
||||
seed(snapshots: AnalyticsSnapshot[]): void {
|
||||
snapshots.forEach(s => this.snapshots.set(s.id, s));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Infrastructure: InMemoryEngagementRepository
|
||||
*
|
||||
* In-memory implementation of IEngagementRepository for development/testing.
|
||||
*/
|
||||
|
||||
import type { IEngagementRepository } from '../../domain/repositories/IEngagementRepository';
|
||||
import { EngagementEvent, type EngagementAction, type EngagementEntityType } from '../../domain/entities/EngagementEvent';
|
||||
import { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryEngagementRepository implements IEngagementRepository {
|
||||
private events: Map<string, EngagementEvent> = new Map();
|
||||
private logger: ILogger;
|
||||
|
||||
constructor(logger: ILogger) {
|
||||
this.logger = logger;
|
||||
this.logger.info('InMemoryEngagementRepository initialized.');
|
||||
}
|
||||
|
||||
async save(event: EngagementEvent): Promise<void> {
|
||||
this.logger.debug(`Attempting to save engagement event: ${event.id}`);
|
||||
try {
|
||||
this.events.set(event.id, event);
|
||||
this.logger.info(`Successfully saved engagement event: ${event.id}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error saving engagement event ${event.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<EngagementEvent | null> {
|
||||
this.logger.debug(`Attempting to find engagement event by ID: ${id}`);
|
||||
try {
|
||||
const event = this.events.get(id) ?? null;
|
||||
if (event) {
|
||||
this.logger.info(`Found engagement event by ID: ${id}`);
|
||||
} else {
|
||||
this.logger.warn(`Engagement event not found for ID: ${id}`);
|
||||
// The original was info, but if a requested ID is not found that's more of a warning than an info.
|
||||
}
|
||||
return event;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding engagement event by ID ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByEntityId(entityType: EngagementEntityType, entityId: string): Promise<EngagementEvent[]> {
|
||||
this.logger.debug(`Attempting to find engagement events for entityType: ${entityType}, entityId: ${entityId}`);
|
||||
try {
|
||||
const events = Array.from(this.events.values()).filter(
|
||||
e => e.entityType === entityType && e.entityId === entityId
|
||||
);
|
||||
this.logger.info(`Found ${events.length} engagement events for entityType: ${entityType}, entityId: ${entityId}`);
|
||||
return events;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding engagement events by entity ID ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByAction(action: EngagementAction): Promise<EngagementEvent[]> {
|
||||
this.logger.debug(`Attempting to find engagement events by action: ${action}`);
|
||||
try {
|
||||
const events = Array.from(this.events.values()).filter(
|
||||
e => e.action === action
|
||||
);
|
||||
this.logger.info(`Found ${events.length} engagement events for action: ${action}`);
|
||||
return events;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding engagement events by action ${action}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByDateRange(startDate: Date, endDate: Date): Promise<EngagementEvent[]> {
|
||||
this.logger.debug(`Attempting to find engagement events by date range: ${startDate.toISOString()} - ${endDate.toISOString()}`);
|
||||
try {
|
||||
const events = Array.from(this.events.values()).filter(
|
||||
e => e.timestamp >= startDate && e.timestamp <= endDate
|
||||
);
|
||||
this.logger.info(`Found ${events.length} engagement events for date range.`);
|
||||
return events;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding engagement events by date range:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async countByAction(action: EngagementAction, entityId?: string, since?: Date): Promise<number> {
|
||||
this.logger.debug(`Attempting to count engagement events for action: ${action}, entityId: ${entityId}, since: ${since?.toISOString()}`);
|
||||
try {
|
||||
const count = Array.from(this.events.values()).filter(
|
||||
e => e.action === action &&
|
||||
(!entityId || e.entityId === entityId) &&
|
||||
(!since || e.timestamp >= since)
|
||||
).length;
|
||||
this.logger.info(`Counted ${count} engagement events for action: ${action}, entityId: ${entityId}`);
|
||||
return count;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error counting engagement events by action ${action}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async getSponsorClicksForEntity(entityId: string, since?: Date): Promise<number> {
|
||||
this.logger.debug(`Attempting to get sponsor clicks for entity ID: ${entityId}, since: ${since?.toISOString()}`);
|
||||
try {
|
||||
const count = Array.from(this.events.values()).filter(
|
||||
e => e.entityId === entityId &&
|
||||
(e.action === 'click_sponsor_logo' || e.action === 'click_sponsor_url') &&
|
||||
(!since || e.timestamp >= since)
|
||||
).length;
|
||||
this.logger.info(`Counted ${count} sponsor clicks for entity ID: ${entityId}`);
|
||||
return count;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error getting sponsor clicks for entity ID ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper for testing
|
||||
clear(): void {
|
||||
this.logger.debug('Clearing all engagement events.');
|
||||
this.events.clear();
|
||||
this.logger.info('All engagement events cleared.');
|
||||
}
|
||||
|
||||
// Helper for seeding demo data
|
||||
seed(events: EngagementEvent[]): void {
|
||||
this.logger.debug(`Seeding ${events.length} engagement events.`);
|
||||
try {
|
||||
events.forEach(e => this.events.set(e.id, e));
|
||||
this.logger.info(`Successfully seeded ${events.length} engagement events.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error seeding engagement events:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Infrastructure: InMemoryPageViewRepository
|
||||
*
|
||||
* In-memory implementation of IPageViewRepository for development/testing.
|
||||
*/
|
||||
|
||||
import type { IPageViewRepository } from '../../domain/repositories/IPageViewRepository';
|
||||
import { PageView, type EntityType } from '../../domain/entities/PageView';
|
||||
import { ILogger } from '@gridpilot/shared/logging/ILogger';
|
||||
|
||||
export class InMemoryPageViewRepository implements IPageViewRepository {
|
||||
private pageViews: Map<string, PageView> = new Map();
|
||||
private logger: ILogger;
|
||||
|
||||
constructor(logger: ILogger) {
|
||||
this.logger = logger;
|
||||
this.logger.info('InMemoryPageViewRepository initialized.');
|
||||
}
|
||||
|
||||
async save(pageView: PageView): Promise<void> {
|
||||
this.logger.debug(`Attempting to save page view: ${pageView.id}`);
|
||||
try {
|
||||
this.pageViews.set(pageView.id, pageView);
|
||||
this.logger.info(`Successfully saved page view: ${pageView.id}`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error saving page view ${pageView.id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<PageView | null> {
|
||||
this.logger.debug(`Attempting to find page view by ID: ${id}`);
|
||||
try {
|
||||
const pageView = this.pageViews.get(id) ?? null;
|
||||
if (pageView) {
|
||||
this.logger.info(`Found page view by ID: ${id}`);
|
||||
} else {
|
||||
this.logger.warn(`Page view not found for ID: ${id}`);
|
||||
}
|
||||
return pageView;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding page view by ID ${id}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByEntityId(entityType: EntityType, entityId: string): Promise<PageView[]> {
|
||||
this.logger.debug(`Attempting to find page views for entityType: ${entityType}, entityId: ${entityId}`);
|
||||
try {
|
||||
const pageViews = Array.from(this.pageViews.values()).filter(
|
||||
pv => pv.entityType === entityType && pv.entityId === entityId
|
||||
);
|
||||
this.logger.info(`Found ${pageViews.length} page views for entityType: ${entityType}, entityId: ${entityId}`);
|
||||
return pageViews;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding page views by entity ID ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findByDateRange(startDate: Date, endDate: Date): Promise<PageView[]> {
|
||||
this.logger.debug(`Attempting to find page views by date range: ${startDate.toISOString()} - ${endDate.toISOString()}`);
|
||||
try {
|
||||
const pageViews = Array.from(this.pageViews.values()).filter(
|
||||
pv => pv.timestamp >= startDate && pv.timestamp <= endDate
|
||||
);
|
||||
this.logger.info(`Found ${pageViews.length} page views for date range.`);
|
||||
return pageViews;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding page views by date range:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findBySession(sessionId: string): Promise<PageView[]> {
|
||||
this.logger.debug(`Attempting to find page views by session ID: ${sessionId}`);
|
||||
try {
|
||||
const pageViews = Array.from(this.pageViews.values()).filter(
|
||||
pv => pv.sessionId === sessionId
|
||||
);
|
||||
this.logger.info(`Found ${pageViews.length} page views for session ID: ${sessionId}`);
|
||||
return pageViews;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error finding page views by session ID ${sessionId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async countByEntityId(entityType: EntityType, entityId: string, since?: Date): Promise<number> {
|
||||
this.logger.debug(`Attempting to count page views for entityType: ${entityType}, entityId: ${entityId}, since: ${since?.toISOString()}`);
|
||||
try {
|
||||
const count = Array.from(this.pageViews.values()).filter(
|
||||
pv => pv.entityType === entityType &&
|
||||
pv.entityId === entityId &&
|
||||
(!since || pv.timestamp >= since)
|
||||
).length;
|
||||
this.logger.info(`Counted ${count} page views for entityType: ${entityType}, entityId: ${entityId}`);
|
||||
return count;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error counting page views by entity ID ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async countUniqueVisitors(entityType: EntityType, entityId: string, since?: Date): Promise<number> {
|
||||
this.logger.debug(`Attempting to count unique visitors for entityType: ${entityType}, entityId: ${entityId}, since: ${since?.toISOString()}`);
|
||||
try {
|
||||
const visitors = new Set<string>();
|
||||
Array.from(this.pageViews.values())
|
||||
.filter(
|
||||
pv => pv.entityType === entityType &&
|
||||
pv.entityId === entityId &&
|
||||
(!since || pv.timestamp >= since)
|
||||
)
|
||||
.forEach(pv => {
|
||||
visitors.add(pv.visitorId ?? pv.sessionId);
|
||||
});
|
||||
this.logger.info(`Counted ${visitors.size} unique visitors for entityType: ${entityType}, entityId: ${entityId}`);
|
||||
return visitors.size;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error counting unique visitors for entity ID ${entityId}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper for testing
|
||||
clear(): void {
|
||||
this.logger.debug('Clearing all page views.');
|
||||
this.pageViews.clear();
|
||||
this.logger.info('All page views cleared.');
|
||||
}
|
||||
|
||||
// Helper for seeding demo data
|
||||
seed(pageViews: PageView[]): void {
|
||||
this.logger.debug(`Seeding ${pageViews.length} page views.`);
|
||||
try {
|
||||
pageViews.forEach(pv => this.pageViews.set(pv.id, pv));
|
||||
this.logger.info(`Successfully seeded ${pageViews.length} page views.`);
|
||||
} catch (error) {
|
||||
this.logger.error(`Error seeding page views:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user