Update prices

From atanasster/electionsbg · View interactive version

Update prices is an AI agent skill from the GitHub repository atanasster/electionsbg. Refresh the КЗП "Колко струва" retail-price data (Postgres, migration 048) — fetch the daily euro-adoption open-data ZIP from kolkostruva.bg, load ~1.4M store rows for the 101-product consumer basket into Postgres as an SCD-2 delta, then rebuild the ~118k-product canonical catalogue and the price_pa

How to use this skill

Skill file

Update Prices skill

<!-- pipeline-pointer:start (generated from the registry; edit scripts/pipeline/registry) -->

Pipeline: handler CODE0 runs this unattended — pnpm run pipeline:ingest --only update-prices. The registry holds its commands, gates and publish set; this runbook covers what a script cannot do (operator steps, judgement calls, troubleshooting). When a step fails, start from the repair packet the run writes, not from the procedure below.

<!-- pipeline-pointer:end -->

Refreshes the retail-price layer — behind the "Цени / Prices" section on the Governance + Consumption dashboards, the My-Area price tile, the /consumption/products browser (~118k products), the /product/:slug pages, and the "did the euro raise prices?" verdict. Postgres-only since migration 048 (price_facts / price_current / price_grid_days / price_products / price_payloads); there is no data/prices/*.json serving tree.

Source: kolkostruva.bg — the КЗП (Consumer Protection Commission) price-monitoring portal mandated by ЗВЕРБ чл. 55б for the euro changeover. One ZIP per day at https://kolkostruva.bg/opendata_files/YYYY-MM-DD.zip (free, no registration), one CSV per retail chain. Day D's file appears D+1 ~00:01. Prices are in EUR since 2026-01-01. Granularity: ~245 settlements (EKATTE-keyed), ~208 chains, ~2,650 stores, 101 product groups in 14 categories, plus ~95k distinct product names per day. See the [[project_prices_pg_migration]] memory.

Not official CPI. What we publish is a monitoring basket index (unweighted Jevons of the median-of-per-settlement price). Keep that framing in any copy; it sits beside — never replaces — the macro CPI/HICP tile.

When to run

| Trigger | Action |

|---|---|

| Daily watcher reports kzp_prices: new daily price archive: <date> | pnpm run prices (fetches every advertised day not yet cached, parses, rebuilds) |

| User asks "refresh prices" / "update цени" / "обнови цените" | pnpm run prices |

| One-off historical replay back to euro-adoption day | pnpm run prices --backfill --from 2026-01-02 (operator step; loads all ~189 days into Postgres oldest-first, ~2h, never in the watcher/CI) |

| Fresh clone with an empty price_facts table | pnpm run prices --backfill --from 2026-01-02 (rebuilds the whole PG store from the retained ZIPs) |

Daily run

pnpm run prices               # local Postgres
pnpm run prices:ingest:cloud  # against the Cloud SQL proxy (:5434) — the real serving DB

This:

  1. reads the advertised dates from /opendata, downloads each ZIP not already loaded (tracked by price_grid_days.day) to raw_data/prices/<date>.zip — a gitignored symlink onto the external drive (/Volumes/Storage/data-bg/raw_data/prices), which is the ONLY copy of any day kolkostruva.bg no longer advertises (~14 days). With the drive unmounted the ingest throws rather than skipping old days as "not published yet"; a fresh machine needs the symlink recreated (ln -s /Volumes/Storage/data-bg/raw_data/prices raw_data/prices) before a --backfill,
  2. load_day.ts COPYs every chain CSV into price_stage, upserts the dimension tables, then writes the SCD-2 delta: closes runs whose price moved, opens new runs (~25–40k rows/day, not 1.4M), and rewrites price_current + price_grid_days + price_chain_grid_days from the day's own observations (BOM / ,-vs-; / quoting absorbed; EKATTE normalized incl. Sofia -NN → 68134; product codes outside 1–101 skipped),
  3. rebuilds the catalogue + serving blobs:
  1. self-reports its /data/updates row via appendDataChange when it loaded ≥1 day (so the orchestrator must NOT run append-data-change.ts for it — see process-watch-report). The row is keyed on the loaded price day (dedupeKey), not on the wall clock, and the cloud half (prices:ingest:cloud) does not report at all — it is the same script re-run to publish, and the two runs straddle UTC midnight often enough to have published one refresh as two days of activity three times.

Methodology notes (keep stable)

After a successful run

⚠️ Ship the BUNDLE before running it whenever the chain set widens. The retained-price fallback brings back every chain silent for less than STALE_DAYS (measured: 98 → 215 blobs, 114 chains carrying prices 1–30 days old), and only ChainProfileScreen knows how to date them. build_payloads withholds marketMin from any row not observed on the latest day, so a payload-first publish cannot make an older bundle assert that a days-old price is today's cheapest — but it cannot supply the notice either, so those pages stay undated until hosting ships.

Adding the product dictionary (rare)

The 101-product / 14-category dictionary lives in scripts/prices/products.json (set id == CSV Категория). If КЗП revises the basket, re-scrape https://kolkostruva.bg/compare?city=68134 (each product-card <h3> is a category; each <a ...set=ID> a product) and regenerate.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.