Skip to content

Commit ed4e462

Browse files
committed
fix(ingest): enforce non-null nextState across sources
1 parent 2432a8d commit ed4e462

19 files changed

Lines changed: 29 additions & 85 deletions

src/modules/ingest/app/sources/airkorea-o3-warning.source.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ export class AirkoreaO3WarningSource implements Source {
5252

5353
const html = await response.text();
5454
const rows = parseWarningRows(html);
55+
const previousState = parseState(state);
56+
const seen = new Map<string, string>(Object.entries(previousState.seen));
5557
if (rows.length === 0) {
56-
return { events: [], nextState: state };
58+
return { events: [], nextState: buildState(seen) };
5759
}
5860

5961
const now = new Date();
6062
const nowIso = now.toISOString();
6163
const nowMs = now.getTime();
6264
const groups = groupWarningRows(rows, nowMs);
63-
const previousState = parseState(state);
64-
const seen = new Map<string, string>(Object.entries(previousState.seen));
6565

6666
const events: SourceEvent[] = [];
6767
for (const group of groups) {
@@ -233,11 +233,7 @@ const parseState = (state: string | null): O3WarningState => {
233233
}
234234
};
235235

236-
const buildState = (seen: Map<string, string>): string | null => {
237-
if (seen.size === 0) {
238-
return null;
239-
}
240-
236+
const buildState = (seen: Map<string, string>): string => {
241237
const payload: Record<string, string> = {};
242238
for (const [key, value] of seen) {
243239
payload[key] = value;

src/modules/ingest/app/sources/airkorea-pm-warning.source.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ export class AirkoreaPmWarningSource implements Source {
8989
page += 1;
9090
}
9191

92+
const previousState = parseState(state);
93+
const seen = new Map<string, string>(Object.entries(previousState.seen));
9294
if (rows.length === 0) {
93-
return { events: [], nextState: state };
95+
return { events: [], nextState: buildState(seen) };
9496
}
9597

9698
const groups = groupWarningRows(rows, nowMs);
97-
const previousState = parseState(state);
98-
const seen = new Map<string, string>(Object.entries(previousState.seen));
9999

100100
const events: SourceEvent[] = [];
101101
for (const group of groups) {
@@ -285,11 +285,7 @@ const parseState = (state: string | null): PmWarningState => {
285285
}
286286
};
287287

288-
const buildState = (seen: Map<string, string>): string | null => {
289-
if (seen.size === 0) {
290-
return null;
291-
}
292-
288+
const buildState = (seen: Map<string, string>): string => {
293289
const payload: Record<string, string> = {};
294290
for (const [key, value] of seen) {
295291
payload[key] = value;

src/modules/ingest/app/sources/disaster-sms.source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,9 @@ const parseSerial = (value: string | null): number | null => {
492492
return Math.trunc(parsed);
493493
};
494494

495-
const getNextSerialState = (items: DisasterSmsItem[], lastSeenSerial: number | null): string | null => {
495+
const getNextSerialState = (items: DisasterSmsItem[], lastSeenSerial: number | null): string => {
496496
if (items.length === 0) {
497-
return lastSeenSerial === null ? null : String(lastSeenSerial);
497+
return lastSeenSerial === null ? '' : String(lastSeenSerial);
498498
}
499499

500500
const maxSerial = Math.max(...items.map((item) => item.MD101_SN));

src/modules/ingest/app/sources/flood-alert.source.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,6 @@ function parseState(state: string | null): FloodAlertState {
338338
}
339339
}
340340

341-
function buildState(seen: Map<string, string>): string | null {
342-
if (seen.size === 0) {
343-
return null;
344-
}
345-
341+
function buildState(seen: Map<string, string>): string {
346342
return JSON.stringify({ seen: Object.fromEntries(seen) });
347343
}

src/modules/ingest/app/sources/kasa-space-weather-crisis-alert.source.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,7 @@ function parseState(state: string | null): SpaceWeatherCrisisState {
227227
}
228228
}
229229

230-
function buildState(seen: Map<string, string>): string | null {
231-
if (seen.size === 0) {
232-
return null;
233-
}
234-
230+
function buildState(seen: Map<string, string>): string {
235231
const payload: Record<string, string> = {};
236232
for (const [key, value] of seen) {
237233
payload[key] = value;

src/modules/ingest/app/sources/kdca-press-release.source.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,7 @@ function parseState(state: string | null): KdcaPressState {
272272
}
273273
}
274274

275-
function buildState(seen: Map<string, string>): string | null {
276-
if (seen.size === 0) {
277-
return null;
278-
}
279-
275+
function buildState(seen: Map<string, string>): string {
280276
const payload: Record<string, string> = {};
281277
for (const [key, value] of seen) {
282278
payload[key] = value;

src/modules/ingest/app/sources/kma-aws-observation.source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ function parseState(state: string | null): AwsState {
960960
}
961961
}
962962

963-
function buildState(stateMap: Map<string, AwsStateEntry>): string | null {
963+
function buildState(stateMap: Map<string, AwsStateEntry>): string {
964964
return JSON.stringify({ entries: Object.fromEntries(stateMap) });
965965
}
966966

src/modules/ingest/app/sources/kma-overseas-earthquake.source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,6 @@ function parseState(state: string | null): OverseasEarthquakeState {
471471
}
472472
}
473473

474-
function buildState(seen: Map<string, string>): string | null {
474+
function buildState(seen: Map<string, string>): string {
475475
return JSON.stringify({ seen: Object.fromEntries(seen) });
476476
}

src/modules/ingest/app/sources/kma-pews-earthquake.source.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class KmaPewsEarthquakeSource implements Source {
7575
const simulatedTimeMs = Date.now() - this.timeOffsetMs;
7676
if (simulatedTimeMs >= this.simEndUtcMs) {
7777
this.stopSimulation();
78-
return { events: [], nextState: state };
78+
return { events: [], nextState: buildState(parsedState) };
7979
}
8080
}
8181

@@ -107,7 +107,7 @@ export class KmaPewsEarthquakeSource implements Source {
107107

108108
const phase = parsePhase(bytes, this.headerLengthBytes);
109109
if (phase < 2) {
110-
return { events: [], nextState: state };
110+
return { events: [], nextState: buildState(parsedState) };
111111
}
112112

113113
const info = parseEarthquakeInfo(bytes);
@@ -119,7 +119,7 @@ export class KmaPewsEarthquakeSource implements Source {
119119
const previousAlarmId = buildAlarmId(parsedState.lastEqkId, parsedState.lastPhase);
120120
const currentAlarmId = buildAlarmId(info.eqkId, phase);
121121
if (currentAlarmId && previousAlarmId === currentAlarmId) {
122-
return { events: [], nextState: state };
122+
return { events: [], nextState: buildState(parsedState) };
123123
}
124124

125125
const event = buildEarthquakeEvent(phase, info, parsedState.lastEqkId, parsedState.lastPhase, binTimeStr);
@@ -232,11 +232,7 @@ const parseState = (state: string | null): PewsState => {
232232
}
233233
};
234234

235-
const buildState = (state: PewsState): string | null => {
236-
if (!state.lastEqkId && state.lastPhase === null) {
237-
return null;
238-
}
239-
235+
const buildState = (state: PewsState): string => {
240236
return JSON.stringify(state);
241237
};
242238

src/modules/ingest/app/sources/kma-weather-warning.source.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,7 @@ const parseState = (state: string | null): WarningState => {
358358
}
359359
};
360360

361-
const buildState = (seen: Map<string, string>): string | null => {
362-
if (seen.size === 0) {
363-
return null;
364-
}
365-
361+
const buildState = (seen: Map<string, string>): string => {
366362
return JSON.stringify({ seen: Object.fromEntries(seen) });
367363
};
368364

0 commit comments

Comments
 (0)