stock-bot/tools/curate-etl/src/parquet_posts.rs

109 lines
4.6 KiB
Rust

//! Adapter mapping curated `ceoPosts` rows ([`PostRow<i32>`] / [`PostsBuild<i32>`]) into the
//! 7-column `events` Parquet contract ([`EventsParquetRow`], `events/year=YYYY/part-posts.parquet`).
//!
//! Aligned with `design/DATA-CONTRACTS.md` §1 & §3A and `design/PLATFORM-PLAN.md`:
//! - `kind` is always [`EVENT_KIND_POST`] (`"ceo_post"`).
//! - `subtype` is [`POST_SUBTYPE_NEWSWIRE`] (`"newswire"`) when `is_bot` is `true`, and
//! [`POST_SUBTYPE_RETAIL`] (`"retail"`) otherwise, so wire releases are never pooled with crowd
//! chat.
//! - `ts` is the posting timestamp (`posted_at`) and `known_at` is the point-in-time text
//! availability timestamp (`known_at >= posted_at`).
//! - `known_at_source` is [`KNOWN_AT_SOURCE_POST_EDITED`] (`"post_edited"`) iff
//! `known_at > posted_at`, and [`KNOWN_AT_SOURCE_POST_TIMESTAMP`] (`"post_timestamp"`) otherwise
//! (including when an edit timestamp before `posted_at` was clamped back to `posted_at`).
use std::borrow::Cow;
use crate::events::{push_json_opt_str, push_json_str};
use crate::parquet_events::EventsParquetRow;
use crate::posts::{PostRow, PostsBuild};
/// Canonical `events.kind` label for curated `ceoPosts` rows.
pub const EVENT_KIND_POST: &str = "ceo_post";
/// Canonical `events.subtype` label for `bot == "1"` newswire posts.
pub const POST_SUBTYPE_NEWSWIRE: &str = "newswire";
/// Canonical `events.subtype` label for non-bot retail posts.
pub const POST_SUBTYPE_RETAIL: &str = "retail";
/// Provenance label when `known_at` equals the post's creation timestamp (`posted_at`).
pub const KNOWN_AT_SOURCE_POST_TIMESTAMP: &str = "post_timestamp";
/// Provenance label when `known_at` reflects a post-creation edit timestamp (`known_at > posted_at`).
pub const KNOWN_AT_SOURCE_POST_EDITED: &str = "post_edited";
/// Encodes a curated post's `payload_json` as a compact RFC 8259 JSON object with fixed key order:
/// `{"spiel_id":...,"user_id":...,"is_edited":...,"source":...,"text":...}`.
fn encode_post_payload_json(row: &PostRow<i32>) -> String {
let cap = 72
+ row.spiel_id.len()
+ row.user_id.as_deref().map_or(4, str::len)
+ row.source.len()
+ row.text.len();
let mut out = String::with_capacity(cap);
out.push_str("{\"spiel_id\":");
push_json_str(&mut out, &row.spiel_id);
out.push_str(",\"user_id\":");
push_json_opt_str(&mut out, row.user_id.as_deref());
out.push_str(",\"is_edited\":");
out.push_str(if row.is_edited { "true" } else { "false" });
out.push_str(",\"source\":");
push_json_str(&mut out, row.source);
out.push_str(",\"text\":");
push_json_str(&mut out, &row.text);
out.push('}');
out
}
impl From<&PostRow<i32>> for EventsParquetRow {
fn from(row: &PostRow<i32>) -> Self {
let known_at_source = if row.known_at > row.posted_at {
KNOWN_AT_SOURCE_POST_EDITED
} else {
KNOWN_AT_SOURCE_POST_TIMESTAMP
};
let subtype = if row.is_bot {
POST_SUBTYPE_NEWSWIRE
} else {
POST_SUBTYPE_RETAIL
};
Self {
listing_id: row.key,
ts_micros: row.posted_at,
known_at_micros: row.known_at,
known_at_source: Cow::Borrowed(known_at_source),
kind: Cow::Borrowed(EVENT_KIND_POST),
subtype: subtype.to_string(),
payload_json: encode_post_payload_json(row),
}
}
}
impl From<PostRow<i32>> for EventsParquetRow {
fn from(row: PostRow<i32>) -> Self {
Self::from(&row)
}
}
/// Converts both `newswire` and `retail` streams of a [`PostsBuild<i32>`] into
/// [`EventsParquetRow`] records sorted in the exact order required by
/// [`crate::parquet_events::write_posts_events_dataset`]:
/// `(listing_id, ts_micros, kind, subtype, payload_json, known_at_micros, known_at_source)`.
pub fn posts_to_event_rows(build: &PostsBuild<i32>) -> Vec<EventsParquetRow> {
let mut rows = Vec::with_capacity(build.newswire.len() + build.retail.len());
rows.extend(build.newswire.iter().map(EventsParquetRow::from));
rows.extend(build.retail.iter().map(EventsParquetRow::from));
rows.sort_unstable_by(|a, b| {
a.listing_id
.cmp(&b.listing_id)
.then_with(|| a.ts_micros.cmp(&b.ts_micros))
.then_with(|| a.kind.as_ref().cmp(b.kind.as_ref()))
.then_with(|| a.subtype.cmp(&b.subtype))
.then_with(|| a.payload_json.cmp(&b.payload_json))
.then_with(|| a.known_at_micros.cmp(&b.known_at_micros))
.then_with(|| a.known_at_source.as_ref().cmp(b.known_at_source.as_ref()))
});
rows
}