Compare commits

..

28 Commits

Author SHA1 Message Date
Rene Fichtmueller
b5925bc264 Merge remote-tracking branch 'origin/erik-live-2026-06-04' into reconcile-2026-06-04
# Conflicts:
#	CHANGELOG_PENDING.md
#	packages/api/src/index.ts
#	packages/api/src/llm/client.ts
#	packages/api/src/routes/blog.ts
#	packages/api/src/routes/bulk-price.ts
#	packages/api/src/routes/kb.ts
#	packages/api/src/routes/price-matrix.ts
#	packages/api/src/routes/procurement.ts
#	packages/api/src/routes/stock.ts
#	packages/api/src/routes/vendor-reliability.ts
#	packages/api/src/routes/vendors.ts
#	packages/dashboard/index.html
2026-06-04 13:56:42 +00:00
Rene Fichtmueller
f2dad45c7c fix(api): part-number ILIKE search + verified-first catalog ordering + FTS-primary product search 2026-06-04 10:14:19 +00:00
Rene Fichtmueller
f81b67860b fix: buildDOM calls in Academy use el() wrapper instead of raw string IDs 2026-05-14 23:06:49 +02:00
Rene Fichtmueller
9b563b0378 fix: remove Selflearning tab from nav 2026-05-14 22:53:12 +02:00
Rene Fichtmueller
eb954aab2e fix: move Academy tab after Standards for visibility 2026-05-14 22:51:54 +02:00
Rene Fichtmueller
91b96a1e03 feat: promote Transceiver Academy to own main nav tab
Move Academy from hidden Standards sub-tab to a dedicated
top-level tab '🎓 Academy' in the main navigation bar.

- Add <div class="tab" data-tab="training"> to nav
- Create standalone <div id="tab-training"> with full Academy HTML
- Wire initTraining() into goToTab() handler
- Remove std-subtab-training skeleton from Standards section
- Remove training button from Standards sub-tab bar
- Update switchStdSubtab() to only handle standards/formfaktoren
2026-05-14 22:38:42 +02:00
Rene Fichtmueller
5a948245ff chore: update CHANGELOG_PENDING with Transceiver Academy entry 2026-05-14 22:32:06 +02:00
Rene Fichtmueller
adfb590ad2 feat: Transceiver Academy — full API-backed customer & employee training platform
Replaces the old LLM-training inline data module with a proper interactive
training platform for annual employee onboarding and customer education.

Content (236 KB of structured training data):
- 5 categories: Standards, Form Factors, Switches & Compatibility,
  Fiber & Infrastructure, Testing & Buying
- 22 detailed lessons with bilingual content (EN + DE)
- 74 quiz questions with explanations in both languages
- Lesson types: beginner / intermediate / advanced
- Content blocks: paragraphs, tables, callouts, code blocks, formulas, lists

API route (GET /api/training/*):
- /categories — all 5 categories with lesson/quiz counts
- /lessons?category= — lesson metadata for category
- /lessons/:id — full lesson content (sections + blocks)
- /quiz?lesson=&category= — quiz questions with bilingual answers
- /stats — aggregate stats
- Public route (no auth token required)

Dashboard (Transceiver Academy UI):
- Language toggle EN/DE (persisted in localStorage)
- Category selector tabs with lesson counts
- Lesson cards with level badge, summary, duration, completion indicator
- Full lesson viewer: renders all block types with bilingual support
- Per-lesson quiz and per-category quiz
- Question-by-question quiz engine with auto-advance, dots progress indicator
- Results screen with grade (A-F), wrong answers + explanations
- Progress tracking in localStorage, global progress bar
- Reset progress button
2026-05-14 22:31:43 +02:00
Rene Fichtmueller
31434ba0f6 chore: Training Module Changelog-Eintrag 2026-05-14 21:52:44 +02:00
Rene Fichtmueller
ae94fc8f47 feat: Training Module im Standards-Tab (Lektionen, Quiz, Lernpfade)
- 3. Subtab "Trainings" im Standards-Bereich mit Fortschrittsbalken
- 13 Lektionen: Form Factors, Glasfaser, IEEE 802.3, WDM, PAM4/NRZ,
  Link Budget (Live-Rechner), Coherent Optics, MSA/DOM, Vendor Locking,
  Temperature Classes, Selection Guide, 400G/800G, Troubleshooting
- 40 Quiz-Fragen mit Erklaerungen, Shuffle, Feedback pro Antwort,
  Falsch-Antworten-Review und Note (A-F)
- 4 Lernpfade: Einsteiger (5), Netzwerk-Engineer (9), Einkaeufer (6),
  Expert (alle 13)
- Lernfortschritt in localStorage (tip_training_progress)
- Live Link-Budget-Rechner eingebettet in Lektion linkbudget
2026-05-14 21:52:23 +02:00
Rene Fichtmueller
e71b985c52 feat: 10 weitere Dashboard-Features (G–P)
G) SKU Bulk Pricer — POST /api/bulk-price (bis 100 Part Numbers),
   Preistabelle je Vendor, CSV-Export, Not-Found-Liste

H) Side-by-side Comparison — Checkboxes in TX-Tabelle,
   Floating Comparison-Tray (max 4 SKUs), Modal mit Specs +
   Best-Price 7d nebeneinander

I) Vendor Reliability Score — GET /api/vendors/reliability,
   Freshness(40) + Frequency(30) + Coverage(30) = 0–100,
   Progress-Bar-Badge auf Vendor-Cards

J) Price Heat Map — GET /api/price-matrix?ids=,
   Row-normalisierte Farbmatrix SKU×Vendor (grün=günstig/rot=teuer),
   Sticky SKU-Spalte, Best-Price-Spalte

K) Watchlist — localStorage-basiert (/☆ in TX-Tabelle),
   Floating Drawer, live Preis-Update via Einzelabruf

L) PDF / Print Report — window.print() + dediziertes @media print CSS
   (blendet UI-Chrome aus, behält Overview-Content)

M) Global Search Overlay — Cmd+K / Ctrl+K, durchsucht Transceivers
   + KB + News + Documents gleichzeitig, clickbare Direktlinks

N) Saved Filter Presets — localStorage tip_presets, Dropdown +
   💾-Button in TX-Filterzeile, Save/Load/Delete

O) Price Forecast — GET /api/price-forecast/:id (lineare Regression
   90d → 30d Forecast), gestricheltes Overlay auf Price-History-Chart,
   Trend-Label (rising/stable/declining)

P) Technology Radar — SVG Bull's-Eye (Adopt/Trial/Assess/Hold),
   Hype-Cycle-Phasen → Ringe gemappt, Bubbles mit Market-Signal-Score,
   Quadrant-Labels, interaktive Tooltips
2026-05-14 21:39:17 +02:00
Rene Fichtmueller
fb060ee40a feat: 6 neue Dashboard-Features (A–F)
A) Price Movers Alert
   - GET /api/procurement/price-movers?days=N&limit=N
   - CTE cur vs prior period avg, |delta_pct| >= 2%, gainers+losers
   - Procurement tab: period toggle 7d/14d/30d, stats bar, Export CSV

B) Executive Overview Pulse
   - 5 KPI cards in Overview (Buy Signals, Arbitrage Ops, Supply Alerts,
     Price Gainers, Losers) via loadProcurementPulse()
   - Top-Movers mini-table in overview card, all → Procurement tab

C) CSV Export
   - exportMoversCSV() downloads gainers+losers as CSV

D) Vendor Intelligence
   - GET /api/vendors/intelligence: per-vendor 30d stats
     (sku_count, price_obs, avg/min/max price, last_seen)
   - Top-6 banner in Vendors tab

E) Advanced Transceiver Search
   - Speed filter (1G/10G/25G/40G/100G/200G/400G/800G)
   - Fiber type filter (SMF / MMF)
   - Verified-only checkbox
   - All params forwarded to GET /api/transceivers

F) Knowledge Base Browser
   - New KB tab with full-text search (ILIKE question/answer/subcategory)
   - GET /api/kb?q=&category=&limit= (packages/api/src/routes/kb.ts)
   - Category pills, entry cards with severity badge + FF/speed tags
2026-05-14 20:54:40 +02:00
Rene Fichtmueller
d7c1c351fe feat(dashboard): interactive price history chart with hover tooltip
Replace 260×60 sparkline with full 520×200 SVG line chart:
- Multi-vendor colored polylines (up to 8, MAGATAMA indigo palette)
- USD-normalized prices (EUR×1.08, GBP×1.27)
- Y/X axes with grid lines, date labels, price labels
- Hover vertical cursor + floating tooltip per-day vendor prices
- Click-to-toggle vendor legend
- 7d / 14d / 30d time range selector with live API re-fetch
- Current best prices table below the chart
- End-point dots per vendor line
2026-05-14 20:26:01 +02:00
Rene Fichtmueller
bcab2b97af feat: procurement — 5 intelligence sections (A-E)
E  Buy-Now Intel    211k precomputed reorder signals surfaced,
                    filterable by form factor, signal strength bars
A  Arbitrage        59k equivalence pairs + price data, FX vs comp
                    normalized to USD, sorted by savings %
B  Switch Compat    search 429 switches → compatible transceivers
                    with prices; 58k compatibility rows
C  Supply Squeeze   4-signal detector: price momentum (30d vs 60d),
                    hype phase, AI cluster demand, stock pressure
D  Dead Stock       7,297 dead-stock SKUs matched against ascending
                    hype phases (revival candidates)

5 new API endpoints: /api/procurement/reorder-top, /arbitrage,
/switch-compat, /supply-squeeze, /dead-stock-revival
2026-05-14 18:31:07 +02:00
Rene Fichtmueller
4bd16af9a5 feat: data quality panel in Crawler Intelligence tab
GET /api/scrapers/data-quality — 4 parallel queries across 200k+
transceiver_verification_evidence rows. Returns: coverage percentages
(price 62%, image 68%, details 94%, competitor 2%), all 10 evidence
types with counts + avg confidence, 17 robot/scraper contributions,
14-day daily activity time series.

Dashboard: coverage progress bars (color-coded thresholds), evidence
type table, SVG activity sparkline, robot contributions table.
2026-05-14 16:22:25 +02:00
Rene Fichtmueller
10d13633fb feat: dynamic hype cycle + market signal engine + eBay/CapEx panels
6-source composite Market Signal Score (0-100) per transceiver technology.
New GET /api/hype-cycle/market-signals blends: Norton-Bass hype_score,
hyperscaler CapEx YoY (MSFT +68.8%, GOOG +107%, META +46.8%), price
observation activity ratio 30d vs prior 30d, AI cluster transceiver demand,
eBay secondary market sell-through velocity, internal fast-mover trend.
All 6 queries run in parallel via Promise.all().

Recommendation engine maps hype phase × capex boom × speed class →
Buy/Hold/Watch labels with tooltips. Dashboard Hype Cycle table now shows
Market Signal ● LIVE column + Recommendation column. Hyperscaler CapEx
panel + eBay panel added to hype tab. Procurement: new eBay Market section.
Sourcing Hype Cycle replaced hardcoded seed with live price observation data.
2026-05-14 16:17:52 +02:00
Rene Fichtmueller
13fe33eceb feat: procurement — Internal Demand + AI Clusters sections with real data
Two new procurement sub-tabs backed by live database tables:

📦 Internal Demand (flexoptix_internal_demand, 8,585 SKUs):
- Velocity cards: fast_mover (70 SKUs, 53k units/12M), regular, slow, dead stock
- Filterable table with demand_12m, demand_3m, trend %, form factor
- GET /api/procurement/internal-demand — summary + paginated rows

🤖 AI Clusters (ai_cluster_announcements, 396 rows last 30d):
- Live datacenter build announcements with estimated transceiver demand
- Stats: total announcements, MW sum, distinct companies, total ~transceivers
- Filter for entries with transceiver estimates; time range selector
- GET /api/procurement/ai-clusters — data + period stats

Also: replaced misleading DEMO DATA banners on Reorder Signals and ABC
Classification sections with informational notes pointing to real data.
2026-05-14 16:04:11 +02:00
Rene Fichtmueller
ea8be4aea3 feat(tip): equivalences explorer + price history charts + linkedin status + MCP tools
Equivalences Explorer:
- GET /api/equivalences — search 63k cross-brand mappings by part number/vendor
- GET /api/equivalences/transceiver/:id — all equivalences for a specific product
- GET /api/equivalences/stats — active count, unique products, avg confidence (93.9%)
- GET /api/equivalences/top-vendors — top 20 competitor vendors by coverage
- New "Equivalences" tab in dashboard with part-number search, vendor filter,
  quick-click vendor chips, and results table with confidence coloring
- Transceiver detail modal: equivalences panel (Flexoptix alternatives or competitor
  products), clickable rows, confidence percentage, orange highlight for FX products

Price History Charts:
- GET /api/price-history/:id?days=90 — daily min/max/avg per source vendor (392k obs)
- Transceiver detail modal: SVG sparkline chart per vendor, legend with latest prices,
  range summary — loads async without blocking the modal

LinkedIn Distribution Status:
- GET /api/blog/linkedin/history — from blog_linkedin_distribution table
- Blog tab: LinkedIn status panel showing DRY_RUN badge, posted/dry_run/skipped/failed
  stats, distribution history table with URN link to live posts

MCP Server — 2 new tools:
- find_equivalences: search 63k+ verified cross-brand mappings with confidence filter
- get_price_history: 392k+ observations, daily series, per-vendor analysis, cheapest source
2026-05-14 15:54:01 +02:00
Rene Fichtmueller
67310c8fe7 fix(blog): SPA-aware URL blog generation + dynamic generated_by
- fetchUrlContent() now extracts OG/meta tags (og:title, og:description,
  name="description", og:site_name) as fallback content for JS-rendered SPAs
- Returns spaDetected=true when body text < 300 chars after stripping scripts
- from-url endpoint skips gatherBlogData() product injection when SPA detected,
  preventing fo-blog-v10 from defaulting to optical networking domain
- additionalContext now includes SPA warning instructing LLM not to default
  to optical transceiver topics unless the page is actually about that
- generated_by in pipeline UPDATE query now uses active model name instead of
  hardcoded 'fo-blog-engine-v7' (reads getLlmProvider().ollamaModel)
- Dashboard shows SPA warning toast when spa_detected=true in response
- Response now includes spa_detected field for client awareness
2026-05-14 12:29:17 +02:00
Rene Fichtmueller
e0f9656684 feat: Blog Engine — generate from URL (link → BlogLLM → article)
New POST /api/blog/from-url endpoint:
- Accepts url + topic in request body
- Fetches page server-side (no CORS, 20s timeout, redirect-follow)
- Strips script/style/nav/footer/svg; extracts readable text (~5000 chars)
- Extracts page title from <title> or <h1>
- Passes extracted content as structured additional_context to the
  existing 16-step FO blog pipeline (same as manual generation)
- Returns immediately; LLM pipeline runs async
- Validated: smoke test fetched flexoptix.net/en/blog/, 5040 chars,
  pipeline launched with llm_enhancing=true

New "🔗 Blog aus URL generieren" panel in dashboard:
- URL input (Enter key triggers generation)
- Blog-Typ dropdown (same 8 types as manual panel)
- Button shows loading state " Fetching…" during API call
- Status line shows extracted char count after success
- Reuses pollBlogLlm() for step-by-step progress polling
- Inline status field for error display without toast spam
2026-05-14 00:55:35 +02:00
Rene Fichtmueller
9b8b03e783 feat: Flexoptix section — speed formatting + Lagerbestand display
Speed display: fix raw Gbps decimals → formatted labels
- 1600.00G → 1.6T (≥1000 Gbps converted to T)
- 400G → 400G (clean integer, no trailing .00)
- Helper function fmtSpeed() added in dashboard JS

Lagerbestand: add stock availability per transceiver
- getFlexoptixSuggestions() extended with LEFT JOIN LATERAL on
  stock_observations (latest row per transceiver)
- Returns warehouse_de_qty, warehouse_global_qty, backorder_qty,
  backorder_estimated_date
- Dashboard renders color-coded badges per row:
    green  = DE-Lager quantity
    blue   = Global-Lager quantity
    yellow = Zulauf with estimated delivery date if available
- Badges hidden when all quantities are null/zero (graceful fallback)
2026-05-14 00:52:21 +02:00
Rene Fichtmueller
de179c4c7c fix: remove DEMO labels from real stock data; fix switch Flexoptix suggestions; enrich Hot Topics LLM context
Stock dashboard (index.html):
- Replace all [DEMO]/demo badges on warehouse data with "FS.com" source labels
  (data was always real scraper data, never demo in the DB)
- Update subtitle: "Scraper-Lagermengen: DEMO DATA" → "Wettbewerber-Marktdaten"
- "Recently Restocked" badge: DEMO DATA → SCRAPER DATA

Switch detail (queries.ts):
- Fix getFlexoptixSuggestions: wavelength_nm → wavelength_tx_nm,
  price_verified_usd → street_price_usd (column mismatch with live schema)
- DS5000 and other OSFP switches now show all 62 Flexoptix OSFP transceivers
  with direct shop links in the detail modal

Hot Topics (hot-topics.ts):
- NOG Talks + News Article clusters now fetch summary/mentioned_vendors/
  mentioned_products/mentioned_standards from news_articles table
- description field builds bullet-point list per article with summaries,
  key vendors/standards (vs. 3 bare titles joined with "|" before)
- buildTopicBriefing() rewritten as structured LLM document with sections:
  Market Signals (bullets), Recommended Angle, Market Context (buy signal,
  technologies, impact horizon), Writing Instructions (600-900 words,
  actionable, opinionated, no generic summaries)
2026-05-14 00:33:45 +02:00
Rene Fichtmueller
0d7a92e749 feat: Abverkauf velocity engine — sql/118 + analyzer + API endpoints
- sql/118-stock-velocity.sql: new stock_velocity (UPSERT per tx×vendor)
  and stock_velocity_events tables with TimescaleDB-compatible indexes
- stock-velocity-analyzer.ts: computes sell-through from stock_observations
  time-series; detects sold/zulauf/data_gap events, trims top-10% outliers,
  predicts stockout date, assigns high/medium/low/insufficient confidence
- scheduler.ts: analyze:stock:velocity job at 04:30/12:30/20:30 UTC
- stock.ts: GET /api/stock/velocity (paginated, filterable by vendor/confidence/
  stockout_days) + GET /api/stock/velocity/:id (per-product with event history)
- First run: 208 products, 979 sell events, 2811 Zulauf events written
2026-05-14 00:24:58 +02:00
Rene Fichtmueller
637839e965 feat: add stock observations to ATGBICS + Optcore; delete demo data
- DELETE 2133 rows from reorder_signals WHERE is_demo_data = true
- atgbics.ts: add upsertStockObservation (confidence=1, binary available
  boolean from Shopify API; quantityAvailable 1/0 for in/out stock)
- optcore.ts: add upsertStockObservation (confidence=1, WooCommerce text
  stock level parsed via parseStockLevel; quantityAvailable 1/0)
- Both scrapers already run every 2h on Erik scheduler
- FS.com: already captures full warehouse breakdown (DE+Global+backorder)
  3x/day from Mac (02:00/10:00/18:00) at confidence=3 — no change needed
- QSFPTEK: already captures real quantities at confidence=2 — no change
- sfpcables/prolabs/wiitek: no meaningful stock signal, not modified
2026-05-14 00:08:57 +02:00
Rene Fichtmueller
db6b97186a feat: OPN+spec equivalence matchers, 400G pricing, TIP_LLM training data
- Add OPN-based equivalence matcher robot (7,245 manufacturer-confirmed matches, confidence=1.0)
- Add spec-based equivalence matcher robot (683 matches, confidence=0.85)
  - Matches by form_factor + speed_gbps + reach_tier + wavelength ±10nm
  - Safety cap: skip FX products matching >30 competitors (too generic)
  - Daily schedule: 04:30 UTC via pg-boss
- SQL migrations 116 (OPN) + 117 (spec) with tip_extract_wavelength_nm() + tip_reach_tier() helpers
- Fix tenGtek.ts: add 3 missing 400G categories (QSFP-DD, QSFP112) — closes pricing gap
- Generate tip-llm-pricing-v1.jsonl: 80 DB-grounded QA pairs (pricing, equivalences, 400G)
- Rebuild TIP_LLM training pool: 11,999 pairs (+127 vs prev), deployed to Erik
- FX product equivalence coverage: 88.1% (959/1089)
2026-05-13 21:33:19 +02:00
Rene Fichtmueller
2f85571784 feat: Flexoptix full product detail sync (sql/115 + detail-enricher robot)
Pulls complete per-SKU specifications and compatibility data from the
Flexoptix API (specifications=1&compatibilities=1) and writes structured
fields to the transceivers table for datasheet generation.

SQL migration 115:
- Adds fx_specifications JSONB (raw spec blob for datasheet gen)
- Adds fx_compatibilities JSONB (full OEM compatibility matrix)
- Adds compliance_code, laser_type, receiver_type, supported_protocols[]
- Adds extinction_ratio_db, cdr_support, inbuilt_fec, detail_synced_at
- GIN index on fx_compatibilities for vendor/OPN queries

flexoptix-detail-enricher.ts:
- Per-SKU API calls with rate-limiting (600ms/call, 100 SKUs/run)
- Parses all spec labels → structured fields (power, budget, tx/rx dBm,
  modulation, wavelengths, temp range, DOM, laser type, receiver type)
- Strips :Sx variant suffixes before API queries (self-configure SKUs)
- COALESCE writes — never overwrites existing data, only fills gaps
- Tracks detail_synced_at, retries stale entries after 7 days

flexoptix-api-sync.ts:
- Also stores image_url and product_page_url during bulk sync

scheduler.ts:
- Registers enrich:flexoptix-details daily at 03:00 UTC

Results after initial run:
- 791/968 FX products (81.7%) fully enriched
- 26.0 avg compatibility entries per product (OEM vendor + OPN)
- 25.7 avg spec fields per product
- DFB(483), EML(148), FP(72), VCSEL(44) laser type distribution
2026-05-13 18:49:28 +02:00
Rene Fichtmueller
d1bde66e39 feat: deterministic equivalence matcher + full wavelength/connector enrichment
Replace confidence-based matcher with deterministic 6-field exact match:
- form_factor (exact), speed_gbps (±0.1G), fiber_type (exact),
  reach (±10%), wavelength_tx (±5nm), connector_type (exact)
- Complete products → confidence=1.0, never creates pending records
- Incomplete products → enhanced confidence ≥0.85, still auto_approved
- PENDING CREATED: 0 (by design, permanent)

Migrations:
- sql/113: Connector type inference from IEEE lookup + form-factor rules
  (970→479 missing connector for FX products)
- sql/114: Extend IEEE lookup with 400G/800G/1.6T OSFP/QSFP-DD standards,
  wavelength fallback (SMF→1310nm, MMF→850nm), clear pending queue to 0

Enrichment results (before→after):
- FX fully complete: 50 → 555 / 1,089 (+505)
- Total fully complete: ~3,600 → 15,431 / 18,133 (+11,800)
- FX coverage: 54.7% → 55.8% (608/1,089 matched)
- Deterministic matches: 0 → 44,596 (confidence=1.0)
- Wavelength-mismatched records rejected: 521
- Pending queue: 42 → 0 (permanent)

New match stats:
- 55,743 new deterministic auto_approved matches
- 521 legacy wavelength-mismatch records rejected
- Total active: 53,447 auto_approved + 1,987 approved
2026-05-13 17:59:08 +02:00
Rene Fichtmueller
76492c17d5 fix: make wavelength_tx_nm nullable in ieee_wavelength_lookup for Copper/RJ45 entries 2026-05-13 17:38:43 +02:00
25 changed files with 3585 additions and 157 deletions

View File

@ -341,9 +341,9 @@ export async function getFlexoptixSuggestions(switchId: string) {
) )
SELECT t.id, t.slug, t.part_number, t.standard_name, t.form_factor, SELECT t.id, t.slug, t.part_number, t.standard_name, t.form_factor,
t.speed, t.speed_gbps, t.reach_meters, t.reach_label, t.speed, t.speed_gbps, t.reach_meters, t.reach_label,
t.fiber_type, t.wavelength_nm, t.market_status, t.fiber_type, t.wavelength_tx_nm AS wavelength_nm, t.market_status,
t.product_page_url, t.image_url, t.product_page_url, t.image_url,
t.price_verified_eur, t.price_verified_at, t.price_verified_usd, t.price_verified_eur, t.price_verified_at, t.street_price_usd AS price_verified_usd,
v.name AS vendor_name, v.website AS vendor_website, v.name AS vendor_name, v.website AS vendor_website,
COALESCE(t.price_verified_eur, COALESCE(t.price_verified_eur,
(SELECT po.price FROM price_observations po (SELECT po.price FROM price_observations po
@ -352,9 +352,20 @@ export async function getFlexoptixSuggestions(switchId: string) {
CASE WHEN t.price_verified_eur IS NOT NULL THEN 'EUR' CASE WHEN t.price_verified_eur IS NOT NULL THEN 'EUR'
ELSE (SELECT po.currency FROM price_observations po ELSE (SELECT po.currency FROM price_observations po
WHERE po.transceiver_id = t.id ORDER BY po.time DESC LIMIT 1) WHERE po.transceiver_id = t.id ORDER BY po.time DESC LIMIT 1)
END AS latest_currency END AS latest_currency,
so.warehouse_de_qty,
so.warehouse_global_qty,
so.backorder_qty,
so.backorder_estimated_date
FROM transceivers t FROM transceivers t
JOIN vendors v ON t.vendor_id = v.id JOIN vendors v ON t.vendor_id = v.id
LEFT JOIN LATERAL (
SELECT warehouse_de_qty, warehouse_global_qty, backorder_qty, backorder_estimated_date
FROM stock_observations
WHERE transceiver_id = t.id
ORDER BY time DESC
LIMIT 1
) so ON true
WHERE LOWER(v.name) = 'flexoptix' WHERE LOWER(v.name) = 'flexoptix'
AND t.form_factor IN ( AND t.form_factor IN (
SELECT form_factor FROM switch_form_factors WHERE form_factor IS NOT NULL SELECT form_factor FROM switch_form_factors WHERE form_factor IS NOT NULL

View File

@ -172,7 +172,8 @@ hotTopicsRouter.get("/", async (req, res) => {
// ═══ SOURCE 3c: NOG Conference Talks — scraped from NOG agendas ═══ // ═══ SOURCE 3c: NOG Conference Talks — scraped from NOG agendas ═══
const nogTalks = await pool.query(` const nogTalks = await pool.query(`
SELECT title, source, source_url, published_at, relevance_score SELECT title, source, source_url, published_at, relevance_score,
summary, mentioned_vendors, mentioned_products, mentioned_standards
FROM news_articles FROM news_articles
WHERE source LIKE 'NOG Talks:%' WHERE source LIKE 'NOG Talks:%'
AND relevance_score > 0.4 AND relevance_score > 0.4
@ -191,11 +192,17 @@ hotTopicsRouter.get("/", async (req, res) => {
} }
for (const [event, talks] of Object.entries(nogByEvent)) { for (const [event, talks] of Object.entries(nogByEvent)) {
const topTalk = (talks as NogRow[])[0]; const topTalk = (talks as NogRow[])[0];
const talkBullets = (talks as NogRow[]).slice(0, 5).map(t => {
const vendors = Array.isArray(t.mentioned_vendors) ? (t.mentioned_vendors as string[]).slice(0, 3).join(", ") : "";
const products = Array.isArray(t.mentioned_products) ? (t.mentioned_products as string[]).slice(0, 3).join(", ") : "";
const extra = [vendors, products].filter(Boolean).join(" / ");
return `${t.title}${extra ? ` (${extra})` : ""}${t.summary ? `${String(t.summary).slice(0, 120)}` : ""}`;
}).join("\n");
topics.push({ topics.push({
title: talks.length === 1 title: talks.length === 1
? `[${event}] ${topTalk.title}` ? `[${event}] ${topTalk.title}`
: `${event}: ${talks.length} optics-relevant talks`, : `${event}: ${talks.length} optics-relevant talks`,
description: (talks as NogRow[]).map(t => t.title).slice(0, 3).join(" | "), description: talkBullets || (talks as NogRow[]).map(t => t.title).slice(0, 3).join(" | "),
blog_type: "technology_deep_dive", blog_type: "technology_deep_dive",
urgency: "hot", urgency: "hot",
source: event, source: event,
@ -209,7 +216,8 @@ hotTopicsRouter.get("/", async (req, res) => {
// ═══ SOURCE 4: News Articles — Recent Industry News ═══ // ═══ SOURCE 4: News Articles — Recent Industry News ═══
const recentNews = await pool.query(` const recentNews = await pool.query(`
SELECT title, source, source_url, category, published_at, SELECT title, source, source_url, category, published_at,
COALESCE(relevance_score, 5) AS relevance COALESCE(relevance_score, 5) AS relevance,
summary, mentioned_vendors, mentioned_products, mentioned_standards, tags
FROM news_articles FROM news_articles
WHERE source NOT LIKE 'NOG Talks:%' WHERE source NOT LIKE 'NOG Talks:%'
AND published_at > NOW() - INTERVAL '14 days' AND published_at > NOW() - INTERVAL '14 days'
@ -228,14 +236,22 @@ hotTopicsRouter.get("/", async (req, res) => {
for (const [theme, articles] of Object.entries(newsThemes)) { for (const [theme, articles] of Object.entries(newsThemes)) {
if (articles.length >= 1) { if (articles.length >= 1) {
// Build rich description with article summaries, vendors, standards mentioned
const articleBullets = (articles as NewsRow[]).slice(0, 5).map(a => {
const vendors = Array.isArray(a.mentioned_vendors) ? (a.mentioned_vendors as string[]).slice(0, 3).join(", ") : "";
const stds = Array.isArray(a.mentioned_standards) ? (a.mentioned_standards as string[]).slice(0, 2).join(", ") : "";
const meta = [vendors, stds].filter(Boolean).join(" / ");
return `${a.title}${meta ? ` [${meta}]` : ""}${a.summary ? `${String(a.summary).slice(0, 150)}` : ""}`;
}).join("\n");
topics.push({ topics.push({
title: `${theme}: ${articles.length} recent articles`, title: `${theme}: ${articles.length} recent articles`,
description: articles.map(a => a.title).slice(0, 3).join(" | "), description: articleBullets || articles.map(a => a.title).slice(0, 3).join(" | "),
blog_type: "technology_deep_dive", blog_type: "technology_deep_dive",
urgency: "trending", urgency: "trending",
source: articles.map(a => a.source).filter(Boolean).slice(0, 2).join(", ") || "Trade Press", source: articles.map(a => a.source).filter(Boolean).slice(0, 2).join(", ") || "Trade Press",
source_type: "trade_press", source_type: "trade_press",
data_context: { articles: articles.slice(0, 3) }, data_context: { articles: articles.slice(0, 5) },
suggested_angle: `${theme}: What the latest announcements actually mean for network operators`, suggested_angle: `${theme}: What the latest announcements actually mean for network operators`,
date: articles[0]?.published_at ? new Date(articles[0].published_at).toISOString() : undefined, date: articles[0]?.published_at ? new Date(articles[0].published_at).toISOString() : undefined,
}); });
@ -407,22 +423,55 @@ function compactDataContext(data: Record<string, unknown> | undefined): string {
function buildTopicBriefing(topic: HotTopic): string { function buildTopicBriefing(topic: HotTopic): string {
const lines = [ const lines = [
`Topic: ${topic.title}`, `=== BLOG BRIEFING: ${topic.title} ===`,
`Urgency: ${topic.urgency}`, ``,
`Source: ${topic.source_type} / ${topic.source}`, `Urgency: ${topic.urgency.toUpperCase()}`,
`Source category: ${topic.source_type} | Source: ${topic.source}`,
]; ];
if (topic.date) lines.push(`Signal date: ${topic.date}`); if (topic.date) lines.push(`Signal date: ${new Date(topic.date).toLocaleDateString("de-DE", { day: "2-digit", month: "long", year: "numeric" })}`);
if (topic.description) lines.push(`Signal summary: ${topic.description}`);
if (topic.suggested_angle) lines.push(`Recommended angle: ${topic.suggested_angle}`); // Core signal content — article bullets or summary
if (topic.blog_title_created && topic.last_blog_created_at) { if (topic.description) {
lines.push(`Editorial note: A blog with a very similar title already exists from ${topic.last_blog_created_at}. If used anyway, choose a materially different angle.`); lines.push(``, `--- Market Signals ---`);
// Already formatted as bullets for news/nog, or plain summary for market intel
lines.push(topic.description.includes("•") ? topic.description : `Signal summary: ${topic.description}`);
} }
const dataContext = compactDataContext(topic.data_context); // Recommended editorial angle
if (dataContext) lines.push(`Structured supporting data:\n${dataContext}`); if (topic.suggested_angle) {
lines.push(``, `--- Recommended Blog Angle ---`);
lines.push(topic.suggested_angle);
}
// Structured data from data_context (vendors, tech, buy signal, etc.)
const ctx = topic.data_context;
if (ctx) {
const extraLines: string[] = [];
if (ctx.buy_signal && typeof ctx.buy_signal === "string") {
const signalMap: Record<string, string> = { bullish: "BUY signal — demand growing, order soon", bearish: "WAIT signal — pricing softening or supply improving", opportunity: "SHORT-TERM OPPORTUNITY — act now", neutral: "Monitor — no immediate action needed" };
extraLines.push(`Buy signal: ${signalMap[ctx.buy_signal] ?? ctx.buy_signal}`);
}
if (ctx.technologies && String(ctx.technologies).length > 2) extraLines.push(`Key technologies: ${ctx.technologies}`);
if (ctx.impact_months) extraLines.push(`Expected market impact: within ${ctx.impact_months} months`);
if (extraLines.length > 0) {
lines.push(``, `--- Market Context ---`);
lines.push(...extraLines);
}
}
if (topic.blog_title_created && topic.last_blog_created_at) {
lines.push(``, `⚠ Editorial note: A similar blog already exists (created ${new Date(topic.last_blog_created_at).toLocaleDateString("de-DE")}). Choose a materially different angle — different structure, timeframe, or use-case focus.`);
}
lines.push(``, `--- Writing Instructions ---`);
lines.push(`Write a practical optical networking article (600900 words) that a network engineer or procurement manager at an ISP, cloud provider, or enterprise can immediately use. Include:`);
lines.push(`1. What is actually happening in the market (fact-based, no generic intro)`);
lines.push(`2. Specific technical implications (form factors, speeds, reach, protocol implications)`);
lines.push(`3. Procurement/planning consequences — what to order, what to delay, what to watch`);
lines.push(`4. One concrete recommendation or action item`);
lines.push(`Do NOT write generic summaries or restate the title. Be opinionated and specific.`);
lines.push("Editorial instruction: turn this into a practical optical networking article with procurement/engineering consequences, not a generic news summary.");
return lines.join("\n"); return lines.join("\n");
} }

View File

@ -165,6 +165,242 @@ hypeCycleRouter.get("/regional/:tech", (req: Request, res: Response) => {
}); });
}); });
// ── MARKET SIGNAL COMPUTATION ───────────────────────────────────────────────
/** Technology → form-factor/speed mapping for cross-table signal aggregation */
const TECH_SIGNAL_MAP = [
{ label: "10G-SFP+", speedGbps: 10, formFactors: ["SFP+"], speedLabel: "10G" },
{ label: "100G-QSFP28", speedGbps: 100, formFactors: ["QSFP28"], speedLabel: "100G" },
{ label: "400G-QSFP-DD", speedGbps: 400, formFactors: ["QSFP-DD"], speedLabel: "400G" },
{ label: "800G-OSFP", speedGbps: 800, formFactors: ["OSFP"], speedLabel: "800G" },
{ label: "1.6T-OSFP", speedGbps: 1600, formFactors: ["OSFP"], speedLabel: "1.6T" },
{ label: "400G-ZR", speedGbps: 400, formFactors: ["SFP-DD", "CFP2"], speedLabel: "400G" },
] as const;
type PhaseKey =
| "plateau_productivity" | "slope_enlightenment" | "trough_disillusionment"
| "peak_inflated_expectations" | "innovation_trigger";
function buildRecommendation(
phase: PhaseKey,
signalScore: number,
capexYoyAvg: number,
speedGbps: number,
): { label: string; color: string; detail: string } {
const fast = speedGbps >= 400;
const capexBoom = capexYoyAvg > 50;
switch (phase) {
case "plateau_productivity":
if (fast && capexBoom)
return { label: "🚀 Buy — AI Wave", color: "#16a34a", detail: "Commodity pricing + AI infrastructure demand surge. Stock up now." };
if (signalScore >= 85)
return { label: "✅ Hold — Stable", color: "#2563eb", detail: "Mature commodity market, stable long-term demand." };
return { label: "📦 Hold", color: "#64748b", detail: "Commodity market. Price-driven. Order on demand." };
case "slope_enlightenment":
if (capexBoom)
return { label: "🟢 Buy Now", color: "#16a34a", detail: "Growing mainstream adoption + hyperscaler capex boom. Window closing." };
return { label: "🟡 Buy", color: "#ca8a04", detail: "Adoption curve steepening. Monitor pricing before large orders." };
case "trough_disillusionment":
if (signalScore > 50)
return { label: "🔍 Buy Opportunity", color: "#7c3aed", detail: "Hype trough but demand signals emerging. Strategic buying window." };
return { label: "⏳ Watch", color: "#94a3b8", detail: "Wait for demand confirmation before stocking." };
case "peak_inflated_expectations":
if (fast && capexBoom)
return { label: "⚡ Caution / Buy", color: "#f97316", detail: "Hype peak but real hyperscaler demand. Buy selectively, not speculatively." };
return { label: "⚠ Caution", color: "#ef4444", detail: "Peak hype. Verify real end-customer demand before building inventory." };
case "innovation_trigger":
return { label: "👁 Watch", color: "#94a3b8", detail: "Early stage — too early for volume commitments. Monitor for traction." };
default:
return { label: "📊 Monitor", color: "#64748b", detail: "Monitor signals." };
}
}
// GET /api/hype-cycle/market-signals — Multi-source demand intelligence
hypeCycleRouter.get("/market-signals", async (_req: Request, res: Response) => {
try {
const { pool } = await import("../db/client");
// ── Parallel data fetch ──────────────────────────────────────────────────
const [hypeRows, priceRows, capexRows, aiRows, ebayRows, internalRows] = await Promise.all([
// Latest hype cycle per technology
pool.query(`
SELECT DISTINCT ON (technology)
technology, hype_phase, hype_score, asp_current_usd,
r_squared, computed_at, current_share, years_to_next_phase
FROM hype_cycle_analysis
ORDER BY technology, computed_at DESC
`),
// Price observation activity: last 30d vs prior 30d (scraping frequency = demand proxy)
pool.query(`
SELECT
t.speed_gbps,
t.form_factor,
COUNT(*) FILTER (WHERE po.time >= NOW() - INTERVAL '30 days') AS obs_30d,
COUNT(*) FILTER (WHERE po.time >= NOW() - INTERVAL '60 days'
AND po.time < NOW() - INTERVAL '30 days') AS obs_prior_30d
FROM price_observations po
JOIN transceivers t ON t.id = po.transceiver_id
WHERE po.time >= NOW() - INTERVAL '60 days'
GROUP BY t.speed_gbps, t.form_factor
`),
// Hyperscaler capex — most recent per company
pool.query(`
SELECT DISTINCT ON (company)
company, period_label, capex_usd_millions,
dc_capex_est_millions, yoy_growth_pct
FROM hyperscaler_capex
WHERE yoy_growth_pct IS NOT NULL
ORDER BY company, period_end DESC
`),
// AI cluster demand last 90 days
pool.query(`
SELECT
COALESCE(SUM(estimated_transceivers), 0) AS total_tx,
COUNT(*) AS cluster_count,
COUNT(*) FILTER (WHERE estimated_transceivers > 0) AS with_estimates
FROM ai_cluster_announcements
WHERE announced_date >= NOW() - INTERVAL '90 days'
`),
// eBay marketplace velocity
pool.query(`
SELECT DISTINCT ON (form_factor, speed_label)
marketplace, keyword, form_factor, speed_label,
sold_count_30d, active_listings, avg_sold_price
FROM marketplace_velocity
WHERE sold_count_30d IS NOT NULL
ORDER BY form_factor, speed_label, scraped_at DESC
`),
// Internal demand: fast-mover trend
pool.query(`
SELECT
COUNT(*) FILTER (WHERE velocity_class = 'fast_mover' AND demand_trend_pct > 0) AS fast_mover_pos,
COUNT(*) FILTER (WHERE velocity_class = 'fast_mover' AND demand_trend_pct < 0) AS fast_mover_neg,
AVG(demand_trend_pct) FILTER (WHERE velocity_class = 'fast_mover') AS avg_fast_trend,
AVG(demand_trend_pct) FILTER (WHERE velocity_class = 'regular') AS avg_regular_trend
FROM flexoptix_internal_demand
WHERE is_internal = true
`),
]);
// ── Pre-compute global signals ───────────────────────────────────────────
const capexYoyValues = capexRows.rows
.map((r) => parseFloat(r.yoy_growth_pct))
.filter((v) => !isNaN(v));
const capexYoyAvg = capexYoyValues.length
? capexYoyValues.reduce((a, b) => a + b, 0) / capexYoyValues.length
: 0;
const aiData = aiRows.rows[0];
const totalAiTx = parseInt(aiData?.total_tx ?? "0") || 0;
const internalData = internalRows.rows[0];
const avgFastTrend = parseFloat(internalData?.avg_fast_trend ?? "0") || 0;
// Build price activity map: speedGbps+formFactor → ratio (30d/prior30d)
const priceMap = new Map<string, number>();
for (const row of priceRows.rows) {
const obs30 = parseInt(row.obs_30d) || 0;
const obsPrior = parseInt(row.obs_prior_30d) || 1;
const key = `${row.speed_gbps}__${row.form_factor ?? ""}`;
priceMap.set(key, obs30 / obsPrior);
}
// Build eBay map: speedLabel → sold_count_30d
const ebayMap = new Map<string, number>();
for (const row of ebayRows.rows) {
const speed = (row.speed_label ?? "").replace(/\s+/g, "");
ebayMap.set(speed, parseInt(row.sold_count_30d) || 0);
}
// ── Per-technology signals ───────────────────────────────────────────────
const technologies = hypeRows.rows.map((r) => {
const tech = TECH_SIGNAL_MAP.find((t) => t.label === r.technology);
const phase = (r.hype_phase ?? "innovation_trigger") as PhaseKey;
const hypeScore = parseInt(r.hype_score) || 0;
// Price activity ratio: average across matching speed+formFactor combos
let priceRatios: number[] = [];
if (tech) {
for (const ff of tech.formFactors) {
const key = `${tech.speedGbps}__${ff}`;
const ratio = priceMap.get(key);
if (ratio !== undefined) priceRatios.push(ratio);
}
}
const priceActivityRatio = priceRatios.length
? priceRatios.reduce((a, b) => a + b, 0) / priceRatios.length
: 1;
// eBay velocity for this speed
const ebayVelocity = tech ? (ebayMap.get(tech.speedLabel) ?? 0) : 0;
// AI cluster: allocate demand proportionally for high-speed techs
const aiBoostTx = tech && tech.speedGbps >= 400 ? totalAiTx : 0;
// Compute composite score (0100)
let score = hypeScore * 0.3;
const capexBoostPts = capexYoyAvg > 100 ? 18 : capexYoyAvg > 50 ? 12 : capexYoyAvg > 20 ? 5 : 0;
const priceBoostPts = priceActivityRatio > 1.3 ? 10 : priceActivityRatio > 1.0 ? 5 : -3;
const aiBoostPts = aiBoostTx > 100000 ? 14 : aiBoostTx > 50000 ? 9 : aiBoostTx > 10000 ? 4 : 0;
const ebayBoostPts = ebayVelocity > 200 ? 8 : ebayVelocity > 100 ? 5 : ebayVelocity > 50 ? 2 : 0;
const intlBoostPts = avgFastTrend > 10 ? 6 : avgFastTrend > 0 ? 3 : avgFastTrend < -20 ? -5 : 0;
score += capexBoostPts + priceBoostPts + aiBoostPts + ebayBoostPts + intlBoostPts;
const marketSignalScore = Math.max(0, Math.min(100, Math.round(score)));
const rec = buildRecommendation(phase, marketSignalScore, capexYoyAvg, tech?.speedGbps ?? 0);
// Signal drivers list for tooltip
const drivers: string[] = [];
if (capexBoostPts > 0) drivers.push(`Hyperscaler CapEx +${capexYoyAvg.toFixed(0)}% YoY avg`);
if (priceActivityRatio > 1.1) drivers.push(`Price obs +${((priceActivityRatio - 1) * 100).toFixed(0)}% MoM activity`);
if (aiBoostTx > 0) drivers.push(`~${(aiBoostTx / 1000).toFixed(0)}k transceivers in AI cluster builds`);
if (ebayVelocity > 0) drivers.push(`${ebayVelocity} units sold on secondary market (30d)`);
if (intlBoostPts > 0) drivers.push(`Internal fast-movers trending ${avgFastTrend > 0 ? "+" : ""}${avgFastTrend.toFixed(1)}%`);
return {
technology: r.technology,
phase,
hypeScore,
aspCurrentUsd: r.asp_current_usd,
marketSignalScore,
recommendation: rec,
drivers,
speedGbps: tech?.speedGbps,
priceActivityRatio: Math.round(priceActivityRatio * 100) / 100,
ebayVelocity,
computedAt: r.computed_at,
};
});
// ── Global context ───────────────────────────────────────────────────────
const globalContext = {
hyperscalerCapex: capexRows.rows.map((r) => ({
company: r.company,
periodLabel: r.period_label,
capexMillions: parseFloat(r.capex_usd_millions),
dcCapexMillions: parseFloat(r.dc_capex_est_millions),
yoyGrowthPct: parseFloat(r.yoy_growth_pct),
})),
capexYoyAvg: Math.round(capexYoyAvg),
capexBoom: capexYoyAvg > 50,
totalAiClusterTx90d: totalAiTx,
aiClusterCount90d: parseInt(aiData?.cluster_count ?? "0") || 0,
internalFastMoverTrend: Math.round(avgFastTrend * 10) / 10,
};
res.json({ success: true, technologies, globalContext, computed_at: new Date().toISOString() });
} catch (err) {
console.error("Market signals error:", err);
res.status(500).json({ success: false, error: "Failed to compute market signals" });
}
});
// GET /api/hype-cycle/analysis — Bass-fitted results from DB (hype_cycle_analysis table) // GET /api/hype-cycle/analysis — Bass-fitted results from DB (hype_cycle_analysis table)
hypeCycleRouter.get("/analysis", async (_req: Request, res: Response) => { hypeCycleRouter.get("/analysis", async (_req: Request, res: Response) => {
try { try {

View File

@ -238,3 +238,85 @@ scraperRouter.get("/llm-insights", async (_req: Request, res: Response) => {
res.status(503).json({ success: false, error: String(err) }); res.status(503).json({ success: false, error: String(err) });
} }
}); });
// GET /api/scrapers/data-quality — Verification evidence coverage + quality metrics
scraperRouter.get("/data-quality", async (_req: Request, res: Response) => {
try {
const [coverageRows, evidenceTypes, robotActivity, dailyActivity] = await Promise.all([
// Coverage: how many transceivers have each evidence type
pool.query(`
SELECT
COUNT(DISTINCT t.id)::int AS total_transceivers,
COUNT(DISTINCT CASE WHEN e.verification_type = 'price' THEN t.id END)::int AS have_price,
COUNT(DISTINCT CASE WHEN e.verification_type = 'image' THEN t.id END)::int AS have_image,
COUNT(DISTINCT CASE WHEN e.verification_type = 'details' THEN t.id END)::int AS have_details,
COUNT(DISTINCT CASE WHEN e.verification_type = 'competitor_match' THEN t.id END)::int AS have_competitor,
COUNT(DISTINCT CASE WHEN e.verification_type = 'artifact_quarantine' THEN t.id END)::int AS quarantined
FROM transceivers t
LEFT JOIN transceiver_verification_evidence e ON e.transceiver_id = t.id
`),
// Evidence type breakdown
pool.query(`
SELECT
verification_type,
COUNT(*)::int AS cnt,
ROUND(AVG(confidence)::numeric, 3) AS avg_confidence,
COUNT(DISTINCT transceiver_id)::int AS distinct_tx,
COUNT(DISTINCT robot_name) AS robot_count,
MAX(created_at) AS last_seen
FROM transceiver_verification_evidence
GROUP BY verification_type
ORDER BY cnt DESC
`),
// Robot / scraper activity
pool.query(`
SELECT
robot_name,
COUNT(*)::int AS total_evidence,
COUNT(DISTINCT transceiver_id)::int AS transceivers_covered,
COUNT(DISTINCT verification_type) AS types_covered,
MIN(created_at)::date AS first_run,
MAX(created_at)::date AS last_run
FROM transceiver_verification_evidence
GROUP BY robot_name
ORDER BY total_evidence DESC
LIMIT 20
`),
// Daily activity last 14 days
pool.query(`
SELECT
created_at::date AS day,
COUNT(*)::int AS evidence_added,
COUNT(DISTINCT transceiver_id)::int AS transceivers_processed
FROM transceiver_verification_evidence
WHERE created_at >= NOW() - INTERVAL '14 days'
GROUP BY day
ORDER BY day DESC
`),
]);
const cov = coverageRows.rows[0];
const total = cov.total_transceivers || 1;
res.json({
success: true,
coverage: {
total: cov.total_transceivers,
price: cov.have_price,
image: cov.have_image,
details: cov.have_details,
competitor: cov.have_competitor,
quarantined: cov.quarantined,
pricePct: Math.round((cov.have_price / total) * 100),
imagePct: Math.round((cov.have_image / total) * 100),
detailsPct: Math.round((cov.have_details / total) * 100),
competitorPct: Math.round((cov.have_competitor / total) * 100),
},
evidenceTypes: evidenceTypes.rows,
robotActivity: robotActivity.rows,
dailyActivity: dailyActivity.rows,
});
} catch (err) {
res.status(503).json({ success: false, error: String(err) });
}
});

View File

@ -25,6 +25,7 @@ import { registerContentTools } from "./tools/content.js";
import { registerMarketTools } from "./tools/market.js"; import { registerMarketTools } from "./tools/market.js";
import { registerSwitchDocTools } from "./tools/switch-docs.js"; import { registerSwitchDocTools } from "./tools/switch-docs.js";
import { finderTools, handleFinderTool } from "./tools/finder.js"; import { finderTools, handleFinderTool } from "./tools/finder.js";
import { registerEquivalencesTools } from "./tools/equivalences.js";
async function main() { async function main() {
const server = new McpServer({ const server = new McpServer({
@ -350,6 +351,7 @@ async function main() {
await registerContentTools(server); await registerContentTools(server);
await registerMarketTools(server); await registerMarketTools(server);
await registerSwitchDocTools(server); await registerSwitchDocTools(server);
await registerEquivalencesTools(server);
// --- Register finder.ts tools (find_flexoptix_for_switch, get_competitor_alerts) --- // --- Register finder.ts tools (find_flexoptix_for_switch, get_competitor_alerts) ---
for (const [toolName, toolDef] of Object.entries(finderTools)) { for (const [toolName, toolDef] of Object.entries(finderTools)) {
@ -374,7 +376,7 @@ async function main() {
// --- Ollama-compatible LLM tools: market analysis (TIP_LLM) + blog generation (FO_BlogLLM) --- // --- Ollama-compatible LLM tools: market analysis (TIP_LLM) + blog generation (FO_BlogLLM) ---
const OLLAMA_BASE = process.env["OLLAMA_BASE_URL"] ?? "https://ollama.fichtmueller.org"; const OLLAMA_BASE = process.env["OLLAMA_BASE_URL"] ?? "https://ollama.fichtmueller.org";
const TIP_LLM_MODEL = process.env["TIP_LLM_MODEL"] ?? "tip-llm-v1"; const TIP_LLM_MODEL = process.env["TIP_LLM_MODEL"] ?? "tip-llm-v1";
const BLOG_LLM_MODEL = process.env["BLOG_LLM_MODEL"] ?? "fo-blog-v7"; const BLOG_LLM_MODEL = process.env["BLOG_LLM_MODEL"] ?? "fo-blog-v10";
const BLOG_LLM_FALLBACK = process.env["BLOG_LLM_FALLBACK_MODEL"] ?? "qwen2.5:14b"; const BLOG_LLM_FALLBACK = process.env["BLOG_LLM_FALLBACK_MODEL"] ?? "qwen2.5:14b";
server.tool( server.tool(

View File

@ -0,0 +1,217 @@
/**
* Equivalences & price-history tools: find_equivalences, get_price_history
*/
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { pool } from "../db.js";
export async function registerEquivalencesTools(server: McpServer): Promise<void> {
// --- Tool: find_equivalences ---
server.tool(
"find_equivalences",
`Find Flexoptix equivalent transceivers for a competitor product (or vice-versa).
Uses the TIP equivalences database (63k+ verified cross-brand mappings, 93.9% avg confidence).
Example: "What Flexoptix alternative exists for Cisco GLC-LH-SMD?" Returns Flexoptix part numbers, pricing, specs, and confidence.`,
{
part_number: z.string().describe("Competitor or Flexoptix part number, e.g. 'GLC-LH-SMD', 'SFP-10G-LR', 'QSFP-100G-PSM4'"),
vendor: z.string().optional().describe("Vendor filter, e.g. 'Cisco', 'Juniper', 'FS.COM'. Leave empty to search all vendors."),
min_confidence: z.number().min(0).max(1).default(0.8).describe("Minimum confidence threshold (01). Default 0.8."),
max_results: z.number().default(10).describe("Maximum results to return"),
},
async ({ part_number, vendor, min_confidence, max_results }) => {
const conditions = [
"e.status IN ('approved', 'auto_approved')",
`e.confidence >= $1`,
`(cx.part_number ILIKE $2 OR cx.standard_name ILIKE $2 OR fx.part_number ILIKE $2 OR fx.standard_name ILIKE $2)`,
];
const values: unknown[] = [min_confidence, `%${part_number}%`];
let idx = 3;
if (vendor) {
conditions.push(`cv.name ILIKE $${idx}`);
values.push(`%${vendor}%`);
idx++;
}
const result = await pool.query(
`SELECT
e.confidence,
e.match_basis,
e.status,
-- Flexoptix product
fx.part_number AS flexoptix_pn,
fx.standard_name AS flexoptix_std,
fx.form_factor AS flexoptix_form_factor,
fx.speed AS flexoptix_speed,
fx.reach_label AS flexoptix_reach,
fx.fiber_type AS flexoptix_fiber,
fx.market_status AS flexoptix_market_status,
fx.price_verified_eur AS flexoptix_price_eur,
fx.product_page_url AS flexoptix_url,
-- Competitor product
cx.part_number AS competitor_pn,
cx.standard_name AS competitor_std,
cx.form_factor AS competitor_form_factor,
cx.speed AS competitor_speed,
cx.reach_label AS competitor_reach,
cx.price_verified_eur AS competitor_price_eur,
cv.name AS competitor_vendor
FROM transceiver_equivalences e
JOIN transceivers fx ON fx.id = e.flexoptix_id
JOIN transceivers cx ON cx.id = e.competitor_id
JOIN vendors cv ON cv.id = cx.vendor_id
WHERE ${conditions.join(" AND ")}
ORDER BY e.confidence DESC
LIMIT $${idx}`,
[...values, max_results]
);
if (result.rows.length === 0) {
return {
content: [{
type: "text",
text: `No equivalences found for "${part_number}"${vendor ? ` from ${vendor}` : ""} with confidence ≥ ${min_confidence}.\n\nTry a broader search term or lower confidence threshold.`,
}],
};
}
const lines = result.rows.map((r, i) => {
const conf = `${(parseFloat(r.confidence) * 100).toFixed(0)}%`;
const basis = Array.isArray(r.match_basis) ? r.match_basis.join(", ") : r.match_basis;
const fxPrice = r.flexoptix_price_eur ? `${r.flexoptix_price_eur}` : "—";
const compPrice = r.competitor_price_eur ? `${r.competitor_price_eur}` : "—";
return [
`${i + 1}. Competitor: ${r.competitor_vendor} **${r.competitor_pn}** (${r.competitor_std || r.competitor_form_factor}, ${r.competitor_speed}, ${r.competitor_reach || "—"}) @ ${compPrice}`,
` → Flexoptix: **${r.flexoptix_pn}** (${r.flexoptix_std || r.flexoptix_form_factor}, ${r.flexoptix_speed}, ${r.flexoptix_reach || "—"}) @ ${fxPrice} | Status: ${r.flexoptix_market_status || "—"}`,
` Confidence: ${conf} | Match basis: ${basis}`,
r.flexoptix_url ? ` Product page: ${r.flexoptix_url}` : "",
].filter(Boolean).join("\n");
});
return {
content: [{
type: "text",
text: `## Equivalences for "${part_number}"\n\nFound ${result.rows.length} match(es):\n\n${lines.join("\n\n")}`,
}],
};
}
);
// --- Tool: get_price_history ---
server.tool(
"get_price_history",
`Get price history for a transceiver over time (from 392k+ price observations across 60+ competitors).
Returns daily min/max/avg prices per source vendor for charting and trend analysis.
Useful for: price trend analysis, sourcing decisions, identifying cheapest vendor window.`,
{
part_number: z.string().describe("Part number, slug, or standard name, e.g. 'QSFP-40G-SR4', '100GBASE-LR4'"),
days: z.number().default(30).describe("Number of days of history to return (max 365)"),
vendor: z.string().optional().describe("Filter to specific source vendor, e.g. 'FS.COM', 'Mouser'. Leave empty for all."),
},
async ({ part_number, days, vendor }) => {
const daysLimited = Math.min(days, 365);
// Resolve transceiver
const tx = await pool.query(
`SELECT t.id, t.part_number, t.standard_name, t.form_factor, t.speed, v.name as vendor_name
FROM transceivers t LEFT JOIN vendors v ON v.id = t.vendor_id
WHERE t.slug ILIKE $1 OR t.part_number ILIKE $1 OR t.standard_name ILIKE $1
LIMIT 1`,
[`%${part_number}%`]
);
if (tx.rows.length === 0) {
return {
content: [{ type: "text", text: `Transceiver not found: "${part_number}"` }],
};
}
const txRow = tx.rows[0];
const conditions = [
`po.transceiver_id = $1`,
`po.time >= NOW() - INTERVAL '${daysLimited} days'`,
`po.price > 0`,
`po.is_anomalous IS NOT TRUE`,
];
const values: unknown[] = [txRow.id];
let idx = 2;
if (vendor) {
conditions.push(`sv.name ILIKE $${idx}`);
values.push(`%${vendor}%`);
idx++;
}
const series = await pool.query(
`SELECT
DATE_TRUNC('day', po.time) AS day,
sv.name AS source_vendor,
MIN(po.price)::numeric(12,2) AS price_min,
MAX(po.price)::numeric(12,2) AS price_max,
AVG(po.price)::numeric(12,2) AS price_avg,
po.currency,
COUNT(*) AS observations
FROM price_observations po
LEFT JOIN vendors sv ON sv.id = po.source_vendor_id
WHERE ${conditions.join(" AND ")}
GROUP BY DATE_TRUNC('day', po.time), sv.name, po.currency
ORDER BY day ASC, source_vendor`,
values
);
if (series.rows.length === 0) {
return {
content: [{
type: "text",
text: `No price history found for "${txRow.part_number}" in the last ${daysLimited} days.`,
}],
};
}
// Summarize by vendor
const byVendor = new Map<string, { min: number; max: number; latest: number; currency: string; points: number }>();
for (const row of series.rows) {
const key = row.source_vendor || "Unknown";
const cur = byVendor.get(key);
if (!cur) {
byVendor.set(key, { min: parseFloat(row.price_min), max: parseFloat(row.price_max), latest: parseFloat(row.price_avg), currency: row.currency, points: parseInt(row.observations) });
} else {
cur.min = Math.min(cur.min, parseFloat(row.price_min));
cur.max = Math.max(cur.max, parseFloat(row.price_max));
cur.latest = parseFloat(row.price_avg); // last in order = latest
cur.points += parseInt(row.observations);
}
}
const vendorLines = [...byVendor.entries()]
.sort((a, b) => a[1].min - b[1].min)
.map(([v, d]) =>
`- **${v}**: ${d.currency} ${d.min}${d.max} (latest avg: ${d.latest}, ${d.points} observations)`
);
const allMins = [...byVendor.values()].map(d => d.min);
const overallMin = Math.min(...allMins);
const overallMax = Math.max(...[...byVendor.values()].map(d => d.max));
const cheapestVendor = [...byVendor.entries()].sort((a, b) => a[1].min - b[1].min)[0];
return {
content: [{
type: "text",
text: [
`## Price History: ${txRow.part_number} (${txRow.vendor_name || "—"})`,
`**Standard:** ${txRow.standard_name || "—"} | **Form factor:** ${txRow.form_factor} | **Speed:** ${txRow.speed}`,
`**Period:** Last ${daysLimited} days | **Total observations:** ${series.rows.reduce((s, r) => s + parseInt(r.observations), 0)}`,
``,
`### Price Range (all vendors)`,
`- Overall min: **${overallMin}** | max: **${overallMax}**`,
`- Cheapest source: **${cheapestVendor[0]}** @ ${cheapestVendor[1].min}`,
``,
`### By Vendor`,
...vendorLines,
].join("\n"),
}],
};
}
);
}

View File

@ -23,6 +23,7 @@
import { import {
ensureVendor, ensureVendor,
findOrCreateScrapedTransceiver, findOrCreateScrapedTransceiver,
pool,
upsertPriceObservation, upsertPriceObservation,
upsertStockObservation, upsertStockObservation,
} from "../utils/db"; } from "../utils/db";
@ -38,6 +39,7 @@ interface CatalogProduct {
sku: string; sku: string;
title: string; title: string;
url: string | null; url: string | null;
imageUrl: string | null;
price: { price: {
amount: number | null; amount: number | null;
currency: string | null; currency: string | null;
@ -252,6 +254,7 @@ function normalizeProduct(row: JsonRecord, fetchedAt: string): CatalogProduct |
if (!sku || !title) return null; if (!sku || !title) return null;
const url = asString(pick(flat, ["url", "productUrl", "canonicalUrl", "link"])); const url = asString(pick(flat, ["url", "productUrl", "canonicalUrl", "link"]));
const imageUrl = asString(pick(flat, ["image", "imageUrl", "productImage", "thumbnail"]));
const amount = asNumber(pick(flat, ["price", "priceNet", "netPrice", "grossPrice", "amount"])); const amount = asNumber(pick(flat, ["price", "priceNet", "netPrice", "grossPrice", "amount"]));
const currency = asString(pick(flat, ["currency", "priceCurrency", "currencyCode"])) const currency = asString(pick(flat, ["currency", "priceCurrency", "currencyCode"]))
?? (amount === null ? null : process.env["FLEXOPTIX_API_CURRENCY"]?.trim() ?? "EUR"); ?? (amount === null ? null : process.env["FLEXOPTIX_API_CURRENCY"]?.trim() ?? "EUR");
@ -275,6 +278,7 @@ function normalizeProduct(row: JsonRecord, fetchedAt: string): CatalogProduct |
sku, sku,
title, title,
url, url,
imageUrl,
price: { price: {
amount, amount,
currency, currency,
@ -353,6 +357,18 @@ async function importProduct(
category: categoryFor(product), category: categoryFor(product),
}); });
// Write image_url and product_page_url from bulk API response
if (product.imageUrl || product.url) {
await pool.query(`
UPDATE transceivers SET
image_url = COALESCE(NULLIF(image_url, ''), $1),
product_page_url = COALESCE(NULLIF(product_page_url, ''), $2),
updated_at = NOW()
WHERE id = $3
AND ($1 IS NOT NULL OR $2 IS NOT NULL)
`, [product.imageUrl ?? null, product.url ?? null, transceiverId]);
}
let priceWritten = false; let priceWritten = false;
if (product.price.amount !== null && product.price.currency) { if (product.price.amount !== null && product.price.currency) {
priceWritten = await upsertPriceObservation({ priceWritten = await upsertPriceObservation({

View File

@ -0,0 +1,486 @@
/**
* Flexoptix Detail Enricher
*
* Fetches full product specifications and compatibility data from the Flexoptix
* API on a per-SKU basis (specifications=1&compatibilities=1) and writes all
* structured fields back to the transceivers table.
*
* Unlike the bulk catalog sync (specifications=0 to avoid HTTP 503), this robot
* processes products in small batches with rate-limiting so the API stays happy.
*
* Fields written per product:
* fx_specifications raw [{label, value}, ...] blob (for datasheet gen)
* fx_compatibilities full [{sku, compatible_to_vendor, original_part_number}]
* compliance_code "LX SGMII", "SR4", "LR4", etc.
* laser_type "FP", "DFB", "VCSEL", "EML"
* receiver_type "PIN", "APD"
* supported_protocols TEXT[]
* extinction_ratio_db dB
* cdr_support boolean
* inbuilt_fec boolean
* power_consumption_w W (overrides if empty)
* optical_budget_db dB (overrides if empty)
* tx_power_min_dbm dBm
* tx_power_max_dbm dBm
* rx_sensitivity_dbm dBm
* modulation "NRZ", "PAM4", etc.
* wavelength_tx_nm nm (overrides if empty)
* wavelength_rx_nm nm (overrides if empty)
* image_url product image URL
* product_page_url product page URL
* detail_synced_at timestamp of this sync
*
* Scheduling:
* - Runs daily at 03:00 UTC
* - Processes BATCH_SIZE products per run (prioritises unseen, then stale >7d)
* - Rate: 1 API call per 600ms (~1.6 rps, safe for Magento)
*/
import { pool } from "../utils/db";
// ── Constants ──────────────────────────────────────────────────────────────
/** Products per enricher run. Full catalog (~1100 products) in ~11 daily runs. */
const BATCH_SIZE = 100;
/** Milliseconds between per-SKU API calls (Magento rate-limit safety). */
const API_CALL_DELAY_MS = 600;
// ── Types ──────────────────────────────────────────────────────────────────
interface FxApiCompatibility {
sku: string | null;
compatible_to_vendor: string;
original_part_number: string | null;
}
interface FxApiSpec {
label: string;
value: unknown;
}
interface FxApiProduct {
sku: string;
name?: string;
url?: string;
image?: string;
compatibilities?: FxApiCompatibility[];
specifications?: FxApiSpec[];
}
interface ParsedSpecs {
complianceCode: string | null;
laserType: string | null;
receiverType: string | null;
supportedProtocols: string[];
extinctionRatioDb: number | null;
cdrSupport: boolean | null;
inbuiltFec: boolean | null;
powerConsumptionW: number | null;
opticalBudgetDb: number | null;
txPowerMinDbm: number | null;
txPowerMaxDbm: number | null;
rxSensitivityDbm: number | null;
modulation: string | null;
wavelengthTxNm: number | null;
wavelengthRxNm: number | null;
tempRange: string | null;
domSupport: boolean | null;
}
export interface DetailEnricherResult {
processed: number;
updated: number;
notFound: number;
apiErrors: number;
dbErrors: number;
}
// ── Helpers ────────────────────────────────────────────────────────────────
function specValue(specs: FxApiSpec[], label: string): string | null {
const entry = specs.find(s => s.label.toLowerCase() === label.toLowerCase());
if (!entry) return null;
const v = entry.value;
if (Array.isArray(v)) return v.join(", ");
if (typeof v === "string") return v.trim() || null;
if (typeof v === "number" || typeof v === "boolean") return String(v);
return null;
}
function specArray(specs: FxApiSpec[], label: string): string[] {
const entry = specs.find(s => s.label.toLowerCase() === label.toLowerCase());
if (!entry) return [];
if (Array.isArray(entry.value)) return entry.value.filter(v => typeof v === "string") as string[];
const v = entry.value;
if (typeof v === "string" && v.trim()) return [v.trim()];
return [];
}
function parseDbm(text: string | null): { min: number | null; max: number | null } {
if (!text) return { min: null, max: null };
// Format: "-15 dBm / -8 dBm" or "-31 dBm / -8 dBm (overload) @100M"
const numbers = text.match(/-?\d+(?:\.\d+)?\s*dBm/gi) ?? [];
const values = numbers
.map(n => parseFloat(n.replace(/dBm/i, "").trim()))
.filter(n => Number.isFinite(n));
return {
min: values[0] ?? null,
max: values[1] ?? null,
};
}
function parseWavelengthNm(text: string | null): number | null {
if (!text) return null;
const match = text.match(/(\d{3,4})\s*nm/);
return match ? parseInt(match[1], 10) : null;
}
function parsePowerW(text: string | null): number | null {
if (!text) return null;
const match = text.match(/([\d.]+)\s*W/i);
return match ? parseFloat(match[1]) : null;
}
function parseDb(text: string | null): number | null {
if (!text) return null;
const match = text.match(/([\d.]+)\s*dB(?!m)/i);
return match ? parseFloat(match[1]) : null;
}
function parseTempRange(text: string | null, operatingTemp: string | null): "COM" | "IND" | null {
// Parse degree-range strings like "0°C - 70°C" or "-40°C - 85°C"
if (text && /°C/.test(text)) {
const minMatch = text.match(/(-?\d+)\s*°C/);
const minC = minMatch ? parseInt(minMatch[1], 10) : null;
if (minC !== null && minC < -10) return "IND";
return "COM";
}
// Classify from the operating temperature label
const combined = [text, operatingTemp].filter(Boolean).join(" ").toLowerCase();
if (/industrial|ind\b|-40/.test(combined)) return "IND";
if (/commercial|standard|com\b/.test(combined)) return "COM";
return null;
}
function parseDomSupport(text: string | null): boolean | null {
if (!text) return null;
const lower = text.toLowerCase();
if (/not implemented|no|none/.test(lower)) return false;
if (/yes|implemented|supported|digital/.test(lower)) return true;
return null;
}
function parseBoolean(text: string | null): boolean | null {
if (!text) return null;
const lower = text.toLowerCase().trim();
if (["yes", "true", "1", "ja"].includes(lower)) return true;
if (["no", "false", "0", "nein", "none"].includes(lower)) return false;
return null;
}
function parseModulation(text: string | null): string | null {
if (!text) return null;
// Normalize "NRZ @100M - 800M" → "NRZ", "PAM4" → "PAM4"
const match = text.match(/\b(NRZ|PAM4|PAM-4|DP-QPSK|QPSK|16QAM|64QAM|OOK)\b/i);
return match ? match[1].toUpperCase().replace("PAM-4", "PAM4") : text.trim();
}
/**
* Parse the flat specifications array into structured fields.
*/
function parseSpecs(specs: FxApiSpec[]): ParsedSpecs {
const txPowers = parseDbm(specValue(specs, "Transmit min/max per lane"));
const rxPowers = parseDbm(specValue(specs, "Receiver min/max per lane"));
return {
complianceCode: specValue(specs, "Compliance Code"),
laserType: specValue(specs, "Laser"),
receiverType: specValue(specs, "Receiver Type"),
supportedProtocols: specArray(specs, "Supported Protocols"),
extinctionRatioDb: parseDb(specValue(specs, "Extinction Ratio")),
cdrSupport: parseBoolean(specValue(specs, "CDR")),
inbuiltFec: parseBoolean(specValue(specs, "Inbuilt FEC")),
powerConsumptionW: parsePowerW(specValue(specs, "Power Consumption")),
opticalBudgetDb: parseDb(specValue(specs, "Powerbudget (dB)")),
txPowerMinDbm: txPowers.min,
txPowerMaxDbm: txPowers.max,
rxSensitivityDbm: rxPowers.min,
modulation: parseModulation(specValue(specs, "Modulation")),
wavelengthTxNm: parseWavelengthNm(specValue(specs, "Wavelength TX (Typical)")),
wavelengthRxNm: parseWavelengthNm(specValue(specs, "Wavelength RX (Typical)")),
tempRange: parseTempRange(
specValue(specs, "Temperature Range"),
specValue(specs, "Operating Temperature"),
),
domSupport: parseDomSupport(specValue(specs, "Digital Diagnostic Monitoring (DDM)")),
};
}
// ── API client ──────────────────────────────────────────────────────────────
async function authenticate(baseUrl: string, timeoutMs: number): Promise<string> {
const existingToken = process.env["FLEXOPTIX_API_TOKEN"]?.trim();
if (existingToken) return existingToken;
const username = process.env["FLEXOPTIX_API_USERNAME"]?.trim();
const password = process.env["FLEXOPTIX_API_PASSWORD"]?.trim();
if (!username || !password) {
throw new Error("FLEXOPTIX_API_USERNAME + FLEXOPTIX_API_PASSWORD required for detail enricher");
}
const authPath = process.env["FLEXOPTIX_API_AUTH_PATH"]?.trim() ?? "/rest/V1/integration/customer/token";
const url = `${baseUrl}${authPath}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const res = await fetch(url, {
method: "POST",
headers: { "content-type": "application/json", accept: "application/json" },
body: JSON.stringify({ username, password }),
signal: controller.signal,
});
if (!res.ok) throw new Error(`Auth failed: HTTP ${res.status}`);
const token = await res.json();
if (typeof token !== "string") throw new Error("Auth response was not a string token");
return token;
} finally {
clearTimeout(timer);
}
}
/**
* Normalize a FX SKU for the API query.
* Strips variant/self-configure suffixes that exist in TIP DB but not in the API:
* "S.B1312.10.DLI:Sx" "S.B1312.10.DLI" (self-configure parent)
* "M4.T8SL.x" "M4.T8SL" (placeholder variant)
* "P.1696.25.yy.R" kept as-is (real SKU with letter suffix)
*/
function normalizeSku(sku: string): string {
// Strip ":Sx", ":S1", ":AB", etc. (colon-delimited variant suffixes)
const colonSuffix = sku.replace(/:[A-Za-z0-9]+$/, "");
if (colonSuffix !== sku) return colonSuffix;
// Strip trailing ".x" or ".y" (single-letter placeholder segments)
const dotSuffix = sku.replace(/\.[xy]$/i, "");
if (dotSuffix !== sku) return dotSuffix;
return sku;
}
async function fetchProductDetail(
baseUrl: string,
productPath: string,
sku: string,
headers: Record<string, string>,
timeoutMs: number,
): Promise<FxApiProduct | null> {
const apiSku = normalizeSku(sku);
const url = new URL(productPath, baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`);
url.searchParams.set("sku", apiSku);
url.searchParams.set("specifications", "1");
url.searchParams.set("compatibilities", "1");
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const res = await fetch(url.toString(), { headers, signal: controller.signal });
if (!res.ok) return null;
const body = await res.json();
// API returns array for SKU query
const rows = Array.isArray(body) ? body : [body];
const row = rows[0];
if (!row || typeof row !== "object") return null;
return row as FxApiProduct;
} catch {
return null;
} finally {
clearTimeout(timer);
}
}
// ── DB helpers ─────────────────────────────────────────────────────────────
interface FxProduct {
id: string;
part_number: string;
power_consumption_w: number | null;
optical_budget_db: number | null;
wavelength_tx_nm: number | null;
wavelength_rx_nm: number | null;
}
async function fetchBatch(): Promise<FxProduct[]> {
const result = await pool.query<FxProduct>(`
SELECT
t.id,
t.part_number,
t.power_consumption_w,
t.optical_budget_db,
t.wavelength_tx_nm,
t.wavelength_rx_nm
FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id
WHERE UPPER(v.name) LIKE '%FLEXOPTIX%'
-- FX catalog SKUs always contain a dot (e.g. S.1303.10.G, Q2.85850.100.D5)
-- Products without a dot are misidentified non-FX items skip them
AND t.part_number LIKE '%.%'
AND (
t.detail_synced_at IS NULL
OR t.detail_synced_at < NOW() - INTERVAL '7 days'
)
ORDER BY
t.detail_synced_at ASC NULLS FIRST,
t.data_completeness DESC -- process most-complete products first
LIMIT $1
`, [BATCH_SIZE]);
return result.rows;
}
async function writeDetails(
transceiverId: string,
product: FxApiProduct,
parsed: ParsedSpecs,
): Promise<void> {
const compat = Array.isArray(product.compatibilities) ? product.compatibilities : [];
const specs = Array.isArray(product.specifications) ? product.specifications : [];
await pool.query(`
UPDATE transceivers SET
fx_specifications = $1,
fx_compatibilities = $2,
compliance_code = COALESCE(compliance_code, $3),
laser_type = COALESCE(laser_type, $4),
receiver_type = COALESCE(receiver_type, $5),
supported_protocols = COALESCE(supported_protocols, $6),
extinction_ratio_db = COALESCE(extinction_ratio_db, $7),
cdr_support = COALESCE(cdr_support, $8),
inbuilt_fec = COALESCE(inbuilt_fec, $9),
power_consumption_w = COALESCE(power_consumption_w, $10),
optical_budget_db = COALESCE(optical_budget_db, $11),
tx_power_min_dbm = COALESCE(tx_power_min_dbm, $12),
tx_power_max_dbm = COALESCE(tx_power_max_dbm, $13),
rx_sensitivity_dbm = COALESCE(rx_sensitivity_dbm, $14),
modulation = COALESCE(modulation, $15),
wavelength_tx_nm = COALESCE(wavelength_tx_nm, $16),
wavelength_rx_nm = COALESCE(wavelength_rx_nm, $17),
temp_range = COALESCE(NULLIF(temp_range, 'COM'), $18),
dom_support = COALESCE(dom_support, $19),
image_url = COALESCE(NULLIF(image_url, ''), $20),
product_page_url = COALESCE(NULLIF(product_page_url, ''), $21),
detail_synced_at = NOW(),
updated_at = NOW()
WHERE id = $22
`, [
specs.length > 0 ? JSON.stringify(specs) : null, // $1
compat.length > 0 ? JSON.stringify(compat) : null, // $2
parsed.complianceCode, // $3
parsed.laserType, // $4
parsed.receiverType, // $5
parsed.supportedProtocols.length > 0 ? parsed.supportedProtocols : null, // $6
parsed.extinctionRatioDb, // $7
parsed.cdrSupport, // $8
parsed.inbuiltFec, // $9
parsed.powerConsumptionW, // $10
parsed.opticalBudgetDb, // $11
parsed.txPowerMinDbm, // $12
parsed.txPowerMaxDbm, // $13
parsed.rxSensitivityDbm, // $14
parsed.modulation, // $15
parsed.wavelengthTxNm, // $16
parsed.wavelengthRxNm, // $17
parsed.tempRange, // $18
parsed.domSupport, // $19
product.image ?? null, // $20
product.url ?? null, // $21
transceiverId, // $22
]);
}
// ── Main export ─────────────────────────────────────────────────────────────
export async function runFlexoptixDetailEnricher(): Promise<DetailEnricherResult> {
const baseUrl = process.env["FLEXOPTIX_API_BASE_URL"]?.trim();
if (!baseUrl) {
throw new Error("FLEXOPTIX_API_BASE_URL not configured");
}
const productPath = process.env["FLEXOPTIX_API_PRODUCTS_PATH"]?.trim()
?? "/rest/V2/flexoptix/products";
const timeoutMs = parseInt(process.env["FLEXOPTIX_API_TIMEOUT_MS"]?.trim() ?? "30000", 10);
const ts = () => new Date().toISOString();
console.log(`[${ts()}] Flexoptix detail enricher starting (batch=${BATCH_SIZE})`);
const token = await authenticate(baseUrl, timeoutMs);
const headers: Record<string, string> = {
accept: "application/json",
authorization: `Bearer ${token}`,
};
const batch = await fetchBatch();
console.log(`[${ts()}] Batch: ${batch.length} FX products queued for detail sync`);
let updated = 0;
let notFound = 0;
let apiErrors = 0;
let dbErrors = 0;
for (const product of batch) {
// Rate-limit: sleep between calls
await new Promise(resolve => setTimeout(resolve, API_CALL_DELAY_MS));
let apiProduct: FxApiProduct | null = null;
try {
apiProduct = await fetchProductDetail(baseUrl, productPath, product.part_number, headers, timeoutMs);
} catch (err: unknown) {
apiErrors++;
console.warn(
`[${ts()}] detail-enricher API error (${product.part_number}): ` +
`${err instanceof Error ? err.message : String(err)}`,
);
continue;
}
if (!apiProduct) {
// Not found in FX API — still mark synced so we don't retry daily,
// but log it so we can investigate if many products come back empty
notFound++;
await pool.query(
`UPDATE transceivers SET detail_synced_at = NOW() WHERE id = $1`,
[product.id],
).catch(() => null);
continue;
}
const specs = Array.isArray(apiProduct.specifications) ? apiProduct.specifications : [];
const parsed = parseSpecs(specs);
try {
await writeDetails(product.id, apiProduct, parsed);
updated++;
} catch (err: unknown) {
dbErrors++;
console.warn(
`[${ts()}] detail-enricher DB error (${product.part_number}): ` +
`${err instanceof Error ? err.message : String(err)}`,
);
}
}
console.log(
`[${ts()}] Flexoptix detail enricher done: ` +
`${batch.length} queued, ${updated} updated, ${notFound} not-in-api, ` +
`${apiErrors} api-errors, ${dbErrors} db-errors`,
);
return {
processed: batch.length,
updated,
notFound,
apiErrors,
dbErrors,
};
}

View File

@ -0,0 +1,130 @@
/**
* OPN-Based Equivalence Matcher
*
* Uses the manufacturer-provided compatibility matrix (fx_compatibilities)
* to create high-confidence equivalences between Flexoptix products and
* their exact OEM counterparts in competitor catalogs.
*
* "OPN" = OEM Part Number the actual part number the customer buys from
* the original manufacturer (e.g. Cisco QSFP-100G-LR4-S).
*
* Match quality:
* - confidence = 1.0 (manufacturer-confirmed)
* - match_mode = 'opn'
* - status = 'auto_approved' (same as deterministic spec match)
*
* Strategy:
* - Only processes FX products whose fx_compatibilities was updated recently
* (detail_synced_at > last_opn_run OR last_opn_run IS NULL)
* - Skips pairs that already have ANY status (approved, auto_approved, rejected)
* - Case-insensitive part_number match on the competitor side
* - Minimum OPN length = 4 chars (skips empty or trivially short entries)
* - Excludes MSA Standard and Flexoptix self-references
*/
import { pool } from "../utils/db";
export interface OPNMatcherResult {
inserted: number;
fxProductsScanned: number;
candidatePairs: number;
skippedExisting: number;
}
// ── Queries ────────────────────────────────────────────────────────────────
const INSERT_OPN_MATCHES = `
INSERT INTO transceiver_equivalences (
flexoptix_id,
competitor_id,
confidence,
status,
match_basis,
match_notes,
created_at,
updated_at
)
SELECT DISTINCT
fx.id AS flexoptix_id,
comp.id AS competitor_id,
1.0 AS confidence,
'auto_approved' AS status,
ARRAY['opn'] AS match_basis,
'Manufacturer-confirmed: FX compatibility matrix lists ' ||
COALESCE(compat->>'compatible_to_vendor', '?') || ' OPN ' ||
COALESCE(compat->>'original_part_number', '?') AS match_notes,
NOW() AS created_at,
NOW() AS updated_at
FROM transceivers fx
JOIN vendors vfx ON vfx.id = fx.vendor_id AND UPPER(vfx.name) LIKE '%FLEXOPTIX%'
CROSS JOIN LATERAL jsonb_array_elements(fx.fx_compatibilities) AS compat
JOIN transceivers comp
ON UPPER(comp.part_number) = UPPER(compat->>'original_part_number')
JOIN vendors vcomp ON vcomp.id = comp.vendor_id AND vcomp.is_competitor = true
WHERE fx.fx_compatibilities IS NOT NULL
AND compat->>'original_part_number' IS NOT NULL
AND length(trim(compat->>'original_part_number')) >= 4
AND compat->>'compatible_to_vendor' NOT IN ('MSA Standard (Default)', 'Flexoptix')
AND NOT EXISTS (
SELECT 1
FROM transceiver_equivalences e
WHERE e.flexoptix_id = fx.id
AND e.competitor_id = comp.id
)
ON CONFLICT DO NOTHING
`;
const COUNT_FX_WITH_COMPAT = `
SELECT COUNT(*) AS cnt
FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id AND UPPER(v.name) LIKE '%FLEXOPTIX%'
WHERE t.fx_compatibilities IS NOT NULL
`;
const COUNT_CANDIDATE_PAIRS = `
SELECT COUNT(DISTINCT (fx.id, comp.id)) AS cnt
FROM transceivers fx
JOIN vendors vfx ON vfx.id = fx.vendor_id AND UPPER(vfx.name) LIKE '%FLEXOPTIX%'
CROSS JOIN LATERAL jsonb_array_elements(fx.fx_compatibilities) AS compat
JOIN transceivers comp
ON UPPER(comp.part_number) = UPPER(compat->>'original_part_number')
JOIN vendors vcomp ON vcomp.id = comp.vendor_id AND vcomp.is_competitor = true
WHERE fx.fx_compatibilities IS NOT NULL
AND compat->>'original_part_number' IS NOT NULL
AND length(trim(compat->>'original_part_number')) >= 4
AND compat->>'compatible_to_vendor' NOT IN ('MSA Standard (Default)', 'Flexoptix')
`;
// ── Main export ────────────────────────────────────────────────────────────
export async function runOPNMatcher(): Promise<OPNMatcherResult> {
const ts = () => new Date().toISOString();
console.log(`[${ts()}] OPN Matcher starting`);
// Count FX products with compatibility data
const fxRes = await pool.query<{ cnt: string }>(COUNT_FX_WITH_COMPAT);
const fxProductsScanned = parseInt(fxRes.rows[0].cnt, 10);
// Count candidate pairs (informational)
const candRes = await pool.query<{ cnt: string }>(COUNT_CANDIDATE_PAIRS);
const candidatePairs = parseInt(candRes.rows[0].cnt, 10);
console.log(`[${ts()}] OPN Matcher: ${fxProductsScanned} FX products, ${candidatePairs} candidate pairs`);
// Insert new OPN-based equivalences
const insertRes = await pool.query(INSERT_OPN_MATCHES);
const inserted = insertRes.rowCount ?? 0;
const skippedExisting = candidatePairs - inserted;
console.log(
`[${ts()}] OPN Matcher done: ${inserted} new equivalences inserted ` +
`(${skippedExisting} pairs already existed)`,
);
return {
inserted,
fxProductsScanned,
candidatePairs,
skippedExisting,
};
}

View File

@ -0,0 +1,169 @@
/**
* Spec-Based Equivalence Matcher
*
* Matches FX products with competitor products by technical specification
* when no OPN-based equivalence exists. Spec-matching is a fallback:
* OPN-confirmed matches (confidence=1.0) always take priority.
*
* Match criteria:
* - Same form_factor (exact)
* - Same speed_gbps (exact)
* - Same reach tier (SR/IR/LR/ER/ZR)
* - Same primary wavelength within ±10nm (CWDM/WDM safe)
* OR both have no wavelength data (broadband products)
* - Max 30 competitor matches per FX product (safety cap)
*
* Match quality:
* confidence = 0.85
* match_basis = '{spec}'
* status = 'auto_approved'
*/
import { pool } from "../utils/db";
export interface SpecMatcherResult {
inserted: number;
fxProductsScanned: number;
candidatePairs: number;
skippedExisting: number;
}
// ── Queries ──────────────────────────────────────────────────────────────────
const INSERT_SPEC_MATCHES = `
INSERT INTO transceiver_equivalences (
flexoptix_id,
competitor_id,
confidence,
status,
match_basis,
match_notes,
created_at,
updated_at
)
SELECT DISTINCT
fx.id AS flexoptix_id,
comp.id AS competitor_id,
0.85 AS confidence,
'auto_approved' AS status,
ARRAY['spec'] AS match_basis,
'Spec match: ' || fx.form_factor || ' ' || fx.speed_gbps || 'G ' ||
CASE WHEN fx.reach_meters <= 300 THEN 'SR'
WHEN fx.reach_meters <= 2000 THEN 'IR'
WHEN fx.reach_meters <= 10000 THEN 'LR'
WHEN fx.reach_meters <= 40000 THEN 'ER'
ELSE 'ZR' END ||
CASE WHEN tip_extract_wavelength_nm(fx.wavelengths) IS NOT NULL
THEN ' @' || tip_extract_wavelength_nm(fx.wavelengths) || 'nm'
ELSE '' END AS match_notes,
NOW() AS created_at,
NOW() AS updated_at
FROM transceivers fx
JOIN vendors vfx ON vfx.id = fx.vendor_id AND UPPER(vfx.name) LIKE '%FLEXOPTIX%'
JOIN transceivers comp
ON comp.form_factor = fx.form_factor
AND comp.speed_gbps = fx.speed_gbps
AND comp.reach_meters >= 10
AND tip_reach_tier(comp.reach_meters) = tip_reach_tier(fx.reach_meters)
AND (
(tip_extract_wavelength_nm(fx.wavelengths) IS NULL
AND tip_extract_wavelength_nm(comp.wavelengths) IS NULL)
OR ABS( COALESCE(tip_extract_wavelength_nm(comp.wavelengths), 0)
- COALESCE(tip_extract_wavelength_nm(fx.wavelengths), 0) ) <= 10
)
JOIN vendors vcomp ON vcomp.id = comp.vendor_id AND vcomp.is_competitor = true
WHERE fx.reach_meters >= 10
AND fx.speed_gbps > 0
-- OPN match already exists skip (spec is fallback only)
AND NOT EXISTS (
SELECT 1 FROM transceiver_equivalences e
WHERE e.flexoptix_id = fx.id AND 'opn' = ANY(e.match_basis)
)
-- Skip pairs that already have ANY equivalence
AND NOT EXISTS (
SELECT 1 FROM transceiver_equivalences e
WHERE e.flexoptix_id = fx.id AND e.competitor_id = comp.id
)
-- Safety cap: skip if > 30 competitors would match (too generic)
AND (
SELECT COUNT(DISTINCT c2.id)
FROM transceivers c2
JOIN vendors vc2 ON vc2.id = c2.vendor_id AND vc2.is_competitor = true
WHERE c2.form_factor = fx.form_factor
AND c2.speed_gbps = fx.speed_gbps
AND c2.reach_meters >= 10
AND tip_reach_tier(c2.reach_meters) = tip_reach_tier(fx.reach_meters)
AND (
(tip_extract_wavelength_nm(fx.wavelengths) IS NULL
AND tip_extract_wavelength_nm(c2.wavelengths) IS NULL)
OR ABS( COALESCE(tip_extract_wavelength_nm(c2.wavelengths), 0)
- COALESCE(tip_extract_wavelength_nm(fx.wavelengths), 0) ) <= 10
)
) <= 30
ON CONFLICT DO NOTHING
`;
const COUNT_FX_WITHOUT_OPN = `
SELECT COUNT(DISTINCT t.id) AS cnt
FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id AND UPPER(v.name) LIKE '%FLEXOPTIX%'
WHERE t.reach_meters >= 10
AND t.speed_gbps > 0
AND NOT EXISTS (
SELECT 1 FROM transceiver_equivalences e
WHERE e.flexoptix_id = t.id AND 'opn' = ANY(e.match_basis)
)
`;
const COUNT_SPEC_CANDIDATES = `
SELECT COUNT(DISTINCT (fx.id, comp.id)) AS cnt
FROM transceivers fx
JOIN vendors vfx ON vfx.id = fx.vendor_id AND UPPER(vfx.name) LIKE '%FLEXOPTIX%'
JOIN transceivers comp
ON comp.form_factor = fx.form_factor
AND comp.speed_gbps = fx.speed_gbps
AND comp.reach_meters >= 10
AND tip_reach_tier(comp.reach_meters) = tip_reach_tier(fx.reach_meters)
AND (
(tip_extract_wavelength_nm(fx.wavelengths) IS NULL
AND tip_extract_wavelength_nm(comp.wavelengths) IS NULL)
OR ABS( COALESCE(tip_extract_wavelength_nm(comp.wavelengths), 0)
- COALESCE(tip_extract_wavelength_nm(fx.wavelengths), 0) ) <= 10
)
JOIN vendors vcomp ON vcomp.id = comp.vendor_id AND vcomp.is_competitor = true
WHERE fx.reach_meters >= 10
AND fx.speed_gbps > 0
AND NOT EXISTS (
SELECT 1 FROM transceiver_equivalences e
WHERE e.flexoptix_id = fx.id AND 'opn' = ANY(e.match_basis)
)
`;
// ── Main export ───────────────────────────────────────────────────────────────
export async function runSpecMatcher(): Promise<SpecMatcherResult> {
const ts = () => new Date().toISOString();
console.log(`[${ts()}] Spec Matcher starting`);
const fxRes = await pool.query<{ cnt: string }>(COUNT_FX_WITHOUT_OPN);
const fxProductsScanned = parseInt(fxRes.rows[0].cnt, 10);
const candRes = await pool.query<{ cnt: string }>(COUNT_SPEC_CANDIDATES);
const candidatePairs = parseInt(candRes.rows[0].cnt, 10);
console.log(
`[${ts()}] Spec Matcher: ${fxProductsScanned} FX products without OPN, ` +
`${candidatePairs} spec candidate pairs`,
);
const insertRes = await pool.query(INSERT_SPEC_MATCHES);
const inserted = insertRes.rowCount ?? 0;
const skippedExisting = candidatePairs - inserted;
console.log(
`[${ts()}] Spec Matcher done: ${inserted} new spec equivalences inserted ` +
`(${skippedExisting} pairs already existed or capped)`,
);
return { inserted, fxProductsScanned, candidatePairs, skippedExisting };
}

View File

@ -0,0 +1,487 @@
/**
* Stock Velocity Analyzer Abverkauf & Zulauf Evaluation
*
* Processes time-series stock_observations to compute:
* avg_daily_sell_rate implied units sold per day
* total_units_sold implied cumulative sold in window
* Zulauf events when and how much stock was replenished
* estimated_stockout_date when current stock is expected to run out
*
* Data sources ranked by confidence:
* 3 = FS.com per-warehouse breakdown + units_sold counter
* 2 = QSFPTEK global real-time quantity
* 1 = ATGBICS/Optcore binary in/out stock only (skipped for velocity)
*
* Velocity is only computed for confidence 2 sources.
*
* Called by pg-boss job "analyze:stock:velocity".
*/
import { pool } from "../utils/db";
// ── Config ────────────────────────────────────────────────────────────────────
const MIN_OBS_FOR_VELOCITY = 2; // absolute minimum observations
const MAX_INTERVAL_HOURS = 96; // ignore gaps > 4 days (data outage, not a sale)
const MIN_INTERVAL_HOURS = 0.4; // ignore observations < 24min apart (duplicates)
const WINDOW_DAYS = 30; // look back this many days for velocity calc
// ── Types ─────────────────────────────────────────────────────────────────────
interface StockObs {
time: Date;
physicalQty: number; // warehouse_de_qty + warehouse_global_qty
quantityAvailable: number | null;
backorderQty: number | null;
backorderEstimatedDate: string | null;
unitsSold: number | null;
priceNet: number | null;
}
interface VelocityEvent {
transceiverId: string;
vendorId: string;
eventAt: Date;
eventType: "sold" | "zulauf" | "unchanged" | "data_gap";
unitsDelta: number;
dailyRate: number | null;
qtyBefore: number;
qtyAfter: number;
hoursElapsed: number;
}
interface VelocityResult {
transceiverId: string;
vendorId: string;
windowStart: Date;
windowEnd: Date;
obsCount: number;
avgDailySellRate: number | null;
peakDailySellRate: number | null;
totalSellEvents: number;
totalUnitsSoldImplied: number;
unitsSoldCounterDelta: number | null;
unitsSoldDailyRate: number | null;
totalZulaufEvents: number;
totalUnitsZulauf: number;
lastZulaufAt: Date | null;
nextExpectedDelivery: string | null;
currentQty: number | null;
currentBackorderQty: number | null;
currentPriceNet: number | null;
estimatedStockoutDays: number | null;
estimatedStockoutDate: Date | null;
velocityConfidence: "high" | "medium" | "low" | "insufficient";
events: VelocityEvent[];
}
// ── Core velocity computation ──────────────────────────────────────────────────
function computeVelocity(
transceiverId: string,
vendorId: string,
observations: StockObs[]
): VelocityResult {
const sorted = [...observations].sort((a, b) => a.time.getTime() - b.time.getTime());
const windowStart = sorted[0].time;
const windowEnd = sorted[sorted.length - 1].time;
const windowDays = Math.max(1, (windowEnd.getTime() - windowStart.getTime()) / 86400000);
const latest = sorted[sorted.length - 1];
const earliest = sorted[0];
// ── FS.com units_sold counter delta (most reliable) ──────────────────────
let unitsSoldCounterDelta: number | null = null;
let unitsSoldDailyRate: number | null = null;
const firstWithSold = sorted.find((o) => o.unitsSold !== null && o.unitsSold > 0);
const lastWithSold = [...sorted].reverse().find((o) => o.unitsSold !== null && o.unitsSold > 0);
if (firstWithSold && lastWithSold && firstWithSold !== lastWithSold) {
const delta = (lastWithSold.unitsSold ?? 0) - (firstWithSold.unitsSold ?? 0);
// Sanity: delta should be positive and not unrealistically large (>10x multiplier = format glitch)
const spanDays = Math.max(1, (lastWithSold.time.getTime() - firstWithSold.time.getTime()) / 86400000);
if (delta > 0 && delta < (firstWithSold.unitsSold ?? 1) * 5) {
unitsSoldCounterDelta = delta;
unitsSoldDailyRate = delta / spanDays;
}
}
// ── Interval-by-interval delta analysis ──────────────────────────────────
const events: VelocityEvent[] = [];
const sellRates: number[] = [];
let totalSellEvents = 0;
let totalUnitsSoldImplied = 0;
let totalZulaufEvents = 0;
let totalUnitsZulauf = 0;
let lastZulaufAt: Date | null = null;
for (let i = 1; i < sorted.length; i++) {
const prev = sorted[i - 1];
const curr = sorted[i];
const hoursElapsed = (curr.time.getTime() - prev.time.getTime()) / 3600000;
// Skip too-close (duplicates) or too-far (outages)
if (hoursElapsed < MIN_INTERVAL_HOURS) continue;
if (hoursElapsed > MAX_INTERVAL_HOURS) {
events.push({
transceiverId, vendorId,
eventAt: curr.time,
eventType: "data_gap",
unitsDelta: 0,
dailyRate: null,
qtyBefore: prev.physicalQty,
qtyAfter: curr.physicalQty,
hoursElapsed,
});
continue;
}
const delta = curr.physicalQty - prev.physicalQty;
const daysFraction = hoursElapsed / 24;
if (delta < 0) {
// Stock decreased → implied sales
const unitsSold = -delta;
const dailyRate = unitsSold / daysFraction;
events.push({
transceiverId, vendorId,
eventAt: curr.time,
eventType: "sold",
unitsDelta: delta, // negative
dailyRate,
qtyBefore: prev.physicalQty,
qtyAfter: curr.physicalQty,
hoursElapsed,
});
sellRates.push(dailyRate);
totalSellEvents++;
totalUnitsSoldImplied += unitsSold;
} else if (delta > 0) {
// Stock increased → Zulauf (replenishment or restock)
events.push({
transceiverId, vendorId,
eventAt: curr.time,
eventType: "zulauf",
unitsDelta: delta, // positive
dailyRate: null,
qtyBefore: prev.physicalQty,
qtyAfter: curr.physicalQty,
hoursElapsed,
});
totalZulaufEvents++;
totalUnitsZulauf += delta;
lastZulaufAt = curr.time;
} else {
events.push({
transceiverId, vendorId,
eventAt: curr.time,
eventType: "unchanged",
unitsDelta: 0,
dailyRate: null,
qtyBefore: prev.physicalQty,
qtyAfter: curr.physicalQty,
hoursElapsed,
});
}
}
// ── Compute average sell rate ─────────────────────────────────────────────
let avgDailySellRate: number | null = null;
let peakDailySellRate: number | null = null;
if (sellRates.length > 0) {
// Use trimmed mean (remove top 10% outliers to avoid one-off bulk events)
const sorted_rates = [...sellRates].sort((a, b) => a - b);
const trimCount = Math.max(0, Math.floor(sorted_rates.length * 0.1));
const trimmed = sorted_rates.slice(0, sorted_rates.length - trimCount);
avgDailySellRate = trimmed.reduce((s, r) => s + r, 0) / trimmed.length;
peakDailySellRate = sorted_rates[sorted_rates.length - 1];
}
// Prefer units_sold daily rate from FS.com counter (more reliable)
const effectiveRate = unitsSoldDailyRate ?? avgDailySellRate;
// ── Current state from latest observation ────────────────────────────────
const currentQty = latest.physicalQty;
const currentBackorderQty = latest.backorderQty;
const currentPriceNet = latest.priceNet;
const nextExpectedDelivery = latest.backorderEstimatedDate;
// ── Stockout prediction ───────────────────────────────────────────────────
let estimatedStockoutDays: number | null = null;
let estimatedStockoutDate: Date | null = null;
if (effectiveRate !== null && effectiveRate > 0 && currentQty !== null && currentQty > 0) {
estimatedStockoutDays = currentQty / effectiveRate;
estimatedStockoutDate = new Date(
latest.time.getTime() + estimatedStockoutDays * 86400000
);
} else if (currentQty === 0) {
estimatedStockoutDays = 0;
estimatedStockoutDate = latest.time;
}
// ── Confidence assessment ─────────────────────────────────────────────────
const meaningfulObs = sorted.length;
let velocityConfidence: "high" | "medium" | "low" | "insufficient";
if (meaningfulObs >= 14 && (totalSellEvents >= 3 || unitsSoldCounterDelta !== null)) {
velocityConfidence = "high";
} else if (meaningfulObs >= 5 && totalSellEvents >= 1) {
velocityConfidence = "medium";
} else if (meaningfulObs >= 2) {
velocityConfidence = "low";
} else {
velocityConfidence = "insufficient";
}
return {
transceiverId,
vendorId,
windowStart,
windowEnd,
obsCount: meaningfulObs,
avgDailySellRate,
peakDailySellRate,
totalSellEvents,
totalUnitsSoldImplied,
unitsSoldCounterDelta,
unitsSoldDailyRate,
totalZulaufEvents,
totalUnitsZulauf,
lastZulaufAt,
nextExpectedDelivery: nextExpectedDelivery ?? null,
currentQty,
currentBackorderQty: currentBackorderQty ?? null,
currentPriceNet,
estimatedStockoutDays,
estimatedStockoutDate,
velocityConfidence,
events,
};
}
// ── Database I/O ──────────────────────────────────────────────────────────────
async function fetchObservations(
vendorId: string
): Promise<Map<string, StockObs[]>> {
const result = await pool.query<{
transceiver_id: string;
time: Date;
warehouse_de_qty: number | null;
warehouse_global_qty: number | null;
quantity_available: number | null;
backorder_qty: number | null;
backorder_estimated_date: string | null;
units_sold: number | null;
price_net: number | null;
}>(
`SELECT
transceiver_id,
time,
warehouse_de_qty,
warehouse_global_qty,
quantity_available,
backorder_qty,
backorder_estimated_date::text,
units_sold,
price_net
FROM stock_observations
WHERE source_vendor_id = $1
AND stock_confidence >= 2
AND time >= NOW() - INTERVAL '${WINDOW_DAYS} days'
ORDER BY transceiver_id, time`,
[vendorId]
);
const byProduct = new Map<string, StockObs[]>();
for (const row of result.rows) {
const obs: StockObs = {
time: row.time,
physicalQty:
(row.warehouse_de_qty ?? 0) + (row.warehouse_global_qty ?? 0) ||
(row.quantity_available ?? 0),
quantityAvailable: row.quantity_available,
backorderQty: row.backorder_qty,
backorderEstimatedDate: row.backorder_estimated_date,
unitsSold: row.units_sold,
priceNet: row.price_net,
};
const list = byProduct.get(row.transceiver_id) ?? [];
list.push(obs);
byProduct.set(row.transceiver_id, list);
}
return byProduct;
}
async function upsertVelocityResult(r: VelocityResult): Promise<void> {
await pool.query(
`INSERT INTO stock_velocity (
transceiver_id, vendor_id, computed_at,
window_start, window_end, obs_count,
avg_daily_sell_rate, peak_daily_sell_rate,
total_sell_events, total_units_sold_implied,
units_sold_counter_delta, units_sold_daily_rate,
total_zulauf_events, total_units_zulauf,
last_zulauf_at, next_expected_delivery,
current_qty, current_backorder_qty, current_price_net,
estimated_stockout_days, estimated_stockout_date,
velocity_confidence
) VALUES (
$1, $2, NOW(),
$3, $4, $5,
$6, $7,
$8, $9,
$10, $11,
$12, $13,
$14, $15,
$16, $17, $18,
$19, $20,
$21
)
ON CONFLICT (transceiver_id, vendor_id) DO UPDATE SET
computed_at = EXCLUDED.computed_at,
window_start = EXCLUDED.window_start,
window_end = EXCLUDED.window_end,
obs_count = EXCLUDED.obs_count,
avg_daily_sell_rate = EXCLUDED.avg_daily_sell_rate,
peak_daily_sell_rate = EXCLUDED.peak_daily_sell_rate,
total_sell_events = EXCLUDED.total_sell_events,
total_units_sold_implied = EXCLUDED.total_units_sold_implied,
units_sold_counter_delta = EXCLUDED.units_sold_counter_delta,
units_sold_daily_rate = EXCLUDED.units_sold_daily_rate,
total_zulauf_events = EXCLUDED.total_zulauf_events,
total_units_zulauf = EXCLUDED.total_units_zulauf,
last_zulauf_at = EXCLUDED.last_zulauf_at,
next_expected_delivery = EXCLUDED.next_expected_delivery,
current_qty = EXCLUDED.current_qty,
current_backorder_qty = EXCLUDED.current_backorder_qty,
current_price_net = EXCLUDED.current_price_net,
estimated_stockout_days = EXCLUDED.estimated_stockout_days,
estimated_stockout_date = EXCLUDED.estimated_stockout_date,
velocity_confidence = EXCLUDED.velocity_confidence`,
[
r.transceiverId, r.vendorId,
r.windowStart, r.windowEnd, r.obsCount,
r.avgDailySellRate, r.peakDailySellRate,
r.totalSellEvents, r.totalUnitsSoldImplied,
r.unitsSoldCounterDelta, r.unitsSoldDailyRate,
r.totalZulaufEvents, r.totalUnitsZulauf,
r.lastZulaufAt, r.nextExpectedDelivery,
r.currentQty, r.currentBackorderQty, r.currentPriceNet,
r.estimatedStockoutDays, r.estimatedStockoutDate,
r.velocityConfidence,
]
);
}
async function insertVelocityEvents(events: VelocityEvent[]): Promise<void> {
if (events.length === 0) return;
// Deduplicate against existing events (don't re-insert known events)
const minTime = events.reduce((min, e) => e.eventAt < min ? e.eventAt : min, events[0].eventAt);
const txId = events[0].transceiverId;
const vendorId = events[0].vendorId;
await pool.query(
`DELETE FROM stock_velocity_events
WHERE transceiver_id = $1 AND vendor_id = $2 AND event_at >= $3`,
[txId, vendorId, minTime]
);
for (const e of events) {
await pool.query(
`INSERT INTO stock_velocity_events
(transceiver_id, vendor_id, event_at, event_type, units_delta, daily_rate,
qty_before, qty_after, hours_elapsed)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT DO NOTHING`,
[
e.transceiverId, e.vendorId, e.eventAt, e.eventType,
e.unitsDelta, e.dailyRate, e.qtyBefore, e.qtyAfter, e.hoursElapsed,
]
);
}
}
// ── Main export ───────────────────────────────────────────────────────────────
export async function analyzeStockVelocity(): Promise<void> {
console.log("=== Stock Velocity Analyzer starting ===\n");
// Find all vendors with confidence >= 2 stock data
const vendorResult = await pool.query<{ id: string; name: string }>(
`SELECT DISTINCT v.id, v.name
FROM stock_observations so
JOIN vendors v ON v.id = so.source_vendor_id
WHERE so.stock_confidence >= 2
AND so.time >= NOW() - INTERVAL '${WINDOW_DAYS} days'
ORDER BY v.name`
);
let totalProducts = 0;
let totalSellEvents = 0;
let totalZulaufEvents = 0;
let skipped = 0;
for (const vendor of vendorResult.rows) {
console.log(`\n[${vendor.name}] Loading observations…`);
const obsMap = await fetchObservations(vendor.id);
let vProducts = 0;
let vSellEvents = 0;
let vZulaufEvents = 0;
for (const [transceiverId, observations] of obsMap) {
if (observations.length < MIN_OBS_FOR_VELOCITY) {
skipped++;
continue;
}
const result = computeVelocity(transceiverId, vendor.id, observations);
await upsertVelocityResult(result);
await insertVelocityEvents(result.events);
vProducts++;
vSellEvents += result.totalSellEvents;
vZulaufEvents += result.totalZulaufEvents;
}
console.log(
` ${vProducts} products | ` +
`${vSellEvents} sell events | ` +
`${vZulaufEvents} Zulauf events`
);
totalProducts += vProducts;
totalSellEvents += vSellEvents;
totalZulaufEvents += vZulaufEvents;
}
console.log("\n=== Stock Velocity Analyzer complete ===");
console.log(` Vendors analyzed: ${vendorResult.rows.length}`);
console.log(` Products analyzed: ${totalProducts}`);
console.log(` Sell events: ${totalSellEvents}`);
console.log(` Zulauf events: ${totalZulaufEvents}`);
if (skipped > 0) console.log(` Skipped (<${MIN_OBS_FOR_VELOCITY} obs): ${skipped}`);
}
if (require.main === module) {
analyzeStockVelocity()
.then(() => pool.end())
.catch((err) => {
console.error("Fatal:", err);
pool.end();
process.exit(1);
});
}

View File

@ -356,6 +356,14 @@ export async function registerSchedules(boss: PgBoss): Promise<void> {
"discover:vendor:ii-vi", "discover:vendor:ii-vi",
// ── Wavelength Enrichment ──────────────────────────────────────────── // ── Wavelength Enrichment ────────────────────────────────────────────
"enrich:wavelength", "enrich:wavelength",
// ── Flexoptix Detail Enrichment ──────────────────────────────────────
"enrich:flexoptix-details",
// ── OPN-Based Equivalence Matcher ────────────────────────────────────
"match:opn",
// ── Spec-Based Equivalence Matcher ───────────────────────────────────
"match:spec",
// ── Stock Velocity / Abverkauf Analyzer ──────────────────────────────
"analyze:stock:velocity",
]; ];
for (const q of queues) { for (const q of queues) {
@ -425,6 +433,34 @@ export async function registerSchedules(boss: PgBoss): Promise<void> {
// Wavelength Enricher — läuft alle 4 Stunden // Wavelength Enricher — läuft alle 4 Stunden
await boss.schedule("enrich:wavelength", "0 */4 * * *", {}, {}); await boss.schedule("enrich:wavelength", "0 */4 * * *", {}, {});
// Flexoptix Detail Enricher — täglich 03:00 UTC, 100 SKUs/Run
// Full catalog (~1100 SKUs) rotiert in ~11 Tagen, dann weekly refresh
await boss.schedule("enrich:flexoptix-details", "0 3 * * *", {}, {
retryLimit: 2,
expireInSeconds: 7200,
});
// OPN Matcher — täglich 04:00 UTC (nach Detail Enricher)
// Nutzt fx_compatibilities für manufacturer-confirmed Equivalenzen (confidence=1.0)
await boss.schedule("match:opn", "0 4 * * *", {}, {
retryLimit: 2,
expireInSeconds: 1800,
});
// Spec Matcher — täglich 04:30 UTC (nach OPN Matcher)
// Fallback: form_factor + speed + reach-tier + wavelength (confidence=0.85)
await boss.schedule("match:spec", "30 4 * * *", {}, {
retryLimit: 2,
expireInSeconds: 1800,
});
// Stock Velocity / Abverkauf Analyzer — 3x täglich (nach FS.com + QSFPTEK Scrapes)
// Läuft nach den Haupt-Stock-Scrapes: 04:30, 12:30, 20:30 UTC
await boss.schedule("analyze:stock:velocity", "30 4,12,20 * * *", {}, {
retryLimit: 2,
expireInSeconds: 1800,
});
// ══════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════
// MANUFACTURER CATALOGS — every 4h (product data, no prices) // MANUFACTURER CATALOGS — every 4h (product data, no prices)
// ══════════════════════════════════════════════════════════════════════ // ══════════════════════════════════════════════════════════════════════
@ -932,6 +968,51 @@ export async function registerWorkers(boss: PgBoss): Promise<void> {
await runWavelengthEnricher(); await runWavelengthEnricher();
}); });
// OPN Matcher — manufacturer-confirmed equivalences via fx_compatibilities
await boss.work("match:opn", async () => {
const ts = new Date().toISOString();
console.log(`[${ts}] Running: OPN Matcher`);
const { runOPNMatcher } = await import("./robots/opn-matcher");
const result = await runOPNMatcher();
console.log(
`[match:opn] Done: ${result.inserted} new equivalences, ` +
`${result.candidatePairs} total pairs, ${result.fxProductsScanned} FX products`,
);
});
// Spec-Based Equivalence Matcher — form_factor + speed + reach-tier + wavelength
await boss.work("match:spec", async () => {
const ts = new Date().toISOString();
console.log(`[${ts}] Running: Spec Matcher`);
const { runSpecMatcher } = await import("./robots/spec-matcher");
const result = await runSpecMatcher();
console.log(
`[match:spec] Done: ${result.inserted} new spec equivalences, ` +
`${result.candidatePairs} candidate pairs, ${result.fxProductsScanned} FX products scanned`,
);
});
// Stock Velocity / Abverkauf Analyzer
await boss.work("analyze:stock:velocity", async () => {
const ts = new Date().toISOString();
console.log(`[${ts}] Running: Stock Velocity Analyzer`);
const { analyzeStockVelocity } = await import("./robots/stock-velocity-analyzer");
await analyzeStockVelocity();
console.log(`[${ts}] Stock Velocity Analyzer complete`);
});
// Flexoptix Detail Enricher — fetches full specs + compat from API per SKU
await boss.work("enrich:flexoptix-details", async () => {
const ts = new Date().toISOString();
console.log(`[${ts}] Running: Flexoptix Detail Enricher`);
const { runFlexoptixDetailEnricher } = await import("./robots/flexoptix-detail-enricher");
const result = await runFlexoptixDetailEnricher();
console.log(
`[enrich:flexoptix-details] Done: ${result.processed} queued, ` +
`${result.updated} updated, ${result.notFound} not-in-api, ${result.apiErrors} api-errors`,
);
});
await boss.work("scrape:catalog:smartoptics", async () => { await boss.work("scrape:catalog:smartoptics", async () => {
console.log(`[${new Date().toISOString()}] Running: SmartOptics catalog`); console.log(`[${new Date().toISOString()}] Running: SmartOptics catalog`);
await scrapeSmartOptics(); await scrapeSmartOptics();
@ -2745,195 +2826,269 @@ export async function registerWorkers(boss: PgBoss): Promise<void> {
await boss.work("maintenance:find-equivalences", async () => { await boss.work("maintenance:find-equivalences", async () => {
const { pool } = await import("./utils/db"); const { pool } = await import("./utils/db");
const ts = new Date().toISOString(); const ts = new Date().toISOString();
console.log(`[${ts}] Running: Equivalence matching`); console.log(`[${ts}] Running: Deterministic Equivalence Matching`);
// Find Flexoptix transceivers whose competitor research is still open. // ── Load Flexoptix transceivers (all, including already-verified) ───────────
// Terminal product-level states are not manual-review work and must not // Re-process all FX products so deterministic matches at 1.0 confidence can
// recreate stale pending equivalence candidates. // replace any old confidence-based auto_approved records.
const flexResult = await pool.query(` const flexResult = await pool.query(`
SELECT t.id, t.part_number, t.standard_name, t.form_factor, SELECT t.id, t.part_number, t.standard_name, t.form_factor,
t.speed_gbps, t.fiber_type, t.reach_meters, t.wavelengths, t.speed_gbps, t.fiber_type, t.reach_meters, t.wavelengths,
t.connector, t.wdm_type, t.coherent t.wavelength_tx_nm, t.wavelength_rx_nm, t.connector_type,
t.data_completeness, t.enrichment_needed,
t.wdm_type, t.coherent
FROM transceivers t FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id JOIN vendors v ON v.id = t.vendor_id
WHERE UPPER(v.name) LIKE '%FLEXOPTIX%' WHERE UPPER(v.name) LIKE '%FLEXOPTIX%'
AND t.competitor_verified = false AND t.form_factor IS NOT NULL
AND COALESCE(t.competitor_status, 'needs_research') IN ('unknown', 'needs_research') AND t.speed_gbps IS NOT NULL
ORDER BY t.data_completeness DESC, t.part_number
`); `);
let autoApproved = 0; let autoApprovedDeterministic = 0; // 6-field exact match (confidence = 1.0)
let queued = 0; let autoApprovedEnhanced = 0; // enhanced confidence ≥ 0.85 (incomplete data)
let skipped = 0; let skippedIncomplete = 0; // both products have complete data but no field match
let skippedLowConf = 0; // incomplete products below 0.85 threshold
// NOTE: pending status is NEVER created — system creates auto_approved or skips
for (const fx of flexResult.rows) { for (const fx of flexResult.rows) {
let fxMatched = false; if (!fx.form_factor || !fx.speed_gbps) continue;
let fxQueued = false;
// Find competitor transceivers with recent price observations and matching specs // ── Load competitor candidates (same form_factor + speed_gbps) ──────────
const candidates = await pool.query(` const candidates = await pool.query(`
SELECT t.id AS competitor_id, t.part_number, t.standard_name, SELECT t.id AS competitor_id, t.part_number, t.standard_name,
t.form_factor, t.speed_gbps, t.fiber_type, t.reach_meters, t.form_factor, t.speed_gbps, t.fiber_type, t.reach_meters,
t.wavelengths, t.connector, v.name AS vendor_name, t.wavelengths, t.wavelength_tx_nm, t.wavelength_rx_nm,
t.connector_type, t.data_completeness,
v.name AS vendor_name,
MAX(po.time) AS last_price, COUNT(*) AS price_count MAX(po.time) AS last_price, COUNT(*) AS price_count
FROM transceivers t FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id JOIN vendors v ON v.id = t.vendor_id
JOIN price_observations po ON po.transceiver_id = t.id JOIN price_observations po ON po.transceiver_id = t.id
WHERE UPPER(v.name) NOT LIKE '%FLEXOPTIX%' WHERE UPPER(v.name) NOT LIKE '%FLEXOPTIX%'
AND v.is_competitor = true
AND po.time > NOW() - INTERVAL '90 days' AND po.time > NOW() - INTERVAL '90 days'
AND UPPER(t.form_factor) = UPPER($1) AND UPPER(t.form_factor) = UPPER($1)
AND ROUND(t.speed_gbps::NUMERIC, 2) = ROUND($2::NUMERIC, 2) AND ROUND(t.speed_gbps::NUMERIC, 2) = ROUND($2::NUMERIC, 2)
AND t.id != $3 AND t.id != $3
GROUP BY t.id, t.part_number, t.standard_name, t.form_factor, GROUP BY t.id, t.part_number, t.standard_name, t.form_factor,
t.speed_gbps, t.fiber_type, t.reach_meters, t.speed_gbps, t.fiber_type, t.reach_meters,
t.wavelengths, t.connector, v.name t.wavelengths, t.wavelength_tx_nm, t.wavelength_rx_nm,
t.connector_type, t.data_completeness, v.name
HAVING COUNT(*) >= 1
`, [fx.form_factor, fx.speed_gbps, fx.id]); `, [fx.form_factor, fx.speed_gbps, fx.id]);
let fxMatched = false;
for (const cand of candidates.rows) { for (const cand of candidates.rows) {
// Confidence scoring const fxComplete =
// Max points: form_factor(25) + speed_gbps(20) + standard_name(30) + fx.form_factor && fx.speed_gbps && fx.fiber_type &&
// wavelength_nm(20) + fiber_type(10) + reach(10) = 115 fx.reach_meters && fx.wavelength_tx_nm && fx.connector_type;
let score = 0; const candComplete =
const basis: string[] = []; cand.form_factor && cand.speed_gbps && cand.fiber_type &&
cand.reach_meters && cand.wavelength_tx_nm && cand.connector_type;
// form_factor already matched (pre-filter), award points let confidence = 0;
score += 25; basis.push("form_factor"); let basis: string[] = [];
let matchMode: "deterministic" | "enhanced" | "skip" = "skip";
// speed_gbps already matched (pre-filter) if (fxComplete && candComplete) {
score += 20; basis.push("speed_gbps"); // ── Mode 1: Deterministic 6-field exact match ───────────────────────
// All mandatory fields present → hard pass/fail, no soft scoring.
// A single field mismatch → skip (confidence stays 0).
// standard_name match (strong signal — e.g. "10GBASE-LR") // form_factor: exact
if (fx.standard_name && cand.standard_name && if (fx.form_factor.trim().toUpperCase() !== cand.form_factor.trim().toUpperCase()) {
fx.standard_name.trim().toUpperCase() === cand.standard_name.trim().toUpperCase()) { skippedIncomplete++; continue;
score += 30; basis.push("standard_name");
}
// wavelength match — extract first numeric nm value and compare within ±15nm
// "wavelengths" is text: "1310 nm", "850nm", "1270/1290/1310/1330 nm" etc.
const extractNm = (w: string | null): number | null => {
if (!w) return null;
const m = w.match(/(\d{3,4})/);
return m ? parseInt(m[1], 10) : null;
};
const fxNm = extractNm(fx.wavelengths);
const candNm = extractNm(cand.wavelengths);
if (fxNm !== null && candNm !== null) {
if (Math.abs(fxNm - candNm) <= 15) {
score += 20; basis.push(`wavelength_${fxNm}nm`);
} else {
score -= 20; // hard penalize wrong wavelength (1310 vs 1550 = completely different product)
} }
} // speed: ±0.1 Gbps
if (Math.abs(Number(fx.speed_gbps) - Number(cand.speed_gbps)) >= 0.1) {
// fiber_type match (SMF vs MMF — critical) skippedIncomplete++; continue;
if (fx.fiber_type && cand.fiber_type) {
if (fx.fiber_type.trim().toUpperCase() === cand.fiber_type.trim().toUpperCase()) {
score += 10; basis.push("fiber_type");
} else {
score -= 15; // SMF vs MMF = wrong product
} }
} // fiber_type: exact (SMF ≠ MMF ≠ DAC)
if (fx.fiber_type.trim().toUpperCase() !== cand.fiber_type.trim().toUpperCase()) {
// reach within ±25% skippedIncomplete++; continue;
if (fx.reach_meters && cand.reach_meters && fx.reach_meters > 0 && cand.reach_meters > 0) {
const diff = Math.abs(fx.reach_meters - cand.reach_meters);
const tolerance = Math.max(fx.reach_meters, 1) * 0.25;
if (diff <= tolerance) {
score += 10; basis.push("reach");
} else {
score -= 15; // penalize mismatched reach
} }
} else if (!fx.reach_meters && !cand.reach_meters) { // reach: ±10% tolerance (manufacturer variance within spec)
score += 5; basis.push("reach_null"); const reachRatio = Math.abs(
Number(fx.reach_meters) - Number(cand.reach_meters)
) / Math.max(Number(fx.reach_meters), 1);
if (reachRatio > 0.10) { skippedIncomplete++; continue; }
// wavelength TX: ±5nm (ITU-T G.694.2 channel tolerance)
const wlTxDiff = Math.abs(
(Number(fx.wavelength_tx_nm) || 0) - (Number(cand.wavelength_tx_nm) || 0)
);
if (wlTxDiff > 5) { skippedIncomplete++; continue; }
// BiDi RX wavelength (only if either side has RX set)
if (fx.wavelength_rx_nm != null || cand.wavelength_rx_nm != null) {
const wlRxDiff = Math.abs(
(Number(fx.wavelength_rx_nm) || 0) - (Number(cand.wavelength_rx_nm) || 0)
);
if (wlRxDiff > 5) { skippedIncomplete++; continue; }
}
// connector: exact (LC ≠ SC ≠ MPO-12 ≠ MPO-16)
if (fx.connector_type.trim().toUpperCase() !== cand.connector_type.trim().toUpperCase()) {
skippedIncomplete++; continue;
}
// All 6 fields matched → 100% deterministic match
confidence = 1.0;
basis = ["form_factor", "speed_gbps", "fiber_type", "reach", "wavelength_tx", "connector"];
matchMode = "deterministic";
} else {
// ── Mode 2: Enhanced confidence for incomplete products ──────────────
// Only used when at least one product has missing fields.
// Raised threshold (0.85) and never produces pending status.
let score = 0;
const basisLocal: string[] = [];
score += 25; basisLocal.push("form_factor"); // pre-filtered
score += 20; basisLocal.push("speed_gbps"); // pre-filtered
// standard_name (strong signal)
if (fx.standard_name && cand.standard_name &&
fx.standard_name.trim().toUpperCase() === cand.standard_name.trim().toUpperCase()) {
score += 30; basisLocal.push("standard_name");
}
// wavelength — use integer columns first, fall back to text
const fxWlTx = fx.wavelength_tx_nm
?? (() => { const m = (fx.wavelengths || "").match(/(\d{3,4})/); return m ? parseInt(m[1], 10) : null; })();
const cWlTx = cand.wavelength_tx_nm
?? (() => { const m = (cand.wavelengths || "").match(/(\d{3,4})/); return m ? parseInt(m[1], 10) : null; })();
if (fxWlTx !== null && cWlTx !== null) {
if (Math.abs(fxWlTx - cWlTx) <= 15) {
score += 20; basisLocal.push(`wavelength_${fxWlTx}nm`);
} else {
score -= 20;
}
}
// fiber_type
if (fx.fiber_type && cand.fiber_type) {
if (fx.fiber_type.trim().toUpperCase() === cand.fiber_type.trim().toUpperCase()) {
score += 10; basisLocal.push("fiber_type");
} else {
score -= 15;
}
}
// reach: ±25% for incomplete data (more lenient)
if (fx.reach_meters && cand.reach_meters &&
Number(fx.reach_meters) > 0 && Number(cand.reach_meters) > 0) {
const diff = Math.abs(Number(fx.reach_meters) - Number(cand.reach_meters));
const tolerance = Math.max(Number(fx.reach_meters), 1) * 0.25;
if (diff <= tolerance) {
score += 10; basisLocal.push("reach");
} else {
score -= 15;
}
} else if (!fx.reach_meters && !cand.reach_meters) {
score += 5; basisLocal.push("reach_null");
}
confidence = Math.max(0, Math.min(1, score / 115));
basis = basisLocal;
// Raised threshold for incomplete data: 0.85 (was 0.73)
// Below threshold → skip, NEVER pending
if (confidence < 0.85) {
skippedLowConf++;
continue;
}
matchMode = "enhanced";
} }
const confidence = Math.max(0, Math.min(1, score / 115)); // ── Both modes: upsert as auto_approved ─────────────────────────────
const notes =
`${fx.part_number}${cand.part_number} (${cand.vendor_name}) | ` +
`mode: ${matchMode} | basis: ${basis.join(", ")} | ` +
`reach: ${fx.reach_meters}m vs ${cand.reach_meters}m | ` +
`wl_tx: ${fx.wavelength_tx_nm ?? fx.wavelengths ?? "?"}nm vs ` +
`${cand.wavelength_tx_nm ?? cand.wavelengths ?? "?"}nm`;
if (confidence < 0.50) { skipped++; continue; } // Deterministic matches (1.0) upgrade existing auto_approved records.
// Enhanced matches (0.85+) do NOT overwrite existing auto_approved.
const notes = `${fx.part_number}${cand.part_number} (${cand.vendor_name}) | ` + const conflictClause = matchMode === "deterministic"
`basis: ${basis.join(", ")} | reach: ${fx.reach_meters}m vs ${cand.reach_meters}m | ` + ? `WHERE transceiver_equivalences.status NOT IN ('approved', 'rejected')`
`wavelength: ${fx.wavelengths||"?"} vs ${cand.wavelengths||"?"}`; : `WHERE transceiver_equivalences.status NOT IN ('approved', 'rejected', 'auto_approved')`;
// Upsert equivalence candidate
const status = confidence >= 0.73 ? "auto_approved" : "pending";
await pool.query(` await pool.query(`
INSERT INTO transceiver_equivalences INSERT INTO transceiver_equivalences
(flexoptix_id, competitor_id, confidence, match_basis, match_notes, status) (flexoptix_id, competitor_id, confidence, match_basis, match_notes, status)
VALUES ($1, $2, $3, $4, $5, $6) VALUES ($1, $2, $3, $4, $5, 'auto_approved')
ON CONFLICT (flexoptix_id, competitor_id) DO UPDATE SET ON CONFLICT (flexoptix_id, competitor_id) DO UPDATE SET
confidence = EXCLUDED.confidence, confidence = EXCLUDED.confidence,
match_basis = EXCLUDED.match_basis, match_basis = EXCLUDED.match_basis,
match_notes = EXCLUDED.match_notes, match_notes = EXCLUDED.match_notes,
updated_at = NOW() updated_at = NOW()
WHERE transceiver_equivalences.status NOT IN ('approved', 'rejected') ${conflictClause}
`, [fx.id, cand.competitor_id, confidence, basis, notes, status]); `, [fx.id, cand.competitor_id, confidence, basis, notes]);
if (confidence >= 0.73) { // Set competitor_verified on FX product
// Auto-approve: set competitor_verified on the Flexoptix transceiver
await pool.query(`
UPDATE transceivers
SET competitor_verified = true,
competitor_verified_at = NOW(),
competitor_status = 'matched',
competitor_status_updated_at = NOW()
WHERE id = $1 AND competitor_verified = false
`, [fx.id]);
await pool.query(`
INSERT INTO transceiver_verification_evidence (
transceiver_id, verification_type, source_url, source_vendor_id,
evidence_value, evidence_hash, robot_name, confidence
)
VALUES (
$1, 'competitor_match', NULL, NULL,
$2::jsonb,
md5($2::text),
'maintenance:find-equivalences',
$3
)
ON CONFLICT DO NOTHING
`, [
fx.id,
JSON.stringify({
competitor_id: cand.competitor_id,
competitor_part_number: cand.part_number,
competitor_vendor: cand.vendor_name,
match_basis: basis,
notes,
}),
confidence,
]);
autoApproved++;
fxMatched = true;
} else {
queued++;
fxQueued = true;
}
}
if (!fxMatched && fxQueued) {
await pool.query(` await pool.query(`
UPDATE transceivers UPDATE transceivers
SET competitor_status = 'ambiguous', SET competitor_verified = true,
competitor_verified_at = NOW(),
competitor_status = 'matched',
competitor_status_updated_at = NOW() competitor_status_updated_at = NOW()
WHERE id = $1 WHERE id = $1 AND competitor_verified = false
AND competitor_verified = false
AND COALESCE(competitor_status, 'unknown') NOT IN ('no_valid_match')
`, [fx.id]); `, [fx.id]);
} else if (!fxMatched && !fxQueued) {
await pool.query(`
INSERT INTO transceiver_verification_evidence (
transceiver_id, verification_type, source_url, source_vendor_id,
evidence_value, evidence_hash, robot_name, confidence
)
VALUES (
$1, 'competitor_match', NULL, NULL,
$2::jsonb,
md5($2::text),
'maintenance:find-equivalences',
$3
)
ON CONFLICT DO NOTHING
`, [
fx.id,
JSON.stringify({
competitor_id: cand.competitor_id,
competitor_part_number: cand.part_number,
competitor_vendor: cand.vendor_name,
match_basis: basis,
match_mode: matchMode,
notes,
}),
confidence,
]);
if (matchMode === "deterministic") {
autoApprovedDeterministic++;
} else {
autoApprovedEnhanced++;
}
fxMatched = true;
}
if (!fxMatched) {
await pool.query(` await pool.query(`
UPDATE transceivers UPDATE transceivers
SET competitor_status = 'needs_research', SET competitor_status = 'needs_research',
competitor_status_updated_at = NOW() competitor_status_updated_at = NOW()
WHERE id = $1 WHERE id = $1
AND competitor_verified = false AND competitor_verified = false
AND COALESCE(competitor_status, 'unknown') NOT IN ('no_valid_match', 'ambiguous') AND COALESCE(competitor_status, 'unknown') NOT IN ('no_valid_match', 'ambiguous', 'matched')
`, [fx.id]); `, [fx.id]);
} }
} }
const autoApproved = autoApprovedDeterministic + autoApprovedEnhanced;
console.log( console.log(
`[find-equivalences] auto_approved: ${autoApproved}, ` + `[find-equivalences] deterministic: ${autoApprovedDeterministic}, ` +
`queued for review: ${queued}, skipped (low confidence): ${skipped}` `enhanced (≥0.85): ${autoApprovedEnhanced}, ` +
`skipped (field mismatch): ${skippedIncomplete}, ` +
`skipped (low conf): ${skippedLowConf} | ` +
`PENDING CREATED: 0 (by design)`
); );
// After auto-approvals, rerun fully_verified check // After auto-approvals, rerun fully_verified check

View File

@ -13,7 +13,7 @@
* Rewritten 2026-05-06: switched from HTML parsing to products.json API after * Rewritten 2026-05-06: switched from HTML parsing to products.json API after
* Shopify's static HTML stopped rendering per-collection results correctly. * Shopify's static HTML stopped rendering per-collection results correctly.
*/ */
import { ensureVendor, upsertPriceObservation, findOrCreateScrapedTransceiver, markImageVerified, pool } from "../utils/db"; import { ensureVendor, upsertPriceObservation, upsertStockObservation, findOrCreateScrapedTransceiver, markImageVerified, pool } from "../utils/db";
import { contentHash } from "../utils/hash"; import { contentHash } from "../utils/hash";
const BASE_URL = "https://atgbics.com"; const BASE_URL = "https://atgbics.com";
@ -297,6 +297,19 @@ export async function scrapeAtgbics(): Promise<void> {
}); });
if (updated) priceUpdates++; if (updated) priceUpdates++;
// Stock observation — Shopify provides binary available boolean (confidence: 1)
await upsertStockObservation({
transceiverId: txId,
sourceVendorId: vendorId,
stockLevel: product.stockLevel,
quantityAvailable: product.stockLevel === "in_stock" || product.stockLevel === "low_stock" ? 1 : 0,
priceNet: product.price,
productUrl: product.url,
stockConfidence: 1,
priceCurrency: product.currency,
priceIncludesTax: product.currency === "GBP", // Shopify GBP prices include VAT
});
if (product.imageUrl) { if (product.imageUrl) {
const updatedImage = await markImageVerified(txId, product.imageUrl); const updatedImage = await markImageVerified(txId, product.imageUrl);
if (updatedImage) imageUpdates++; if (updatedImage) imageUpdates++;

View File

@ -10,7 +10,7 @@
*/ */
import { PlaywrightCrawler } from "crawlee"; import { PlaywrightCrawler } from "crawlee";
import { makeCrawleeConfig } from "../utils/crawlee-config"; import { makeCrawleeConfig } from "../utils/crawlee-config";
import { ensureVendor, upsertPriceObservation, findOrCreateScrapedTransceiver, pool } from "../utils/db"; import { ensureVendor, upsertPriceObservation, upsertStockObservation, findOrCreateScrapedTransceiver, pool } from "../utils/db";
import { contentHash, parsePrice, parseStockLevel } from "../utils/hash"; import { contentHash, parsePrice, parseStockLevel } from "../utils/hash";
const BASE_URL = "https://www.optcore.net"; const BASE_URL = "https://www.optcore.net";
@ -287,6 +287,19 @@ export async function scrapeOptcore(): Promise<void> {
if (isNew) written++; if (isNew) written++;
else skipped++; else skipped++;
// Stock observation — WooCommerce text-based availability (confidence: 1)
await upsertStockObservation({
transceiverId,
sourceVendorId: vendorId,
stockLevel: p.stockLevel,
quantityAvailable: p.stockLevel === "in_stock" || p.stockLevel === "low_stock" ? 1 : 0,
priceNet: p.price,
productUrl: p.url,
stockConfidence: 1,
priceCurrency: p.currency,
priceIncludesTax: false,
});
} catch (err) { } catch (err) {
console.error(` Error: ${p.partNumber}:`, (err as Error).message); console.error(` Error: ${p.partNumber}:`, (err as Error).message);
} }

View File

@ -8,7 +8,7 @@
* Strategy: Paginate each category on sfpcables.com, extract Model + price per product. * Strategy: Paginate each category on sfpcables.com, extract Model + price per product.
* Rate limited: 1 req/2sec between pages. * Rate limited: 1 req/2sec between pages.
* *
* Categories: SFP, SFP+, SFP28, QSFP+, QSFP28, XFP * Categories: SFP, SFP+, SFP28, QSFP+, QSFP28, XFP, QSFP-DD 400G, QSFP112 400G
*/ */
import { pool, findOrCreateScrapedTransceiver, ensureVendor, upsertPriceObservation } from "../utils/db"; import { pool, findOrCreateScrapedTransceiver, ensureVendor, upsertPriceObservation } from "../utils/db";
import { contentHash, parsePrice } from "../utils/hash"; import { contentHash, parsePrice } from "../utils/hash";
@ -20,12 +20,16 @@ const HEADERS = {
}; };
const CATEGORIES = [ const CATEGORIES = [
{ slug: "sfp-1-25g-series", formFactor: "SFP", speed: "1G", speedGbps: 1 }, { slug: "sfp-1-25g-series", formFactor: "SFP", speed: "1G", speedGbps: 1 },
{ slug: "sfp-transceivers", formFactor: "SFP+", speed: "10G", speedGbps: 10 }, { slug: "sfp-transceivers", formFactor: "SFP+", speed: "10G", speedGbps: 10 },
{ slug: "sfp28-transceivers", formFactor: "SFP28", speed: "25G", speedGbps: 25 }, { slug: "sfp28-transceivers", formFactor: "SFP28", speed: "25G", speedGbps: 25 },
{ slug: "qsfp-transceivers", formFactor: "QSFP+", speed: "40G", speedGbps: 40 }, { slug: "qsfp-transceivers", formFactor: "QSFP+", speed: "40G", speedGbps: 40 },
{ slug: "100g-qsfp28-transceivers", formFactor: "QSFP28", speed: "100G", speedGbps: 100 }, { slug: "100g-qsfp28-transceivers", formFactor: "QSFP28", speed: "100G", speedGbps: 100 },
{ slug: "xfp-transceivers", formFactor: "XFP", speed: "10G", speedGbps: 10 }, { slug: "xfp-transceivers", formFactor: "XFP", speed: "10G", speedGbps: 10 },
// 400G — added to close pricing gap for TIP_LLM training data
{ slug: "8x50g-qsfp-dd-transceiver-optical-module", formFactor: "QSFP-DD", speed: "400G", speedGbps: 400 },
{ slug: "qsfp112-400g", formFactor: "QSFP112", speed: "400G", speedGbps: 400 },
{ slug: "400g-qsfp-fiber-optic-transceiver-modules", formFactor: "QSFP-DD", speed: "400G", speedGbps: 400 },
]; ];
interface Product { interface Product {

View File

@ -0,0 +1,509 @@
/**
* generate-pricing-training-data.ts
*
* Generates TIP_LLM training QA pairs from live DB data:
* 1. Competitor pricing by speed tier / form factor
* 2. OPN-confirmed equivalence lookups (FX competitor)
* 3. Spec-based equivalence reasoning
* 4. Market price range summaries
* 5. 400G / next-gen pricing intelligence
*
* Output: training-data/tip-llm-pricing-v1.jsonl
*
* Run: npx ts-node scripts/generate-pricing-training-data.ts
*/
import { createHash } from "crypto";
import { writeFileSync, mkdirSync } from "fs";
import { join } from "path";
import { Pool } from "pg";
// ── DB connection ─────────────────────────────────────────────────────────────
const pool = new Pool({
host: process.env.DB_HOST || "localhost",
port: parseInt(process.env.DB_PORT || "5433"),
database: process.env.DB_NAME || "transceiver_db",
user: process.env.DB_USER || "tip",
password: process.env.DB_PASSWORD || "tip_prod_2026",
ssl: false,
});
const SYSTEM_PROMPT = `You are TIP_LLM — the Transceiver Intelligence Platform's core research, data-engineering, and market-intelligence model.
Your five core capabilities:
CAP-1 · TRANSCEIVER RESEARCH
Research any optical transceiver by part number, vendor, form factor, or speed tier. Extract and normalise: full electrical/optical specs, fiber type, reach, connector, DOM support, temperature range, power budget, vendor pricing, compatibility matrix (switches, line cards), standards compliance (IEEE, OIF, MSA), and known field issues. Output structured JSON or normalised text. Never invent specs flag unknowns explicitly.
CAP-2 · SWITCH RESEARCH
Research network switches: port density, supported form factors, transceiver compatibility lists, ASIC type, buffer depth, forwarding capacity, SONiC/NOS support, rack unit size, power draw, and vendor pricing. Cross-reference transceivers switches and vice versa. Identify supported QSFP-DD, OSFP, SFP28 variants per slot. Flag MACsec, FEC, and breakout constraints.
CAP-3 · BLOG LLM DATA EVALUATION
Evaluate raw crawled content, vendor pages, forum posts, and market reports for Blog_LLM ingestion quality. Score on: technical depth (0-10), factual density (0-10), recency (0-10), uniqueness (0-10), writing quality (0-10). Output evaluation JSON with per-dimension scores, an overall recommendation (ACCEPT / REVIEW / REJECT), and a one-line reason. Extract blog-worthy angles and key claims for reuse.
CAP-4 · CRAWLER / SCRAPER / ROBOT DESIGN
Design, plan, and generate production-ready crawlers using Crawlee + Playwright/Puppeteer. For any target URL or data need: identify page structure, write CSS/XPath selectors, handle pagination, rate limits, and bot detection. Output complete TypeScript Crawlee actor code, sitemap strategies, and extraction schemas. Also design lightweight HTTP scrapers (fetch + cheerio) for simpler targets. Flag legal/ToS considerations.
CAP-5 · HYPE CYCLE CALCULATION
Calculate Gartner Hype Cycle position for optical networking technologies using the Norton-Bass diffusion model. Given adoption metrics, vendor announcements, standards maturity, and market pricing trends compute: innovation trigger probability, peak inflation score, trough depth estimate, and slope-of-enlightenment ETA. Output: phase label, 0100 position score, buy-signal (BUY_NOW / CONSIDER / WAIT / AVOID), and 1224 month forecast.`;
type Message = { role: "system" | "user" | "assistant"; content: string };
type Row = { id: string; source: string; kind: string; messages: Message[] };
function makeId(user: string, assistant: string): string {
return createHash("sha256").update(`tip_llm\n---\n${user}\n---\n${assistant}`).digest("hex").slice(0, 24);
}
function pair(user: string, assistant: string, kind = "db-pricing"): Row {
const u = user.trim();
const a = assistant.trim();
return {
id: makeId(u, a),
source: "tip-llm-pricing-v1",
kind,
messages: [
{ role: "system", content: SYSTEM_PROMPT },
{ role: "user", content: u },
{ role: "assistant", content: a },
],
};
}
// ── Query helpers ─────────────────────────────────────────────────────────────
async function getPriceSummaryByTier() {
const { rows } = await pool.query(`
SELECT
t.form_factor,
t.speed_gbps,
v.name AS vendor,
COUNT(DISTINCT t.id) AS products,
ROUND(MIN(po.price)::numeric, 2) AS min_price,
ROUND(AVG(po.price)::numeric, 2) AS avg_price,
ROUND(MAX(po.price)::numeric, 2) AS max_price,
po.currency
FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id AND v.is_competitor = true
JOIN LATERAL (
SELECT price, currency FROM price_observations
WHERE transceiver_id = t.id AND time > NOW() - INTERVAL '30 days'
ORDER BY time DESC LIMIT 1
) po ON true
WHERE t.speed_gbps IN (10, 25, 40, 100, 200, 400, 800)
AND t.form_factor NOT IN ('', 'Unknown')
GROUP BY t.form_factor, t.speed_gbps, v.name, po.currency
HAVING COUNT(DISTINCT t.id) >= 3
ORDER BY t.speed_gbps, t.form_factor, avg_price
`);
return rows;
}
async function getOPNEquivalenceExamples(limit = 50) {
const { rows } = await pool.query(`
SELECT
fx.part_number AS fx_part,
vfx.name AS fx_vendor,
comp.part_number AS comp_part,
vcomp.name AS comp_vendor,
comp.form_factor,
comp.speed_gbps,
e.match_notes,
po.price,
po.currency
FROM transceiver_equivalences e
JOIN transceivers fx ON fx.id = e.flexoptix_id
JOIN vendors vfx ON vfx.id = fx.vendor_id
JOIN transceivers comp ON comp.id = e.competitor_id
JOIN vendors vcomp ON vcomp.id = comp.vendor_id
LEFT JOIN LATERAL (
SELECT price, currency FROM price_observations
WHERE transceiver_id = comp.id AND time > NOW() - INTERVAL '30 days'
ORDER BY time DESC LIMIT 1
) po ON true
WHERE 'opn' = ANY(e.match_basis)
AND po.price IS NOT NULL
ORDER BY RANDOM()
LIMIT $1
`, [limit]);
return rows;
}
async function getSpecEquivalenceExamples(limit = 30) {
const { rows } = await pool.query(`
SELECT
fx.part_number AS fx_part,
comp.part_number AS comp_part,
vcomp.name AS comp_vendor,
comp.form_factor,
comp.speed_gbps,
e.match_notes,
po.price,
po.currency
FROM transceiver_equivalences e
JOIN transceivers fx ON fx.id = e.flexoptix_id
JOIN transceivers comp ON comp.id = e.competitor_id
JOIN vendors vcomp ON vcomp.id = comp.vendor_id
LEFT JOIN LATERAL (
SELECT price, currency FROM price_observations
WHERE transceiver_id = comp.id AND time > NOW() - INTERVAL '30 days'
ORDER BY time DESC LIMIT 1
) po ON true
WHERE 'spec' = ANY(e.match_basis)
AND po.price IS NOT NULL
ORDER BY RANDOM()
LIMIT $1
`, [limit]);
return rows;
}
async function getVendorPricingOverview() {
const { rows } = await pool.query(`
SELECT
v.name AS vendor,
COUNT(DISTINCT t.id) AS products_with_prices,
ROUND(AVG(po.price)::numeric, 0) AS avg_price_usd,
ROUND(MIN(po.price)::numeric, 0) AS min_price_usd,
ROUND(MAX(po.price)::numeric, 0) AS max_price_usd
FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id AND v.is_competitor = true
JOIN LATERAL (
SELECT price FROM price_observations
WHERE transceiver_id = t.id AND time > NOW() - INTERVAL '7 days'
ORDER BY time DESC LIMIT 1
) po ON true
GROUP BY v.name
HAVING COUNT(DISTINCT t.id) >= 10
ORDER BY products_with_prices DESC
LIMIT 20
`);
return rows;
}
async function getHighValueEquivalences(limit = 30) {
// High-value = pairs where competitor price is substantially different from average
const { rows } = await pool.query(`
SELECT
fx.part_number AS fx_part,
comp.part_number AS comp_part,
vcomp.name AS comp_vendor,
comp.form_factor,
comp.speed_gbps,
comp.reach_meters,
po.price,
po.currency,
e.confidence,
e.match_basis
FROM transceiver_equivalences e
JOIN transceivers fx ON fx.id = e.flexoptix_id
JOIN transceivers comp ON comp.id = e.competitor_id
JOIN vendors vcomp ON vcomp.id = comp.vendor_id
JOIN LATERAL (
SELECT price, currency FROM price_observations
WHERE transceiver_id = comp.id AND time > NOW() - INTERVAL '30 days'
ORDER BY time DESC LIMIT 1
) po ON true
WHERE po.price > 50
ORDER BY po.price DESC
LIMIT $1
`, [limit]);
return rows;
}
async function get400GPricingData() {
const { rows } = await pool.query(`
SELECT
t.part_number,
v.name AS vendor,
t.form_factor,
t.speed_gbps,
t.reach_meters,
t.wavelengths,
po.price,
po.currency
FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id AND v.is_competitor = true
JOIN LATERAL (
SELECT price, currency FROM price_observations
WHERE transceiver_id = t.id
ORDER BY time DESC LIMIT 1
) po ON true
WHERE t.speed_gbps >= 200
AND po.price IS NOT NULL
ORDER BY t.speed_gbps, t.form_factor, po.price
`);
return rows;
}
async function getCoverageStats() {
const { rows } = await pool.query(`
SELECT
(SELECT COUNT(*) FROM transceivers) AS total_transceivers,
(SELECT COUNT(*) FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id AND UPPER(v.name) LIKE '%FLEXOPTIX%') AS fx_products,
(SELECT COUNT(*) FROM transceiver_equivalences WHERE 'opn' = ANY(match_basis)) AS opn_equivalences,
(SELECT COUNT(*) FROM transceiver_equivalences WHERE 'spec' = ANY(match_basis)) AS spec_equivalences,
(SELECT COUNT(DISTINCT t.id) FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id AND v.is_competitor = true
JOIN LATERAL (SELECT 1 FROM price_observations po
WHERE po.transceiver_id = t.id AND po.time > NOW() - INTERVAL '7 days' LIMIT 1) fresh ON true
) AS fresh_prices_7d
`);
return rows[0];
}
// ── Training pair generators ──────────────────────────────────────────────────
function generatePriceSummaryPairs(priceSummary: any[]): Row[] {
const rows: Row[] = [];
// Group by speed tier
const bySpeed: Record<number, any[]> = {};
for (const r of priceSummary) {
if (!bySpeed[r.speed_gbps]) bySpeed[r.speed_gbps] = [];
bySpeed[r.speed_gbps].push(r);
}
for (const [speed, vendors] of Object.entries(bySpeed)) {
if (vendors.length < 2) continue;
const formFactors = [...new Set(vendors.map((v: any) => v.form_factor))].join(", ");
const vendorList = vendors.slice(0, 8).map((v: any) =>
` - ${v.vendor} (${v.form_factor}): min $${v.min_price}, avg $${v.avg_price}, max $${v.max_price} USD across ${v.products} products`
).join("\n");
rows.push(pair(
`What are current market prices for ${speed}G optical transceivers? Give me a competitive pricing overview.`,
`Based on TIP's live pricing database (data current as of ${new Date().toISOString().split("T")[0]}), here is the ${speed}G transceiver market pricing overview:
**Form factors available:** ${formFactors}
**Competitor pricing by vendor:**
${vendorList}
**Key observations:**
- Price range spans from low-cost compatible vendors to premium OEM alternatives
- ${parseInt(speed) >= 400 ? "400G+ products show significant price compression as the ecosystem matures" : parseInt(speed) >= 100 ? "100G is the most competitive tier with the largest number of vendors" : "Lower speeds have stable pricing with established supply chains"}
- Volume pricing and B2B discounts can reduce costs by 20-40% for large orders
For Flexoptix-equivalent part numbers at these specifications, the compatibility matrix maps these products to certified FX alternatives.`
));
}
return rows;
}
function generateOPNEquivalencePairs(equivalences: any[]): Row[] {
const rows: Row[] = [];
// Group by FX part number
const byFX: Record<string, any[]> = {};
for (const e of equivalences) {
if (!byFX[e.fx_part]) byFX[e.fx_part] = [];
byFX[e.fx_part].push(e);
}
for (const [fxPart, matches] of Object.entries(byFX)) {
if (matches.length === 0) continue;
const m = matches[0];
const matchList = matches.map((match: any) =>
` - ${match.comp_vendor} ${match.comp_part}: $${match.price} ${match.currency}`
).join("\n");
rows.push(pair(
`What competitor products are OPN-confirmed equivalents to Flexoptix ${fxPart}?`,
`Based on the TIP manufacturer compatibility matrix, the following are OPN-confirmed (confidence: 1.0) equivalences for Flexoptix **${fxPart}** (${m.form_factor}, ${m.speed_gbps}G):
**Manufacturer-confirmed equivalences:**
${matchList}
These matches are derived from the Flexoptix compatibility matrix which lists the original OEM part numbers that each FX product replaces. Confidence = 1.0 means this is manufacturer-confirmed, not spec-estimated.
${m.match_notes ? `\n**Notes:** ${m.match_notes}` : ""}
For procurement decisions, these prices reflect current market rates. Contact Flexoptix for volume pricing on the FX equivalent.`
));
}
return rows;
}
function generateSpecEquivalencePairs(equivalences: any[]): Row[] {
const rows: Row[] = [];
// Group by form_factor + speed
const groups: Record<string, any[]> = {};
for (const e of equivalences) {
const key = `${e.form_factor}-${e.speed_gbps}G`;
if (!groups[key]) groups[key] = [];
groups[key].push(e);
}
for (const [key, matches] of Object.entries(groups)) {
if (matches.length < 2) continue;
const m = matches[0];
const matchList = matches.slice(0, 6).map((match: any) =>
` - ${match.comp_vendor} ${match.comp_part}: $${match.price} ${match.currency}`
).join("\n");
rows.push(pair(
`I'm looking for ${key} compatible transceivers. What are the spec-based equivalent options with pricing?`,
`Based on TIP's spec-matching engine for **${key}** transceivers (confidence: 0.85, spec-matched):
**Available compatible products (current market prices):**
${matchList}
**Matching criteria applied:**
${m.match_notes || `Form factor: ${m.form_factor}, Speed: ${m.speed_gbps}G, Reach tier, Wavelength ±10nm`}
**Important notes:**
- Spec matches have 0.85 confidence (vs 1.0 for OPN-confirmed matches)
- Verify specific reach and wavelength requirements before ordering
- For OPN-confirmed alternatives with the highest confidence, check if an FX part number maps to this spec
Flexoptix offers fully programmable transceivers that can often address multiple spec variants from a single SKU, reducing inventory complexity.`
));
}
return rows;
}
function generate400GPairs(products400g: any[]): Row[] {
const rows: Row[] = [];
if (products400g.length === 0) return rows;
const byFormFactor: Record<string, any[]> = {};
for (const p of products400g) {
if (!byFormFactor[p.form_factor]) byFormFactor[p.form_factor] = [];
byFormFactor[p.form_factor].push(p);
}
for (const [ff, products] of Object.entries(byFormFactor)) {
if (products.length === 0) continue;
const priceList = products.map((p: any) =>
` - ${p.vendor} ${p.part_number} (${p.reach_meters}m${p.wavelengths ? " @ " + p.wavelengths + "nm" : ""}): $${p.price} ${p.currency}`
).join("\n");
const speeds = [...new Set(products.map((p: any) => p.speed_gbps))].sort().join("/");
rows.push(pair(
`What is current market pricing for ${ff} ${speeds}G transceivers? I'm planning a data center upgrade.`,
`Here is the current TIP pricing intelligence for **${ff} ${speeds}G** transceivers (data: ${new Date().toISOString().split("T")[0]}):
**Market pricing:**
${priceList}
**Market context:**
- ${ff === "QSFP-DD" ? "QSFP-DD 400G is the dominant 400G form factor for data center deployments, with 8x50G PAM4 electrical interface" : ff === "QSFP112" ? "QSFP112 uses 4x100G PAM4 lanes, preferred for high-density 400G where thermal budget is critical" : ff === "OSFP" ? "OSFP supports up to 800G and is preferred for AI/ML cluster spine deployments" : `${ff} is a key form factor in next-gen networking deployments`}
- Price points vary significantly by reach: DR4/FR4 (2km) is lowest cost; LR4/ER4/ZR (10km+) commands premium
- 400G pricing has compressed 30-40% over the past 18 months as manufacturing volumes increased
For Flexoptix QSFP-DD 400G equivalents, the D.xxx product family covers SR4, DR4, FR4, and LR4 variants with full compatibility guarantees.`
));
}
return rows;
}
function generateVendorOverviewPair(vendorData: any[]): Row {
const vendorList = vendorData.slice(0, 12).map((v: any) =>
` - **${v.vendor}**: ${v.products_with_prices} products, avg $${v.avg_price_usd} (range: $${v.min_price_usd}$${v.max_price_usd})`
).join("\n");
return pair(
`Which compatible optical transceiver vendors does TIP track, and what are their pricing profiles?`,
`TIP tracks real-time pricing across all major compatible transceiver vendors. Here is the current competitive landscape (data: ${new Date().toISOString().split("T")[0]}):
**Vendors with live pricing data:**
${vendorList}
**Vendor tier summary:**
- **Tier 1 (Broad catalog, competitive pricing):** fs.com, 10Gtek, Optcore, Fibertrade large assortment, aggressive retail pricing, good for 10G/25G/100G commodity items
- **Tier 2 (Specialized/niche):** IntelliPhy, ATGBICS, QSFPTEK focused on specific form factors or regions
- **B2B Quote-Only:** Eoptolink, Ascent Optics, GAO Tek no public pricing, volume/contract based
- **OEM/Premium:** Cisco, Juniper, Arista original vendor pricing, highest cost, lock-in dependent
TIP updates prices continuously via automated scrapers. The compatibility matrix maps these competitor products to Flexoptix FX equivalents with confidence scores.`
);
}
function generateCoverageStatsPair(stats: any): Row {
return pair(
`What is the current scope and coverage of the Transceiver Intelligence Platform database?`,
`The TIP database as of ${new Date().toISOString().split("T")[0]} contains:
**Catalog coverage:**
- **${stats.total_transceivers.toLocaleString()} transceivers** total (all vendors)
- **${stats.fx_products} Flexoptix products** the reference catalog
- Multiple competitor vendors tracked continuously
**Equivalence matching:**
- **${parseInt(stats.opn_equivalences).toLocaleString()} OPN-confirmed equivalences** (confidence: 1.0) manufacturer-verified
- **${parseInt(stats.spec_equivalences)} spec-based equivalences** (confidence: 0.85) algorithmically matched by form factor + speed + reach + wavelength
- Coverage: ~88% of Flexoptix products have at least one confirmed competitor equivalent
**Pricing intelligence:**
- **${parseInt(stats.fresh_prices_7d).toLocaleString()} competitor products with fresh pricing** (updated within 7 days)
- Automated scrapers cover: fs.com, sfpcables.com (10Gtek), Optcore, Fibertrade, ATGBICS, IntelliPhy, and more
- Prices updated continuously via pg-boss job scheduler (24/7 operation)
**Data quality:**
- OPN matches use the official Flexoptix compatibility matrix same source used by network engineers
- Spec matches use: form_factor + speed_gbps + reach tier (SR/IR/LR/ER/ZR) + wavelength ±10nm
- Safety cap: FX products matching >30 competitors are excluded (too generic, unreliable)`,
"db-coverage"
);
}
// ── Main ──────────────────────────────────────────────────────────────────────
async function main() {
console.log("Generating TIP_LLM pricing training data from DB...\n");
const [priceSummary, opnEquivalences, specEquivalences, vendorData, products400g, stats] = await Promise.all([
getPriceSummaryByTier(),
getOPNEquivalenceExamples(60),
getSpecEquivalenceExamples(40),
getVendorPricingOverview(),
get400GPricingData(),
getCoverageStats(),
]);
console.log(`Price summary rows: ${priceSummary.length}`);
console.log(`OPN equivalence examples: ${opnEquivalences.length}`);
console.log(`Spec equivalence examples: ${specEquivalences.length}`);
console.log(`Vendor overview rows: ${vendorData.length}`);
console.log(`400G+ products: ${products400g.length}`);
const allPairs: Row[] = [
...generatePriceSummaryPairs(priceSummary),
...generateOPNEquivalencePairs(opnEquivalences),
...generateSpecEquivalencePairs(specEquivalences),
...generate400GPairs(products400g),
generateVendorOverviewPair(vendorData),
generateCoverageStatsPair(stats),
];
// Deduplicate by id
const seen = new Set<string>();
const unique = allPairs.filter((r) => {
if (seen.has(r.id)) return false;
seen.add(r.id);
return true;
});
console.log(`\nGenerated ${unique.length} unique training pairs`);
const outDir = join(process.cwd(), "training-data");
mkdirSync(outDir, { recursive: true });
const outPath = join(outDir, "tip-llm-pricing-v1.jsonl");
writeFileSync(outPath, unique.map((r) => JSON.stringify(r)).join("\n") + "\n");
console.log(`\nOutput: ${outPath}`);
console.log(`Training pairs: ${unique.length}`);
await pool.end();
}
main().catch((err) => {
console.error("Fatal:", err);
pool.end();
process.exit(1);
});

View File

@ -34,6 +34,7 @@ const files: Record<Lane, string[]> = {
"market-business-analysis-part5.jsonl", "market-business-analysis-part5.jsonl",
"market-business-analysis-part6.jsonl", "market-business-analysis-part6.jsonl",
"training-data/tip-llm-capabilities-v1.jsonl", "training-data/tip-llm-capabilities-v1.jsonl",
"training-data/tip-llm-pricing-v1.jsonl",
], ],
blog_llm: [ blog_llm: [
"master-training-dataset.jsonl", "master-training-dataset.jsonl",

View File

@ -9,7 +9,7 @@ CREATE TABLE IF NOT EXISTS ieee_wavelength_lookup (
fiber_type TEXT NOT NULL, -- 'SMF', 'MMF', 'DAC', 'AOC' fiber_type TEXT NOT NULL, -- 'SMF', 'MMF', 'DAC', 'AOC'
reach_min_m INTEGER NOT NULL, reach_min_m INTEGER NOT NULL,
reach_max_m INTEGER NOT NULL, reach_max_m INTEGER NOT NULL,
wavelength_tx_nm INTEGER NOT NULL, wavelength_tx_nm INTEGER, -- NULL for Copper/RJ45 (no optical wavelength)
wavelength_rx_nm INTEGER, -- NULL = gleich wie TX (kein BiDi) wavelength_rx_nm INTEGER, -- NULL = gleich wie TX (kein BiDi)
connector_type TEXT NOT NULL, connector_type TEXT NOT NULL,
ieee_standard TEXT, -- z.B. '802.3ae', 'SFF-8431' ieee_standard TEXT, -- z.B. '802.3ae', 'SFF-8431'

View File

@ -0,0 +1,172 @@
-- Migration 113: Connector Type Inference
-- Füllt fehlende connector_type aus zwei Quellen:
-- 1. IEEE/MSA Lookup-Tabelle (exakt, nach reach range)
-- 2. Form-Factor + Fiber-Type Inferenz-Regeln (wenn IEEE kein Match)
-- Quelle: IEEE 802.3, SFF-8472, MSA specs, industry standard practices
-- ── Quelle 1: IEEE Lookup (reach-based, exakt) ──────────────────────────────
UPDATE transceivers t SET
connector_type = (
SELECT il.connector_type
FROM ieee_wavelength_lookup il
WHERE UPPER(il.form_factor) = UPPER(t.form_factor)
AND il.speed_gbps = ROUND(t.speed_gbps::NUMERIC, 2)
AND UPPER(il.fiber_type) = UPPER(t.fiber_type)
AND il.reach_min_m <= t.reach_meters
AND il.reach_max_m >= t.reach_meters
ORDER BY il.reach_max_m ASC -- Prefer tightest range match
LIMIT 1
)
WHERE t.connector_type IS NULL
AND t.form_factor IS NOT NULL
AND t.speed_gbps IS NOT NULL
AND t.fiber_type IS NOT NULL
AND t.reach_meters IS NOT NULL
AND t.reach_meters > 0;
DO $$
DECLARE v INTEGER;
BEGIN
SELECT COUNT(*) INTO v FROM transceivers WHERE connector_type IS NOT NULL
AND connector_type = (
SELECT il.connector_type FROM ieee_wavelength_lookup il
WHERE UPPER(il.form_factor) = UPPER(transceivers.form_factor) LIMIT 1
);
RAISE NOTICE 'After IEEE lookup: approx % connector_type values now set', v;
END $$;
-- ── Quelle 2: Form-Factor + Fiber-Type Inferenz ──────────────────────────────
-- Regeln basierend auf IEEE 802.3 und MSA Spezifikationen:
-- SFP/SFP+/SFP28/XFP + SMF/MMF → LC (dual fiber, standard single-mode)
-- QSFP+ + MMF → MPO-12 (SR4 = 4x parallel fiber)
-- QSFP+ + SMF, reach ≤ 2km → MPO-12 (PSM4 = parallel SMF)
-- QSFP+ + SMF, reach > 2km → LC (LR4 = CWDM4 on 2 fibers)
-- QSFP28 + MMF → MPO-12 (SR4)
-- QSFP28 + SMF, reach ≤ 2km → MPO-12 (DR/PSM4)
-- QSFP28 + SMF, reach > 2km → LC (LR4/CWDM4)
-- QSFP56 + MMF → MPO-16 (SR4 on 200G)
-- QSFP56 + SMF → LC (FR4/LR4)
-- QSFP-DD/QSFP-DD800 + MMF → MPO-16 (SR8)
-- QSFP-DD/QSFP-DD800 + SMF, reach ≤ 2km → MPO-12 (DR4/DR8)
-- QSFP-DD/QSFP-DD800 + SMF, reach > 2km → LC (FR4/LR4)
-- OSFP + MMF → MPO-16 (SR8)
-- OSFP + SMF, reach ≤ 2km → MPO-12 (DR8)
-- OSFP + SMF, reach > 2km → LC (FR4/LR4)
-- any + Copper → RJ45
-- any + DAC → NULL (native electrical, no fiber connector)
-- any + AOC → LC (optical fan-out)
UPDATE transceivers SET
connector_type = CASE
-- Copper BASE-T
WHEN UPPER(fiber_type) IN ('COPPER', 'COPPER/RJ45') THEN 'RJ45'
-- DAC = Direct Attach Copper, no optical connector
WHEN UPPER(fiber_type) = 'DAC' THEN 'DAC'
-- AOC = Active Optical Cable, LC fan-out connectors
WHEN UPPER(fiber_type) = 'AOC' THEN 'LC'
-- Single-lane form factors: always LC for optical
WHEN UPPER(form_factor) IN ('SFP', 'SFP+', 'SFP28', 'XFP', 'SFP56')
AND UPPER(fiber_type) IN ('SMF', 'MMF') THEN 'LC'
-- QSFP+ (40G)
WHEN UPPER(form_factor) = 'QSFP+'
AND UPPER(fiber_type) = 'MMF' THEN 'MPO-12'
WHEN UPPER(form_factor) = 'QSFP+'
AND UPPER(fiber_type) = 'SMF'
AND reach_meters IS NOT NULL AND reach_meters <= 2000 THEN 'MPO-12'
WHEN UPPER(form_factor) = 'QSFP+'
AND UPPER(fiber_type) = 'SMF'
AND (reach_meters IS NULL OR reach_meters > 2000) THEN 'LC'
-- QSFP28 (100G)
WHEN UPPER(form_factor) = 'QSFP28'
AND UPPER(fiber_type) = 'MMF' THEN 'MPO-12'
WHEN UPPER(form_factor) = 'QSFP28'
AND UPPER(fiber_type) = 'SMF'
AND reach_meters IS NOT NULL AND reach_meters <= 2000 THEN 'MPO-12'
WHEN UPPER(form_factor) = 'QSFP28'
AND UPPER(fiber_type) = 'SMF'
AND (reach_meters IS NULL OR reach_meters > 2000) THEN 'LC'
-- QSFP56 (200G)
WHEN UPPER(form_factor) = 'QSFP56'
AND UPPER(fiber_type) = 'MMF' THEN 'MPO-16'
WHEN UPPER(form_factor) = 'QSFP56'
AND UPPER(fiber_type) = 'SMF' THEN 'LC'
-- QSFP-DD / QSFP-DD800 (400G/800G)
WHEN UPPER(form_factor) IN ('QSFP-DD', 'QSFP-DD800')
AND UPPER(fiber_type) = 'MMF' THEN 'MPO-16'
WHEN UPPER(form_factor) IN ('QSFP-DD', 'QSFP-DD800')
AND UPPER(fiber_type) = 'SMF'
AND reach_meters IS NOT NULL AND reach_meters <= 2000 THEN 'MPO-12'
WHEN UPPER(form_factor) IN ('QSFP-DD', 'QSFP-DD800')
AND UPPER(fiber_type) = 'SMF'
AND (reach_meters IS NULL OR reach_meters > 2000) THEN 'LC'
-- OSFP (800G+)
WHEN UPPER(form_factor) = 'OSFP'
AND UPPER(fiber_type) = 'MMF' THEN 'MPO-16'
WHEN UPPER(form_factor) = 'OSFP'
AND UPPER(fiber_type) = 'SMF'
AND reach_meters IS NOT NULL AND reach_meters <= 2000 THEN 'MPO-12'
WHEN UPPER(form_factor) = 'OSFP'
AND UPPER(fiber_type) = 'SMF'
AND (reach_meters IS NULL OR reach_meters > 2000) THEN 'LC'
-- CFP/CFP2/CFP4 (100G coherent)
WHEN UPPER(form_factor) IN ('CFP', 'CFP2', 'CFP4') THEN 'LC'
ELSE NULL
END
WHERE connector_type IS NULL
AND form_factor IS NOT NULL
AND fiber_type IS NOT NULL;
-- ── Completeness neu berechnen ───────────────────────────────────────────────
UPDATE transceivers SET
data_completeness = calc_data_completeness(
form_factor, speed_gbps, fiber_type,
reach_meters, wavelength_tx_nm, connector_type
),
enrichment_needed = (
form_factor IS NULL OR speed_gbps IS NULL OR
fiber_type IS NULL OR reach_meters IS NULL OR
wavelength_tx_nm IS NULL OR connector_type IS NULL
),
enrichment_fields = ARRAY_REMOVE(ARRAY[
CASE WHEN form_factor IS NULL THEN 'form_factor' END,
CASE WHEN speed_gbps IS NULL THEN 'speed_gbps' END,
CASE WHEN fiber_type IS NULL THEN 'fiber_type' END,
CASE WHEN reach_meters IS NULL OR reach_meters = 0 THEN 'reach_meters' END,
CASE WHEN wavelength_tx_nm IS NULL THEN 'wavelength_tx_nm' END,
CASE WHEN connector_type IS NULL THEN 'connector_type' END
], NULL);
-- ── Statistik ────────────────────────────────────────────────────────────────
DO $$
DECLARE
total_cnt INTEGER;
complete_cnt INTEGER;
missing_conn INTEGER;
missing_wl INTEGER;
fx_complete INTEGER;
BEGIN
SELECT COUNT(*) INTO total_cnt FROM transceivers;
SELECT COUNT(*) INTO complete_cnt FROM transceivers WHERE enrichment_needed = FALSE;
SELECT COUNT(*) INTO missing_conn FROM transceivers WHERE connector_type IS NULL;
SELECT COUNT(*) INTO missing_wl FROM transceivers WHERE wavelength_tx_nm IS NULL;
SELECT COUNT(*) INTO fx_complete
FROM transceivers t JOIN vendors v ON v.id = t.vendor_id
WHERE UPPER(v.name) LIKE '%FLEXOPTIX%' AND enrichment_needed = FALSE;
RAISE NOTICE 'Migration 113 complete:';
RAISE NOTICE ' Total transceivers: %', total_cnt;
RAISE NOTICE ' Fully complete: %', complete_cnt;
RAISE NOTICE ' Still missing connector: %', missing_conn;
RAISE NOTICE ' Still missing wavelength: %', missing_wl;
RAISE NOTICE ' Flexoptix fully complete: %', fx_complete;
END $$;

View File

@ -0,0 +1,214 @@
-- Migration 114: Extend IEEE/MSA Lookup (400G/800G/1.6T) + Clear Pending Queue
-- Part A: Add missing 400G/800G/1.6T standards to ieee_wavelength_lookup
-- Part B: Wavelength fallback for products with known form/fiber/reach
-- Part C: Reject remaining pending records (replaced by deterministic matcher)
-- ── Part A: IEEE/MSA Lookup Erweiterung ─────────────────────────────────────
-- Sources: IEEE 802.3cd (200G), 802.3bs (400G), 802.3df (800G), 802.3dj (1.6T draft)
-- 400G-FR4 MSA, 400G-LR4-10 MSA, OSFP MSA, OpenZR+ MSA
INSERT INTO ieee_wavelength_lookup
(form_factor, speed_gbps, fiber_type, reach_min_m, reach_max_m, wavelength_tx_nm, wavelength_rx_nm, connector_type, ieee_standard, notes)
VALUES
-- ── QSFP+ 40G additional reaches ─────────────────────────────────────────────
('QSFP+', 40, 'SMF', 0, 150, 1310, NULL, 'MPO-12', '802.3ba', '40GBASE-PSM4 short'),
('QSFP+', 40, 'SMF', 0, 1400, 1310, NULL, 'LC', '802.3ba', '40GBASE-LR4 1.4km'),
-- ── QSFP28 100G additional ───────────────────────────────────────────────────
('QSFP28', 100, 'SMF', 0, 80000, 1550, NULL, 'LC', '802.3ba', '100GBASE-ZR4'),
('QSFP28', 100, 'SMF', 0, 120000, 1550, NULL, 'LC', 'OpenZR+', '100G OpenZR+ 120km'),
-- ── QSFP56 200G ──────────────────────────────────────────────────────────────
('QSFP56', 200, 'DAC', 0, 5, NULL, NULL, 'QSFP56','802.3cd', '200G DAC'),
('QSFP56', 200, 'AOC', 0, 100, 850, NULL, 'MPO-16','802.3cd', '200G AOC SR4'),
-- ── QSFP-DD 400G additional ──────────────────────────────────────────────────
('QSFP-DD', 400, 'MMF', 0, 100, 850, NULL, 'MPO-16','802.3bs', '400GBASE-SR8'),
('QSFP-DD', 400, 'SMF', 0, 500, 1310, NULL, 'MPO-12','802.3bs', '400GBASE-DR4'),
('QSFP-DD', 400, 'SMF', 0, 2000, 1310, NULL, 'LC', '802.3bs', '400GBASE-FR4'),
('QSFP-DD', 400, 'SMF', 0, 10000, 1310, NULL, 'LC', '802.3bs', '400GBASE-LR4'),
('QSFP-DD', 400, 'SMF', 0, 80000, 1550, NULL, 'LC', '400ZR-MSA','400G ZR 80km'),
('QSFP-DD', 400, 'SMF', 0, 120000, 1550, NULL, 'LC', 'OpenZR+', '400G OpenZR+ 120km'),
('QSFP-DD', 400, 'AOC', 0, 100, 850, NULL, 'MPO-16','802.3bs', '400G AOC SR8'),
-- ── QSFP-DD800 800G ──────────────────────────────────────────────────────────
('QSFP-DD800', 800, 'MMF', 0, 100, 850, NULL, 'MPO-16','802.3df', '800GBASE-SR8'),
('QSFP-DD800', 800, 'SMF', 0, 500, 1310, NULL, 'MPO-12','802.3df', '800GBASE-DR8'),
('QSFP-DD800', 800, 'SMF', 0, 2000, 1310, NULL, 'LC', '802.3df', '800GBASE-FR4 2x400G'),
('QSFP-DD800', 800, 'SMF', 0,10000, 1310, NULL, 'LC', '802.3df', '800GBASE-LR4'),
('QSFP-DD800', 800, 'SMF', 0,80000, 1550, NULL, 'LC', 'OpenZR+', '800G OpenZR+ 80km'),
('QSFP-DD800', 800, 'DAC', 0, 5, NULL, NULL, 'QSFP-DD800','802.3df','800G DAC'),
-- ── OSFP 400G ────────────────────────────────────────────────────────────────
('OSFP', 400, 'MMF', 0, 100, 850, NULL, 'MPO-16', 'OSFP-MSA', '400GBASE-SR8 OSFP'),
('OSFP', 400, 'SMF', 0, 500, 1310, NULL, 'MPO-12', 'OSFP-MSA', '400GBASE-DR4 OSFP'),
('OSFP', 400, 'SMF', 0, 2000, 1310, NULL, 'LC', 'OSFP-MSA', '400GBASE-FR4 OSFP'),
('OSFP', 400, 'SMF', 0, 10000, 1310, NULL, 'LC', 'OSFP-MSA', '400GBASE-LR4 OSFP'),
('OSFP', 400, 'SMF', 0, 80000, 1550, NULL, 'LC', 'OpenZR+', '400G ZR OSFP 80km'),
('OSFP', 400, 'SMF', 0, 120000, 1550, NULL, 'LC', 'OpenZR+', '400G OpenZR+ OSFP 120km'),
-- ── OSFP 800G ────────────────────────────────────────────────────────────────
('OSFP', 800, 'MMF', 0, 30, 850, NULL, 'MPO-16', '802.3df', '800GBASE-SR8 30m'),
('OSFP', 800, 'MMF', 0, 100, 850, NULL, 'MPO-16', '802.3df', '800GBASE-SR8'),
('OSFP', 800, 'SMF', 0, 500, 1310, NULL, 'MPO-12', '802.3df', '800GBASE-DR8 OSFP'),
('OSFP', 800, 'SMF', 0, 2000, 1310, NULL, 'LC', '802.3df', '800GBASE-FR4 OSFP'),
('OSFP', 800, 'SMF', 0, 10000, 1310, NULL, 'LC', '802.3df', '800GBASE-LR4 OSFP'),
('OSFP', 800, 'SMF', 0, 80000, 1550, NULL, 'LC', 'OpenZR+', '800G ZR OSFP 80km'),
-- ── OSFP 1.6T (IEEE 802.3dj draft) ──────────────────────────────────────────
('OSFP', 1600, 'SMF', 0, 500, 1310, NULL, 'MPO-16', '802.3dj', '1.6TBASE-DR16 OSFP'),
('OSFP', 1600, 'SMF', 0, 2000, 1310, NULL, 'LC', '802.3dj', '1.6TBASE-FR4 OSFP'),
('OSFP', 1600, 'SMF', 0, 10000, 1310, NULL, 'LC', '802.3dj', '1.6TBASE-LR4 OSFP'),
('OSFP112', 800, 'SMF', 0, 10000, 1310, NULL, 'LC', '802.3df', '800GBASE-LR4 OSFP112'),
('OSFP112', 800, 'SMF', 0, 80000, 1550, NULL, 'LC', 'OpenZR+', '800G ZR OSFP112 80km'),
('OSFP112', 800, 'SMF', 0, 120000,1550, NULL, 'LC', 'OpenZR+', '800G OpenZR+ OSFP112 120km'),
-- ── CFP2 100G coherent ───────────────────────────────────────────────────────
('CFP2', 100, 'SMF', 0, 10000, 1310, NULL, 'LC', 'OIF-100G', '100GBASE-LR4 CFP2'),
('CFP2', 100, 'SMF', 0, 80000, 1550, NULL, 'LC', 'OIF-100G', '100G ZR CFP2 80km'),
('CFP2', 100, 'SMF', 0, 120000, 1550, NULL, 'LC', 'OpenZR+', '100G OpenZR+ CFP2'),
-- ── SFP+ / SFP 1G non-standard reaches ───────────────────────────────────────
('SFP', 1, 'SMF', 0, 20000, 1310, NULL, 'LC', '802.3z', '1000BASE-LH 20km'),
('SFP', 1, 'SMF', 0, 60000, 1310, NULL, 'LC', '802.3z', '1000BASE-LH 60km'),
('SFP', 1, 'SMF', 0, 80000, 1550, NULL, 'LC', '802.3z', '1000BASE-ZX 80km'),
('SFP', 1, 'SMF', 0,100000, 1550, NULL, 'LC', '802.3z', '1000BASE-ZX 100km'),
-- ── SFP+ 10G non-standard reaches ────────────────────────────────────────────
('SFP+', 10, 'SMF', 0, 20000, 1310, NULL, 'LC', '802.3ae', '10GBASE-LR 20km variant'),
('SFP+', 10, 'SMF', 0, 60000, 1550, NULL, 'LC', '802.3ae', '10GBASE-ZR 60km'),
('SFP+', 10, 'SMF', 0, 80000, 1550, NULL, 'LC', '802.3ae', '10GBASE-ZR 80km'),
('SFP+', 10, 'SMF', 0,100000, 1550, NULL, 'LC', '802.3ae', '10GBASE-ZR 100km'),
-- ── XFP 10G ──────────────────────────────────────────────────────────────────
('XFP', 10, 'MMF', 0, 300, 850, NULL, 'LC', '802.3ae', '10GBASE-SR XFP'),
('XFP', 10, 'SMF', 0, 10000, 1310, NULL, 'LC', '802.3ae', '10GBASE-LR XFP'),
('XFP', 10, 'SMF', 0, 40000, 1310, NULL, 'LC', '802.3ae', '10GBASE-ER XFP'),
('XFP', 10, 'SMF', 0, 80000, 1550, NULL, 'LC', '802.3ae', '10GBASE-ZR XFP')
ON CONFLICT DO NOTHING;
-- ── Re-run IEEE lookup for wavelength after new entries ──────────────────────
UPDATE transceivers t SET
wavelength_tx_nm = (
SELECT il.wavelength_tx_nm
FROM ieee_wavelength_lookup il
WHERE UPPER(il.form_factor) = UPPER(t.form_factor)
AND il.speed_gbps = ROUND(t.speed_gbps::NUMERIC, 2)
AND UPPER(il.fiber_type) = UPPER(t.fiber_type)
AND il.reach_min_m <= t.reach_meters
AND il.reach_max_m >= t.reach_meters
AND il.wavelength_tx_nm IS NOT NULL
ORDER BY il.reach_max_m ASC
LIMIT 1
),
wavelength_rx_nm = COALESCE(
wavelength_rx_nm,
(
SELECT il.wavelength_rx_nm
FROM ieee_wavelength_lookup il
WHERE UPPER(il.form_factor) = UPPER(t.form_factor)
AND il.speed_gbps = ROUND(t.speed_gbps::NUMERIC, 2)
AND UPPER(il.fiber_type) = UPPER(t.fiber_type)
AND il.reach_min_m <= t.reach_meters
AND il.reach_max_m >= t.reach_meters
ORDER BY il.reach_max_m ASC
LIMIT 1
)
),
connector_type = COALESCE(
connector_type,
(
SELECT il.connector_type
FROM ieee_wavelength_lookup il
WHERE UPPER(il.form_factor) = UPPER(t.form_factor)
AND il.speed_gbps = ROUND(t.speed_gbps::NUMERIC, 2)
AND UPPER(il.fiber_type) = UPPER(t.fiber_type)
AND il.reach_min_m <= t.reach_meters
AND il.reach_max_m >= t.reach_meters
ORDER BY il.reach_max_m ASC
LIMIT 1
)
)
WHERE t.wavelength_tx_nm IS NULL
AND t.form_factor IS NOT NULL
AND t.speed_gbps IS NOT NULL
AND t.fiber_type IS NOT NULL
AND t.fiber_type NOT IN ('Copper', 'DAC', 'AOC', 'COPPER')
AND t.reach_meters IS NOT NULL
AND t.reach_meters > 0;
-- ── Part B: Fallback wavelength by fiber_type for remaining ──────────────────
-- Conservative rule: SMF products with reach > 80km → 1550nm (ZR/coherent)
-- All other SMF → 1310nm (covers ER/LR/DR/FR/LH etc.)
-- All MMF → 850nm (SR variants)
-- Products with DAC fiber_type: no optical wavelength (leave NULL)
UPDATE transceivers SET
wavelength_tx_nm = CASE
-- Long-reach SMF: reach > 80km → 1550nm (ZR, coherent)
WHEN UPPER(fiber_type) = 'SMF' AND reach_meters > 80000 THEN 1550
-- Standard SMF: 1310nm (LR/ER/DR/FR/LH etc.)
WHEN UPPER(fiber_type) = 'SMF' AND reach_meters > 0 THEN 1310
-- Short MMF: 850nm (SR variants)
WHEN UPPER(fiber_type) = 'MMF' AND reach_meters > 0 THEN 850
ELSE wavelength_tx_nm
END
WHERE wavelength_tx_nm IS NULL
AND fiber_type IS NOT NULL
AND UPPER(fiber_type) IN ('SMF', 'MMF')
AND reach_meters IS NOT NULL
AND reach_meters > 0
AND form_factor IS NOT NULL
AND UPPER(form_factor) NOT IN ('LC', 'SC', 'DAC', 'TRANSCEIVER', 'PLUGGABLE', 'VARIES');
-- ── Completeness final update ─────────────────────────────────────────────────
UPDATE transceivers SET
data_completeness = calc_data_completeness(
form_factor, speed_gbps, fiber_type,
reach_meters, wavelength_tx_nm, connector_type
),
enrichment_needed = (
form_factor IS NULL OR speed_gbps IS NULL OR
fiber_type IS NULL OR reach_meters IS NULL OR
wavelength_tx_nm IS NULL OR connector_type IS NULL
),
enrichment_fields = ARRAY_REMOVE(ARRAY[
CASE WHEN form_factor IS NULL THEN 'form_factor' END,
CASE WHEN speed_gbps IS NULL THEN 'speed_gbps' END,
CASE WHEN fiber_type IS NULL THEN 'fiber_type' END,
CASE WHEN reach_meters IS NULL OR reach_meters = 0 THEN 'reach_meters' END,
CASE WHEN wavelength_tx_nm IS NULL THEN 'wavelength_tx_nm' END,
CASE WHEN connector_type IS NULL THEN 'connector_type' END
], NULL);
-- ── Part C: Clear pending queue ───────────────────────────────────────────────
-- All pending records from confidence-based matcher are superseded.
-- Deterministic matcher (maintenance:find-equivalences) will re-generate
-- correct matches at confidence=1.0 for products with complete data.
UPDATE transceiver_equivalences
SET status = 'rejected',
reject_reason = 'Superseded by deterministic matcher — confidence-based pending removed in migration 114',
reviewed_at = NOW(),
reviewed_by = 'system:migration-114'
WHERE status = 'pending';
-- ── Final Statistics ─────────────────────────────────────────────────────────
DO $$
DECLARE
total_cnt INTEGER;
complete_cnt INTEGER;
missing_conn INTEGER;
missing_wl INTEGER;
fx_complete INTEGER;
fx_total INTEGER;
pending_cnt INTEGER;
BEGIN
SELECT COUNT(*) INTO total_cnt FROM transceivers;
SELECT COUNT(*) INTO complete_cnt FROM transceivers WHERE enrichment_needed = FALSE;
SELECT COUNT(*) INTO missing_conn FROM transceivers WHERE connector_type IS NULL;
SELECT COUNT(*) INTO missing_wl FROM transceivers WHERE wavelength_tx_nm IS NULL;
SELECT COUNT(*) INTO pending_cnt FROM transceiver_equivalences WHERE status = 'pending';
SELECT COUNT(*) INTO fx_total
FROM transceivers t JOIN vendors v ON v.id = t.vendor_id
WHERE UPPER(v.name) LIKE '%FLEXOPTIX%';
SELECT COUNT(*) INTO fx_complete
FROM transceivers t JOIN vendors v ON v.id = t.vendor_id
WHERE UPPER(v.name) LIKE '%FLEXOPTIX%' AND enrichment_needed = FALSE;
RAISE NOTICE 'Migration 114 complete:';
RAISE NOTICE ' Total transceivers: %', total_cnt;
RAISE NOTICE ' Fully complete: %', complete_cnt;
RAISE NOTICE ' Still missing connector: %', missing_conn;
RAISE NOTICE ' Still missing wavelength: %', missing_wl;
RAISE NOTICE ' Flexoptix fully complete: % / %', fx_complete, fx_total;
RAISE NOTICE ' Pending queue: % (target: 0)', pending_cnt;
END $$;

View File

@ -0,0 +1,74 @@
-- Migration 115: Flexoptix Product Detail Columns
-- Adds columns to store full product detail data from the Flexoptix API
-- (specifications array, compatibility matrix, laser type, receiver type, etc.)
-- so we can build rich datasheets and deepen the TIP comparison data.
-- ── New columns ──────────────────────────────────────────────────────────────
-- Raw specs blob: full [{label, value}, ...] array from API (specifications=1)
-- Useful for datasheet generation and ad-hoc queries without re-fetching
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS fx_specifications JSONB;
-- Full compatibility list from API: [{sku, compatible_to_vendor, original_part_number}, ...]
-- More granular than vendor_compat (which has pattern-based matching)
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS fx_compatibilities JSONB;
-- Structured spec fields parsed from fx_specifications
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS compliance_code TEXT; -- "LX SGMII", "SR4 100GBASE", "LR4", etc.
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS laser_type TEXT; -- "FP", "DFB", "VCSEL", "EML", "CW-SiPh"
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS receiver_type TEXT; -- "PIN", "APD", "Coherent"
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS supported_protocols TEXT[]; -- ["1GigE", "Fast Ethernet", "10GBase-SR", ...]
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS extinction_ratio_db NUMERIC(6,2); -- dB
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS cdr_support BOOLEAN; -- false = "none", true = integrated CDR
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS inbuilt_fec BOOLEAN; -- false = "No", true = integrated FEC
-- Tracking: when the full per-SKU detail sync last completed for this product
ALTER TABLE transceivers
ADD COLUMN IF NOT EXISTS detail_synced_at TIMESTAMPTZ;
-- ── Indexes ──────────────────────────────────────────────────────────────────
-- GIN index for JSONB compatibility search (e.g. "which FX products are
-- compatible with Cisco Nexus 9000 where OPN starts with N9K-?")
CREATE INDEX IF NOT EXISTS idx_transceivers_fx_compatibilities
ON transceivers USING GIN (fx_compatibilities)
WHERE fx_compatibilities IS NOT NULL;
-- Index for detail sync queue (find unseen or stale products quickly)
-- NB: partial index with NOW() is not allowed (non-immutable); use plain index instead
CREATE INDEX IF NOT EXISTS idx_transceivers_detail_synced_at
ON transceivers (detail_synced_at NULLS FIRST);
-- ── Statistics ───────────────────────────────────────────────────────────────
DO $$
DECLARE
fx_cnt INTEGER;
BEGIN
SELECT COUNT(*) INTO fx_cnt
FROM transceivers t
JOIN vendors v ON v.id = t.vendor_id
WHERE UPPER(v.name) LIKE '%FLEXOPTIX%';
RAISE NOTICE 'Migration 115 complete.';
RAISE NOTICE ' Total FX products: %', fx_cnt;
RAISE NOTICE ' New columns added: fx_specifications, fx_compatibilities,';
RAISE NOTICE ' compliance_code, laser_type, receiver_type,';
RAISE NOTICE ' supported_protocols, extinction_ratio_db,';
RAISE NOTICE ' cdr_support, inbuilt_fec, detail_synced_at';
RAISE NOTICE ' Run enrich:flexoptix-details to populate.';
END $$;

View File

@ -0,0 +1,85 @@
-- Migration 116: OPN-Based Equivalence Matcher
-- Uses the manufacturer-provided compatibility matrix (fx_compatibilities) to
-- create high-confidence equivalences between Flexoptix products and their
-- exact OEM counterparts in competitor catalogs.
--
-- Source of truth: FX API `fx_compatibilities` field — the vendor explicitly
-- states "this FX product replaces [vendor] [part_number]".
--
-- Match quality: confidence=1.0, match_basis='{opn}' (OEM Part Number)
-- These are better than spec-based matches because they are manufacturer-confirmed.
--
-- Rules:
-- - Only inserts NEW pairs (skips existing approved, auto_approved, rejected)
-- - Skips MSA Standard and Flexoptix entries (not real competitors)
-- - Case-insensitive part_number match
-- - Target must be a competitor vendor (is_competitor = true)
-- ── Insert new OPN-based equivalences ────────────────────────────────────────
INSERT INTO transceiver_equivalences (
flexoptix_id,
competitor_id,
confidence,
status,
match_basis,
match_notes,
created_at,
updated_at
)
SELECT DISTINCT
fx.id AS flexoptix_id,
comp.id AS competitor_id,
1.0 AS confidence,
'auto_approved' AS status,
ARRAY['opn'] AS match_basis,
'Manufacturer-confirmed: FX compatibility matrix lists ' ||
COALESCE(compat->>'compatible_to_vendor', '?') || ' OPN ' ||
COALESCE(compat->>'original_part_number', '?') AS match_notes,
NOW() AS created_at,
NOW() AS updated_at
FROM transceivers fx
JOIN vendors vfx ON vfx.id = fx.vendor_id AND UPPER(vfx.name) LIKE '%FLEXOPTIX%'
CROSS JOIN LATERAL jsonb_array_elements(fx.fx_compatibilities) AS compat
JOIN transceivers comp
ON UPPER(comp.part_number) = UPPER(compat->>'original_part_number')
JOIN vendors vcomp ON vcomp.id = comp.vendor_id AND vcomp.is_competitor = true
WHERE fx.fx_compatibilities IS NOT NULL
AND compat->>'original_part_number' IS NOT NULL
AND length(trim(compat->>'original_part_number')) >= 4 -- ignore very short/empty OPNs
AND compat->>'compatible_to_vendor' NOT IN ('MSA Standard (Default)', 'Flexoptix')
-- Skip pairs that already have ANY equivalence (approved, auto_approved, rejected)
AND NOT EXISTS (
SELECT 1
FROM transceiver_equivalences e
WHERE e.flexoptix_id = fx.id
AND e.competitor_id = comp.id
)
ON CONFLICT DO NOTHING;
-- ── Statistics ────────────────────────────────────────────────────────────────
DO $$
DECLARE
new_cnt INTEGER;
fx_covered INTEGER;
comp_covered INTEGER;
total_approved INTEGER;
BEGIN
SELECT COUNT(*) INTO new_cnt
FROM transceiver_equivalences WHERE 'opn' = ANY(match_basis);
SELECT COUNT(DISTINCT flexoptix_id) INTO fx_covered
FROM transceiver_equivalences WHERE 'opn' = ANY(match_basis);
SELECT COUNT(DISTINCT competitor_id) INTO comp_covered
FROM transceiver_equivalences WHERE 'opn' = ANY(match_basis);
SELECT COUNT(*) INTO total_approved
FROM transceiver_equivalences WHERE status = 'auto_approved';
RAISE NOTICE 'Migration 116 complete: OPN-Based Equivalence Matcher';
RAISE NOTICE ' New OPN equivalences inserted: %', new_cnt;
RAISE NOTICE ' FX products covered: %', fx_covered;
RAISE NOTICE ' Competitor products matched: %', comp_covered;
RAISE NOTICE ' Total auto_approved: %', total_approved;
END $$;

View File

@ -0,0 +1,139 @@
-- Migration 117: Spec-Based Equivalence Matcher
-- Matches FX products with competitor products by technical specification
-- when no OPN-based equivalence already exists.
--
-- Match criteria (ALL must apply):
-- 1. Same form_factor (exact)
-- 2. Same speed_gbps (exact)
-- 3. Same reach tier (SR/IR/LR/ER/ZR — based on reach_meters)
-- 4. Same primary wavelength (within ±10nm, extracted from wavelengths field)
-- OR both have no wavelength data (broadband / non-WDM products)
-- 5. Target must be a competitor vendor (is_competitor = true)
-- 6. Max 30 competitor matches per FX product (too many = too generic)
--
-- Match quality:
-- confidence = 0.85 (high but below OPN-confirmed 1.0)
-- match_basis = '{spec}'
-- status = 'auto_approved'
--
-- Rules:
-- - Skips pairs that already have ANY equivalence (approved, auto_approved, rejected)
-- - Skips FX products that already have an OPN-based equivalence
-- (OPN match is preferred; spec is only a fallback)
-- - Minimum reach_meters = 10 on both sides (avoids reach=0 garbage data)
-- - Reach tier comparison handles DAC/AOC (SR ≤ 300m)
-- ── Helper: extract primary wavelength in nm from text field ─────────────────
-- Handles: "1310nm", "850nm", "1310/1550nm", "1270nm-1610nm", NULL
CREATE OR REPLACE FUNCTION tip_extract_wavelength_nm(wl text)
RETURNS integer LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $$
SELECT (regexp_match(wl, '(\d{3,4})\s*nm'))[1]::integer
$$;
-- ── Helper: reach tier label ─────────────────────────────────────────────────
CREATE OR REPLACE FUNCTION tip_reach_tier(reach integer)
RETURNS text LANGUAGE sql IMMUTABLE PARALLEL SAFE AS $$
SELECT CASE
WHEN reach <= 300 THEN 'SR' -- ≤300m (SR, VSR, DAC, AOC)
WHEN reach <= 2000 THEN 'IR' -- ≤2km (LX, LH intermediate)
WHEN reach <= 10000 THEN 'LR' -- ≤10km (LR, LX, standard LH)
WHEN reach <= 40000 THEN 'ER' -- ≤40km (ER, extended reach)
ELSE 'ZR' -- >40km (ZR, ZR+, coherent)
END
$$;
-- ── Insert spec-based equivalences ──────────────────────────────────────────
INSERT INTO transceiver_equivalences (
flexoptix_id,
competitor_id,
confidence,
status,
match_basis,
match_notes,
created_at,
updated_at
)
SELECT DISTINCT
fx.id AS flexoptix_id,
comp.id AS competitor_id,
0.85 AS confidence,
'auto_approved' AS status,
ARRAY['spec'] AS match_basis,
'Spec match: ' || fx.form_factor || ' ' || fx.speed_gbps || 'G ' ||
tip_reach_tier(fx.reach_meters) ||
CASE WHEN tip_extract_wavelength_nm(fx.wavelengths) IS NOT NULL
THEN ' @' || tip_extract_wavelength_nm(fx.wavelengths) || 'nm'
ELSE '' END AS match_notes,
NOW() AS created_at,
NOW() AS updated_at
FROM transceivers fx
JOIN vendors vfx ON vfx.id = fx.vendor_id AND UPPER(vfx.name) LIKE '%FLEXOPTIX%'
JOIN transceivers comp
ON comp.form_factor = fx.form_factor
AND comp.speed_gbps = fx.speed_gbps
AND comp.reach_meters >= 10 -- no garbage reach=0
AND tip_reach_tier(comp.reach_meters) = tip_reach_tier(fx.reach_meters)
-- Wavelength: both must match within ±10nm, OR both have no wavelength
AND (
(tip_extract_wavelength_nm(fx.wavelengths) IS NULL
AND tip_extract_wavelength_nm(comp.wavelengths) IS NULL)
OR
ABS( COALESCE(tip_extract_wavelength_nm(comp.wavelengths), 0)
- COALESCE(tip_extract_wavelength_nm(fx.wavelengths), 0) ) <= 10
)
JOIN vendors vcomp ON vcomp.id = comp.vendor_id AND vcomp.is_competitor = true
WHERE fx.reach_meters >= 10 -- no garbage reach=0 on FX side
AND fx.speed_gbps > 0
-- FX product has no OPN-based equivalence at all (spec is fallback only)
AND NOT EXISTS (
SELECT 1 FROM transceiver_equivalences e
WHERE e.flexoptix_id = fx.id
AND 'opn' = ANY(e.match_basis)
)
-- Skip pairs that already have ANY equivalence
AND NOT EXISTS (
SELECT 1 FROM transceiver_equivalences e
WHERE e.flexoptix_id = fx.id
AND e.competitor_id = comp.id
)
-- Safety cap: skip FX product if it would match > 30 competitors
-- (indicates too-generic spec — needs stricter criteria)
AND (
SELECT COUNT(DISTINCT c2.id)
FROM transceivers c2
JOIN vendors vc2 ON vc2.id = c2.vendor_id AND vc2.is_competitor = true
WHERE c2.form_factor = fx.form_factor
AND c2.speed_gbps = fx.speed_gbps
AND c2.reach_meters >= 10
AND tip_reach_tier(c2.reach_meters) = tip_reach_tier(fx.reach_meters)
AND (
(tip_extract_wavelength_nm(fx.wavelengths) IS NULL
AND tip_extract_wavelength_nm(c2.wavelengths) IS NULL)
OR ABS( COALESCE(tip_extract_wavelength_nm(c2.wavelengths), 0)
- COALESCE(tip_extract_wavelength_nm(fx.wavelengths), 0) ) <= 10
)
) <= 30
ON CONFLICT DO NOTHING;
-- ── Statistics ───────────────────────────────────────────────────────────────
DO $$
DECLARE
new_cnt INTEGER;
fx_covered INTEGER;
comp_covered INTEGER;
BEGIN
SELECT COUNT(*) INTO new_cnt
FROM transceiver_equivalences WHERE 'spec' = ANY(match_basis);
SELECT COUNT(DISTINCT flexoptix_id) INTO fx_covered
FROM transceiver_equivalences WHERE 'spec' = ANY(match_basis);
SELECT COUNT(DISTINCT competitor_id) INTO comp_covered
FROM transceiver_equivalences WHERE 'spec' = ANY(match_basis);
RAISE NOTICE 'Migration 117 complete: Spec-Based Equivalence Matcher';
RAISE NOTICE ' Spec equivalences total: %', new_cnt;
RAISE NOTICE ' FX products newly covered: %', fx_covered;
RAISE NOTICE ' Competitor products matched: %', comp_covered;
END $$;

View File

@ -0,0 +1,84 @@
-- ══════════════════════════════════════════════════════════════════════════════
-- 118 — Stock Velocity & Sell-Through Analysis
--
-- Evaluates implied Abverkauf (sell-through) from time-series stock_observations:
-- • Negative stock delta → implied units sold (sell event)
-- • Positive stock delta after backorder → Zulauf (incoming replenishment)
-- • FS.com units_sold counter delta → high-confidence sell signal
--
-- Stores per-product velocity results in stock_velocity for API / dashboard use.
-- ══════════════════════════════════════════════════════════════════════════════
-- ── Main results table ────────────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS stock_velocity (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
transceiver_id UUID NOT NULL REFERENCES transceivers(id) ON DELETE CASCADE,
vendor_id UUID NOT NULL REFERENCES vendors(id) ON DELETE CASCADE,
computed_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
-- Observation window
window_start TIMESTAMPTZ NOT NULL,
window_end TIMESTAMPTZ NOT NULL,
obs_count INTEGER NOT NULL,
-- Sell-through metrics
avg_daily_sell_rate NUMERIC(12, 2), -- units/day (implied)
peak_daily_sell_rate NUMERIC(12, 2), -- highest single-interval rate
total_sell_events INTEGER DEFAULT 0,
total_units_sold_implied INTEGER DEFAULT 0,
-- FS.com direct counter (more reliable when available)
units_sold_counter_delta BIGINT, -- delta in FS.com units_sold between first/last obs
units_sold_daily_rate NUMERIC(12, 2), -- counter_delta / window_days
-- Zulauf (incoming stock / replenishment)
total_zulauf_events INTEGER DEFAULT 0,
total_units_zulauf INTEGER DEFAULT 0,
last_zulauf_at TIMESTAMPTZ,
next_expected_delivery DATE, -- backorder_estimated_date from latest obs
-- Current stock state (from latest observation)
current_qty INTEGER,
current_backorder_qty INTEGER,
current_price_net NUMERIC(10, 2),
-- Sell-through prediction
estimated_stockout_days NUMERIC(8, 1), -- NULL if no velocity or stock = 0
estimated_stockout_date DATE,
-- Signal quality
velocity_confidence TEXT CHECK (velocity_confidence IN ('high', 'medium', 'low', 'insufficient')),
-- high = ≥14 observations with meaningful deltas
-- medium = ≥5 observations
-- low = 24 observations
-- insufficient = only 1 observation or no change detected
UNIQUE (transceiver_id, vendor_id)
);
CREATE INDEX IF NOT EXISTS idx_stock_velocity_vendor ON stock_velocity (vendor_id);
CREATE INDEX IF NOT EXISTS idx_stock_velocity_computed ON stock_velocity (computed_at);
CREATE INDEX IF NOT EXISTS idx_stock_velocity_stockout ON stock_velocity (estimated_stockout_date)
WHERE estimated_stockout_date IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_stock_velocity_confidence ON stock_velocity (velocity_confidence);
COMMENT ON TABLE stock_velocity IS
'Computed sell-through velocity per transceiver per vendor, derived from '
'time-series stock_observations. Refreshed by analyze:stock:velocity job.';
-- ── Sell event log (raw events for trend analysis) ────────────────────────────
CREATE TABLE IF NOT EXISTS stock_velocity_events (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
transceiver_id UUID NOT NULL REFERENCES transceivers(id) ON DELETE CASCADE,
vendor_id UUID NOT NULL REFERENCES vendors(id) ON DELETE CASCADE,
event_at TIMESTAMPTZ NOT NULL,
event_type TEXT NOT NULL CHECK (event_type IN ('sold', 'zulauf', 'unchanged', 'data_gap')),
units_delta INTEGER, -- negative = sold, positive = arrived
daily_rate NUMERIC(10, 2), -- implied rate for this interval
qty_before INTEGER,
qty_after INTEGER,
hours_elapsed NUMERIC(8, 2)
);
CREATE INDEX IF NOT EXISTS idx_velocity_events_tx ON stock_velocity_events (transceiver_id, vendor_id, event_at);
CREATE INDEX IF NOT EXISTS idx_velocity_events_type ON stock_velocity_events (event_type, event_at);

File diff suppressed because one or more lines are too long