@v587d/capital-generation 2.26.0 → 2.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/README.md +8 -6
- package/automation/package.json +1 -1
- package/chart-ui/package.json +1 -1
- package/config/package.json +1 -1
- package/lib/sources/tencent-http.d.ts +20 -0
- package/lib/sources/tencent-http.js +37 -58
- package/lib/sources/tencent-public-core.d.ts +31 -0
- package/lib/sources/tencent-public-core.js +63 -0
- package/lib/sources/trend-series.d.ts +53 -0
- package/lib/sources/trend-series.js +383 -0
- package/lib/web-retriever/local-fetch.d.ts +1 -1
- package/lib/web-retriever/local-fetch.js +1 -1
- package/lib/web-retriever/wind-client.d.ts +1 -1
- package/lib/web-retriever/wind-client.js +1 -1
- package/package.json +2 -1
- package/watchlist/client.js +65 -17
- package/watchlist/client.src.cjs +746 -52
- package/watchlist/index.js +171 -1
- package/watchlist/package.json +1 -1
package/watchlist/index.js
CHANGED
|
@@ -16,6 +16,12 @@ import {
|
|
|
16
16
|
normalizeHkCode,
|
|
17
17
|
} from '../lib/sources/tencent-public-core.js'
|
|
18
18
|
import { ASSET_TYPES, isHoldable, weightCeiling, otherHeldWeightPct, MAX_ROW_WEIGHT_PCT, MAX_TOTAL_WEIGHT_PCT } from './holding-rules.js'
|
|
19
|
+
// Intraday series core is shared with the plugin side (one parser per source, `docs/dev` §9.7);
|
|
20
|
+
// the host only gates identity, caches and turns failures into codes.
|
|
21
|
+
import { fetchTrendSeries, resolveTrendTarget } from '../lib/sources/trend-series.js'
|
|
22
|
+
// Daily bars come from the same executors and parsers as the plugin's `get_kline`; the desk only
|
|
23
|
+
// adds identity, a cache and the count it needs.
|
|
24
|
+
import { fetchDailyBars } from '../lib/sources/tencent-http.js'
|
|
19
25
|
|
|
20
26
|
export const name = 'capital-watchlist'
|
|
21
27
|
|
|
@@ -52,6 +58,18 @@ const REFRESH_BUDGET_CEIL_MS = 90_000
|
|
|
52
58
|
export function refreshBudgetFor(count) {
|
|
53
59
|
return Math.min(REFRESH_BUDGET_CEIL_MS, Math.max(REFRESH_BUDGET_FLOOR_MS, REFRESH_BUDGET_BASE_MS + REFRESH_BUDGET_PER_ITEM_MS * count))
|
|
54
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Intraday series: one upstream call per selection, reused for a minute so flipping rows does not
|
|
63
|
+
* queue behind the shared throttles (Tencent 120ms, Eastmoney 1s across the whole process).
|
|
64
|
+
*/
|
|
65
|
+
const TREND_TTL_MS = 60_000
|
|
66
|
+
const TREND_CACHE_MAX = 64
|
|
67
|
+
/** Daily bars gain one closed bar per session and one moving last bar: five minutes is plenty. */
|
|
68
|
+
const KLINE_TTL_MS = 300_000
|
|
69
|
+
const KLINE_CACHE_MAX = 32
|
|
70
|
+
const KLINE_TIMEOUT_MS = 15_000
|
|
71
|
+
const KLINE_BARS = 120
|
|
72
|
+
const TREND_TIMEOUT_MS = 10_000
|
|
55
73
|
const MAX_BODY_BYTES = 65_536
|
|
56
74
|
const MAX_QUERY_LENGTH = 64
|
|
57
75
|
/** Same entry id as the `capital-config` card (settings namespace = entry id). */
|
|
@@ -375,6 +393,11 @@ export function createWatchlistService(options = {}) {
|
|
|
375
393
|
let domainPromise
|
|
376
394
|
/** Single gate for panel open and manual refresh: later callers share the in-flight run. */
|
|
377
395
|
let refreshing = null
|
|
396
|
+
/** Insertion-ordered by design: the oldest key is what the capacity cap drops. */
|
|
397
|
+
const trendCache = new Map()
|
|
398
|
+
const trendInflight = new Map()
|
|
399
|
+
const klineCache = new Map()
|
|
400
|
+
const klineInflight = new Map()
|
|
378
401
|
|
|
379
402
|
/**
|
|
380
403
|
* Never memoize a failed open: `storageDomain` may not be provided yet during
|
|
@@ -842,8 +865,95 @@ export function createWatchlistService(options = {}) {
|
|
|
842
865
|
return result
|
|
843
866
|
}
|
|
844
867
|
|
|
868
|
+
/**
|
|
869
|
+
* One symbol's intraday series. Identity comes from the stored list only: a route that accepted
|
|
870
|
+
* the caller's asset_type would be an arbitrary quote proxy.
|
|
871
|
+
*/
|
|
872
|
+
async function trend(input) {
|
|
873
|
+
const thscode = typeof input?.thscode === 'string' ? input.thscode.trim() : ''
|
|
874
|
+
if (thscode.length === 0 || thscode.length > MAX_QUERY_LENGTH || !KEY_REGEX.test(thscode)) {
|
|
875
|
+
return { ok: false, code: 'invalid_query', message: '代码格式不符' }
|
|
876
|
+
}
|
|
877
|
+
await ensureSeeded()
|
|
878
|
+
const opened = await domain()
|
|
879
|
+
const item = opened.table('items').get(thscode)
|
|
880
|
+
if (item === undefined || item === null) return { ok: false, code: 'not_watched', message: '该标的不在自选清单里' }
|
|
881
|
+
const target = resolveTrendTarget(item)
|
|
882
|
+
if (target === undefined) return { ok: false, code: 'trend_unavailable', message: '该市场暂时没有分时数据', reason: 'market_unsupported' }
|
|
883
|
+
if (input?.refresh !== true) {
|
|
884
|
+
const cached = trendCache.get(thscode)
|
|
885
|
+
if (cached !== undefined && now() - cached.at < TREND_TTL_MS) return cached.payload
|
|
886
|
+
}
|
|
887
|
+
const running = trendInflight.get(thscode)
|
|
888
|
+
if (running !== undefined) return running
|
|
889
|
+
const run = (async () => {
|
|
890
|
+
try {
|
|
891
|
+
const series = await fetchTrendSeries(target, { signal: AbortSignal.timeout(TREND_TIMEOUT_MS), now: now() })
|
|
892
|
+
const payload = { ok: true, ...series }
|
|
893
|
+
trendCache.set(thscode, { at: now(), payload })
|
|
894
|
+
if (trendCache.size > TREND_CACHE_MAX) trendCache.delete(trendCache.keys().next().value)
|
|
895
|
+
return payload
|
|
896
|
+
} catch (error) {
|
|
897
|
+
return trendFailure(error)
|
|
898
|
+
} finally {
|
|
899
|
+
trendInflight.delete(thscode)
|
|
900
|
+
}
|
|
901
|
+
})()
|
|
902
|
+
trendInflight.set(thscode, run)
|
|
903
|
+
return run
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* One symbol's daily bars. Same identity gate as `trend` — the stored row decides the market, so a
|
|
908
|
+
* caller cannot turn this into an arbitrary history proxy.
|
|
909
|
+
*/
|
|
910
|
+
async function kline(input) {
|
|
911
|
+
const thscode = typeof input?.thscode === 'string' ? input.thscode.trim() : ''
|
|
912
|
+
if (thscode.length === 0 || thscode.length > MAX_QUERY_LENGTH || !KEY_REGEX.test(thscode)) {
|
|
913
|
+
return { ok: false, code: 'invalid_query', message: '代码格式不符' }
|
|
914
|
+
}
|
|
915
|
+
await ensureSeeded()
|
|
916
|
+
const opened = await domain()
|
|
917
|
+
const item = opened.table('items').get(thscode)
|
|
918
|
+
if (item === undefined || item === null) return { ok: false, code: 'not_watched', message: '该标的不在自选清单里' }
|
|
919
|
+
// HK/US indices have no daily series on the shared core (Tencent's K-line entries take a 5-digit
|
|
920
|
+
// HK code and a US ticker, not an index); the pane says so instead of drawing a fake line.
|
|
921
|
+
if (item.asset_type === 'hk-index' || item.asset_type === 'us-index') return { ok: false, code: 'kline_unavailable', message: '该市场暂时没有日K数据', reason: 'market_unsupported' }
|
|
922
|
+
if (input?.refresh !== true) {
|
|
923
|
+
const cached = klineCache.get(thscode)
|
|
924
|
+
if (cached !== undefined && now() - cached.at < KLINE_TTL_MS) return cached.payload
|
|
925
|
+
}
|
|
926
|
+
const running = klineInflight.get(thscode)
|
|
927
|
+
if (running !== undefined) return running
|
|
928
|
+
const run = (async () => {
|
|
929
|
+
try {
|
|
930
|
+
const day = await fetchDailyBars({ thscode }, AbortSignal.timeout(KLINE_TIMEOUT_MS), KLINE_BARS)
|
|
931
|
+
const payload = {
|
|
932
|
+
ok: true,
|
|
933
|
+
thscode,
|
|
934
|
+
source: 'tencent',
|
|
935
|
+
adjust: day.adjust,
|
|
936
|
+
volume_unit: day.volume_unit,
|
|
937
|
+
currency: resolveTrendTarget(item)?.currency ?? null,
|
|
938
|
+
bars: day.bars,
|
|
939
|
+
}
|
|
940
|
+
klineCache.set(thscode, { at: now(), payload })
|
|
941
|
+
if (klineCache.size > KLINE_CACHE_MAX) klineCache.delete(klineCache.keys().next().value)
|
|
942
|
+
return payload
|
|
943
|
+
} catch (error) {
|
|
944
|
+
return klineFailure(error)
|
|
945
|
+
} finally {
|
|
946
|
+
klineInflight.delete(thscode)
|
|
947
|
+
}
|
|
948
|
+
})()
|
|
949
|
+
klineInflight.set(thscode, run)
|
|
950
|
+
return run
|
|
951
|
+
}
|
|
952
|
+
|
|
845
953
|
return {
|
|
846
954
|
list,
|
|
955
|
+
trend,
|
|
956
|
+
kline,
|
|
847
957
|
search,
|
|
848
958
|
add,
|
|
849
959
|
remove,
|
|
@@ -859,6 +969,35 @@ export function createWatchlistService(options = {}) {
|
|
|
859
969
|
}
|
|
860
970
|
}
|
|
861
971
|
|
|
972
|
+
/** A body we cannot read: empty series, changed row shape, unreadable field. */
|
|
973
|
+
const PARSE_FAILURES = new Set(['tencent_parse_failed', 'tencent_invalid_response', 'tencent_empty_response'])
|
|
974
|
+
|
|
975
|
+
/** Upstream failures degrade to one block in the desk, never to a whole-route error. */
|
|
976
|
+
function trendFailure(error) {
|
|
977
|
+
const name = typeof error?.name === 'string' ? error.name : ''
|
|
978
|
+
if (name === 'AbortError' || name === 'TimeoutError') return { ok: false, code: 'trend_timeout', message: '上游没有及时回话' }
|
|
979
|
+
const code = typeof error?.code === 'string' ? error.code : ''
|
|
980
|
+
if (PARSE_FAILURES.has(code) || code === 'trend_parse_failed') return { ok: false, code: 'trend_parse_failed', message: '上游这次回的报文不完整' }
|
|
981
|
+
if (code === 'trend_unavailable') return { ok: false, code, message: '该市场暂时没有分时数据' }
|
|
982
|
+
if (code === 'rate_limited' || code === 'tencent_rate_limit' || code === 'eastmoney_rate_limit') {
|
|
983
|
+
return { ok: false, code: 'rate_limited', message: '行情服务限流,请稍后再试' }
|
|
984
|
+
}
|
|
985
|
+
// "All three minute hosts are down" is an upstream outage, not a market without data: the desk
|
|
986
|
+
// must not answer it with the market hint.
|
|
987
|
+
return { ok: false, code: 'trend_upstream_unavailable', message: '行情源这次没有给出分时' }
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
function klineFailure(error) {
|
|
991
|
+
const name = typeof error?.name === 'string' ? error.name : ''
|
|
992
|
+
if (name === 'AbortError' || name === 'TimeoutError') return { ok: false, code: 'kline_timeout', message: '上游没有及时回话' }
|
|
993
|
+
const code = typeof error?.code === 'string' ? error.code : ''
|
|
994
|
+
if (PARSE_FAILURES.has(code) || code === 'trend_parse_failed') return { ok: false, code: 'kline_parse_failed', message: '上游这次回的报文不完整' }
|
|
995
|
+
if (code === 'rate_limited' || code === 'tencent_rate_limit' || code === 'eastmoney_rate_limit') {
|
|
996
|
+
return { ok: false, code: 'rate_limited', message: '行情服务限流,请稍后再试' }
|
|
997
|
+
}
|
|
998
|
+
return { ok: false, code: 'kline_upstream_unavailable', message: '行情源这次没有给出日线' }
|
|
999
|
+
}
|
|
1000
|
+
|
|
862
1001
|
/** Only whole-batch failures become batch errors (rate limit, missing key, service down). */
|
|
863
1002
|
function batchLevelCode(error, source = 'fuyao') {
|
|
864
1003
|
const code = watchlistErrorCode(error, { source })
|
|
@@ -918,6 +1057,15 @@ function send(res, status, payload) {
|
|
|
918
1057
|
const FAILURE_STATUS = {
|
|
919
1058
|
invalid_query: 400,
|
|
920
1059
|
not_found: 404,
|
|
1060
|
+
not_watched: 404,
|
|
1061
|
+
trend_unavailable: 502,
|
|
1062
|
+
trend_timeout: 504,
|
|
1063
|
+
trend_parse_failed: 502,
|
|
1064
|
+
trend_upstream_unavailable: 502,
|
|
1065
|
+
kline_unavailable: 502,
|
|
1066
|
+
kline_timeout: 504,
|
|
1067
|
+
kline_parse_failed: 502,
|
|
1068
|
+
kline_upstream_unavailable: 502,
|
|
921
1069
|
ambiguous: 409,
|
|
922
1070
|
list_full: 409,
|
|
923
1071
|
credential_missing: 503,
|
|
@@ -992,6 +1140,14 @@ export function createRouteHandler(service, options = {}) {
|
|
|
992
1140
|
return send(res, 200, await service.refresh())
|
|
993
1141
|
}
|
|
994
1142
|
if (method === 'GET' && pathname === '/list') return send(res, 200, await service.list())
|
|
1143
|
+
if (method === 'GET' && pathname === '/kline') {
|
|
1144
|
+
const thscode = searchParams.get('thscode') ?? ''
|
|
1145
|
+
return sendBusiness(res, logUpstreamFailure(options.logger, 'kline', thscode, await service.kline({ thscode, refresh: searchParams.get('refresh') === '1' })))
|
|
1146
|
+
}
|
|
1147
|
+
if (method === 'GET' && pathname === '/trend') {
|
|
1148
|
+
const thscode = searchParams.get('thscode') ?? ''
|
|
1149
|
+
return sendBusiness(res, logUpstreamFailure(options.logger, 'trend', thscode, await service.trend({ thscode, refresh: searchParams.get('refresh') === '1' })))
|
|
1150
|
+
}
|
|
995
1151
|
if (method === 'GET' && pathname === '/search') return sendBusiness(res, await service.search(searchParams.get('q') ?? ''))
|
|
996
1152
|
send(res, 404, { ok: false, code: 'not_found', message: 'unknown watchlist route' })
|
|
997
1153
|
} catch (error) {
|
|
@@ -1005,6 +1161,20 @@ function storeCause(error) {
|
|
|
1005
1161
|
return [error?.code, error?.message ?? error].filter(Boolean).join(':') || 'unknown'
|
|
1006
1162
|
}
|
|
1007
1163
|
|
|
1164
|
+
/** Caller mistakes (a stale row, a bad code) are not incidents; only upstream and storage faults are. */
|
|
1165
|
+
const REQUEST_FAULTS = new Set(['invalid_query', 'not_watched', 'not_found', 'ambiguous', 'list_full'])
|
|
1166
|
+
|
|
1167
|
+
/**
|
|
1168
|
+
* The panel shows one sentence and nothing else, so the failure code has to land somewhere the
|
|
1169
|
+
* maintainer can read it back: the host log names the route, the symbol and the upstream code.
|
|
1170
|
+
*/
|
|
1171
|
+
function logUpstreamFailure(logger, route, thscode, result) {
|
|
1172
|
+
if (result?.ok !== true && !REQUEST_FAULTS.has(result.code)) {
|
|
1173
|
+
logger?.warn?.(`capital-watchlist: ${route} ${thscode} → ${result.code}${result.reason === undefined ? '' : ` (${result.reason})`}`)
|
|
1174
|
+
}
|
|
1175
|
+
return result
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1008
1178
|
function sendBusiness(res, result) {
|
|
1009
1179
|
if (result.ok === true) return send(res, 200, result)
|
|
1010
1180
|
return send(res, FAILURE_STATUS[result.code] ?? 400, result)
|
|
@@ -1061,7 +1231,7 @@ export function apply(ctx) {
|
|
|
1061
1231
|
const dispose = webCtx.webServer.register({
|
|
1062
1232
|
kind: 'prefix',
|
|
1063
1233
|
path: ROUTE_PATH,
|
|
1064
|
-
handler: createRouteHandler(service, { authorize }),
|
|
1234
|
+
handler: createRouteHandler(service, { authorize, logger: ctx.logger }),
|
|
1065
1235
|
})
|
|
1066
1236
|
return () => {
|
|
1067
1237
|
if (typeof dispose === 'function') dispose()
|
package/watchlist/package.json
CHANGED