File size: 5,516 Bytes
ee888e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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;
export const CHINA_MACRO_MAX_CONTENT_AGE_MIN = 45 * 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_bank_fx_settlement: 45,
});

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));

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;
}