@v587d/capital-generation 2.26.0 → 2.27.0
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 +41 -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 +382 -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 +58 -17
- package/watchlist/client.src.cjs +716 -52
- package/watchlist/index.js +146 -0
- 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,29 @@ export function createWatchlistService(options = {}) {
|
|
|
859
969
|
}
|
|
860
970
|
}
|
|
861
971
|
|
|
972
|
+
/** Upstream failures degrade to one block in the desk, never to a whole-route error. */
|
|
973
|
+
function trendFailure(error) {
|
|
974
|
+
const name = typeof error?.name === 'string' ? error.name : ''
|
|
975
|
+
if (name === 'AbortError' || name === 'TimeoutError') return { ok: false, code: 'trend_timeout', message: '分时数据请求超时' }
|
|
976
|
+
const code = typeof error?.code === 'string' ? error.code : ''
|
|
977
|
+
if (code === 'trend_unavailable' || code === 'trend_parse_failed') return { ok: false, code, message: '分时数据暂时取不到' }
|
|
978
|
+
if (code === 'rate_limited' || code === 'tencent_rate_limit' || code === 'eastmoney_rate_limit') {
|
|
979
|
+
return { ok: false, code: 'rate_limited', message: '行情服务限流,请稍后再试' }
|
|
980
|
+
}
|
|
981
|
+
return { ok: false, code: 'trend_unavailable', message: '分时数据暂时取不到' }
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
function klineFailure(error) {
|
|
985
|
+
const name = typeof error?.name === 'string' ? error.name : ''
|
|
986
|
+
if (name === 'AbortError' || name === 'TimeoutError') return { ok: false, code: 'kline_timeout', message: '日K数据请求超时' }
|
|
987
|
+
const code = typeof error?.code === 'string' ? error.code : ''
|
|
988
|
+
if (code === 'tencent_parse_failed' || code === 'trend_parse_failed') return { ok: false, code: 'kline_parse_failed', message: '日K数据解析失败' }
|
|
989
|
+
if (code === 'rate_limited' || code === 'tencent_rate_limit' || code === 'eastmoney_rate_limit') {
|
|
990
|
+
return { ok: false, code: 'rate_limited', message: '行情服务限流,请稍后再试' }
|
|
991
|
+
}
|
|
992
|
+
return { ok: false, code: 'kline_unavailable', message: '日K数据暂时取不到' }
|
|
993
|
+
}
|
|
994
|
+
|
|
862
995
|
/** Only whole-batch failures become batch errors (rate limit, missing key, service down). */
|
|
863
996
|
function batchLevelCode(error, source = 'fuyao') {
|
|
864
997
|
const code = watchlistErrorCode(error, { source })
|
|
@@ -918,6 +1051,13 @@ function send(res, status, payload) {
|
|
|
918
1051
|
const FAILURE_STATUS = {
|
|
919
1052
|
invalid_query: 400,
|
|
920
1053
|
not_found: 404,
|
|
1054
|
+
not_watched: 404,
|
|
1055
|
+
trend_unavailable: 502,
|
|
1056
|
+
trend_timeout: 504,
|
|
1057
|
+
trend_parse_failed: 502,
|
|
1058
|
+
kline_unavailable: 502,
|
|
1059
|
+
kline_timeout: 504,
|
|
1060
|
+
kline_parse_failed: 502,
|
|
921
1061
|
ambiguous: 409,
|
|
922
1062
|
list_full: 409,
|
|
923
1063
|
credential_missing: 503,
|
|
@@ -992,6 +1132,12 @@ export function createRouteHandler(service, options = {}) {
|
|
|
992
1132
|
return send(res, 200, await service.refresh())
|
|
993
1133
|
}
|
|
994
1134
|
if (method === 'GET' && pathname === '/list') return send(res, 200, await service.list())
|
|
1135
|
+
if (method === 'GET' && pathname === '/kline') {
|
|
1136
|
+
return sendBusiness(res, await service.kline({ thscode: searchParams.get('thscode') ?? '', refresh: searchParams.get('refresh') === '1' }))
|
|
1137
|
+
}
|
|
1138
|
+
if (method === 'GET' && pathname === '/trend') {
|
|
1139
|
+
return sendBusiness(res, await service.trend({ thscode: searchParams.get('thscode') ?? '', refresh: searchParams.get('refresh') === '1' }))
|
|
1140
|
+
}
|
|
995
1141
|
if (method === 'GET' && pathname === '/search') return sendBusiness(res, await service.search(searchParams.get('q') ?? ''))
|
|
996
1142
|
send(res, 404, { ok: false, code: 'not_found', message: 'unknown watchlist route' })
|
|
997
1143
|
} catch (error) {
|
package/watchlist/package.json
CHANGED