Coverage for src/retrospeedlab/lake/sources/silver.py: 100%
40 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-02 22:44 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-02 22:44 +0000
1from pyspark.sql import DataFrame, SparkSession
2from pyspark.sql import functions as F
3from pyspark.sql.types import (
4 DateType,
5 DoubleType,
6 LongType,
7 MapType,
8 StringType,
9 StructField,
10 StructType,
11 TimestampType,
12)
14from retrospeedlab.lake.batch.lab_runs import without_deleted_lab_runs
15from retrospeedlab.lake.lake.catalog import SOURCES
16from retrospeedlab.lake.lake.tables import exists, read
17from retrospeedlab.lake.lake.write import replace_counted
18from retrospeedlab.lake.quality.gate import Gate
19from retrospeedlab.lake.settings import LakeSettings
20from retrospeedlab.lake.sources.bronze import latest
21from retrospeedlab.lake.sources.catalog import (
22 SILVER_BLUESKY_POSTS,
23 SILVER_DROPOFF,
24 SILVER_ITCH_DAILY,
25 SILVER_ITCH_PLAYS,
26 SILVER_LAB_COSTS,
27 SILVER_LAB_ROUNDS,
28 SILVER_NUMBERS,
29 SILVER_PLAYS,
30 TRACE_DAILY,
31 YOUTUBE_DAILY,
32)
33from retrospeedlab.lake.sources.lab_traces import LAB_ROUND
34from retrospeedlab.lake.sources.registry import DAILY, DROPOFF, LAB_COSTS, NUMBERS, PLAYS, POSTS
35from retrospeedlab.lake.sources.rounds import ROUNDS, TEMPO
36from retrospeedlab.lake.sources.youtube import METRICS
38NUMBER = StructType(
39 [
40 StructField("metric", StringType(), False),
41 StructField("label", StringType(), False),
42 StructField("value", DoubleType(), False),
43 StructField("unit", StringType(), False),
44 StructField("window", StringType(), False),
45 ]
46)
47DAILY_COST = StructType(
48 [
49 StructField("date", DateType(), False),
50 StructField("observations", LongType(), False),
51 StructField("tokens", LongType(), False),
52 StructField("cost_usd", DoubleType(), False),
53 ]
54)
55COUNTS = MapType(StringType(), LongType(), False)
56MODE_PLAYS = StructType(
57 [
58 StructField("mode", StringType(), False),
59 StructField("window", StringType(), False),
60 StructField("started", COUNTS, False),
61 StructField("won", COUNTS, False),
62 ]
63)
65DROPOFF_COUNTS = StructType(
66 [
67 StructField("game", StringType(), False),
68 StructField("ended", COUNTS, False),
69 StructField("won", LongType(), False),
70 StructField("since", TimestampType(), False),
71 ]
72)
73ITCH_DAY = StructType(
74 [
75 StructField("date", DateType(), False),
76 StructField("pageviews", LongType(), False),
77 StructField("visitors", LongType(), False),
78 StructField("views", COUNTS, False),
79 StructField("campaign", COUNTS, False),
80 StructField("endless", COUNTS, False),
81 StructField("cleared", COUNTS, False),
82 StructField("won", COUNTS, False),
83 ]
84)
85LAB_COST = StructType(
86 [
87 StructField("stamp", StringType(), False),
88 StructField("observations", LongType(), False),
89 StructField("tokens", LongType(), False),
90 StructField("cost_usd", DoubleType(), False),
91 ]
92)
93BLUESKY_POST = StructType(
94 [
95 StructField("uri", StringType(), False),
96 StructField("text", StringType(), False),
97 StructField("created_at", TimestampType(), False),
98 StructField("likes", LongType(), False),
99 StructField("reposts", LongType(), False),
100 StructField("replies", LongType(), False),
101 StructField("quotes", LongType(), False),
102 StructField("followers", LongType(), True),
103 ]
104)
105YOUTUBE_DAY = StructType(
106 [
107 StructField("date", DateType(), False),
108 *(StructField(name, LongType(), False) for _, name, _, _ in METRICS),
109 ]
110)
113def newest(records: DataFrame, stream: str, schema: StructType) -> DataFrame:
114 rows = records.where(F.col("stream") == stream)
115 latest = rows.groupBy("source").agg(F.max("fetched_at").alias("fetched_at"))
116 return (
117 rows.join(latest, ["source", "fetched_at"])
118 .select("source", F.from_json("body", schema).alias("item"), F.col("fetched_at").alias("measured_at"))
119 .select("source", "item.*", "measured_at")
120 )
123def plays_of(records: DataFrame, source: str) -> DataFrame:
124 return newest(records.where(F.col("source") == source), PLAYS, MODE_PLAYS).drop("source")
127def parsed(records: DataFrame, schema: StructType) -> DataFrame:
128 return records.select(F.from_json("body", schema).alias("item"), F.col("fetched_at").alias("measured_at")).select(
129 "item.*", "measured_at"
130 )
133def stream_of(records: DataFrame, source: str, stream: str) -> DataFrame:
134 return records.where((F.col("source") == source) & (F.col("stream") == stream))
137def build_source_silver(spark: SparkSession, lake: LakeSettings, gate: Gate) -> int:
138 if not exists(spark, SOURCES, lake):
139 return 0
140 records = read(spark, SOURCES, lake)
141 streams = [
142 (SILVER_NUMBERS, newest(records, NUMBERS, NUMBER)),
143 (SILVER_PLAYS, plays_of(records, "umami")),
144 (SILVER_ITCH_PLAYS, plays_of(records, "itch")),
145 (SILVER_DROPOFF, newest(records, DROPOFF, DROPOFF_COUNTS).drop("source")),
146 (SILVER_LAB_COSTS, without_deleted_lab_runs(spark, lake, newest(records, LAB_COSTS, LAB_COST).drop("source"))),
147 (TRACE_DAILY, parsed(latest(spark, lake, TEMPO, DAILY), DAILY_COST)),
148 (
149 SILVER_LAB_ROUNDS,
150 without_deleted_lab_runs(spark, lake, parsed(latest(spark, lake, TEMPO, ROUNDS), LAB_ROUND)),
151 ),
152 (SILVER_ITCH_DAILY, parsed(latest(spark, lake, "itch", DAILY), ITCH_DAY)),
153 (SILVER_BLUESKY_POSTS, parsed(stream_of(records, "bluesky", POSTS), BLUESKY_POST)),
154 (YOUTUBE_DAILY, parsed(latest(spark, lake, "youtube", DAILY), YOUTUBE_DAY)),
155 ]
156 return sum(replace_counted(table, lake, rows, gate) for table, rows in streams)