-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* getProviderClients * getProviderBiggestClientDistribution * bigInt returned as int now url format change * removed -week- from histogramDto * aggregation retry * weekly count * cleanup * Provider Retrievability * cleanup, allocators API wireframe * enabled CORS * Allocator biggest client distribution (#8) * Add SQL for getting allocator biggest client distribution * Implement allocator biggest client distribution API * Update src/aggregation/aggregation-tasks.service.ts --------- Co-authored-by: neti-mateusz-mazur <[email protected]> * cleanup * histogram results include same number of buckets * Allocator Retrievability * getAllocatorSpsCompliance * Update src/service/allocator/allocator.service.ts Co-authored-by: Kacper Żuk (Neti) <[email protected]> * renamed histogram property * getAllocatorRetrievability average taken from last week * getProviderRetrievability success average taken from last week * getProviderRetrievability switched to providers_weekly * switched Retrievability to only take full week for average calculation * switched dates to luxon --------- Co-authored-by: Kacper Żuk (Neti) <[email protected]>
- Loading branch information
1 parent
edb7d7b
commit 4ecefa9
Showing
28 changed files
with
934 additions
and
251 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
with allocators_with_ratio as ( | ||
select | ||
week, | ||
allocator, | ||
max(sum_of_allocations) / sum(sum_of_allocations) biggest_to_total_ratio | ||
from client_allocator_distribution_weekly | ||
group by | ||
week, | ||
allocator | ||
) | ||
select | ||
week, | ||
100 * ceil(biggest_to_total_ratio::float8 * 20) / 20 - 5 as "valueFromExclusive", | ||
100 * ceil(biggest_to_total_ratio::float8 * 20) / 20 as "valueToInclusive", | ||
count(*)::int as count | ||
from allocators_with_ratio | ||
group by | ||
week, | ||
"valueFromExclusive", | ||
"valueToInclusive" | ||
order by | ||
week, | ||
"valueFromExclusive"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
select | ||
ceil(avg_weighted_retrievability_success_rate*20)*5 - 5 as "valueFromExclusive", | ||
ceil(avg_weighted_retrievability_success_rate*20)*5 as "valueToInclusive", | ||
count(*)::int as "count", | ||
week | ||
from allocators_weekly | ||
group by 1, 2, week | ||
order by week; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
with providers_with_ratio as (select provider, | ||
max(total_deal_size) / sum(total_deal_size) biggest_to_total_ratio, | ||
week | ||
from client_provider_distribution_weekly | ||
group by provider, week) | ||
select 100 * ceil(biggest_to_total_ratio::float8 * 20) / 20 - 5 as "valueFromExclusive", | ||
100 * ceil(biggest_to_total_ratio::float8 * 20) / 20 as "valueToInclusive", | ||
count(*)::int as "count", | ||
week | ||
from providers_with_ratio | ||
group by "valueFromExclusive", "valueToInclusive", week | ||
order by week, 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
with clients_per_provider as (select count(distinct client) as clients_count, | ||
week | ||
from client_provider_distribution_weekly | ||
group by provider, week) | ||
select (clients_count - 1)::float as "valueFromExclusive", | ||
clients_count::float as "valueToInclusive", | ||
week as "week", | ||
count(*)::int as "count" | ||
from clients_per_provider | ||
group by 1, 2, 3 | ||
order by 3, 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
select 100 * ceil(avg_retrievability_success_rate * 20) / 20 - 5 as "valueFromExclusive", | ||
100 * ceil(avg_retrievability_success_rate * 20) / 20 as "valueToInclusive", | ||
count(*)::int as "count", | ||
week as "week" | ||
from providers_weekly | ||
group by 1, 2, 4 | ||
order by 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
src/controller/stats/allocators/allocators.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AllocatorsController } from './allocators.controller'; | ||
|
||
describe('AllocatorsController', () => { | ||
let controller: AllocatorsController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AllocatorsController], | ||
}).compile(); | ||
|
||
controller = module.get<AllocatorsController>(AllocatorsController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AllocatorService } from '../../../service/allocator/allocator.service'; | ||
|
||
@Controller('stats/allocators') | ||
export class AllocatorsController { | ||
constructor(private readonly allocatorService: AllocatorService) {} | ||
|
||
@Get('retrievability') | ||
async getAllocatorRetrievability() { | ||
return await this.allocatorService.getAllocatorRetrievability(); | ||
} | ||
|
||
@Get('biggest-client-distribution') | ||
async getAllocatorBiggestClientDistribution() { | ||
return await this.allocatorService.getAllocatorBiggestClientDistribution(); | ||
} | ||
|
||
@Get('sps-compliance') | ||
async getAllocatorSpsCompliance() { | ||
return await this.allocatorService.getAllocatorSpsCompliance(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/controller/stats/providers/providers.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ProvidersController } from './providers.controller'; | ||
|
||
describe('ProvidersController', () => { | ||
let controller: ProvidersController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ProvidersController], | ||
}).compile(); | ||
|
||
controller = module.get<ProvidersController>(ProvidersController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { ProviderService } from '../../../service/provider/provider.service'; | ||
|
||
@Controller('stats/providers') | ||
export class ProvidersController { | ||
constructor(private readonly providerService: ProviderService) {} | ||
|
||
@Get('clients') | ||
async getProviderClients() { | ||
return await this.providerService.getProviderClients(); | ||
} | ||
|
||
@Get('biggest-client-distribution') | ||
async getProviderBiggestClientDistribution() { | ||
return await this.providerService.getProviderBiggestClientDistribution(); | ||
} | ||
|
||
@Get('retrievability') | ||
async getProviderRetrievability() { | ||
return await this.providerService.getProviderRetrievability(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { groupBy } from 'lodash'; | ||
import { HistogramWeekDto } from '../types/histogramWeek.dto'; | ||
import { HistogramDto } from '../types/histogram.dto'; | ||
import { HistogramWeekResponseDto } from '../types/histogramWeek.response.dto'; | ||
|
||
@Injectable() | ||
export class HistogramHelper { | ||
async getWeeklyHistogramResult( | ||
results: { | ||
valueFromExclusive: number | null; | ||
valueToInclusive: number | null; | ||
count: number | null; | ||
week: Date; | ||
}[], | ||
totalCount: number, | ||
): Promise<HistogramWeekResponseDto> { | ||
const resultsByWeek = groupBy(results, (p) => p.week); | ||
|
||
const histogramWeekDtos: HistogramWeekDto[] = []; | ||
for (const key in resultsByWeek) { | ||
const value = resultsByWeek[key]; | ||
const weekResponses = value.map((r) => { | ||
return new HistogramDto( | ||
r.valueFromExclusive, | ||
r.valueToInclusive, | ||
r.count, | ||
); | ||
}); | ||
histogramWeekDtos.push( | ||
new HistogramWeekDto( | ||
new Date(key), | ||
weekResponses, | ||
weekResponses.reduce( | ||
(partialSum, response) => partialSum + response.count, | ||
0, | ||
), | ||
), | ||
); | ||
} | ||
|
||
// calculate missing histogram buckets | ||
const { maxMinSpan, allBucketTopValues } = | ||
this.getAllHistogramBucketTopValues(histogramWeekDtos); | ||
|
||
for (const histogramWeekDto of histogramWeekDtos) { | ||
const missingValues = allBucketTopValues.filter( | ||
(topValue) => | ||
!histogramWeekDto.results.some( | ||
(p) => p.valueToInclusive === topValue, | ||
), | ||
); | ||
|
||
if (missingValues.length > 0) { | ||
histogramWeekDto.results.push( | ||
...missingValues.map((v) => new HistogramDto(v - maxMinSpan, v, 0)), | ||
); | ||
|
||
histogramWeekDto.results.sort( | ||
(a, b) => a.valueToInclusive - b.valueToInclusive, | ||
); | ||
} | ||
} | ||
|
||
return new HistogramWeekResponseDto(totalCount, histogramWeekDtos); | ||
} | ||
|
||
private getAllHistogramBucketTopValues( | ||
histogramWeekDtos: HistogramWeekDto[], | ||
) { | ||
const maxRangeTopValue = Math.max( | ||
...histogramWeekDtos.flatMap((p) => | ||
p.results.map((r) => r.valueToInclusive), | ||
), | ||
); | ||
|
||
const maxHistogramEntry = histogramWeekDtos | ||
.flatMap((p) => p.results) | ||
.find((p) => p.valueToInclusive === maxRangeTopValue); | ||
|
||
const maxMinSpan = | ||
maxHistogramEntry.valueToInclusive - maxHistogramEntry.valueFromExclusive; | ||
|
||
const allBucketTopValues: number[] = []; | ||
for (let i = maxRangeTopValue; i > 0; i -= maxMinSpan) { | ||
allBucketTopValues.push(i); | ||
} | ||
return { maxMinSpan, allBucketTopValues }; | ||
} | ||
} |
Oops, something went wrong.