-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathStorageOptimizer.ts
More file actions
55 lines (47 loc) · 1.75 KB
/
StorageOptimizer.ts
File metadata and controls
55 lines (47 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export enum StorageTier {
HOT = 'HOT', // Frequent access, high cost, low latency
COOL = 'COOL', // Infrequent access, medium cost
ARCHIVE = 'ARCHIVE' // Rare access, lowest cost, high latency
}
export interface StorageMetrics {
accessCount: number;
lastAccessed: Date;
size: number;
}
export class StorageOptimizer {
private readonly HOT_THRESHOLD_DAYS = 30;
private readonly COOL_THRESHOLD_DAYS = 90;
/**
* Determines the optimal storage tier based on access patterns
*/
public determineTier(metrics: StorageMetrics): StorageTier {
const now = new Date();
const daysSinceLastAccess = (now.getTime() - metrics.lastAccessed.getTime()) / (1000 * 3600 * 24);
if (daysSinceLastAccess < this.HOT_THRESHOLD_DAYS || metrics.accessCount > 50) {
return StorageTier.HOT;
} else if (daysSinceLastAccess < this.COOL_THRESHOLD_DAYS) {
return StorageTier.COOL;
} else {
return StorageTier.ARCHIVE;
}
}
/**
* Calculates estimated cost savings by moving to a target tier
*/
public estimateCostSavings(sizeGB: number, currentTier: StorageTier, targetTier: StorageTier): number {
const rates = {
[StorageTier.HOT]: 0.023,
[StorageTier.COOL]: 0.01,
[StorageTier.ARCHIVE]: 0.004
};
const currentMonthlyCost = sizeGB * rates[currentTier];
const targetMonthlyCost = sizeGB * rates[targetTier];
return Math.max(0, currentMonthlyCost - targetMonthlyCost);
}
public shouldOptimize(metrics: StorageMetrics): boolean {
const currentTier = this.determineTier(metrics);
// Logic to decide if a migration is worth the API overhead
const daysOld = (new Date().getTime() - metrics.lastAccessed.getTime()) / (1000 * 3600 * 24);
return daysOld > this.HOT_THRESHOLD_DAYS;
}
}