mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
refactor: share unique sorted list normalization helper (#1484)
This commit is contained in:
3
server/_shared/normalize-list.ts
Normal file
3
server/_shared/normalize-list.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function toUniqueSortedLimited(values: string[], limit: number): string[] {
|
||||
return Array.from(new Set(values)).sort().slice(0, limit);
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
import { getCachedJsonBatch, cachedFetchJson } from '../../../_shared/redis';
|
||||
import { CHROME_UA } from '../../../_shared/constants';
|
||||
import { ISO2_TO_ISO3 } from './_shared';
|
||||
import { toUniqueSortedLimited } from '../../../_shared/normalize-list';
|
||||
|
||||
const REDIS_CACHE_KEY = 'conflict:humanitarian:v1';
|
||||
const REDIS_CACHE_TTL = 21600;
|
||||
@@ -100,8 +101,7 @@ export async function getHumanitarianSummaryBatch(
|
||||
const normalized = req.countryCodes
|
||||
.map((c) => c.trim().toUpperCase())
|
||||
.filter((c) => ISO2_PATTERN.test(c));
|
||||
const uniqueSorted = Array.from(new Set(normalized)).sort();
|
||||
const limitedList = uniqueSorted.slice(0, 25);
|
||||
const limitedList = toUniqueSortedLimited(normalized, 25);
|
||||
|
||||
const results: Record<string, HumanitarianCountrySummary> = {};
|
||||
const toFetch: string[] = [];
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
} from '../../../../src/generated/server/worldmonitor/economic/v1/service_server';
|
||||
|
||||
import { getCachedJsonBatch, cachedFetchJson } from '../../../_shared/redis';
|
||||
import { toUniqueSortedLimited } from '../../../_shared/normalize-list';
|
||||
|
||||
const FRED_API_BASE = 'https://api.stlouisfed.org/fred';
|
||||
const REDIS_CACHE_KEY = 'economic:fred:v1';
|
||||
@@ -73,8 +74,7 @@ export async function getFredSeriesBatch(
|
||||
const normalized = req.seriesIds
|
||||
.map((id) => id.trim().toUpperCase())
|
||||
.filter((id) => ALLOWED_SERIES.has(id));
|
||||
const uniqueSorted = Array.from(new Set(normalized)).sort();
|
||||
const limitedList = uniqueSorted.slice(0, 10);
|
||||
const limitedList = toUniqueSortedLimited(normalized, 10);
|
||||
const limit = req.limit > 0 ? Math.min(req.limit, 1000) : 120;
|
||||
|
||||
const results: Record<string, FredSeries> = {};
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
import { mapWingbitsDetails } from './_shared';
|
||||
import { CHROME_UA } from '../../../_shared/constants';
|
||||
import { getCachedJsonBatch, cachedFetchJson } from '../../../_shared/redis';
|
||||
import { toUniqueSortedLimited } from '../../../_shared/normalize-list';
|
||||
|
||||
interface CachedAircraftDetails {
|
||||
details: AircraftDetails | null;
|
||||
@@ -25,8 +26,7 @@ export async function getAircraftDetailsBatch(
|
||||
const normalized = req.icao24s
|
||||
.map((id) => id.trim().toLowerCase())
|
||||
.filter((id) => id.length > 0);
|
||||
const uniqueSorted = Array.from(new Set(normalized)).sort();
|
||||
const limitedList = uniqueSorted.slice(0, 10);
|
||||
const limitedList = toUniqueSortedLimited(normalized, 10);
|
||||
|
||||
// Redis shared cache — batch GET all keys in a single pipeline round-trip
|
||||
const SINGLE_KEY = 'military:aircraft:v1';
|
||||
|
||||
Reference in New Issue
Block a user