T-186: M2.3n — tools/curate-etl: curate-bars-1m CLI command (file mode) wiring run_curate_bars_1m into cli.rs + main.rs with parser tests (no new crates, no bench changes, no --from-mongo, no live Mongo/QuestDB)
This commit is contained in:
parent
f1a9915cdf
commit
eebbdd511c
4 changed files with 851 additions and 1 deletions
|
|
@ -1673,7 +1673,28 @@ See [apps/stock/data-pipeline/README.md](../../apps/stock/data-pipeline/README.m
|
||||||
|
|
||||||
- **Bounded-memory chunking & listing-keyed splits:** Drives `RawBsonChunkReader::with_options`, `next_chunk()`, and `decode_eod_intraday_buffer` directly (the same reader and decoder wrapped by `decode_eod_intraday_chunks`, which hides chunk boundaries), resolving each bar's `listing_id` on its Eastern Time `session_date` before applying `listing_id`-keyed `qmSplits` un-adjustment (preventing split leakage across reused tickers) into one shared `IlpShardedWriter` and calling `finish()` once after EOF. Peak memory is bounded by the reference tables, `BsonStreamOptions::chunk_capacity_bytes` (growing only for a single document up to `max_doc_bytes`), per-chunk curation buffers, and one ILP batch buffer per shard.
|
- **Bounded-memory chunking & listing-keyed splits:** Drives `RawBsonChunkReader::with_options`, `next_chunk()`, and `decode_eod_intraday_buffer` directly (the same reader and decoder wrapped by `decode_eod_intraday_chunks`, which hides chunk boundaries), resolving each bar's `listing_id` on its Eastern Time `session_date` before applying `listing_id`-keyed `qmSplits` un-adjustment (preventing split leakage across reused tickers) into one shared `IlpShardedWriter` and calling `finish()` once after EOF. Peak memory is bounded by the reference tables, `BsonStreamOptions::chunk_capacity_bytes` (growing only for a single document up to `max_doc_bytes`), per-chunk curation buffers, and one ILP batch buffer per shard.
|
||||||
- **Cross-chunk semantics & reporting:** `mapping_source` must be `"eod"`. Collision collapse/drop and `(listing_id, ts)` ordering apply per chunk; the same `(listing_id, ts)` appearing in two chunks is emitted twice and relies on QuestDB `DEDUP UPSERT KEYS(ts, listing_id)`. Aggregated `curate_stats.listings_out` and `curate_stats.builder.keys_out` are sums of per-chunk counts (not global distinct counts). `known_at_micros` is caller-supplied and supplies no evidence of earlier market availability.
|
- **Cross-chunk semantics & reporting:** `mapping_source` must be `"eod"`. Collision collapse/drop and `(listing_id, ts)` ordering apply per chunk; the same `(listing_id, ts)` appearing in two chunks is emitted twice and relies on QuestDB `DEDUP UPSERT KEYS(ts, listing_id)`. Aggregated `curate_stats.listings_out` and `curate_stats.builder.keys_out` are sums of per-chunk counts (not global distinct counts). `known_at_micros` is caller-supplied and supplies no evidence of earlier market availability.
|
||||||
- **Fail-closed publication:** Preflight scans `out_dir` for any pre-existing `bars_1m.shard-<digits>.ilp` file (regardless of `shard_count`) and rejects before staging, writes to exclusively created temp files in `out_dir`, flushes and `sync_all`s on `finish`, publishes via same-directory `hard_link` + temp removal, and rolls back only final files created by the current run on any publication error. CLI wiring, `--from-mongo`, and live QuestDB ingestion are separate follow-ups.
|
- **Fail-closed publication:** Preflight scans `out_dir` for any pre-existing `bars_1m.shard-<digits>.ilp` file (regardless of `shard_count`) and rejects before staging, writes to exclusively created temp files in `out_dir`, flushes and `sync_all`s on `finish`, publishes via same-directory `hard_link` + temp removal, and rolls back only final files created by the current run on any publication error. File-mode CLI wiring is available via `curate-bars-1m` below; `--from-mongo` and live QuestDB ingestion remain follow-ups.
|
||||||
|
|
||||||
|
### `curate-bars-1m` Command (`M2.3n`)
|
||||||
|
|
||||||
|
File-mode CLI command wiring `run_curate_bars_1m` into the `curate-etl` binary:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curate-etl curate-bars-1m \
|
||||||
|
--eod-intraday eodIntraday1M.bson \
|
||||||
|
--qm-splits qmSplits.bson \
|
||||||
|
--provider-mapping provider_mapping.tsv \
|
||||||
|
--out ./out \
|
||||||
|
--shards 4 \
|
||||||
|
--known-at-micros 1750000000000000 \
|
||||||
|
[--mapping-source eod]
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Required flags:** `--eod-intraday <path>`, `--qm-splits <path>`, `--provider-mapping <path>`, `--out <dir>`, `--shards <N>` (positive base-10 integer, no default), and `--known-at-micros <i64>` (positive base-10 UTC microsecond timestamp, no default).
|
||||||
|
- **Optional flag:** `--mapping-source <name>` (defaults to `eod`; only `eod` is accepted by the runner).
|
||||||
|
- **Timestamp semantics:** `--known-at-micros` records the caller-supplied curation run timestamp verbatim in each ILP row's `known_at` column; it does not establish historical market availability.
|
||||||
|
- **Outputs & fail-closed publication:** writes `<out>/bars_1m.shard-<NN>.ilp` (`00`..`<shards-1>`) and prints the `format_curate_bars_1m_report` summary to stdout. Inherits the runner's fail-closed preflight and staging/rollback guarantees (refuses to overwrite existing `bars_1m.shard-*.ilp` files and leaves no partial shard or staging files on error).
|
||||||
|
- **File mode only:** MongoDB streaming flags (such as `--from-mongo` and `--mongo-uri`) are rejected; `--from-mongo` streaming and live QuestDB ingestion remain follow-ups.
|
||||||
|
|
||||||
## `run_curate_fx_1m` File-to-File Runner (`M2.3l`)
|
## `run_curate_fx_1m` File-to-File Runner (`M2.3l`)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,9 @@ use crate::bson_qm_events::{
|
||||||
DEFAULT_EARNINGS_ANNOUNCE_TIME_FIELD, DEFAULT_EARNINGS_KEY_FIELD,
|
DEFAULT_EARNINGS_ANNOUNCE_TIME_FIELD, DEFAULT_EARNINGS_KEY_FIELD,
|
||||||
DEFAULT_EARNINGS_REPORT_DATE_FIELD, DEFAULT_FILINGS_KEY_FIELD, DEFAULT_FILINGS_TIMEFILED_FIELD,
|
DEFAULT_EARNINGS_REPORT_DATE_FIELD, DEFAULT_FILINGS_KEY_FIELD, DEFAULT_FILINGS_TIMEFILED_FIELD,
|
||||||
};
|
};
|
||||||
|
use crate::bson_stream::BsonStreamOptions;
|
||||||
use crate::curate_adj::AdjPointInTimeFence;
|
use crate::curate_adj::AdjPointInTimeFence;
|
||||||
|
use crate::ilp::IlpWriterOptions;
|
||||||
use crate::mongo_stream::{
|
use crate::mongo_stream::{
|
||||||
MongoCliOptions, MongoUriSource, DEFAULT_MONGO_BATCH_SIZE, DEFAULT_MONGO_URI_ENV,
|
MongoCliOptions, MongoUriSource, DEFAULT_MONGO_BATCH_SIZE, DEFAULT_MONGO_URI_ENV,
|
||||||
MAX_MONGO_BATCH_SIZE,
|
MAX_MONGO_BATCH_SIZE,
|
||||||
|
|
@ -41,6 +43,7 @@ use crate::run_curate_adj::CurateAdjFactorsArgs;
|
||||||
use crate::run_curate_bars::{
|
use crate::run_curate_bars::{
|
||||||
default_curate_bars_threads, CurateBars1dArgs, DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
default_curate_bars_threads, CurateBars1dArgs, DEFAULT_CURATE_BARS_REL_TOLERANCE,
|
||||||
};
|
};
|
||||||
|
use crate::run_curate_bars_1m::{CurateBars1mArgs, DEFAULT_BARS_1M_MAPPING_SOURCE};
|
||||||
use crate::run_curate_corporate_actions::CurateCorporateActionsArgs;
|
use crate::run_curate_corporate_actions::CurateCorporateActionsArgs;
|
||||||
use crate::run_curate_events::{CurateEventsArgs, DEFAULT_EVENTS_MAPPING_SOURCE};
|
use crate::run_curate_events::{CurateEventsArgs, DEFAULT_EVENTS_MAPPING_SOURCE};
|
||||||
use crate::run_curate_fx::{is_supported_te_symbol, CurateFx1dArgs, DEFAULT_TE_SYMBOL};
|
use crate::run_curate_fx::{is_supported_te_symbol, CurateFx1dArgs, DEFAULT_TE_SYMBOL};
|
||||||
|
|
@ -68,6 +71,7 @@ pub enum CliAction {
|
||||||
CurateShortsMongo(CurateShortsMongoArgs),
|
CurateShortsMongo(CurateShortsMongoArgs),
|
||||||
CurateEvents(CurateEventsArgs),
|
CurateEvents(CurateEventsArgs),
|
||||||
CurateEventsMongo(CurateEventsMongoArgs),
|
CurateEventsMongo(CurateEventsMongoArgs),
|
||||||
|
CurateBars1m(CurateBars1mArgs),
|
||||||
BenchSplit(usize),
|
BenchSplit(usize),
|
||||||
BenchAdj(usize),
|
BenchAdj(usize),
|
||||||
BenchCorporateActions(usize),
|
BenchCorporateActions(usize),
|
||||||
|
|
@ -129,6 +133,8 @@ pub fn format_usage(name: &str, version: &str) -> String {
|
||||||
{name} curate-events --qm-filings <path> --qm-earnings <path> --provider-mapping <path> --out <dir> --filings-form-type-field <name> [--filings-key-field <name>] [--filings-timefiled-field <name>] [--filings-filing-id-field <name>] [--filings-url-field <name>] [--earnings-key-field <name>] [--earnings-report-date-field <name>] [--earnings-announce-time-field <name>] [--earnings-fiscal-period-end-field <name>] [--earnings-actual-eps-field <name>] [--earnings-revenue-field <name>] [--mapping-source <name>]\n\
|
{name} curate-events --qm-filings <path> --qm-earnings <path> --provider-mapping <path> --out <dir> --filings-form-type-field <name> [--filings-key-field <name>] [--filings-timefiled-field <name>] [--filings-filing-id-field <name>] [--filings-url-field <name>] [--earnings-key-field <name>] [--earnings-report-date-field <name>] [--earnings-announce-time-field <name>] [--earnings-fiscal-period-end-field <name>] [--earnings-actual-eps-field <name>] [--earnings-revenue-field <name>] [--mapping-source <name>]\n\
|
||||||
{name} curate-events --from-mongo [--mongo-uri-env <VAR> | --mongo-uri <uri>] [--batch-size <N>] [--limit <N>] [--date-gte <YYYY-MM-DD>] [--date-lt <YYYY-MM-DD>] --provider-mapping <path> --out <dir> --filings-form-type-field <name> [--filings-key-field <name>] [--filings-timefiled-field <name>] [--filings-filing-id-field <name>] [--filings-url-field <name>] [--earnings-key-field <name>] [--earnings-report-date-field <name>] [--earnings-announce-time-field <name>] [--earnings-fiscal-period-end-field <name>] [--earnings-actual-eps-field <name>] [--earnings-revenue-field <name>] [--mapping-source <name>]\n\
|
{name} curate-events --from-mongo [--mongo-uri-env <VAR> | --mongo-uri <uri>] [--batch-size <N>] [--limit <N>] [--date-gte <YYYY-MM-DD>] [--date-lt <YYYY-MM-DD>] --provider-mapping <path> --out <dir> --filings-form-type-field <name> [--filings-key-field <name>] [--filings-timefiled-field <name>] [--filings-filing-id-field <name>] [--filings-url-field <name>] [--earnings-key-field <name>] [--earnings-report-date-field <name>] [--earnings-announce-time-field <name>] [--earnings-fiscal-period-end-field <name>] [--earnings-actual-eps-field <name>] [--earnings-revenue-field <name>] [--mapping-source <name>]\n\
|
||||||
Curate events from qmFilings + qmEarnings BSON or read-only MongoDB + provider_mapping TSV into events/year=YYYY/part.parquet (defaults: --filings-key-field {DEFAULT_FILINGS_KEY_FIELD}, --filings-timefiled-field {DEFAULT_FILINGS_TIMEFILED_FIELD}, --earnings-key-field {DEFAULT_EARNINGS_KEY_FIELD}, --earnings-report-date-field {DEFAULT_EARNINGS_REPORT_DATE_FIELD}, --earnings-announce-time-field {DEFAULT_EARNINGS_ANNOUNCE_TIME_FIELD}, --mapping-source {DEFAULT_EVENTS_MAPPING_SOURCE})\n\
|
Curate events from qmFilings + qmEarnings BSON or read-only MongoDB + provider_mapping TSV into events/year=YYYY/part.parquet (defaults: --filings-key-field {DEFAULT_FILINGS_KEY_FIELD}, --filings-timefiled-field {DEFAULT_FILINGS_TIMEFILED_FIELD}, --earnings-key-field {DEFAULT_EARNINGS_KEY_FIELD}, --earnings-report-date-field {DEFAULT_EARNINGS_REPORT_DATE_FIELD}, --earnings-announce-time-field {DEFAULT_EARNINGS_ANNOUNCE_TIME_FIELD}, --mapping-source {DEFAULT_EVENTS_MAPPING_SOURCE})\n\
|
||||||
|
{name} curate-bars-1m --eod-intraday <path> --qm-splits <path> --provider-mapping <path> --out <dir> --shards <N> --known-at-micros <i64> [--mapping-source <name>]\n\
|
||||||
|
Curate split-unadjusted 1-minute bars from eodIntraday1M + qmSplits BSON + provider_mapping TSV into bars_1m.shard-<NN>.ilp (default --mapping-source: {DEFAULT_BARS_1M_MAPPING_SOURCE})\n\
|
||||||
{name} bench-split [rows] Run fixed-seed split lookup + un-adjust benchmark (default: {DEFAULT_BENCH_ROWS})\n\
|
{name} bench-split [rows] Run fixed-seed split lookup + un-adjust benchmark (default: {DEFAULT_BENCH_ROWS})\n\
|
||||||
{name} bench-adj [events] Run fixed-seed adj_factors build benchmark (default: {DEFAULT_ADJ_BENCH_EVENTS})\n\
|
{name} bench-adj [events] Run fixed-seed adj_factors build benchmark (default: {DEFAULT_ADJ_BENCH_EVENTS})\n\
|
||||||
{name} bench-corporate-actions [events] Run fixed-seed corporate_actions build benchmark (default: {DEFAULT_CORPORATE_ACTIONS_BENCH_EVENTS})\n\
|
{name} bench-corporate-actions [events] Run fixed-seed corporate_actions build benchmark (default: {DEFAULT_CORPORATE_ACTIONS_BENCH_EVENTS})\n\
|
||||||
|
|
@ -177,6 +183,7 @@ pub fn format_usage_stderr(name: &str) -> String {
|
||||||
{name} curate-fx-1d --eod-prices <path> --te-market-data <path> [--te-symbol <sym>] --out <dir>\n\
|
{name} curate-fx-1d --eod-prices <path> --te-market-data <path> [--te-symbol <sym>] --out <dir>\n\
|
||||||
{name} curate-shorts --ceo-shorts <path> --provider-mapping <path> --out <dir> --shares-short-field <name> [--key-field <name>] [--report-date-field <name>] [--net-change-field <name>] [--pct-float-field <name>] [--mapping-source <name>]\n\
|
{name} curate-shorts --ceo-shorts <path> --provider-mapping <path> --out <dir> --shares-short-field <name> [--key-field <name>] [--report-date-field <name>] [--net-change-field <name>] [--pct-float-field <name>] [--mapping-source <name>]\n\
|
||||||
{name} curate-events --qm-filings <path> --qm-earnings <path> --provider-mapping <path> --out <dir> --filings-form-type-field <name> [--filings-key-field <name>] [--filings-timefiled-field <name>] [--filings-filing-id-field <name>] [--filings-url-field <name>] [--earnings-key-field <name>] [--earnings-report-date-field <name>] [--earnings-announce-time-field <name>] [--earnings-fiscal-period-end-field <name>] [--earnings-actual-eps-field <name>] [--earnings-revenue-field <name>] [--mapping-source <name>]\n\
|
{name} curate-events --qm-filings <path> --qm-earnings <path> --provider-mapping <path> --out <dir> --filings-form-type-field <name> [--filings-key-field <name>] [--filings-timefiled-field <name>] [--filings-filing-id-field <name>] [--filings-url-field <name>] [--earnings-key-field <name>] [--earnings-report-date-field <name>] [--earnings-announce-time-field <name>] [--earnings-fiscal-period-end-field <name>] [--earnings-actual-eps-field <name>] [--earnings-revenue-field <name>] [--mapping-source <name>]\n\
|
||||||
|
{name} curate-bars-1m --eod-intraday <path> --qm-splits <path> --provider-mapping <path> --out <dir> --shards <N> --known-at-micros <i64> [--mapping-source <name>]\n\
|
||||||
{name} bench-split [rows]\n\
|
{name} bench-split [rows]\n\
|
||||||
{name} bench-adj [events]\n\
|
{name} bench-adj [events]\n\
|
||||||
{name} bench-corporate-actions [events]\n\
|
{name} bench-corporate-actions [events]\n\
|
||||||
|
|
@ -1422,6 +1429,161 @@ fn parse_curate_events_args(args: &[&str]) -> Result<CliAction, String> {
|
||||||
Ok(CliAction::CurateEvents(parsed))
|
Ok(CliAction::CurateEvents(parsed))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_curate_bars_1m_args(args: &[&str]) -> Result<CliAction, String> {
|
||||||
|
let mut intraday_path: Option<PathBuf> = None;
|
||||||
|
let mut splits_path: Option<PathBuf> = None;
|
||||||
|
let mut provider_mapping_path: Option<PathBuf> = None;
|
||||||
|
let mut out_dir: Option<PathBuf> = None;
|
||||||
|
let mut shard_count: Option<usize> = None;
|
||||||
|
let mut known_at_micros: Option<i64> = None;
|
||||||
|
let mut mapping_source: Option<String> = None;
|
||||||
|
|
||||||
|
let mut i = 0usize;
|
||||||
|
while i < args.len() {
|
||||||
|
let token = args[i];
|
||||||
|
if !token.starts_with("--") {
|
||||||
|
return Err(format!(
|
||||||
|
"unexpected positional argument '{token}' for curate-bars-1m"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let flag_name = token.split_once('=').map_or(token, |(k, _)| k);
|
||||||
|
if matches!(
|
||||||
|
flag_name,
|
||||||
|
"--from-mongo"
|
||||||
|
| "--mongo-uri"
|
||||||
|
| "--mongo-uri-env"
|
||||||
|
| "--batch-size"
|
||||||
|
| "--limit"
|
||||||
|
| "--date-gte"
|
||||||
|
| "--date-lt"
|
||||||
|
| "--with-qm-prices"
|
||||||
|
) {
|
||||||
|
return Err(format!(
|
||||||
|
"MongoDB streaming flag '{flag_name}' is not supported by curate-bars-1m (file mode only)"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let (flag, value) = if let Some((k, v)) = token.split_once('=') {
|
||||||
|
i += 1;
|
||||||
|
(k, v)
|
||||||
|
} else {
|
||||||
|
let Some(&v) = args.get(i + 1) else {
|
||||||
|
return Err(format!("missing value for flag '{token}'"));
|
||||||
|
};
|
||||||
|
if v.starts_with("--") {
|
||||||
|
return Err(format!("missing value for flag '{token}'"));
|
||||||
|
}
|
||||||
|
i += 2;
|
||||||
|
(token, v)
|
||||||
|
};
|
||||||
|
|
||||||
|
let set_path = |slot: &mut Option<PathBuf>, flag_name: &str, raw: &str| -> Result<(), String> {
|
||||||
|
if slot.is_some() {
|
||||||
|
return Err(format!("duplicate flag '{flag_name}'"));
|
||||||
|
}
|
||||||
|
if raw.is_empty() {
|
||||||
|
return Err(format!("empty value for flag '{flag_name}'"));
|
||||||
|
}
|
||||||
|
*slot = Some(PathBuf::from(raw));
|
||||||
|
Ok(())
|
||||||
|
};
|
||||||
|
|
||||||
|
let set_name = |slot: &mut Option<String>, flag_name: &str, raw: &str| -> Result<(), String> {
|
||||||
|
if slot.is_some() {
|
||||||
|
return Err(format!("duplicate flag '{flag_name}'"));
|
||||||
|
}
|
||||||
|
if raw.trim().is_empty() {
|
||||||
|
return Err(format!("empty value for flag '{flag_name}'"));
|
||||||
|
}
|
||||||
|
*slot = Some(raw.to_string());
|
||||||
|
Ok(())
|
||||||
|
};
|
||||||
|
|
||||||
|
match flag {
|
||||||
|
"--eod-intraday" => set_path(&mut intraday_path, "--eod-intraday", value)?,
|
||||||
|
"--qm-splits" => set_path(&mut splits_path, "--qm-splits", value)?,
|
||||||
|
"--provider-mapping" => {
|
||||||
|
set_path(&mut provider_mapping_path, "--provider-mapping", value)?;
|
||||||
|
}
|
||||||
|
"--out" => set_path(&mut out_dir, "--out", value)?,
|
||||||
|
"--shards" => {
|
||||||
|
if shard_count.is_some() {
|
||||||
|
return Err("duplicate flag '--shards'".to_string());
|
||||||
|
}
|
||||||
|
if value.is_empty() {
|
||||||
|
return Err("empty value for flag '--shards'".to_string());
|
||||||
|
}
|
||||||
|
if !value.bytes().all(|b| b.is_ascii_digit()) {
|
||||||
|
return Err(format!(
|
||||||
|
"invalid --shards '{value}' (expected a positive integer >= 1)"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let parsed = value.parse::<usize>().map_err(|_| {
|
||||||
|
format!("invalid --shards '{value}' (expected a positive integer >= 1)")
|
||||||
|
})?;
|
||||||
|
if parsed == 0 {
|
||||||
|
return Err(format!(
|
||||||
|
"invalid --shards '{value}' (expected a positive integer >= 1)"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
shard_count = Some(parsed);
|
||||||
|
}
|
||||||
|
"--known-at-micros" => {
|
||||||
|
if known_at_micros.is_some() {
|
||||||
|
return Err("duplicate flag '--known-at-micros'".to_string());
|
||||||
|
}
|
||||||
|
if value.is_empty() {
|
||||||
|
return Err("empty value for flag '--known-at-micros'".to_string());
|
||||||
|
}
|
||||||
|
if !value.bytes().all(|b| b.is_ascii_digit()) {
|
||||||
|
return Err(format!(
|
||||||
|
"invalid --known-at-micros '{value}' (expected a positive 64-bit integer >= 1)"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let parsed = value.parse::<i64>().map_err(|_| {
|
||||||
|
format!(
|
||||||
|
"invalid --known-at-micros '{value}' (expected a positive 64-bit integer >= 1)"
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
if parsed <= 0 {
|
||||||
|
return Err(format!(
|
||||||
|
"invalid --known-at-micros '{value}' (expected a positive 64-bit integer >= 1)"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
known_at_micros = Some(parsed);
|
||||||
|
}
|
||||||
|
"--mapping-source" => set_name(&mut mapping_source, "--mapping-source", value)?,
|
||||||
|
other => return Err(format!("unknown flag '{other}' for curate-bars-1m")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let intraday_path =
|
||||||
|
intraday_path.ok_or_else(|| "missing required flag '--eod-intraday'".to_string())?;
|
||||||
|
let splits_path =
|
||||||
|
splits_path.ok_or_else(|| "missing required flag '--qm-splits'".to_string())?;
|
||||||
|
let provider_mapping_path = provider_mapping_path
|
||||||
|
.ok_or_else(|| "missing required flag '--provider-mapping'".to_string())?;
|
||||||
|
let out_dir = out_dir.ok_or_else(|| "missing required flag '--out'".to_string())?;
|
||||||
|
let shard_count =
|
||||||
|
shard_count.ok_or_else(|| "missing required flag '--shards'".to_string())?;
|
||||||
|
let known_at_micros =
|
||||||
|
known_at_micros.ok_or_else(|| "missing required flag '--known-at-micros'".to_string())?;
|
||||||
|
|
||||||
|
Ok(CliAction::CurateBars1m(CurateBars1mArgs {
|
||||||
|
intraday_path,
|
||||||
|
splits_path,
|
||||||
|
provider_mapping_path,
|
||||||
|
out_dir,
|
||||||
|
mapping_source: mapping_source
|
||||||
|
.unwrap_or_else(|| DEFAULT_BARS_1M_MAPPING_SOURCE.to_string()),
|
||||||
|
shard_count,
|
||||||
|
known_at_micros,
|
||||||
|
stream_options: BsonStreamOptions::default(),
|
||||||
|
ilp_options: IlpWriterOptions::default(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
/// Parses CLI arguments (excluding `argv[0]`) into a [`CliAction`].
|
/// Parses CLI arguments (excluding `argv[0]`) into a [`CliAction`].
|
||||||
pub fn parse_args(args: &[String]) -> Result<CliAction, String> {
|
pub fn parse_args(args: &[String]) -> Result<CliAction, String> {
|
||||||
let slices: Vec<&str> = args.iter().map(String::as_str).collect();
|
let slices: Vec<&str> = args.iter().map(String::as_str).collect();
|
||||||
|
|
@ -1437,6 +1599,7 @@ pub fn parse_args(args: &[String]) -> Result<CliAction, String> {
|
||||||
["curate-fx-1d", rest @ ..] => parse_curate_fx_1d_args(rest),
|
["curate-fx-1d", rest @ ..] => parse_curate_fx_1d_args(rest),
|
||||||
["curate-shorts", rest @ ..] => parse_curate_shorts_args(rest),
|
["curate-shorts", rest @ ..] => parse_curate_shorts_args(rest),
|
||||||
["curate-events", rest @ ..] => parse_curate_events_args(rest),
|
["curate-events", rest @ ..] => parse_curate_events_args(rest),
|
||||||
|
["curate-bars-1m", rest @ ..] => parse_curate_bars_1m_args(rest),
|
||||||
["bench-split"] => Ok(CliAction::BenchSplit(DEFAULT_BENCH_ROWS)),
|
["bench-split"] => Ok(CliAction::BenchSplit(DEFAULT_BENCH_ROWS)),
|
||||||
["bench-split", raw] => match raw.parse::<usize>() {
|
["bench-split", raw] => match raw.parse::<usize>() {
|
||||||
Ok(n) if n > 0 => Ok(CliAction::BenchSplit(n)),
|
Ok(n) if n > 0 => Ok(CliAction::BenchSplit(n)),
|
||||||
|
|
@ -3459,4 +3622,329 @@ mod tests {
|
||||||
assert!(parse_args(&to_args(&["bench-fundamentals", "abc"])).is_err());
|
assert!(parse_args(&to_args(&["bench-fundamentals", "abc"])).is_err());
|
||||||
assert!(parse_args(&to_args(&["bench-fundamentals", "1", "2"])).is_err());
|
assert!(parse_args(&to_args(&["bench-fundamentals", "1", "2"])).is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_curate_bars_1m_valid_default_and_explicit_mapping_source() {
|
||||||
|
let parsed = parse_args(&to_args(&[
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday",
|
||||||
|
"intraday.bson",
|
||||||
|
"--qm-splits",
|
||||||
|
"splits.bson",
|
||||||
|
"--provider-mapping",
|
||||||
|
"mapping.tsv",
|
||||||
|
"--out",
|
||||||
|
"out_dir",
|
||||||
|
"--shards",
|
||||||
|
"4",
|
||||||
|
"--known-at-micros",
|
||||||
|
"1735689600000000",
|
||||||
|
]))
|
||||||
|
.expect("valid curate-bars-1m args with default mapping source");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
parsed,
|
||||||
|
CliAction::CurateBars1m(CurateBars1mArgs {
|
||||||
|
intraday_path: PathBuf::from("intraday.bson"),
|
||||||
|
splits_path: PathBuf::from("splits.bson"),
|
||||||
|
provider_mapping_path: PathBuf::from("mapping.tsv"),
|
||||||
|
out_dir: PathBuf::from("out_dir"),
|
||||||
|
mapping_source: DEFAULT_BARS_1M_MAPPING_SOURCE.to_string(),
|
||||||
|
shard_count: 4,
|
||||||
|
known_at_micros: 1_735_689_600_000_000,
|
||||||
|
stream_options: BsonStreamOptions::default(),
|
||||||
|
ilp_options: IlpWriterOptions::default(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
let parsed_eq = parse_args(&to_args(&[
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday=i2.bson",
|
||||||
|
"--qm-splits=s2.bson",
|
||||||
|
"--provider-mapping=m2.tsv",
|
||||||
|
"--out=o2",
|
||||||
|
"--shards=2",
|
||||||
|
"--known-at-micros=9223372036854775807",
|
||||||
|
"--mapping-source=eod_alt",
|
||||||
|
]))
|
||||||
|
.expect("valid curate-bars-1m args with equals form and explicit mapping source");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
parsed_eq,
|
||||||
|
CliAction::CurateBars1m(CurateBars1mArgs {
|
||||||
|
intraday_path: PathBuf::from("i2.bson"),
|
||||||
|
splits_path: PathBuf::from("s2.bson"),
|
||||||
|
provider_mapping_path: PathBuf::from("m2.tsv"),
|
||||||
|
out_dir: PathBuf::from("o2"),
|
||||||
|
mapping_source: "eod_alt".to_string(),
|
||||||
|
shard_count: 2,
|
||||||
|
known_at_micros: i64::MAX,
|
||||||
|
stream_options: BsonStreamOptions::default(),
|
||||||
|
ilp_options: IlpWriterOptions::default(),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn curate_bars_1m_usage_mentions_command_and_all_flags() {
|
||||||
|
let expected_tokens = [
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday",
|
||||||
|
"--qm-splits",
|
||||||
|
"--provider-mapping",
|
||||||
|
"--out",
|
||||||
|
"--shards",
|
||||||
|
"--known-at-micros",
|
||||||
|
"--mapping-source",
|
||||||
|
];
|
||||||
|
let usage = format_usage("curate-etl", "0.1.0");
|
||||||
|
let usage_stderr = format_usage_stderr("curate-etl");
|
||||||
|
for token in expected_tokens {
|
||||||
|
assert!(usage.contains(token), "format_usage missing {token}");
|
||||||
|
assert!(
|
||||||
|
usage_stderr.contains(token),
|
||||||
|
"format_usage_stderr missing {token}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_curate_bars_1m_rejects_missing_required_flags() {
|
||||||
|
let required: [(&str, &str); 6] = [
|
||||||
|
("--eod-intraday", "intraday.bson"),
|
||||||
|
("--qm-splits", "splits.bson"),
|
||||||
|
("--provider-mapping", "mapping.tsv"),
|
||||||
|
("--out", "out_dir"),
|
||||||
|
("--shards", "2"),
|
||||||
|
("--known-at-micros", "1735689600000000"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for omit in 0..required.len() {
|
||||||
|
let mut argv: Vec<&str> = vec!["curate-bars-1m"];
|
||||||
|
for (idx, &(flag, val)) in required.iter().enumerate() {
|
||||||
|
if idx != omit {
|
||||||
|
argv.push(flag);
|
||||||
|
argv.push(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let err = parse_args(&to_args(&argv)).unwrap_err();
|
||||||
|
let missing_flag = required[omit].0;
|
||||||
|
assert!(
|
||||||
|
err.contains(&format!("missing required flag '{missing_flag}'")),
|
||||||
|
"expected missing required flag '{missing_flag}', got: {err}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_curate_bars_1m_rejects_duplicates_empty_missing_value_unknown_and_positional() {
|
||||||
|
let base = [
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday",
|
||||||
|
"i.bson",
|
||||||
|
"--qm-splits",
|
||||||
|
"s.bson",
|
||||||
|
"--provider-mapping",
|
||||||
|
"m.tsv",
|
||||||
|
"--out",
|
||||||
|
"o",
|
||||||
|
"--shards",
|
||||||
|
"2",
|
||||||
|
"--known-at-micros",
|
||||||
|
"1000",
|
||||||
|
"--mapping-source",
|
||||||
|
"eod",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Duplicate flags (path, numeric, and name).
|
||||||
|
for (flag, extra_val) in [
|
||||||
|
("--eod-intraday", "i2.bson"),
|
||||||
|
("--qm-splits", "s2.bson"),
|
||||||
|
("--provider-mapping", "m2.tsv"),
|
||||||
|
("--out", "o2"),
|
||||||
|
("--shards", "3"),
|
||||||
|
("--known-at-micros", "2000"),
|
||||||
|
("--mapping-source", "eod"),
|
||||||
|
] {
|
||||||
|
let mut argv = base.to_vec();
|
||||||
|
argv.push(flag);
|
||||||
|
argv.push(extra_val);
|
||||||
|
let err = parse_args(&to_args(&argv)).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains(&format!("duplicate flag '{flag}'")),
|
||||||
|
"expected duplicate error for {flag}, got: {err}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty values via --flag= and whitespace-only --mapping-source.
|
||||||
|
for flag in [
|
||||||
|
"--eod-intraday",
|
||||||
|
"--qm-splits",
|
||||||
|
"--provider-mapping",
|
||||||
|
"--out",
|
||||||
|
"--shards",
|
||||||
|
"--known-at-micros",
|
||||||
|
"--mapping-source",
|
||||||
|
] {
|
||||||
|
let empty_token = format!("{flag}=");
|
||||||
|
let err = parse_args(&to_args(&["curate-bars-1m", &empty_token])).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains(&format!("empty value for flag '{flag}'")),
|
||||||
|
"expected empty value error for {flag}, got: {err}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let err = parse_args(&to_args(&["curate-bars-1m", "--mapping-source", " "])).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains("empty value for flag '--mapping-source'"),
|
||||||
|
"{err}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Flag at end of input or followed by another flag.
|
||||||
|
let err = parse_args(&to_args(&["curate-bars-1m", "--eod-intraday"])).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains("missing value for flag '--eod-intraday'"),
|
||||||
|
"{err}"
|
||||||
|
);
|
||||||
|
let err = parse_args(&to_args(&[
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--shards",
|
||||||
|
"--out",
|
||||||
|
"o",
|
||||||
|
]))
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(err.contains("missing value for flag '--shards'"), "{err}");
|
||||||
|
|
||||||
|
// Unknown flag.
|
||||||
|
let mut unknown = base.to_vec();
|
||||||
|
unknown.push("--nope");
|
||||||
|
unknown.push("1");
|
||||||
|
let err = parse_args(&to_args(&unknown)).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains("unknown flag '--nope' for curate-bars-1m"),
|
||||||
|
"{err}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Positional argument.
|
||||||
|
let err = parse_args(&to_args(&["curate-bars-1m", "positional"])).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains("unexpected positional argument 'positional' for curate-bars-1m"),
|
||||||
|
"{err}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_curate_bars_1m_rejects_invalid_numeric_flags() {
|
||||||
|
let base_without_shards = [
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday",
|
||||||
|
"i.bson",
|
||||||
|
"--qm-splits",
|
||||||
|
"s.bson",
|
||||||
|
"--provider-mapping",
|
||||||
|
"m.tsv",
|
||||||
|
"--out",
|
||||||
|
"o",
|
||||||
|
"--known-at-micros",
|
||||||
|
"1000",
|
||||||
|
];
|
||||||
|
|
||||||
|
for bad_shards in [
|
||||||
|
"0",
|
||||||
|
"-1",
|
||||||
|
"+1",
|
||||||
|
"abc",
|
||||||
|
"1.5",
|
||||||
|
"999999999999999999999999999999999999999",
|
||||||
|
] {
|
||||||
|
let mut argv = base_without_shards.to_vec();
|
||||||
|
argv.push("--shards");
|
||||||
|
argv.push(bad_shards);
|
||||||
|
let err = parse_args(&to_args(&argv)).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains("invalid --shards"),
|
||||||
|
"expected invalid --shards for '{bad_shards}', got: {err}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let base_without_known_at = [
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday",
|
||||||
|
"i.bson",
|
||||||
|
"--qm-splits",
|
||||||
|
"s.bson",
|
||||||
|
"--provider-mapping",
|
||||||
|
"m.tsv",
|
||||||
|
"--out",
|
||||||
|
"o",
|
||||||
|
"--shards",
|
||||||
|
"2",
|
||||||
|
];
|
||||||
|
|
||||||
|
for bad_known_at in [
|
||||||
|
"0",
|
||||||
|
"-1",
|
||||||
|
"-9223372036854775808",
|
||||||
|
"+1000",
|
||||||
|
"abc",
|
||||||
|
"1.5",
|
||||||
|
"9223372036854775808",
|
||||||
|
"999999999999999999999999999999",
|
||||||
|
] {
|
||||||
|
let mut argv = base_without_known_at.to_vec();
|
||||||
|
argv.push("--known-at-micros");
|
||||||
|
argv.push(bad_known_at);
|
||||||
|
let err = parse_args(&to_args(&argv)).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err.contains("invalid --known-at-micros"),
|
||||||
|
"expected invalid --known-at-micros for '{bad_known_at}', got: {err}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_curate_bars_1m_rejects_all_mongo_streaming_flags_without_echoing_values() {
|
||||||
|
let mongo_flags = [
|
||||||
|
"--from-mongo",
|
||||||
|
"--mongo-uri",
|
||||||
|
"--mongo-uri-env",
|
||||||
|
"--batch-size",
|
||||||
|
"--limit",
|
||||||
|
"--date-gte",
|
||||||
|
"--date-lt",
|
||||||
|
"--with-qm-prices",
|
||||||
|
];
|
||||||
|
|
||||||
|
let secret = "mongodb://user:s3cr3t_pw@localhost:27017/stock";
|
||||||
|
for flag in mongo_flags {
|
||||||
|
// Bare form (at end of argv).
|
||||||
|
let err_bare = parse_args(&to_args(&["curate-bars-1m", flag])).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err_bare.contains(flag) && err_bare.contains("file mode only"),
|
||||||
|
"bare {flag} should fail with file-mode error, got: {err_bare}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Separated value form.
|
||||||
|
let err_sep = parse_args(&to_args(&["curate-bars-1m", flag, secret])).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err_sep.contains(flag) && err_sep.contains("file mode only"),
|
||||||
|
"separated {flag} should fail with file-mode error, got: {err_sep}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!err_sep.contains("s3cr3t_pw"),
|
||||||
|
"separated {flag} error leaked value: {err_sep}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Equals form.
|
||||||
|
let eq_token = format!("{flag}={secret}");
|
||||||
|
let err_eq = parse_args(&to_args(&["curate-bars-1m", &eq_token])).unwrap_err();
|
||||||
|
assert!(
|
||||||
|
err_eq.contains(flag) && err_eq.contains("file mode only"),
|
||||||
|
"equals {flag} should fail with file-mode error, got: {err_eq}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!err_eq.contains("s3cr3t_pw"),
|
||||||
|
"equals {flag} error leaked value: {err_eq}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ use curate_etl::run_curate_adj::{
|
||||||
format_curate_adj_report, run_curate_adj_factors, run_curate_adj_factors_fenced,
|
format_curate_adj_report, run_curate_adj_factors, run_curate_adj_factors_fenced,
|
||||||
};
|
};
|
||||||
use curate_etl::run_curate_bars::{format_curate_bars_report, run_curate_bars_1d};
|
use curate_etl::run_curate_bars::{format_curate_bars_report, run_curate_bars_1d};
|
||||||
|
use curate_etl::run_curate_bars_1m::{format_curate_bars_1m_report, run_curate_bars_1m};
|
||||||
use curate_etl::run_curate_corporate_actions::{
|
use curate_etl::run_curate_corporate_actions::{
|
||||||
format_curate_corporate_actions_report, run_curate_corporate_actions,
|
format_curate_corporate_actions_report, run_curate_corporate_actions,
|
||||||
};
|
};
|
||||||
|
|
@ -197,6 +198,16 @@ fn main() -> ExitCode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(CliAction::CurateBars1m(curate_args)) => match run_curate_bars_1m(&curate_args) {
|
||||||
|
Ok(report) => {
|
||||||
|
println!("{}", format_curate_bars_1m_report(&report));
|
||||||
|
ExitCode::SUCCESS
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("error: {err}");
|
||||||
|
ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
},
|
||||||
Ok(CliAction::BenchSplit(rows)) => {
|
Ok(CliAction::BenchSplit(rows)) => {
|
||||||
let report = match run_split_benchmark(rows) {
|
let report = match run_split_benchmark(rows) {
|
||||||
Ok(report) => report,
|
Ok(report) => report,
|
||||||
|
|
|
||||||
330
tools/curate-etl/tests/curate_bars_1m.rs
Normal file
330
tools/curate-etl/tests/curate_bars_1m.rs
Normal file
|
|
@ -0,0 +1,330 @@
|
||||||
|
//! End-to-end binary integration tests for `curate-etl curate-bars-1m` (`M2.3n`).
|
||||||
|
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::process::{Command, Output};
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
|
use bson::raw::{cstr, RawDocumentBuf};
|
||||||
|
use curate_etl::{
|
||||||
|
et_to_utc_micros, format_curate_bars_1m_report, run_curate_bars_1m, ymd_to_epoch_days,
|
||||||
|
BsonStreamOptions, CurateBars1mArgs, IlpWriterOptions, DEFAULT_BARS_1M_MAPPING_SOURCE,
|
||||||
|
};
|
||||||
|
|
||||||
|
static TEST_DIR_SEQ: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
struct TempDir {
|
||||||
|
path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TempDir {
|
||||||
|
fn new(tag: &str) -> Self {
|
||||||
|
let pid = std::process::id();
|
||||||
|
let seq = TEST_DIR_SEQ.fetch_add(1, Ordering::Relaxed);
|
||||||
|
let path =
|
||||||
|
std::env::temp_dir().join(format!("curate-etl-it-bars-1m-{tag}-{pid}-{seq}"));
|
||||||
|
let _ = fs::remove_dir_all(&path);
|
||||||
|
fs::create_dir_all(&path).expect("create integration test temp dir");
|
||||||
|
Self { path }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn path(&self) -> &Path {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TempDir {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let _ = fs::remove_dir_all(&self.path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_bin(args: &[&str]) -> Output {
|
||||||
|
Command::new(env!("CARGO_BIN_EXE_curate-etl"))
|
||||||
|
.args(args)
|
||||||
|
.output()
|
||||||
|
.expect("spawn curate-etl binary")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn day(y: i32, m: u32, d: u32) -> i32 {
|
||||||
|
ymd_to_epoch_days(y, m, d).expect("valid date")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn et_ts_sec(y: i32, m: u32, d: u32, hour: u8, minute: u8) -> i64 {
|
||||||
|
let micros = et_to_utc_micros(day(y, m, d), hour, minute, 0).expect("valid ET timestamp");
|
||||||
|
assert_eq!(micros % 1_000_000, 0);
|
||||||
|
micros / 1_000_000
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_intraday_doc(
|
||||||
|
code: &str,
|
||||||
|
timestamp_sec: i64,
|
||||||
|
open: f64,
|
||||||
|
high: f64,
|
||||||
|
low: f64,
|
||||||
|
close: f64,
|
||||||
|
volume: f64,
|
||||||
|
) -> RawDocumentBuf {
|
||||||
|
let mut doc = RawDocumentBuf::new();
|
||||||
|
doc.append(cstr!("eodSearchCode"), code);
|
||||||
|
doc.append(cstr!("timestamp"), timestamp_sec);
|
||||||
|
doc.append(cstr!("open"), open);
|
||||||
|
doc.append(cstr!("high"), high);
|
||||||
|
doc.append(cstr!("low"), low);
|
||||||
|
doc.append(cstr!("close"), close);
|
||||||
|
doc.append(cstr!("volume"), volume);
|
||||||
|
doc
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_split_doc(
|
||||||
|
qm_search_code: &str,
|
||||||
|
ex_date: &str,
|
||||||
|
for_factor: f64,
|
||||||
|
to_factor: f64,
|
||||||
|
) -> RawDocumentBuf {
|
||||||
|
let mut doc = RawDocumentBuf::new();
|
||||||
|
doc.append(cstr!("qmSearchCode"), qm_search_code);
|
||||||
|
doc.append(cstr!("date"), ex_date);
|
||||||
|
doc.append(cstr!("forfactor"), for_factor);
|
||||||
|
doc.append(cstr!("tofactor"), to_factor);
|
||||||
|
doc.append(cstr!("splitratio"), for_factor / to_factor);
|
||||||
|
doc
|
||||||
|
}
|
||||||
|
|
||||||
|
fn concat_docs(docs: &[RawDocumentBuf]) -> Vec<u8> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for doc in docs {
|
||||||
|
out.extend_from_slice(doc.as_bytes());
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_dir_files(dir: &Path) -> BTreeMap<String, Vec<u8>> {
|
||||||
|
let mut out = BTreeMap::new();
|
||||||
|
for entry in fs::read_dir(dir).expect("read output dir") {
|
||||||
|
let entry = entry.expect("dir entry");
|
||||||
|
let name = entry
|
||||||
|
.file_name()
|
||||||
|
.into_string()
|
||||||
|
.expect("UTF-8 file name in output dir");
|
||||||
|
let bytes = fs::read(entry.path()).expect("read output file");
|
||||||
|
out.insert(name, bytes);
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strip_timing_line(report: &str) -> Vec<&str> {
|
||||||
|
report
|
||||||
|
.lines()
|
||||||
|
.filter(|line| !line.trim_start().starts_with("timing:"))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn curate_bars_1m_binary_matches_direct_runner_bytes_and_report() {
|
||||||
|
let tmp = TempDir::new("parity");
|
||||||
|
|
||||||
|
// Pin NVDA 10:1 forward split (2024-06-10) and NEXM 1:20 reverse split (2025-06-20)
|
||||||
|
// fixtures in the style of src/run_curate_bars_1m_tests.rs.
|
||||||
|
let intraday_path = tmp.path().join("eodIntraday1M.bson");
|
||||||
|
let intraday_bytes = concat_docs(&[
|
||||||
|
// NVDA pre-split (2024-06-07): 10:1 split un-adjusts prices x10 and volume /10.
|
||||||
|
make_intraday_doc(
|
||||||
|
"NVDA.US",
|
||||||
|
et_ts_sec(2024, 6, 7, 15, 59),
|
||||||
|
120.50,
|
||||||
|
121.00,
|
||||||
|
120.40,
|
||||||
|
120.888,
|
||||||
|
412_390.0,
|
||||||
|
),
|
||||||
|
// NVDA ex-date (2024-06-10): already post-split (factor = 1.0).
|
||||||
|
make_intraday_doc(
|
||||||
|
"NVDA.US",
|
||||||
|
et_ts_sec(2024, 6, 10, 15, 59),
|
||||||
|
121.50,
|
||||||
|
122.00,
|
||||||
|
121.20,
|
||||||
|
121.79,
|
||||||
|
350_000.0,
|
||||||
|
),
|
||||||
|
// NEXM pre-split (2025-06-18): 1:20 reverse split un-adjusts prices x0.05 and volume x20.
|
||||||
|
make_intraday_doc(
|
||||||
|
"NEXM.V",
|
||||||
|
et_ts_sec(2025, 6, 18, 15, 59),
|
||||||
|
24.20,
|
||||||
|
24.40,
|
||||||
|
24.10,
|
||||||
|
24.29,
|
||||||
|
5_000.0,
|
||||||
|
),
|
||||||
|
// NEXM ex-date (2025-06-20): already post-split (factor = 1.0).
|
||||||
|
make_intraday_doc(
|
||||||
|
"NEXM.V",
|
||||||
|
et_ts_sec(2025, 6, 20, 15, 59),
|
||||||
|
24.10,
|
||||||
|
24.35,
|
||||||
|
24.00,
|
||||||
|
24.28,
|
||||||
|
12_500.0,
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
fs::write(&intraday_path, &intraday_bytes).expect("write intraday bson");
|
||||||
|
|
||||||
|
let splits_path = tmp.path().join("qmSplits.bson");
|
||||||
|
let splits_bytes = concat_docs(&[
|
||||||
|
make_split_doc("NVDA", "2024-06-10", 1.0, 10.0),
|
||||||
|
make_split_doc("NEXM:CA", "2025-06-20", 20.0, 1.0),
|
||||||
|
]);
|
||||||
|
fs::write(&splits_path, &splits_bytes).expect("write splits bson");
|
||||||
|
|
||||||
|
let mapping_path = tmp.path().join("provider_mapping.tsv");
|
||||||
|
fs::write(
|
||||||
|
&mapping_path,
|
||||||
|
"eod\tNVDA.US\t2000-01-01\t\t1001\n\
|
||||||
|
qm\tNVDA\t2000-01-01\t\t1001\n\
|
||||||
|
eod\tNEXM.V\t2000-01-01\t\t2002\n\
|
||||||
|
qm\tNEXM:CA\t2000-01-01\t\t2002\n",
|
||||||
|
)
|
||||||
|
.expect("write provider_mapping.tsv");
|
||||||
|
|
||||||
|
let known_at_micros: i64 = 1_750_000_000_000_000;
|
||||||
|
let shard_count: usize = 2;
|
||||||
|
|
||||||
|
let bin_out = tmp.path().join("out-bin");
|
||||||
|
let direct_out = tmp.path().join("out-direct");
|
||||||
|
|
||||||
|
let output = run_bin(&[
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday",
|
||||||
|
intraday_path.to_str().unwrap(),
|
||||||
|
"--qm-splits",
|
||||||
|
splits_path.to_str().unwrap(),
|
||||||
|
"--provider-mapping",
|
||||||
|
mapping_path.to_str().unwrap(),
|
||||||
|
"--out",
|
||||||
|
bin_out.to_str().unwrap(),
|
||||||
|
"--shards",
|
||||||
|
"2",
|
||||||
|
"--known-at-micros",
|
||||||
|
"1750000000000000",
|
||||||
|
]);
|
||||||
|
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
assert!(
|
||||||
|
output.status.success(),
|
||||||
|
"expected exit 0, got {:?}; stderr:\n{stderr}\nstdout:\n{stdout}",
|
||||||
|
output.status.code()
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
stderr.is_empty(),
|
||||||
|
"expected empty stderr on success, got:\n{stderr}"
|
||||||
|
);
|
||||||
|
|
||||||
|
let direct_args = CurateBars1mArgs {
|
||||||
|
intraday_path,
|
||||||
|
splits_path,
|
||||||
|
provider_mapping_path: mapping_path,
|
||||||
|
out_dir: direct_out.clone(),
|
||||||
|
mapping_source: DEFAULT_BARS_1M_MAPPING_SOURCE.to_string(),
|
||||||
|
shard_count,
|
||||||
|
known_at_micros,
|
||||||
|
stream_options: BsonStreamOptions::default(),
|
||||||
|
ilp_options: IlpWriterOptions::default(),
|
||||||
|
};
|
||||||
|
let direct_report = run_curate_bars_1m(&direct_args).expect("direct run_curate_bars_1m");
|
||||||
|
assert_eq!(
|
||||||
|
direct_report.rows_written, 4,
|
||||||
|
"expected 4 emitted bars_1m rows"
|
||||||
|
);
|
||||||
|
|
||||||
|
let direct_formatted = format_curate_bars_1m_report(&direct_report);
|
||||||
|
assert_eq!(
|
||||||
|
strip_timing_line(&stdout),
|
||||||
|
strip_timing_line(&direct_formatted),
|
||||||
|
"binary stdout report should match direct runner report excluding timing line"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
stdout
|
||||||
|
.lines()
|
||||||
|
.any(|line| line.trim_start().starts_with("timing: elapsed_secs=")),
|
||||||
|
"expected timing line in stdout:\n{stdout}"
|
||||||
|
);
|
||||||
|
|
||||||
|
let bin_files = read_dir_files(&bin_out);
|
||||||
|
let direct_files = read_dir_files(&direct_out);
|
||||||
|
|
||||||
|
let expected_names: Vec<String> = vec![
|
||||||
|
"bars_1m.shard-00.ilp".to_string(),
|
||||||
|
"bars_1m.shard-01.ilp".to_string(),
|
||||||
|
];
|
||||||
|
assert_eq!(
|
||||||
|
bin_files.keys().cloned().collect::<Vec<_>>(),
|
||||||
|
expected_names,
|
||||||
|
"unexpected files in binary output dir"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
bin_files, direct_files,
|
||||||
|
"binary shard files must be byte-identical to direct runner output"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
bin_files.values().map(Vec::len).sum::<usize>() > 0,
|
||||||
|
"expected non-empty ILP output across shards"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn curate_bars_1m_binary_missing_input_exits_nonzero_and_leaves_out_clean() {
|
||||||
|
let tmp = TempDir::new("missing-input");
|
||||||
|
|
||||||
|
let missing_intraday = tmp.path().join("does-not-exist.bson");
|
||||||
|
let splits_path = tmp.path().join("qmSplits.bson");
|
||||||
|
fs::write(&splits_path, concat_docs(&[])).expect("write empty splits bson");
|
||||||
|
|
||||||
|
let mapping_path = tmp.path().join("provider_mapping.tsv");
|
||||||
|
fs::write(&mapping_path, "eod\tNVDA.US\t2000-01-01\t\t1001\n")
|
||||||
|
.expect("write provider_mapping.tsv");
|
||||||
|
|
||||||
|
let out_dir = tmp.path().join("out-empty");
|
||||||
|
fs::create_dir_all(&out_dir).expect("create empty out dir");
|
||||||
|
|
||||||
|
let output = run_bin(&[
|
||||||
|
"curate-bars-1m",
|
||||||
|
"--eod-intraday",
|
||||||
|
missing_intraday.to_str().unwrap(),
|
||||||
|
"--qm-splits",
|
||||||
|
splits_path.to_str().unwrap(),
|
||||||
|
"--provider-mapping",
|
||||||
|
mapping_path.to_str().unwrap(),
|
||||||
|
"--out",
|
||||||
|
out_dir.to_str().unwrap(),
|
||||||
|
"--shards",
|
||||||
|
"2",
|
||||||
|
"--known-at-micros",
|
||||||
|
"1750000000000000",
|
||||||
|
]);
|
||||||
|
|
||||||
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
assert!(
|
||||||
|
!output.status.success(),
|
||||||
|
"expected non-zero exit when intraday file is missing"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
stdout.is_empty(),
|
||||||
|
"expected no success report on stdout, got:\n{stdout}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
stderr.contains("error:") && stderr.contains("failed to open eod-intraday"),
|
||||||
|
"expected input-open error on stderr, got:\n{stderr}"
|
||||||
|
);
|
||||||
|
|
||||||
|
let remaining = read_dir_files(&out_dir);
|
||||||
|
assert!(
|
||||||
|
remaining.is_empty(),
|
||||||
|
"expected no shard or staging files in output dir, found: {:?}",
|
||||||
|
remaining.keys().collect::<Vec<_>>()
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue