Ctrl K
ComunidadGuías…

scripts/_china-macro-contract.mjs

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
// Railway builds macro/health seeders with rootDirectory=scripts, so this
// runtime-safe mirror must stay inside scripts/. Keep it in parity with
// shared/china-macro-contract.js; tests/china-macro-production-registration
// pins every constant and helper across the container boundary.

export const CHINA_MACRO_SCHEMA_VERSION = 2;
export const CHINA_MACRO_CACHE_KEY = 'economic:china:macro:v2';
export const CHINA_MACRO_PROVENANCE_FAMILY = 'china_macro_official_numeric_observation';
export const CHINA_MACRO_MAX_TRANSPORT_AGE_MIN = 3 * 24 * 60;
// 60 days, not 45. NBS publishes monthly with a roughly two-week lag, so the
// newest official observation is a month-END that ages until the next release:
// measured 2026-08-17 it stood at 2026-06-30, 47.2 days old, and the natural
// peak just before a release is ~51 days. A 45-day budget therefore sits BELOW
// the cycle's own maximum and reports CHINA_DEGRADED every month on a publisher
// behaving normally. 60 clears the peak and still leaves a genuinely skipped
// release visible (#6799).
export const CHINA_MACRO_MAX_CONTENT_AGE_MIN = 60 * 24 * 60;

export const CHINA_MACRO_REQUIRED_SERIES = Object.freeze([
  'nbs_industrial_value_added_yoy',
  'nbs_fixed_asset_investment_yoy',
  'nbs_real_estate_investment_yoy',
  'safe_fx_reserves',
  'safe_bank_fx_settlement',
]);

export const CHINA_MACRO_SERIES_MAX_AGE_DAYS = Object.freeze({
  nbs_industrial_value_added_yoy: 75,
  nbs_fixed_asset_investment_yoy: 75,
  nbs_real_estate_investment_yoy: 75,
  safe_fx_reserves: 45,
  // SAFE releases month M's settlement data 13-28 days after month end, so
  // month M must stay usable through all of month M+1 plus that lag. From
  // 2022 to 2026 the oldest it reached before its successor landed was 58
  // days (August 2022, superseded 2022-10-28). 45 rejected the whole snapshot
  // for days in most months, e.g. 2026-10-15 to 2026-10-20.
  safe_bank_fx_settlement: 60,
  // June's month-end had to last until the July report on 2026-08-17 (48
  // days), plus the macro worker's 36h interval. 50 days covers that cycle.
  pboc_m2_yoy: 50,
});

export const CHINA_MACRO_PUBLISHER_IDS = Object.freeze({
  nbs: 'publisher:nbs-cn',
  pboc: 'publisher:pboc-cn',
  safe: 'publisher:safe-cn',
  gacc: 'publisher:gacc-cn',
});

export const CHINA_MACRO_SERIES_CONTRACT = Object.freeze({
  nbs_industrial_value_added_yoy: Object.freeze({
    pillar: 'activity', unit: '%', periodKind: 'month',
    source: 'National Bureau of Statistics of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.nbs,
    sourceHost: 'www.stats.gov.cn', sourcePathPrefix: '/english/PressRelease/',
  }),
  nbs_fixed_asset_investment_yoy: Object.freeze({
    pillar: 'investment_property', unit: '%', periodKind: 'cumulative_year',
    source: 'National Bureau of Statistics of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.nbs,
    sourceHost: 'www.stats.gov.cn', sourcePathPrefix: '/english/PressRelease/',
  }),
  nbs_real_estate_investment_yoy: Object.freeze({
    pillar: 'investment_property', unit: '%', periodKind: 'cumulative_year',
    source: 'National Bureau of Statistics of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.nbs,
    sourceHost: 'www.stats.gov.cn', sourcePathPrefix: '/english/PressRelease/',
  }),
  safe_fx_reserves: Object.freeze({
    pillar: 'external_pressure', unit: 'USD 100 million', periodKind: 'point_in_time',
    source: 'State Administration of Foreign Exchange', publisherId: CHINA_MACRO_PUBLISHER_IDS.safe,
    sourceHost: 'www.safe.gov.cn', sourcePathPrefix: '/safe/',
  }),
  safe_bank_fx_settlement: Object.freeze({
    pillar: 'external_pressure', unit: 'CNY 100 million', periodKind: 'month',
    source: 'State Administration of Foreign Exchange', publisherId: CHINA_MACRO_PUBLISHER_IDS.safe,
    sourceHost: 'www.safe.gov.cn', sourcePathPrefix: '/safe/',
  }),
  pboc_aggregate_financing_flow: Object.freeze({
    pillar: 'credit_liquidity', unit: 'CNY 100 million', periodKind: 'month',
    source: 'People’s Bank of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.pboc,
    sourceHost: 'www.pbc.gov.cn', sourcePathPrefix: '/',
  }),
  pboc_new_rmb_loans: Object.freeze({
    pillar: 'credit_liquidity', unit: 'CNY 100 million', periodKind: 'month',
    source: 'People’s Bank of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.pboc,
    sourceHost: 'www.pbc.gov.cn', sourcePathPrefix: '/',
  }),
  pboc_m2_yoy: Object.freeze({
    pillar: 'credit_liquidity', unit: '%', periodKind: 'month',
    source: 'People’s Bank of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.pboc,
    sourceHost: 'www.pbc.gov.cn', sourcePathPrefix: '/',
  }),
  pboc_policy_liquidity_operation: Object.freeze({
    pillar: 'credit_liquidity', unit: 'CNY 100 million', periodKind: 'point_in_time',
    source: 'People’s Bank of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.pboc,
    sourceHost: 'www.pbc.gov.cn', sourcePathPrefix: '/',
  }),
  gacc_exports: Object.freeze({
    pillar: 'trade', unit: 'USD million', periodKind: 'month',
    source: 'General Administration of Customs of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.gacc,
    sourceHost: 'english.customs.gov.cn', sourcePathPrefix: '/',
  }),
  gacc_imports: Object.freeze({
    pillar: 'trade', unit: 'USD million', periodKind: 'month',
    source: 'General Administration of Customs of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.gacc,
    sourceHost: 'english.customs.gov.cn', sourcePathPrefix: '/',
  }),
  gacc_trade_balance: Object.freeze({
    pillar: 'trade', unit: 'USD million', periodKind: 'month',
    source: 'General Administration of Customs of China', publisherId: CHINA_MACRO_PUBLISHER_IDS.gacc,
    sourceHost: 'english.customs.gov.cn', sourcePathPrefix: '/',
  }),
});

export const CHINA_MACRO_SERIES_IDS = Object.freeze(Object.keys(CHINA_MACRO_SERIES_CONTRACT));

// One reviewed alternative for M2 only. Keep the direct PBOC contract above so
// readers can still validate stored v2 payloads from before this lane launched.
export const CHINA_MACRO_M2_REPUBLICATION = Object.freeze({
  publisherId: 'publisher:shanghai-finance-cn',
  publisherName: 'Shanghai Municipal Financial Commission Office',
  registrySourceName: 'Shanghai Financial Portal (People’s Bank of China republication)',
  source: 'People’s Bank of China via Shanghai Financial Portal',
  sourceHost: 'jrj.sh.gov.cn',
  sourcePathPrefix: '/SCGK194/',
});

export function chinaMacroSourceBinding(seriesId, sourceUrl, value) {
  if (seriesId === 'pboc_m2_yoy') {
    try {
      const url = new URL(sourceUrl);
      if (url.origin === 'https://jrj.sh.gov.cn'
        && (/^\/SCGK194\/\d{8}\/[a-f0-9]{32}\.html$/.test(url.pathname)
          || (value === null && url.pathname === '/SCGK194/index.html'))) {
        return CHINA_MACRO_M2_REPUBLICATION;
      }
    } catch { /* The caller rejects invalid source URLs. */ }
  }
  return CHINA_MACRO_SERIES_CONTRACT[seriesId];
}

export function chinaMacroObservationDateMs(value) {
  if (typeof value !== 'string' || !value) return null;
  const month = /^(\d{4})-(\d{2})$/.exec(value);
  const parsed = month
    ? Date.UTC(Number(month[1]), Number(month[2]), 0, 23, 59, 59)
    : Date.parse(`${value}${/^\d{4}-\d{2}-\d{2}$/.test(value) ? 'T23:59:59Z' : ''}`);
  return Number.isFinite(parsed) ? parsed : null;
}

export function isChinaMacroObservationStale(seriesId, observationPeriod, now = Date.now()) {
  const maxAgeDays = CHINA_MACRO_SERIES_MAX_AGE_DAYS[seriesId];
  const observedAt = chinaMacroObservationDateMs(observationPeriod);
  return !Number.isFinite(maxAgeDays)
    || observedAt == null
    || now - observedAt > maxAgeDays * 86_400_000;
}