wip
This commit is contained in:
@@ -6,22 +6,56 @@
|
||||
|
||||
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.snapshots.set(snapshot.id, snapshot);
|
||||
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> {
|
||||
return this.snapshots.get(id) ?? 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[]> {
|
||||
return Array.from(this.snapshots.values()).filter(
|
||||
s => s.entityType === entityType && s.entityId === entityId
|
||||
);
|
||||
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(
|
||||
@@ -31,13 +65,25 @@ export class InMemoryAnalyticsSnapshotRepository implements IAnalyticsSnapshotRe
|
||||
startDate: Date,
|
||||
endDate: Date
|
||||
): Promise<AnalyticsSnapshot | null> {
|
||||
return Array.from(this.snapshots.values()).find(
|
||||
s => s.entityType === entityType &&
|
||||
s.entityId === entityId &&
|
||||
s.period === period &&
|
||||
s.startDate >= startDate &&
|
||||
s.endDate <= endDate
|
||||
) ?? 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(
|
||||
@@ -45,11 +91,23 @@ export class InMemoryAnalyticsSnapshotRepository implements IAnalyticsSnapshotRe
|
||||
entityId: string,
|
||||
period: SnapshotPeriod
|
||||
): Promise<AnalyticsSnapshot | null> {
|
||||
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());
|
||||
|
||||
return matching[0] ?? 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(
|
||||
@@ -58,10 +116,18 @@ export class InMemoryAnalyticsSnapshotRepository implements IAnalyticsSnapshotRe
|
||||
period: SnapshotPeriod,
|
||||
limit: number
|
||||
): Promise<AnalyticsSnapshot[]> {
|
||||
return 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.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
|
||||
|
||||
Reference in New Issue
Block a user