-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocators.controller.ts
More file actions
623 lines (582 loc) · 19.4 KB
/
allocators.controller.ts
File metadata and controls
623 lines (582 loc) · 19.4 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import { Cache, CACHE_MANAGER, CacheTTL } from '@nestjs/cache-manager';
import {
BadRequestException,
Controller,
Get,
Inject,
Logger,
Param,
Query,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation } from '@nestjs/swagger';
import { DateTime } from 'luxon';
import { getAllocatorsScoresSummaryByMetric } from 'prisma/generated/client/sql';
import { PrismaService } from 'src/db/prisma.service';
import { AllocatorReportChecksService } from 'src/service/allocator-report-checks/allocator-report-checks.service';
import { AllocatorScoringService } from 'src/service/allocator-scoring/allocator-scoring.service';
import { AllocatorService } from 'src/service/allocator/allocator.service';
import {
AllocationByAllocator,
AllocatorAuditOutcomesData,
AllocatorAuditStatesData,
AllocatorAuditTimesByMonthData,
AllocatorAuditTimesByRoundData,
} from 'src/service/allocator/types.allocator';
import { StorageProviderComplianceMetrics } from 'src/service/storage-provider/types.storage-provider';
import { Cacheable } from 'src/utils/cacheable';
import {
getCurrentFilPlusEdition,
getFilPlusEditionByTimestamp,
} from 'src/utils/filplus-edition';
import {
bigIntDiv,
lastWeek,
stringToBool,
stringToDate,
stringToNumber,
} from 'src/utils/utils';
import { FilPlusEditionControllerBase } from '../base/filplus-edition-controller-base';
import {
DashboardStatistic,
DashboardStatisticValue,
} from '../base/types.controller-base';
import { FilPlusEditionRequest } from '../base/types.filplus-edition-controller-base';
import {
DataType,
AllocatorsDashboardStatistic,
AllocatorsDashboardStatisticType,
GetAllocatorsLatestScoresRankingRequest,
GetAllocatorsLatestScoresRankingResponse,
GetAllocatorsRequest,
GetAllocatorsScoresSummaryByMetricRequest,
GetAllocatorsScoresSummaryByMetricResponse,
GetAllocatorsStatisticsRequest,
GetAllocatorVerifiedClientsRequest,
GetDatacapFlowDataRequest,
GetDatacapFlowDataResponse,
GetWeekAllocatorsWithSpsComplianceRequest,
GetWeekAllocatorsWithSpsComplianceRequestData,
GetAllocationsByAllocatorRequest,
} from './types.allocators';
const dashboardStatisticsTitleDict: Record<
AllocatorsDashboardStatisticType,
AllocatorsDashboardStatistic['title']
> = {
TOTAL_APPROVED_ALLOCATORS: 'Approved Allocators',
TOTAL_ACTIVE_ALLOCATORS: 'Active Allocators',
COMPLIANT_ALLOCATORS: 'Compliant Allocators',
NON_COMPLIANT_ALLOCATORS: 'Non Compliant Allocators',
NUMBER_OF_ALERTS: 'Alerts',
AVERAGE_NUMBER_OF_CLIENTS: 'Avg. Clients',
AVERAGE_PERCENTAGE_OF_RETURNING_CLIENTS: 'Avg. Returning Clients',
AVERAGE_TIME_TO_FIRST_DEAL: 'Avg. TTFD',
};
const dashboardStatisticsDescriptionDict: Record<
AllocatorsDashboardStatisticType,
AllocatorsDashboardStatistic['description']
> = {
TOTAL_APPROVED_ALLOCATORS: null,
TOTAL_ACTIVE_ALLOCATORS:
'Number of allocators that spent Datacap in last 60 days',
COMPLIANT_ALLOCATORS: `Percentage of Allocators with score above ${AllocatorService.COMPLIANT_ALLOCATORS_DASHBOARD_STAT_SCORE_PERCENTAGE_THRESHOLD * 100}%`,
NON_COMPLIANT_ALLOCATORS: `Percentage of allocators with score below ${AllocatorService.NON_COMPLIANT_ALLOCATORS_DASHBOARD_STAT_SCORE_PERCENTAGE_THRESHOLD * 100}%`,
NUMBER_OF_ALERTS: null,
AVERAGE_NUMBER_OF_CLIENTS: 'Average Number of Clients per Allocator',
AVERAGE_PERCENTAGE_OF_RETURNING_CLIENTS:
'Average Percentage of returning Clients',
AVERAGE_TIME_TO_FIRST_DEAL: 'Average time to first deal',
};
const negativeStatistics = [
'NON_COMPLIANT_ALLOCATORS',
'NUMBER_OF_ALERTS',
] satisfies AllocatorsDashboardStatisticType[];
@Controller('allocators')
@CacheTTL(1000 * 60 * 30) // 30 minutes
export class AllocatorsController extends FilPlusEditionControllerBase {
private readonly logger = new Logger(AllocatorsController.name);
constructor(
@Inject(CACHE_MANAGER) private cacheManager: Cache,
private readonly allocatorService: AllocatorService,
private readonly allocatorScoringService: AllocatorScoringService,
private readonly allocatorReportChecksService: AllocatorReportChecksService,
private readonly prismaService: PrismaService,
) {
super();
}
@Get('/dc-flow')
@ApiOperation({
summary: 'Get datacap flow data for allocators',
})
@ApiOkResponse({
description: 'Datacap flow data for allocators',
type: GetDatacapFlowDataResponse,
})
public async getDatacapFlowData(
@Query() query: GetDatacapFlowDataRequest,
): Promise<GetDatacapFlowDataResponse> {
const cutoffDate = stringToDate(query.cutoffDate);
const dcFlowData =
await this.allocatorService.getDatacapFlowData(cutoffDate);
return {
cutoffDate: stringToDate(query.cutoffDate) ?? new Date(),
filPlusEditionId: query.cutoffDate
? getFilPlusEditionByTimestamp(cutoffDate.getTime()).id
: getCurrentFilPlusEdition().id,
data: dcFlowData,
};
}
@Get('/latest-scores')
@ApiOperation({
summary: 'Get allocators latest scores ranking',
})
@ApiOkResponse({
description: 'Allocators latest scores ranking',
type: GetAllocatorsLatestScoresRankingResponse,
isArray: true,
})
public async getAllocatorsLatestScoresRanking(
@Query() query: GetAllocatorsLatestScoresRankingRequest,
): Promise<GetAllocatorsLatestScoresRankingResponse[]> {
return (
await this.allocatorScoringService.getLatestScores(query.dataType)
).map((item) => ({
...item,
dataType: item.dataType as DataType,
}));
}
@Get('/scores-summary-by-metric')
@ApiOperation({
summary:
'Get summary of allocators scores grouped by metric and week/month',
})
@ApiOkResponse({
description: 'Summary of allocators scores',
type: GetAllocatorsScoresSummaryByMetricResponse,
isArray: true,
})
public async getAllocatorsScoresSummaryByMetric(
@Query() query: GetAllocatorsScoresSummaryByMetricRequest,
): Promise<GetAllocatorsScoresSummaryByMetricResponse[]> {
query.groupBy ??= 'week';
if (query.groupBy && !['week', 'month'].includes(query.groupBy)) {
throw new BadRequestException(
`Invalid groupBy value: ${query.groupBy}, must be 'week' or 'month'`,
);
}
return (
await this.prismaService.$queryRawTyped(
getAllocatorsScoresSummaryByMetric(
query.groupBy,
query.dataType,
stringToNumber(query.mediumScoreThreshold),
stringToNumber(query.highScoreThreshold),
stringToBool(query.includeDetails),
),
)
).map((item) => ({
...item,
data: item.data as [],
}));
}
@Get('/audit-states')
@ApiOperation({
summary: 'Get audit states data for allocators',
})
@ApiOkResponse({
description: 'Audit states data for allocators',
type: AllocatorAuditStatesData,
isArray: true,
})
public async getAuditStatesData(
@Query() query: FilPlusEditionRequest,
): Promise<AllocatorAuditStatesData[]> {
return await this.allocatorService.getAuditStatesData(
this.getFilPlusEditionFromRequest(query) ?? getCurrentFilPlusEdition(),
);
}
@Get('/audit-times-by-round')
@ApiOperation({
summary: 'Get audit times data for allocators by audit round',
})
@ApiOkResponse({
description: 'Audit times data for allocators by audit round',
type: AllocatorAuditTimesByRoundData,
})
public async getAuditTimesByRoundData(
@Query() query: FilPlusEditionRequest,
): Promise<AllocatorAuditTimesByRoundData> {
return await this.allocatorService.getAuditTimesByRoundData(
this.getFilPlusEditionFromRequest(query) ?? getCurrentFilPlusEdition(),
);
}
@Get('/audit-times-by-month')
@ApiOperation({
summary: 'Get audit times data for allocators by month',
})
@ApiOkResponse({
description: 'Audit times data for allocators by month',
type: AllocatorAuditTimesByMonthData,
isArray: true,
})
public async getAuditTimesByMonthData(
@Query() query: FilPlusEditionRequest,
): Promise<AllocatorAuditTimesByMonthData[]> {
return await this.allocatorService.getAuditTimesByMonthData(
this.getFilPlusEditionFromRequest(query),
);
}
@Get('/audit-outcomes')
@ApiOperation({
summary: 'Get audit outcomes data for allocators by month',
})
@ApiOkResponse({
description: 'Audit outcomes data for allocators by month',
type: AllocatorAuditOutcomesData,
isArray: true,
})
public async getAuditOutcomesData(
@Query() query: FilPlusEditionRequest,
): Promise<AllocatorAuditOutcomesData[]> {
return await this.allocatorService.getAuditOutcomesData(
this.getFilPlusEditionFromRequest(query),
);
}
@Get()
@ApiOperation({
summary: 'Get list of all allocators',
})
@ApiOkResponse({
description: 'List of allocators',
type: null,
})
public async getAllocators(@Query() query: GetAllocatorsRequest) {
const allocators = await this.allocatorService.getAllocators(
stringToBool(query.showInactive) ?? true,
stringToBool(query.isMetaallocator),
query.filter,
query.usingMetaallocator,
query.dataType,
this.getFilPlusEditionFromRequest(query),
);
return this.withPaginationInfo(
{
count: allocators.length,
data: this.paginated(this.sorted(allocators, query), query),
},
query,
allocators.length,
);
}
@Cacheable({ ttl: 1000 * 60 * 30 }) // 30 minutes
private async _getWeekAllocatorsWithSpsCompliance(
query: GetWeekAllocatorsWithSpsComplianceRequestData,
) {
const allocators = await this.allocatorService.getAllocators(
true,
null,
query.filter,
query.usingMetaallocator,
);
const weekAllocatorsSpsCompliance =
await this.allocatorService.getWeekStandardAllocatorSpsCompliance(
stringToDate(query.week!)!,
StorageProviderComplianceMetrics.of(query),
);
const weekAllocatorsCompliance = weekAllocatorsSpsCompliance.allocators.map(
(allocator) =>
this.allocatorService.calculateAllocatorComplianceScore(
allocator,
query.complianceThresholdPercentage,
),
);
// this effectively filters out metaallocators and allocators from previous fil+ edition
return weekAllocatorsCompliance
.map((allocator) => {
const allocatorData = allocators.find(
(a) => a.addressId === allocator.allocator,
);
return (
allocatorData && {
complianceScore: allocator.complianceScore,
...allocatorData,
}
);
})
.filter((allocator) => allocator?.addressId);
}
@Get('/compliance-data')
@ApiOperation({
summary: 'Get list of allocators with compliance score',
})
@ApiOkResponse({
description: 'List of allocators with compliance score',
type: null,
})
public async getWeekAllocatorsWithSpsCompliance(
@Query() query: GetWeekAllocatorsWithSpsComplianceRequest,
) {
query.week ??= lastWeek().toISOString(); // last week default
let allocators = await this._getWeekAllocatorsWithSpsCompliance(query);
if (query.complianceScore) {
allocators = allocators.filter(
(allocator) => allocator.complianceScore === query.complianceScore,
);
}
return this.withPaginationInfo(
{
week: query.week,
metricsChecked: StorageProviderComplianceMetrics.of(query),
complianceThresholdPercentage: query.complianceThresholdPercentage,
complianceScore: query.complianceScore,
count: allocators.length,
data: this.paginated(this.sorted(allocators, query), query),
},
query,
allocators.length,
);
}
@Get(':allocatorId/verified-clients')
@ApiOperation({
summary: 'Get paginated list of verified clients for allocator',
})
@ApiOkResponse({
description: 'Paginated list of verified clients for allocator',
type: null,
})
public async getVerifiedClientsByAllocator(
@Param('allocatorId') allocatorId: string,
@Query() query: GetAllocatorVerifiedClientsRequest,
) {
const verifiedClients =
await this.allocatorService.getVerifiedClientsByAllocator(allocatorId);
return this.withPaginationInfo(
{
data: this.paginated(this.sorted(verifiedClients, query), query),
},
query,
verifiedClients.length,
);
}
@Get(':allocatorId/allocations')
@ApiOperation({
summary: 'Get allocations over time for allocator grouped by week or month',
})
@ApiOkResponse({
description:
'List of allocations over time for allocator grouped by week or month',
type: AllocationByAllocator,
isArray: true,
})
public async getAllocationsByAllocator(
@Param('allocatorId') allocatorId: string,
@Query() query: GetAllocationsByAllocatorRequest,
): Promise<AllocationByAllocator[]> {
query.groupBy ??= 'week';
if (query.groupBy && !['week', 'month'].includes(query.groupBy)) {
throw new BadRequestException(
`Invalid groupBy value: ${query.groupBy}, must be 'week' or 'month'`,
);
}
return this.allocatorService.getAllocationsByAllocator(
allocatorId,
query.groupBy,
stringToBool(query.showEmptyPeriods) ?? false,
stringToBool(query.showAllocations) ?? false,
);
}
@Get('/statistics')
@ApiOperation({
summary: 'Get list of statistics regarding allocators',
})
@ApiOkResponse({
description: 'List of statistics regarding allocators',
type: [AllocatorsDashboardStatistic],
})
public async getAllocatorsStatistics(
@Query() query: GetAllocatorsStatisticsRequest,
): Promise<AllocatorsDashboardStatistic[]> {
const { interval = 'day' } = query;
const cutoffDate = DateTime.now()
.toUTC()
.minus({ [interval]: 1 })
.toJSDate();
const [
currentApprovedAllocatorsCount,
previousApprovedAllocatorsCount,
currentActiveAllocatorsCount,
previousActiveAllocatorsCount,
currentCompliantAllocatorsPercentage,
previousCompliantAllocatorsPercentage,
currentNonCompliantAllocatorsPercentage,
previousNonCompliantAllocatorsPercentage,
currentAlertsCount,
previousAlertsCount,
currentClientsStats,
previousClientsStats,
] = await Promise.all([
this.allocatorService.getApprovedAllocatorsStat(),
this.allocatorService.getApprovedAllocatorsStat({
cutoffDate: cutoffDate,
}),
this.allocatorService.getActiveAllocatorsStat(),
this.allocatorService.getActiveAllocatorsStat({ cutoffDate: cutoffDate }),
this.allocatorService.getCompliantAllocatorsStat(),
this.allocatorService.getCompliantAllocatorsStat({
cutoffDate: cutoffDate,
}),
this.allocatorService.getNonCompliantAllocatorsStat(),
this.allocatorService.getNonCompliantAllocatorsStat({
cutoffDate: cutoffDate,
}),
this.allocatorReportChecksService.getFailedReportChecksCount(),
this.allocatorReportChecksService.getFailedReportChecksCount({
date: cutoffDate,
}),
this.allocatorService.getAllocatorsClientsStats(),
this.allocatorService.getAllocatorsClientsStats({
cutoffDate: cutoffDate,
}),
]);
return [
this.calculateDashboardStatistic({
type: 'TOTAL_APPROVED_ALLOCATORS',
currentValue: {
value: currentApprovedAllocatorsCount,
type: 'numeric',
},
previousValue: {
value: previousApprovedAllocatorsCount,
type: 'numeric',
},
interval: interval,
}),
this.calculateDashboardStatistic({
type: 'TOTAL_ACTIVE_ALLOCATORS',
currentValue: {
value: currentActiveAllocatorsCount,
type: 'numeric',
},
previousValue: {
value: previousActiveAllocatorsCount,
type: 'numeric',
},
interval: interval,
}),
this.calculateDashboardStatistic({
type: 'COMPLIANT_ALLOCATORS',
currentValue: {
value: currentCompliantAllocatorsPercentage,
type: 'percentage',
},
previousValue: {
value: previousCompliantAllocatorsPercentage,
type: 'percentage',
},
interval: interval,
}),
this.calculateDashboardStatistic({
type: 'NON_COMPLIANT_ALLOCATORS',
currentValue: {
value: currentNonCompliantAllocatorsPercentage,
type: 'percentage',
},
previousValue: {
value: previousNonCompliantAllocatorsPercentage,
type: 'percentage',
},
interval: interval,
}),
this.calculateDashboardStatistic({
type: 'NUMBER_OF_ALERTS',
currentValue: {
value: currentAlertsCount,
type: 'numeric',
},
previousValue: {
value: previousAlertsCount,
type: 'numeric',
},
interval: interval,
}),
this.calculateDashboardStatistic({
type: 'AVERAGE_NUMBER_OF_CLIENTS',
currentValue: {
value: currentClientsStats.averageNumberOfClients ?? 0,
type: 'numeric',
},
previousValue: {
value: previousClientsStats.averageNumberOfClients ?? 0,
type: 'numeric',
},
interval: interval,
}),
this.calculateDashboardStatistic({
type: 'AVERAGE_PERCENTAGE_OF_RETURNING_CLIENTS',
currentValue: {
value: currentClientsStats.averageReturningClientsPercentage ?? 0,
type: 'percentage',
},
previousValue: {
value: previousClientsStats.averageReturningClientsPercentage ?? 0,
type: 'percentage',
},
interval: interval,
}),
this.calculateDashboardStatistic({
type: 'AVERAGE_TIME_TO_FIRST_DEAL',
currentValue: {
value: currentClientsStats.averageSecondsToFirstDeal ?? 0,
type: 'duration',
unit: 'second',
},
previousValue: {
value: previousClientsStats.averageSecondsToFirstDeal ?? 0,
type: 'duration',
unit: 'second',
},
interval: interval,
}),
];
}
private calculateDashboardStatistic(options: {
type: AllocatorsDashboardStatistic['type'];
currentValue: DashboardStatisticValue;
previousValue: DashboardStatisticValue;
interval: DashboardStatistic['percentageChange']['interval'];
}): AllocatorsDashboardStatistic {
const { type, currentValue, previousValue, interval } = options;
if (currentValue.type !== previousValue.type) {
throw new TypeError(
'Cannot compare different dashboard statistics types',
);
}
const percentageChange: AllocatorsDashboardStatistic['percentageChange'] =
(() => {
const dividerIsZero =
previousValue.type === 'bigint'
? BigInt(previousValue.value) === 0n
: previousValue.value === 0;
if (dividerIsZero) return null;
const ratio =
currentValue.type === 'bigint' || previousValue.type === 'bigint'
? bigIntDiv(
BigInt(currentValue.value),
BigInt(previousValue.value),
2,
)
: currentValue.value / previousValue.value;
return {
value: ratio - 1,
interval: interval,
increaseNegative: (negativeStatistics as string[]).includes(type),
};
})();
return {
type: type,
title: dashboardStatisticsTitleDict[type],
description: dashboardStatisticsDescriptionDict[type],
value: currentValue,
percentageChange: percentageChange,
};
}
}