@rabbitio/ui-kit 1.0.0-beta.37 → 1.0.0-beta.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,386 @@
1
+ import { improveAndRethrow } from "../common/errorUtils.js";
2
+ import { safeStringify } from "../common/utils/safeStringify.js";
3
+ import { Logger } from "../common/utils/logging/logger.js";
4
+ import { postponeExecution } from "../common/utils/postponeExecution.js";
5
+ import { AxiosAdapter } from "../common/adapters/axiosAdapter.js";
6
+
7
+ import { concurrentCalculationsMetadataHolder } from "./concurrentCalculationsMetadataHolder.js";
8
+ import { ExternalServicesStatsCollector } from "./externalServicesStatsCollector.js";
9
+
10
+ /**
11
+ * TODO: [refactoring, critical] update backend copy of this service. Also there is a task to extract this
12
+ * service and other related to it stuff to dedicated npm package task_id=b008ee5e4a3f42c08c73831c4bb3db4e
13
+ *
14
+ * Template service needed to avoid duplication of the same logic when we need to call
15
+ * external APIs to retrieve some data. The idea is to use several API providers to retrieve the same data. It helps to
16
+ * improve the reliability of a data retrieval.
17
+ */
18
+ export class RobustExternalAPICallerService {
19
+ static statsCollector = new ExternalServicesStatsCollector();
20
+
21
+ static getStats() {
22
+ this.statsCollector.getStats();
23
+ }
24
+
25
+ /**
26
+ * @param bio {string} service name for logging
27
+ * @param providersData {ExternalApiProvider[]} array of providers
28
+ * @param [logger] {function} function to be used for logging
29
+ */
30
+ constructor(bio, providersData, logger = Logger.logError) {
31
+ providersData.forEach((provider) => {
32
+ if (
33
+ (!provider.endpoint && provider.endpoint !== "") ||
34
+ !provider.httpMethod
35
+ ) {
36
+ throw new Error(
37
+ `Wrong format of providers data for: ${JSON.stringify(
38
+ provider
39
+ )}`
40
+ );
41
+ }
42
+ });
43
+
44
+ // We add niceFactor - just number to order the providers array by. It is helpful to call
45
+ // less robust APIs only if more robust fails
46
+ this.providers = providersData;
47
+ providersData.forEach((provider) => provider.resetNiceFactor());
48
+ this.bio = bio;
49
+ this._logger = Logger.logError;
50
+ }
51
+
52
+ static defaultRPSFactor = 1;
53
+ static rpsMultiplier = 1.05;
54
+
55
+ /**
56
+ * Performs data retrieval from external APIs. Tries providers till the data is retrieved.
57
+ *
58
+ * @param parametersValues {array} array of values of the parameters for URL query string [and/or body]
59
+ * @param timeoutMS {number} http timeout to wait for response. If provider has its specific timeout value then it is used
60
+ * @param [cancelToken] {object|undefined} axios token to force-cancel requests from high-level code
61
+ * @param [attemptsCount] {number|undefined} number of attempts to be performed
62
+ * @param [doNotFailForNowData] {boolean|undefined} pass true if you do not want us to throw an error if we retrieved null data from all the providers
63
+ * @return {Promise<any>} resolving to retrieved data (or array of results if specific provider requires
64
+ * several requests. NOTE: we flatten nested arrays - results of each separate request done for the specific provider)
65
+ * @throws Error if requests to all providers are failed
66
+ */
67
+ async callExternalAPI(
68
+ parametersValues = [],
69
+ timeoutMS = 3500,
70
+ cancelToken = null,
71
+ attemptsCount = 1,
72
+ doNotFailForNowData = false
73
+ ) {
74
+ let result;
75
+ const calculationUuid =
76
+ concurrentCalculationsMetadataHolder.startCalculation(this.bio);
77
+
78
+ try {
79
+ for (
80
+ let i = 0;
81
+ (i < attemptsCount || result?.shouldBeForceRetried) &&
82
+ result?.data == null;
83
+ ++i
84
+ ) {
85
+ /**
86
+ * We use rpsFactor to improve re-attempting to call the providers if the last attempt resulted with
87
+ * the fail due to abused RPSes of some (most part of) providers.
88
+ * The _performCallAttempt in such a case will return increased rpsFactor inside the result object.
89
+ */
90
+ const rpsFactor = result
91
+ ? result.rpsFactor
92
+ : RobustExternalAPICallerService.defaultRPSFactor;
93
+
94
+ result = null;
95
+
96
+ try {
97
+ if (i === 0 && !result?.shouldBeForceRetried) {
98
+ result = await this._performCallAttempt(
99
+ parametersValues,
100
+ timeoutMS,
101
+ cancelToken,
102
+ rpsFactor,
103
+ doNotFailForNowData
104
+ );
105
+ } else {
106
+ const maxRps = Math.max(
107
+ ...this.providers.map(
108
+ (provider) => provider.getRps() ?? 0
109
+ )
110
+ );
111
+ const waitingTimeMs = maxRps
112
+ ? 1000 / (maxRps / rpsFactor)
113
+ : 0;
114
+
115
+ result = await new Promise((resolve, reject) => {
116
+ setTimeout(async () => {
117
+ try {
118
+ resolve(
119
+ await this._performCallAttempt(
120
+ parametersValues,
121
+ timeoutMS,
122
+ cancelToken,
123
+ rpsFactor,
124
+ doNotFailForNowData
125
+ )
126
+ );
127
+ } catch (e) {
128
+ reject(e);
129
+ }
130
+ }, waitingTimeMs);
131
+ });
132
+ }
133
+ if (result.errors?.length) {
134
+ const errors = result.errors;
135
+ this._logger(
136
+ new Error(
137
+ `Failed at attempt ${i}. ${
138
+ errors.length
139
+ } errors. Messages: ${safeStringify(
140
+ errors.map((error) => error.message)
141
+ )}: ${safeStringify(errors)}.`
142
+ ),
143
+ `${this.bio}.callExternalAPI`,
144
+ "",
145
+ true
146
+ );
147
+ }
148
+ } catch (e) {
149
+ this._logger(
150
+ e,
151
+ `${this.bio}.callExternalAPI`,
152
+ "Failed to perform external providers calling"
153
+ );
154
+ }
155
+ }
156
+
157
+ if (result?.data == null) {
158
+ // TODO: [feature, moderate] looks like we should not fail for null data as it is strange - the provider will fail when processing data internally
159
+ const error = new Error(
160
+ `Failed to retrieve data. It means all attempts have been failed. DEV: add more attempts to this data retrieval`
161
+ );
162
+ if (!doNotFailForNowData) {
163
+ throw error;
164
+ } else {
165
+ this._logger(error, `${this.bio}.callExternalAPI`);
166
+ }
167
+ }
168
+
169
+ return result?.data;
170
+ } catch (e) {
171
+ improveAndRethrow(e, `${this.bio}.callExternalAPI`);
172
+ } finally {
173
+ concurrentCalculationsMetadataHolder.endCalculation(
174
+ this.bio,
175
+ calculationUuid
176
+ );
177
+ }
178
+ }
179
+
180
+ async _performCallAttempt(
181
+ parametersValues,
182
+ timeoutMS,
183
+ cancelToken,
184
+ rpsFactor,
185
+ doNotFailForNowData
186
+ ) {
187
+ const providers = this._reorderProvidersByNiceFactor();
188
+ let data = undefined,
189
+ providerIndex = 0,
190
+ countOfRequestsDeclinedByRps = 0,
191
+ errors = [];
192
+ while (!data && providerIndex < providers.length) {
193
+ let provider = providers[providerIndex];
194
+ if (provider.isRpsExceeded()) {
195
+ /**
196
+ * Current provider's RPS is exceeded, so we try next provider. Also, we count such cases to make
197
+ * a decision about the force-retry need.
198
+ */
199
+ ++providerIndex;
200
+ ++countOfRequestsDeclinedByRps;
201
+ continue;
202
+ }
203
+
204
+ try {
205
+ const axiosConfig = {
206
+ ...(cancelToken ? { cancelToken } : {}),
207
+ timeout: provider.timeout || timeoutMS,
208
+ headers: provider.specificHeaders ?? {},
209
+ };
210
+ const httpMethods = Array.isArray(provider.httpMethod)
211
+ ? provider.httpMethod
212
+ : [provider.httpMethod];
213
+ const iterationsData = [];
214
+ for (
215
+ let subRequestIndex = 0;
216
+ subRequestIndex < httpMethods.length;
217
+ ++subRequestIndex
218
+ ) {
219
+ const query = provider.composeQueryString(
220
+ parametersValues,
221
+ subRequestIndex
222
+ );
223
+ const endpoint = `${provider.endpoint}${query}`;
224
+ const axiosParams = [endpoint, axiosConfig];
225
+ if (
226
+ ["post", "put", "patch"].find(
227
+ (method) => method === httpMethods[subRequestIndex]
228
+ )
229
+ ) {
230
+ const body =
231
+ provider.composeBody(
232
+ parametersValues,
233
+ subRequestIndex
234
+ ) ?? null;
235
+ axiosParams.splice(1, 0, body);
236
+ }
237
+
238
+ let pageNumber = 0;
239
+ const responsesForPages = [];
240
+ let hasNextPage = provider.doesSupportPagination();
241
+ do {
242
+ if (subRequestIndex === 0 && pageNumber === 0) {
243
+ provider.actualizeLastCalledTimestamp();
244
+ responsesForPages[pageNumber] =
245
+ await AxiosAdapter.call(
246
+ httpMethods[subRequestIndex],
247
+ ...axiosParams
248
+ );
249
+ RobustExternalAPICallerService.statsCollector.externalServiceCalledWithoutError(
250
+ provider.getApiGroupId()
251
+ );
252
+ } else {
253
+ if (pageNumber > 0) {
254
+ const actualizedParams =
255
+ provider.changeQueryParametersForPageNumber(
256
+ parametersValues,
257
+ responsesForPages[pageNumber - 1],
258
+ pageNumber,
259
+ subRequestIndex
260
+ );
261
+ const query = provider.composeQueryString(
262
+ actualizedParams,
263
+ subRequestIndex
264
+ );
265
+ axiosParams[0] = `${provider.endpoint}${query}`;
266
+ }
267
+ /**
268
+ * For second and more request we postpone each request to not exceed RPS
269
+ * of current provider. We use rpsFactor to dynamically increase the rps to avoid
270
+ * too frequent calls if we continue failing to retrieve the data due to RPS exceeding.
271
+ * TODO: [dev] test RPS factor logic (units or integration)
272
+ */
273
+
274
+ const waitingTimeMS = provider.getRps()
275
+ ? 1000 / (provider.getRps() / rpsFactor)
276
+ : 0;
277
+
278
+ const postponeUntilRpsExceeded = async (
279
+ recursionLevel = 0
280
+ ) => {
281
+ return await postponeExecution(async () => {
282
+ const maxCountOfPostponingAttempts = 2;
283
+ if (
284
+ provider.isRpsExceeded() &&
285
+ recursionLevel <
286
+ maxCountOfPostponingAttempts
287
+ ) {
288
+ return await postponeUntilRpsExceeded(
289
+ recursionLevel + 1
290
+ );
291
+ }
292
+ provider.actualizeLastCalledTimestamp();
293
+ return await AxiosAdapter.call(
294
+ httpMethods[subRequestIndex],
295
+ ...axiosParams
296
+ );
297
+ }, waitingTimeMS);
298
+ };
299
+
300
+ responsesForPages[pageNumber] =
301
+ await postponeUntilRpsExceeded();
302
+ }
303
+
304
+ if (hasNextPage) {
305
+ hasNextPage =
306
+ !provider.checkWhetherResponseIsForLastPage(
307
+ responsesForPages[pageNumber - 1],
308
+ responsesForPages[pageNumber],
309
+ pageNumber,
310
+ subRequestIndex
311
+ );
312
+ }
313
+ pageNumber++;
314
+ } while (hasNextPage);
315
+
316
+ const responsesDataForPages = responsesForPages.map(
317
+ (response) =>
318
+ provider.getDataByResponse(
319
+ response,
320
+ parametersValues,
321
+ subRequestIndex,
322
+ iterationsData
323
+ )
324
+ );
325
+
326
+ let allData = responsesDataForPages;
327
+ if (Array.isArray(responsesDataForPages[0])) {
328
+ allData = responsesDataForPages.flat();
329
+ } else if (responsesDataForPages.length === 1) {
330
+ allData = responsesDataForPages[0];
331
+ }
332
+
333
+ iterationsData.push(allData);
334
+ }
335
+ if (iterationsData.length) {
336
+ if (httpMethods.length > 1) {
337
+ data =
338
+ provider.incorporateIterationsData(iterationsData);
339
+ } else {
340
+ data = iterationsData[0];
341
+ }
342
+ } else if (!doNotFailForNowData) {
343
+ RobustExternalAPICallerService.statsCollector.externalServiceFailed(
344
+ provider.getApiGroupId(),
345
+ "Response data was null for some reason"
346
+ );
347
+ punishProvider(provider);
348
+ }
349
+ } catch (e) {
350
+ punishProvider(provider);
351
+ RobustExternalAPICallerService.statsCollector.externalServiceFailed(
352
+ provider.getApiGroupId(),
353
+ e?.message
354
+ );
355
+ errors.push(e);
356
+ } finally {
357
+ providerIndex++;
358
+ }
359
+ }
360
+
361
+ // If we are declining more than 50% of providers (by exceeding RPS) then we note that it better to retry the whole process of providers requesting
362
+ const shouldBeForceRetried =
363
+ data == null &&
364
+ countOfRequestsDeclinedByRps > Math.floor(providers.length * 0.5);
365
+ const rpsMultiplier = shouldBeForceRetried
366
+ ? RobustExternalAPICallerService.rpsMultiplier
367
+ : 1;
368
+
369
+ return {
370
+ data: data ?? null,
371
+ shouldBeForceRetried,
372
+ rpsFactor: rpsFactor * rpsMultiplier,
373
+ errors,
374
+ };
375
+ }
376
+
377
+ _reorderProvidersByNiceFactor() {
378
+ const providersCopy = [...this.providers];
379
+
380
+ return providersCopy.sort((p1, p2) => p2.niceFactor - p1.niceFactor);
381
+ }
382
+ }
383
+
384
+ function punishProvider(provider) {
385
+ provider.niceFactor = provider.niceFactor - 1;
386
+ }