-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage-provider-url-finder-shanpshot-job.service.ts
More file actions
185 lines (163 loc) · 6.47 KB
/
storage-provider-url-finder-shanpshot-job.service.ts
File metadata and controls
185 lines (163 loc) · 6.47 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Injectable, Logger } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import {
HealthCheckError,
HealthIndicator,
HealthIndicatorResult,
} from '@nestjs/terminus';
import * as _ from 'lodash';
import { DateTime } from 'luxon';
import {
StorageProviderUrlFinderMetricResultCodeType,
StorageProviderUrlFinderMetricType,
} from 'prisma/generated/client';
import { PrismaService } from 'src/db/prisma.service';
import { StorageProviderUrlFinderService } from 'src/service/storage-provider-url-finder/storage-provider-url-finder.service';
import { StorageProviderUrlFinderMetricValue } from 'src/service/storage-provider-url-finder/types.storage-provider-url-finder.service';
import { isTodayUTC } from 'src/utils/utils';
@Injectable()
export class StorageProviderUrlFinderSnapshotMetricService extends HealthIndicator {
private readonly logger = new Logger(
StorageProviderUrlFinderSnapshotMetricService.name,
);
private healthy = true;
private jobInProgress = false;
constructor(
private readonly prismaService: PrismaService,
private readonly storageProviderUrlFinderService: StorageProviderUrlFinderService,
) {
super();
}
public async getHealth(): Promise<HealthIndicatorResult> {
const result = this.getStatus(
StorageProviderUrlFinderSnapshotMetricService.name,
this.healthy,
{},
);
if (this.healthy) return result;
throw new HealthCheckError('Healthcheck failed', result);
}
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
public async fetchLastMetricSnapshotForStorageProviders() {
if (!this.jobInProgress) {
this.jobInProgress = true;
try {
this.logger.log(
'Starting creating the storage provider snapshot metrics job',
);
this.healthy = true;
const latestStoredSnapshot =
await this.prismaService.storage_provider_url_finder_daily_snapshot.findFirst(
{
orderBy: {
snapshot_date: 'desc',
},
},
);
const latestStoredDate = latestStoredSnapshot
? DateTime.fromJSDate(latestStoredSnapshot.snapshot_date, {
zone: 'UTC',
})
: null;
// skip if job already ran today - only for safety, should not happen due to cron
if (!!latestStoredDate && isTodayUTC(latestStoredDate)) {
this.logger.log(
'Storage provider snapshot metrics already created today - skipping',
);
return;
}
const providers = await this.prismaService.provider.findMany({});
const storageProvidersChunks = _.chunk(providers, 100);
const storageProviderSnapshotsToInsert = [];
function isSameUtcDay(a: Date, b: Date): boolean {
return (
a.getUTCFullYear() === b.getUTCFullYear() &&
a.getUTCMonth() === b.getUTCMonth() &&
a.getUTCDate() === b.getUTCDate()
);
}
const snapshotDate = DateTime.now().toUTC().startOf('day').toJSDate();
for (const spChunk of storageProvidersChunks) {
const dataForChunk =
await this.storageProviderUrlFinderService.fetchLastStorageProviderDataInBulk(
spChunk.map((x) => x.id),
);
const chunkMetricsToInsert = dataForChunk.providers.map(
(provider) => {
const {
provider_id,
retrievability_percent,
tested_at,
performance,
diagnostics,
} = provider;
const isPerformanceTestInLastTestDay = isSameUtcDay(
new Date(performance?.bandwidth?.tested_at),
new Date(tested_at),
);
// keep always RPA retreavability
const metrics: StorageProviderUrlFinderMetricValue[] = [
{
metricType:
StorageProviderUrlFinderMetricType.RPA_RETRIEVABILITY,
value:
retrievability_percent != null
? retrievability_percent / 100
: null,
testedAt: new Date(tested_at),
},
performance?.bandwidth?.ttfb_ms &&
isPerformanceTestInLastTestDay // only if test was done same day as retrievability test
? {
metricType: StorageProviderUrlFinderMetricType.TTFB,
value: performance?.bandwidth?.ttfb_ms,
testedAt: new Date(performance?.bandwidth?.tested_at),
}
: null,
performance?.bandwidth?.download_speed_mbps &&
isPerformanceTestInLastTestDay // only if test was done same day as retrievability test
? {
metricType: StorageProviderUrlFinderMetricType.BANDWIDTH,
value: performance?.bandwidth?.download_speed_mbps,
testedAt: new Date(performance?.bandwidth?.tested_at),
}
: null,
].filter(Boolean);
return {
provider: provider_id,
resultCode:
diagnostics?.result_code as StorageProviderUrlFinderMetricResultCodeType,
snapshotDate: snapshotDate,
testedAt: new Date(tested_at),
metricValues: metrics,
};
},
);
storageProviderSnapshotsToInsert.push(...chunkMetricsToInsert);
this.logger.log(
`Processed ${storageProviderSnapshotsToInsert.length} of ${providers.length} storage providers`,
);
}
if (storageProviderSnapshotsToInsert.length) {
await this.storageProviderUrlFinderService.ensureUrlFinderMetricTypesExist();
await this.storageProviderUrlFinderService.storeSnapshotMetricsForStorageProviders(
storageProviderSnapshotsToInsert,
);
}
} catch (err) {
this.healthy = false;
this.logger.error(
`Error while running storage provider snapshot metrics job: ${err.message}`,
err.cause?.stack || err.stack,
);
} finally {
this.jobInProgress = false;
this.logger.log(`Finished the storage provider snapshot metrics job`);
}
} else {
this.logger.warn(
'Storage Provider Snapshot job is already in progress - skipping next execution',
);
}
}
}