Skip to content

Commit

Permalink
fix: better violation names on statistics page (BAL-3294) (#2932)
Browse files Browse the repository at this point in the history
* feat: better violation names on statistics page

* fix: CodeRabbit comments

---------

Co-authored-by: Alon Peretz <[email protected]>
  • Loading branch information
r4zendev and alonp99 authored Jan 5, 2025
1 parent 995102f commit 6f0a6af
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ export const MetricsResponseSchema = z.object({
high: z.number(),
critical: z.number(),
}),
violationCounts: z.record(z.string(), z.number()),
violationCounts: z.array(
z.object({
name: z.string(),
id: z.string(),
count: z.number(),
}),
),
});

export const fetchBusinessReportMetrics = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ export const PortfolioRiskStatistics: FunctionComponent<z.infer<typeof MetricsRe
</TableRow>
</TableHeader>
<TableBody ref={parent}>
{filteredRiskIndicators.map(({ name, count }, index) => (
{filteredRiskIndicators.map(({ name, count, id }, index) => (
<TableRow key={name} className={'border-b-0 hover:bg-[unset]'}>
<TableCell className={ctw('pb-0 ps-0', index !== 0 && 'pt-2')}>
<Link
to={`/${locale}/merchant-monitoring?findings[0]=${name}`}
to={`/${locale}/merchant-monitoring?findings[0]=${id}`}
className={`block h-full cursor-pointer rounded bg-blue-200 p-1 transition-all`}
style={{ width: `${widths[index]}%` }}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ export const usePortfolioRiskStatisticsLogic = ({
},
[],
);
const totalRiskIndicators = Object.values(violationCounts).reduce((acc, curr) => acc + curr, 0);
const totalRiskIndicators = violationCounts.reduce((acc, { count }) => acc + count, 0);
const filteredRiskIndicators = useMemo(
() =>
Object.entries(violationCounts)
.map(([name, count]) => ({ name, count }))
violationCounts
.sort((a, b) => (riskIndicatorsSorting === 'asc' ? a.count - b.count : b.count - a.count))
.slice(0, 5),
[violationCounts, riskIndicatorsSorting],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsNumber, IsObject, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
import { IsArray, IsNumber, IsString, ValidateNested } from 'class-validator';

import { ApiProperty } from '@nestjs/swagger';

Expand Down Expand Up @@ -47,12 +47,26 @@ export class BusinessReportMetricsDto {
riskLevelCounts!: RiskLevelCountsDto;

@ApiProperty({
description: 'Counts of violations by type',
example: { PROHIBITED_CONTENT: 2, MISSING_INFORMATION: 1 },
type: 'object',
additionalProperties: { type: 'number' },
description: 'Detected violations counts',
example: [{ id: 'PROHIBITED_CONTENT', name: 'Prohibited content', count: 2 }],
type: 'array',
})
@IsObject()
@Type(() => Object)
violationCounts!: Record<string, number>;
@IsArray()
@ValidateNested({ each: true })
@Type(() => ViolationCountDto)
violationCounts!: ViolationCountDto[];
}

export class ViolationCountDto {
@ApiProperty()
@IsString()
id!: string;

@ApiProperty()
@IsString()
name!: string;

@ApiProperty()
@IsNumber()
count!: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ const MetricsResponseSchema = z.object({
high: z.number(),
critical: z.number(),
}),
violationCounts: z.record(z.string(), z.number()),
violationCounts: z.array(
z.object({
name: z.string(),
id: z.string(),
count: z.number(),
}),
),
});

@Injectable()
Expand Down

0 comments on commit 6f0a6af

Please sign in to comment.