mirror of
https://github.com/FoggedLens/deflock.git
synced 2026-12-06 17:43:45 +01:00
zip code short circuit
This commit is contained in:
1 parent
3019d87f53
commit
ae861e8e7b
6 files changed
+91
-10
No files matched your search
@@ -0,0 +1 @@
|
||||
{"10001":[40.7484,-73.9967,"New York","NY"],"10002":[40.7152,-73.9877,"New York","NY"],"10003":[40.7313,-73.9892,"New York","NY"],"10004":[40.7143,-74.006,"New York","NY"],"10005":[40.7056,-74.0083,"New York","NY"],"10006":[40.7085,-74.0135,"New York","NY"],"10007":[40.7139,-74.007,"New York","NY"],"10008":[40.7143,-74.006,"New York","NY"],"10009":[40.7262,-73.9796,"New York","NY"],"10010":[40.7375,-73.9813,"New York","NY"],"10011":[40.7402,-73.9996,"New York","NY"],"10012":[40.7255,-73.9983,"New York","NY"],"10013":[40.7185,-74.0025,"New York","NY"],"10014":[40.7339,-74.0054,"New York","NY"],"10016":[40.7443,-73.9781,"New York","NY"],"10017":[40.7517,-73.9707,"New York","NY"],"10018":[40.7547,-73.9925,"New York","NY"],"10019":[40.7651,-73.9858,"New York","NY"],"10020":[40.7354,-73.9968,"New York","NY"],"10021":[40.7685,-73.9588,"New York","NY"],"10022":[40.7571,-73.9657,"New York","NY"],"10023":[40.7764,-73.9827,"New York","NY"],"10024":[40.7864,-73.9764,"New York","NY"],"10025":[40.7975,-73.9683,"New York","NY"],"10026":[40.8019,-73.9531,"New York","NY"],"10027":[40.8116,-73.955,"New York","NY"],"10028":[40.7763,-73.9529,"New York","NY"],"10029":[40.7918,-73.9447,"New York","NY"],"10030":[40.8183,-73.9426,"New York","NY"],"10031":[40.8246,-73.9507,"New York","NY"],"10032":[40.8382,-73.942,"New York","NY"],"10033":[40.8496,-73.9356,"New York","NY"],"10034":[40.8662,-73.9221,"New York","NY"],"10035":[40.8011,-73.9371,"New York","NY"],"10036":[40.7597,-73.9918,"New York","NY"],"10037":[40.8135,-73.9381,"New York","NY"],"10038":[40.7101,-74.0013,"New York","NY"],"10039":[40.8265,-73.9383,"New York","NY"],"10040":[40.8583,-73.9296,"New York","NY"],"10041":[40.7038,-74.0098,"New York","NY"],"10043":[40.7143,-74.006,"New York","NY"],"10044":[40.7618,-73.9505,"New York","NY"],"10045":[40.7086,-74.0087,"New York","NY"],"10055":[40.7808,-73.9772,"New York","NY"],"10060":[40.7808,-73.9772,"New York","NY"],"10065":[40.7651,-73.9638,"New York","NY"],"10069":[40.778,-73.9884,"New York","NY"],"10075":[40.7143,-74.006,"New York","NY"],"10080":[40.7143,-74.006,"New York","NY"],"10081":[40.7143,-74.006,"New York","NY"],"10087":[40.7808,-73.9772,"New York","NY"],"10090":[40.7808,-73.9772,"New York","NY"],"10101":[40.7808,-73.9772,"New York","NY"],"10102":[40.7808,-73.9772,"New York","NY"],"10103":[40.7603,-73.9762,"New York","NY"],"10104":[40.7609,-73.9799,"New York","NY"],"10105":[40.7628,-73.9785,"New York","NY"],"10106":[40.7652,-73.9804,"New York","NY"],"10107":[40.7664,-73.9827,"New York","NY"],"10108":[40.7808,-73.9772,"New York","NY"],"10109":[40.7808,-73.9772,"New York","NY"],"10110":[40.754,-73.9808,"New York","NY"],"10111":[40.7592,-73.9778,"New York","NY"],"10112":[40.7593,-73.9798,"New York","NY"],"10113":[40.7808,-73.9772,"New York","NY"],"10114":[40.7808,-73.9772,"New York","NY"],"10115":[40.8111,-73.9642,"New York","NY"],"10116":[40.7808,-73.9772,"New York","NY"],"10117":[40.7808,-73.9772,"New York","NY"],"10118":[40.749,-73.9865,"New York","NY"],"10119":[40.7808,-73.9772,"New York","NY"],"10120":[40.7506,-73.9894,"New York","NY"],"10121":[40.7496,-73.9919,"New York","NY"],"10122":[40.7518,-73.9922,"New York","NY"],"10123":[40.7515,-73.9905,"New York","NY"],"10124":[40.7808,-73.9772,"New York","NY"],"10125":[40.7808,-73.9772,"New York","NY"],"10126":[40.7808,-73.9772,"New York","NY"],"10128":[40.7816,-73.9511,"New York","NY"],"10129":[40.7808,-73.9772,"New York","NY"],"10130":[40.7808,-73.9772,"New York","NY"],"10131":[40.7808,-73.9772,"New York","NY"],"10132":[40.7808,-73.9772,"New York","NY"],"10133":[40.7808,-73.9772,"New York","NY"],"10138":[40.7808,-73.9772,"New York","NY"],"10150":[40.7808,-73.9772,"New York","NY"],"10151":[40.7634,-73.974,"New York","NY"],"10152":[40.7589,-73.973,"New York","NY"],"10153":[40.7641,-73.9735,"New York","NY"],"10154":[40.7583,-73.9735,"New York","NY"],"10155":[40.7611,-73.968,"New York","NY"],"10156":[40.7808,-73.9772,"New York","NY"],"10157":[40.7808,-73.9772,"New York","NY"],"10158":[40.7494,-73.9758,"New York","NY"],"10159":[40.7808,-73.9772,"New York","NY"],"10160":[40.7808,-73.9772,"New York","NY"],"10161":[40.7808,-73.9772,"New York","NY"],"10162":[40.7699,-73.9511,"New York","NY"],"10163":[40.7808,-73.9772,"New York","NY"],"10164":[40.7808,-73.9772,"New York","NY"],"10165":[40.7524,-73.9791,"New York","NY"],"10166":[40.7546,-73.9762,"New York","NY"],"10167":[40.7549,-73.975,"New York","NY"],"10168":[40.7519,-73.9768,"New York","NY"],"10169":[40.7547,-73.9766,"New York","NY"],"10170":[40.7526,-73.9755,"New York","NY"],"10171":[40.7564,-73.9748,"New York","NY"],"10172":[40.7558,-73.9753,"New York","NY"],"10173":[40.7543,-73.9796,"New York","NY"],"10174":[40.7517,-73.9752,"New York","NY"],"10175":[40.7543,-73.9798,"New York","NY"],"10176":[40.7556,-73.9789,"New York","NY"],"10177":[40.7553,-73.9761,"New York","NY"],"10178":[40.7514,-73.9785,"New York","NY"],"10179":[40.7808,-73.9772,"New York","NY"],"10185":[40.7808,-73.9772,"New York","NY"],"10199":[40.7503,-74.0006,Line truncated
|
||||
@@ -33,6 +33,13 @@ processors:
|
||||
span:
|
||||
- 'status.code != STATUS_CODE_ERROR'
|
||||
|
||||
# Drop 404 log records to reduce noise
|
||||
filter/drop_404_logs:
|
||||
error_mode: ignore
|
||||
logs:
|
||||
log_record:
|
||||
- 'attributes["http.response.status_code"] == 404'
|
||||
|
||||
exporters:
|
||||
otlp_http/grafana:
|
||||
endpoint: ${env:GRAFANA_OTLP_ENDPOINT}
|
||||
@@ -60,7 +67,7 @@ service:
|
||||
exporters: [otlp_http/grafana]
|
||||
logs:
|
||||
receivers: [otlp]
|
||||
processors: [batch]
|
||||
processors: [filter/drop_404_logs, batch]
|
||||
exporters: [otlp_http/grafana]
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
|
||||
+13
-7
@@ -9,6 +9,7 @@ declare module 'fastify' {
|
||||
span?: Span;
|
||||
traceId?: string;
|
||||
errorHandled?: boolean;
|
||||
meta?: Record<string, unknown>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +32,7 @@ function classifyByStatus(statusCode: number): string {
|
||||
}
|
||||
import cors from '@fastify/cors';
|
||||
import { NominatimClient, NominatimResultSchema } from './services/NominatimClient';
|
||||
import { isZipCode } from './services/ZipCodeService';
|
||||
import { GithubClient, SponsorsResponseSchema } from './services/GithubClient';
|
||||
import { TurnstileClient } from './services/TurnstileClient';
|
||||
import { ZammadClient, ContactMessageBodySchema, ContactMessageBody } from './services/ZammadClient';
|
||||
@@ -80,13 +82,15 @@ const start = async () => {
|
||||
|
||||
request.errorHandled = true;
|
||||
|
||||
server.log.error({
|
||||
url: request.url,
|
||||
method: request.method,
|
||||
traceId: request.traceId,
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
}, 'Request error');
|
||||
if (statusCode !== 404) {
|
||||
server.log.error({
|
||||
url: request.url,
|
||||
method: request.method,
|
||||
traceId: request.traceId,
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
}, 'Request error');
|
||||
}
|
||||
|
||||
reply.status(statusCode).send({ error: 'Internal Server Error' });
|
||||
});
|
||||
@@ -136,6 +140,7 @@ const start = async () => {
|
||||
'http.route': route,
|
||||
'http.request.method': request.method,
|
||||
'http.response.status_code': statusCode,
|
||||
...(request.meta?.geocodeSource ? { 'geocode.source': request.meta.geocodeSource as string } : {}),
|
||||
});
|
||||
span.setAttribute('http.response.status_code', statusCode);
|
||||
if (statusCode >= 500) {
|
||||
@@ -222,6 +227,7 @@ const start = async () => {
|
||||
},
|
||||
}, async (request, reply) => {
|
||||
const { query } = request.query as { query: string };
|
||||
request.meta = { geocodeSource: isZipCode(query) ? 'local_zip' : 'nominatim' };
|
||||
reply.header('Cache-Control', 'public, max-age=86400, s-maxage=86400');
|
||||
const result = await nominatim.geocodePhrase(query, false, request.span);
|
||||
return result;
|
||||
|
||||
@@ -3,6 +3,7 @@ import { createCache, Cache } from 'cache-manager';
|
||||
import { Type, Static } from '@sinclair/typebox';
|
||||
import { type Span, SpanKind, SpanStatusCode, context, trace } from '@opentelemetry/api';
|
||||
import { tracer, otelLogger, SeverityNumber } from '../telemetry';
|
||||
import { isZipCode, lookupZipCode } from './ZipCodeService';
|
||||
const { DiskStore } = require('cache-manager-fs-hash');
|
||||
|
||||
export const NominatimResultSchema = Type.Object({
|
||||
@@ -56,6 +57,12 @@ export class NominatimClient {
|
||||
baseUrl = 'https://nominatim.openstreetmap.org/search';
|
||||
|
||||
async geocodePhrase(query: string, includeGeoJson: boolean = false, parentSpan?: Span): Promise<NominatimResult[]> {
|
||||
// Short-circuit for ZIP codes — serve from local data, no Nominatim call needed
|
||||
if (isZipCode(query)) {
|
||||
const zipResult = lookupZipCode(query.trim());
|
||||
return zipResult ? [zipResult] : [];
|
||||
}
|
||||
|
||||
const cacheKey = `geocode:${query}`;
|
||||
const cached = await cache.get(cacheKey);
|
||||
if (cached) {
|
||||
|
||||
@@ -106,14 +106,15 @@ export class ZammadClient {
|
||||
}
|
||||
}
|
||||
|
||||
async createTicket(payload: CreateTicketPayload): Promise<void> {
|
||||
async createTicket(payload: CreateTicketPayload, parentSpan?: Span): Promise<void> {
|
||||
const { name, email, topic, subject, message } = payload;
|
||||
const group = TOPIC_GROUP_MAP[topic];
|
||||
|
||||
const ctx = parentSpan ? trace.setSpan(context.active(), parentSpan) : context.active();
|
||||
const span = tracer.startSpan('zammad.createTicket', {
|
||||
kind: SpanKind.CLIENT,
|
||||
attributes: { 'peer.service': 'zammad', 'http.request.method': 'POST' },
|
||||
});
|
||||
}, ctx);
|
||||
try {
|
||||
const customerId = await this.upsertCustomer(name, email, span);
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { NominatimResult } from './NominatimClient';
|
||||
|
||||
// [lat, lon, city, stateAbbr]
|
||||
type ZipEntry = [number, number, string, string];
|
||||
type ZipData = Record<string, ZipEntry>;
|
||||
|
||||
// Load data synchronously at module init — 1.7MB uncompressed, fast
|
||||
const zipData: ZipData = JSON.parse(
|
||||
readFileSync(join(__dirname, '../data/zipcodes-us.json'), 'utf-8')
|
||||
);
|
||||
|
||||
const ZIP_PATTERN = /^\d{5}$/;
|
||||
|
||||
export function isZipCode(query: string): boolean {
|
||||
return ZIP_PATTERN.test(query.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up a 5-digit US ZIP code and return a Nominatim-compatible result,
|
||||
* or null if the ZIP is not found in the local dataset.
|
||||
*/
|
||||
export function lookupZipCode(zip: string): NominatimResult | null {
|
||||
const normalized = zip.trim().slice(0, 5);
|
||||
const entry = zipData[normalized];
|
||||
|
||||
if (!entry) return null;
|
||||
|
||||
const [lat, lon, city, state] = entry;
|
||||
const latStr = String(lat);
|
||||
const lonStr = String(lon);
|
||||
|
||||
return {
|
||||
addresstype: 'postcode',
|
||||
boundingbox: [
|
||||
String(lat - 0.1),
|
||||
String(lat + 0.1),
|
||||
String(lon - 0.1),
|
||||
String(lon + 0.1),
|
||||
],
|
||||
class: 'place',
|
||||
display_name: `${normalized}, ${city}, ${state}, United States`,
|
||||
importance: 0.3,
|
||||
lat: latStr,
|
||||
licence: 'Local ZIP Code Data',
|
||||
lon: lonStr,
|
||||
name: normalized,
|
||||
address: {
|
||||
postcode: normalized,
|
||||
city,
|
||||
state,
|
||||
country: 'United States',
|
||||
country_code: 'us',
|
||||
},
|
||||
place_rank: 11,
|
||||
type: 'postcode',
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user