Files
worldmonitor/server/worldmonitor/trade/v1/get-tariff-trends.ts
Fayez Bast cf1fdefe92 feat: effective tariff rate source (#1790)
* feat: effective tariff rate source

* fix(trade): extract parse helpers, fix tests, add health monitoring

- Extract htmlToPlainText/toIsoDate/parseBudgetLabEffectiveTariffHtml
  to scripts/_trade-parse-utils.mjs so tests can import directly
- Fix toIsoDate to use month-name lookup instead of fragile
  new Date(\`\${text} UTC\`) which is not spec-guaranteed
- Replace new Function() test reconstruction with direct ESM import
- Add test fixtures for parser patterns 2 and 3 (previously untested)
- Add tariffTrendsUs to health.js STANDALONE_KEYS + SEED_META
  (key trade:tariffs:v1:840:all:10, maxStaleMin 900 = 2.5x the 6h TTL)

* fix(test): update sourceVersion assertion for budgetlab addition

---------

Co-authored-by: Elie Habib <elie.habib@gmail.com>
2026-03-19 03:45:32 +04:00

37 lines
1.3 KiB
TypeScript

/**
* RPC: getTariffTrends -- reads seeded WTO MFN tariff trends from Railway seed cache.
* The seed payload may also include an optional US effective tariff snapshot.
*/
import type {
ServerContext,
GetTariffTrendsRequest,
GetTariffTrendsResponse,
} from '../../../../src/generated/server/worldmonitor/trade/v1/service_server';
import { getCachedJson } from '../../../_shared/redis';
const SEED_KEY_PREFIX = 'trade:tariffs:v1';
function isValidCode(c: string): boolean {
return /^[a-zA-Z0-9]{1,10}$/.test(c);
}
export async function getTariffTrends(
_ctx: ServerContext,
req: GetTariffTrendsRequest,
): Promise<GetTariffTrendsResponse> {
try {
const reporter = isValidCode(req.reportingCountry) ? req.reportingCountry : '840';
const productSector = isValidCode(req.productSector) ? req.productSector : '';
const years = Math.max(1, Math.min(req.years > 0 ? req.years : 10, 30));
const seedKey = `${SEED_KEY_PREFIX}:${reporter}:${productSector || 'all'}:${years}`;
const result = await getCachedJson(seedKey, true) as GetTariffTrendsResponse | null;
if (!result?.datapoints?.length) {
return { datapoints: [], fetchedAt: new Date().toISOString(), upstreamUnavailable: true };
}
return result;
} catch {
return { datapoints: [], fetchedAt: new Date().toISOString(), upstreamUnavailable: true };
}
}