-
Notifications
You must be signed in to change notification settings - Fork 43
feat(anomalies): add automated price-anomaly screen #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mupaky
wants to merge
5
commits into
midt-bg:main
Choose a base branch
from
Mupaky:feat/anomaly-screen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb0a045
feat(anomalies): add automated price-anomaly screen
Mupaky 44224d7
refactor(anomalies): address PR #239 review feedback
Mupaky d4969b5
fix(web): remove accidental BOM and restore trailing newline in app.css
Mupaky 174888a
fix(web): disable remote proxy for Vectorize/AI bindings in local dev
Mupaky 99f35c7
refactor(anomalies): address PR review — score dedup + parity test im…
Mupaky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import type { AnomalySignals } from '@sigma/api-contract'; | ||
| import { anomalyBadges, formatTimes } from './anomaly-badges'; | ||
|
|
||
| const NBSP = '\u00A0'; // non-breaking space, as emitted by @sigma/shared formatters | ||
|
|
||
| const none: AnomalySignals = { | ||
| overEstimateRatio: null, | ||
| estimatedEur: null, | ||
| annexGrowthRatio: null, | ||
| priceRatio: null, | ||
| peerMedianEur: null, | ||
| peerCount: null, | ||
| singleBid: false, | ||
| noNotice: false, | ||
| }; | ||
|
|
||
| describe('formatTimes', () => { | ||
| it('renders one decimal with a Bulgarian comma, dropping a trailing ,0', () => { | ||
| expect(formatTimes(2.53)).toBe('×2,5'); | ||
| expect(formatTimes(12)).toBe('×12'); | ||
| expect(formatTimes(1.1)).toBe('×1,1'); | ||
| }); | ||
|
|
||
| it('renders extreme ratios as whole numbers with the thousands separator', () => { | ||
| expect(formatTimes(104527.7)).toBe(`×104${NBSP}528`); | ||
| }); | ||
| }); | ||
|
|
||
| describe('anomalyBadges', () => { | ||
| it('maps every fired signal in severity order, price signals plain and context soft', () => { | ||
| const badges = anomalyBadges({ | ||
| overEstimateRatio: 2.5, | ||
| estimatedEur: 102258, | ||
| annexGrowthRatio: 1.6, | ||
| priceRatio: 12, | ||
| peerMedianEur: 41666, | ||
| peerCount: 120, | ||
| singleBid: true, | ||
| noNotice: true, | ||
| }); | ||
|
|
||
| expect(badges.map((b) => b.key)).toEqual([ | ||
| 'over_estimate', | ||
| 'annex_growth', | ||
| 'price_outlier', | ||
| 'single_bid', | ||
| 'no_notice', | ||
| ]); | ||
| expect(badges.map((b) => b.context)).toEqual([false, false, false, true, true]); | ||
|
|
||
| expect(badges[0]).toMatchObject({ | ||
| label: '×2,5 над прогнозата', | ||
| detail: `(при 102${NBSP}хил.${NBSP}€)`, | ||
| }); | ||
| expect(badges[1]).toMatchObject({ label: '+60% чрез анекси', detail: null }); | ||
| expect(badges[2]!.label).toBe('×12 над типичното'); | ||
| expect(badges[2]!.detail).toBe(`(медиана 42${NBSP}хил.${NBSP}€ от 120 договора)`); | ||
| expect(badges[3]).toMatchObject({ label: 'единствена оферта', detail: null }); | ||
| expect(badges[4]).toMatchObject({ label: 'без обявление', detail: null }); | ||
| }); | ||
|
|
||
| it('renders nothing for a signal-less row and omits absent evidence details', () => { | ||
| expect(anomalyBadges(none)).toEqual([]); | ||
| const noEvidence = anomalyBadges({ ...none, priceRatio: 7.2, peerMedianEur: null }); | ||
| expect(noEvidence).toHaveLength(1); | ||
| expect(noEvidence[0]).toMatchObject({ label: '×7,2 над типичното', detail: null }); | ||
| }); | ||
|
|
||
| it('rounds the annex growth to whole percents', () => { | ||
| const badges = anomalyBadges({ ...none, annexGrowthRatio: 1.2345 }); | ||
| expect(badges[0]!.label).toBe('+23% чрез анекси'); | ||
| }); | ||
| }); |
This file contains hidden or 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,69 @@ | ||
| // Anomaly signal badges — the pure display mapping from an AnomalyListItem's fired signals to the | ||
| // red-flag chips on /anomalies. Kept out of the route component so the copy/formatting is unit | ||
| // tested. Formatting is hand-rolled like @sigma/shared/format: workerd does not carry the bg-BG | ||
| // Intl data, so no Intl/toLocaleString here. | ||
| import type { AnomalySignals } from '@sigma/api-contract'; | ||
| import type { AnomalySignalKey } from '@sigma/config'; | ||
| import { count, money, signedPct } from '@sigma/shared'; | ||
|
|
||
| export interface AnomalyBadge { | ||
| key: AnomalySignalKey; | ||
| /** Headline chip text, e.g. „×2,5 над прогнозата". */ | ||
| label: string; | ||
| /** Baseline evidence rendered de-emphasised inside the chip, e.g. „(при 102 хил. €)". */ | ||
| detail: string | null; | ||
| /** true → context signal (soft chip variant), never the reason the row exists. */ | ||
| context: boolean; | ||
| } | ||
|
|
||
| /** „×2,5" / „×12" / „×104 528" — one decimal under 100 (trailing „,0" dropped, comma decimal), | ||
| * whole numbers with the thousands NBSP above. */ | ||
| export function formatTimes(ratio: number): string { | ||
| const body = | ||
| ratio >= 100 ? count(Math.round(ratio)) : String(Number(ratio.toFixed(1))).replace('.', ','); | ||
| return `×${body}`; | ||
| } | ||
|
|
||
| /** | ||
| * The chips for one row, in severity order (price signals first, context last). Ratio fields are | ||
| * already flag-gated by the query layer (non-null ⇔ the signal fired), so presence alone decides. | ||
| */ | ||
| export function anomalyBadges(s: AnomalySignals): AnomalyBadge[] { | ||
| const badges: AnomalyBadge[] = []; | ||
| if (s.overEstimateRatio != null) { | ||
| badges.push({ | ||
| key: 'over_estimate', | ||
| label: `${formatTimes(s.overEstimateRatio)} над прогнозата`, | ||
| detail: s.estimatedEur != null ? `(при ${money(s.estimatedEur)})` : null, | ||
| context: false, | ||
| }); | ||
| } | ||
| if (s.annexGrowthRatio != null) { | ||
| badges.push({ | ||
| key: 'annex_growth', | ||
| label: `${signedPct(s.annexGrowthRatio - 1, 0)} чрез анекси`, | ||
| detail: null, | ||
| context: false, | ||
| }); | ||
| } | ||
| if (s.priceRatio != null) { | ||
| badges.push({ | ||
| key: 'price_outlier', | ||
| label: `${formatTimes(s.priceRatio)} над типичното`, | ||
| detail: | ||
| s.peerMedianEur != null | ||
| ? `(медиана ${money(s.peerMedianEur)}${ | ||
| s.peerCount != null ? ` от ${count(s.peerCount)} договора` : '' | ||
| })` | ||
| : null, | ||
| context: false, | ||
| }); | ||
| } | ||
| if (s.singleBid) { | ||
| badges.push({ key: 'single_bid', label: 'единствена оферта', detail: null, context: true }); | ||
| } | ||
| if (s.noNotice) { | ||
| badges.push({ key: 'no_notice', label: 'без обявление', detail: null, context: true }); | ||
| } | ||
| return badges; | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
На този ред е въведен BOM символ (U+FEFF) непосредствено преди
@import 'tailwindcss'(виждан в diff-а като невидим знак между+и@import). Освен това на последния ред на файла е премахнат крайният нов ред (\ No newline at end of file).И двете промени изглеждат непреднамерени и несвързани с функционалността за аномалии. BOM в началото на CSS файл обикновено се толерира от браузърите, но е добре да се премахне, за да се избегнат евентуални проблеми с CSS парсера/Tailwind v4 и за да остане diff-ът фокусиран. Молбата ми е да върнете реда без BOM и с краен нов ред: