Skip to content

Commit 4491dc4

Browse files
committed
Add RPA metrics(in bucket) endpoint for SPs with filters and DC
1 parent 6bad202 commit 4491dc4

File tree

7 files changed

+266
-32
lines changed

7 files changed

+266
-32
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- @param {String} $1:metricType
2+
-- @param {Float} $2:bucketSize
3+
-- @param {DateTime} $3:startDate
4+
-- @param {DateTime} $4:endDate
5+
6+
WITH "metric_data" AS (
7+
select
8+
provider,
9+
value,
10+
DATE_TRUNC('week', "tested_at") AS "week"
11+
FROM "storage_provider_url_finder_metric_value"
12+
JOIN "storage_provider_url_finder_metric"
13+
ON "storage_provider_url_finder_metric_value"."metric_id" = "storage_provider_url_finder_metric"."id"
14+
WHERE "storage_provider_url_finder_metric"."metric_type"::text = $1
15+
AND ($3::date IS NULL OR "tested_at" >= $3)
16+
AND ($4::date IS NULL OR "tested_at" <= $4)
17+
AND value IS NOT NULL
18+
),
19+
"provider_weekly" AS (select "week" AS "week",
20+
"provider" AS "provider",
21+
"total_deal_size" AS "total_deal_size"
22+
from "providers_weekly_acc"
23+
where ($3::date is null or "week" >= $3)
24+
and ($4::date is null or "week" <= $4)),
25+
"bucketed" AS (
26+
SELECT
27+
"metric_data"."week",
28+
"metric_data"."provider",
29+
FLOOR("metric_data"."value" / $2) AS "bucket_index"
30+
FROM "metric_data"
31+
),
32+
"bucketed_with_deal" AS (
33+
SELECT
34+
"bucketed"."week",
35+
"bucketed"."bucket_index",
36+
"provider_weekly"."total_deal_size"
37+
FROM "bucketed"
38+
LEFT JOIN "provider_weekly"
39+
ON "provider_weekly"."week" = "bucketed"."week"
40+
AND "provider_weekly"."provider" = "bucketed"."provider"
41+
)
42+
SELECT
43+
"week" AS "week",
44+
"bucket_index" * $2 AS "valueFromExclusive",
45+
("bucket_index" + 1) * $2 AS "valueToInclusive",
46+
COUNT(*) AS "count",
47+
SUM("total_deal_size")::bigint AS "totalDatacap"
48+
FROM "bucketed_with_deal"
49+
GROUP BY "week", "bucket_index"
50+
ORDER BY "week", "bucket_index";

src/controller/stats/storage-providers/storage-providers-stats.controller.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { CacheTTL } from '@nestjs/cache-manager';
22
import { Controller, Get, Query } from '@nestjs/common';
33
import { ApiOkResponse, ApiOperation } from '@nestjs/swagger';
4+
import { groupBy } from 'lodash';
5+
import { StorageProviderUrlFinderMetricType } from 'prisma/generated/client';
6+
import { getUrlFinderProviderMetricWeeklyAcc } from 'prisma/generated/client/sql';
47
import { DataType } from 'src/controller/allocators/types.allocators';
58
import { FilPlusEditionControllerBase } from 'src/controller/base/filplus-edition-controller-base';
69
import { FilPlusEditionRequest } from 'src/controller/base/types.filplus-edition-controller-base';
710
import { StorageProviderComplianceMetricsRequest } from 'src/controller/storage-providers/types.storage-providers';
811
import { PrismaService } from 'src/db/prisma.service';
912
import {
13+
HistogramTotalDatacap,
1014
HistogramWeek,
15+
HistogramWeekResults,
1116
RetrievabilityWeek,
1217
} from 'src/service/histogram-helper/types.histogram-helper';
1318
import { IpniMisreportingCheckerService } from 'src/service/ipni-misreporting-checker/ipni-misreporting-checker.service';
@@ -22,9 +27,12 @@ import {
2227
StorageProviderComplianceMetrics,
2328
StorageProviderComplianceWeek,
2429
} from 'src/service/storage-provider/types.storage-provider';
25-
import { stringToBool, stringToDate } from 'src/utils/utils';
30+
import { bigIntToNumber, stringToBool, stringToDate } from 'src/utils/utils';
2631
import { GetRetrievabilityWeeklyRequest } from '../allocators/types.allocator-stats';
27-
import { UrlFinderStorageProviderMetricDataRequest } from './types.storage-providers-stats';
32+
import {
33+
UrlFinderStorageProviderMetricBaseRequest,
34+
UrlFinderStorageProviderMetricTypeRequest,
35+
} from './types.storage-providers-stats';
2836

2937
@Controller('stats/acc/providers')
3038
@CacheTTL(1000 * 60 * 30) // 30 minutes
@@ -116,7 +124,7 @@ export class StorageProvidersAccStatsController extends FilPlusEditionController
116124
'Get SP Url Finder retrieval result codes metrics for storage providers',
117125
})
118126
public async getStorageProvidersUrlFinderRetrievalCodesData(
119-
@Query() query: UrlFinderStorageProviderMetricDataRequest,
127+
@Query() query: UrlFinderStorageProviderMetricBaseRequest,
120128
): Promise<StorageProviderMetricHistogramDailyResponse> {
121129
const metrics =
122130
await this.storageProviderUrlFinderService.getUrlFinderSnapshotsForProviders(
@@ -131,4 +139,70 @@ export class StorageProvidersAccStatsController extends FilPlusEditionController
131139

132140
return result;
133141
}
142+
143+
@Get('/rpa/metric/')
144+
@ApiOperation({
145+
summary: 'Get RPA metrics for storage providers',
146+
})
147+
public async getStorageProvidersUrlFinderMetricData(
148+
@Query() query: UrlFinderStorageProviderMetricTypeRequest,
149+
): Promise<HistogramWeek> {
150+
const startDate = query?.startDate
151+
? stringToDate(query?.startDate)
152+
: undefined;
153+
154+
const endDate = query?.endDate ? stringToDate(query?.endDate) : undefined;
155+
let bucketSize = 2000;
156+
157+
switch (query.metricType) {
158+
case StorageProviderUrlFinderMetricType.RPA_RETRIEVABILITY:
159+
bucketSize = 0.05;
160+
break;
161+
case StorageProviderUrlFinderMetricType.TTFB:
162+
bucketSize = 2000;
163+
break;
164+
case StorageProviderUrlFinderMetricType.BANDWIDTH:
165+
bucketSize = 10;
166+
break;
167+
}
168+
169+
const metricWeekData = await this.prismaService.$queryRawTyped(
170+
getUrlFinderProviderMetricWeeklyAcc(
171+
query.metricType,
172+
bucketSize,
173+
startDate,
174+
endDate,
175+
),
176+
);
177+
178+
const rowsByWeek = groupBy(metricWeekData, (r) => r.week.toISOString());
179+
180+
const weekResults: HistogramWeekResults[] = Object.entries(rowsByWeek).map(
181+
([weekIso, weekRows]) => {
182+
const histograms = weekRows.map(
183+
(r) =>
184+
new HistogramTotalDatacap(
185+
r.valueFromExclusive,
186+
r.valueToInclusive,
187+
bigIntToNumber(r.count),
188+
r.totalDatacap,
189+
),
190+
);
191+
192+
const total = weekRows.reduce(
193+
(sum, r) => sum + bigIntToNumber(r.count),
194+
0,
195+
);
196+
197+
return new HistogramWeekResults(new Date(weekIso), total, histograms);
198+
},
199+
);
200+
201+
const totalAcrossAllWeeks = weekResults.reduce(
202+
(sum, w) => sum + w.total,
203+
0,
204+
);
205+
206+
return new HistogramWeek(totalAcrossAllWeeks, weekResults);
207+
}
134208
}

src/controller/stats/storage-providers/types.storage-providers-stats.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiPropertyOptional } from '@nestjs/swagger';
2+
import { StorageProviderUrlFinderMetricType } from 'prisma/generated/client';
23

3-
export class UrlFinderStorageProviderMetricDataRequest {
4+
export class UrlFinderStorageProviderMetricBaseRequest {
45
@ApiPropertyOptional({
56
description: 'Requested start date to fetch historical metrics',
67
format: 'date-time',
@@ -15,3 +16,12 @@ export class UrlFinderStorageProviderMetricDataRequest {
1516
})
1617
endDate?: string;
1718
}
19+
20+
export class UrlFinderStorageProviderMetricTypeRequest extends UrlFinderStorageProviderMetricBaseRequest {
21+
@ApiPropertyOptional({
22+
description: 'Compliance score to filter by',
23+
enum: StorageProviderUrlFinderMetricType,
24+
example: StorageProviderUrlFinderMetricType.RPA_RETRIEVABILITY,
25+
})
26+
metricType: StorageProviderUrlFinderMetricType;
27+
}

src/service/histogram-helper/histogram-helper.service.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Injectable, Logger } from '@nestjs/common';
22
import { groupBy } from 'lodash';
33
import {
4-
Histogram,
5-
HistogramWeekResults,
4+
HistogramTotalDatacap,
65
HistogramWeekFlat,
6+
HistogramWeekResults,
77
} from './types.histogram-helper';
88

99
@Injectable()
@@ -21,7 +21,7 @@ export class HistogramHelperService {
2121

2222
for (const week in histogramsByWeek) {
2323
const weekResults = histogramsByWeek[week].map((r) => {
24-
return new Histogram(
24+
return new HistogramTotalDatacap(
2525
r.valueFromExclusive,
2626
r.valueToInclusive,
2727
r.count,
@@ -94,7 +94,9 @@ export class HistogramHelperService {
9494

9595
if (missingBuckets.length > 0) {
9696
histogramWeek.results.push(
97-
...missingBuckets.map((v) => new Histogram(v - maxMinSpan, v, 0, 0n)),
97+
...missingBuckets.map(
98+
(v) => new HistogramTotalDatacap(v - maxMinSpan, v, 0, 0n),
99+
),
98100
);
99101

100102
histogramWeek.results.sort(

src/service/histogram-helper/types.histogram-helper.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApiProperty } from '@nestjs/swagger';
22

3-
export class Histogram {
3+
export class HistogramBase {
44
@ApiProperty({
55
description: 'Bucket (valueFromExclusive, valueToInclusive> starting value',
66
})
@@ -16,6 +16,18 @@ export class Histogram {
1616
})
1717
count: number;
1818

19+
constructor(
20+
valueFromExclusive: number,
21+
valueToInclusive: number,
22+
count: number,
23+
) {
24+
this.valueFromExclusive = valueFromExclusive;
25+
this.valueToInclusive = valueToInclusive;
26+
this.count = count;
27+
}
28+
}
29+
30+
export class HistogramTotalDatacap extends HistogramBase {
1931
@ApiProperty({
2032
description:
2133
'Total datacap of allocators / storage providers in the bucket',
@@ -31,9 +43,7 @@ export class Histogram {
3143
count: number,
3244
totalDatacap: bigint,
3345
) {
34-
this.valueFromExclusive = valueFromExclusive;
35-
this.valueToInclusive = valueToInclusive;
36-
this.count = count;
46+
super(valueFromExclusive, valueToInclusive, count);
3747
this.totalDatacap = totalDatacap;
3848
}
3949
}
@@ -52,17 +62,17 @@ export class HistogramWeekResults {
5262
})
5363
total: number;
5464

55-
@ApiProperty({ type: Histogram, isArray: true })
56-
results: Histogram[];
65+
@ApiProperty({ type: HistogramTotalDatacap, isArray: true })
66+
results: HistogramTotalDatacap[];
5767

58-
constructor(week: Date, total: number, results: Histogram[]) {
68+
constructor(week: Date, total: number, results: HistogramTotalDatacap[]) {
5969
this.week = week;
6070
this.total = total;
6171
this.results = results;
6272
}
6373
}
6474

65-
export class HistogramWeekFlat extends Histogram {
75+
export class HistogramWeekFlat extends HistogramTotalDatacap {
6676
week: Date;
6777

6878
constructor(
@@ -91,7 +101,7 @@ export class RetrievabilityHistogramWeek extends HistogramWeekResults {
91101
constructor(
92102
week: Date,
93103
total: number,
94-
results: Histogram[],
104+
results: HistogramTotalDatacap[],
95105
averageHttpSuccessRate: number,
96106
averageUrlFinderSuccessRate: number,
97107
) {

0 commit comments

Comments
 (0)