Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions workers/api/depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const THROUGH_SETS = {

/**
* Expands THROUGH_SETS for the parent rows: one query per set, joined from
* the link table to the target table, both restricted to status='ok'.
* the link table to the target table (D1 holds only status='ok' rows).
*
* @param {D1Session} db - The D1 database binding.
* @param {EntityMeta} entity - The parent entity metadata.
Expand All @@ -197,7 +197,7 @@ async function expandThroughSets(db, entity, rows, depth, authenticated, pdbfe)
const target = ENTITIES[ts.targetTag];
const params = [JSON.stringify(parentIds)];
const from = ` FROM "${ts.link}" AS l JOIN "${target.table}" AS t ON t."id" = l."${ts.targetFk}"` +
` WHERE l."${ts.parentFk}" ${IN_IDS} AND l."status" = 'ok' AND t."status" = 'ok'`;
` WHERE l."${ts.parentFk}" ${IN_IDS}`;

if (depth >= 2) {
const cols = getColumns(target, pdbfe).map(c => selectColumn(target.table, c, 't.', authenticated)).join(', '); // ap-ok: SQL construction
Expand Down Expand Up @@ -249,7 +249,7 @@ async function expandDepthOne(db, entity, rows, authenticated, pdbfe) {
const childEntity = childTag ? ENTITIES[childTag] : null;
const anonFilter = resolveAnonFilter(authenticated, childEntity);

let sql = `SELECT "id", "${rel.fk}" FROM "${rel.table}" WHERE "${rel.fk}" ${IN_IDS} AND "status" = 'ok'`;
let sql = `SELECT "id", "${rel.fk}" FROM "${rel.table}" WHERE "${rel.fk}" ${IN_IDS}`;
/** @type {any[]} */
const params = [JSON.stringify(parentIds)];

Expand Down Expand Up @@ -346,16 +346,14 @@ async function expandDepthTwo(db, entity, rows, authenticated, pdbfe) {

sql = `SELECT ${allCols} FROM "${rel.table}" AS t` +
joinParts.join('') +
` WHERE t."${rel.fk}" ${IN_IDS}` +
` AND t."status" = 'ok'`;
` WHERE t."${rel.fk}" ${IN_IDS}`;

sql = appendFilterAndOrder(sql, params, anonFilter, 't.');
} else if (childColumns.length > 0) {
// Standard path: no JOINs
const colExpr = childColumns.map(c => selectColumn(rel.table, c, '', authenticated)).join(", "); // ap-ok: SQL construction
sql = `SELECT "${rel.fk}", ${colExpr} FROM "${rel.table}"` +
` WHERE "${rel.fk}" ${IN_IDS}` +
` AND "status" = 'ok'`;
` WHERE "${rel.fk}" ${IN_IDS}`;

sql = appendFilterAndOrder(sql, params, anonFilter);
} else if (GATED_TABLES.has(rel.table)) {
Expand All @@ -364,8 +362,7 @@ async function expandDepthTwo(db, entity, rows, authenticated, pdbfe) {
} else {
// Fallback: unknown child entity, select everything
sql = `SELECT * FROM "${rel.table}"` +
` WHERE "${rel.fk}" ${IN_IDS}` +
` AND "status" = 'ok'`;
` WHERE "${rel.fk}" ${IN_IDS}`;

sql = appendFilterAndOrder(sql, params, anonFilter);
}
Expand Down Expand Up @@ -426,7 +423,7 @@ async function expandParents(db, entity, rows, depth, authenticated, pdbfe, deta
if (ids.size === 0) return;

const cols = getColumns(parent, pdbfe).map(c => selectColumn(parent.table, c, '', authenticated)).join(', '); // ap-ok: SQL construction
const sql = `SELECT ${cols} FROM "${parent.table}" WHERE "id" ${IN_IDS} AND "status" = 'ok'`;
const sql = `SELECT ${cols} FROM "${parent.table}" WHERE "id" ${IN_IDS}`;
const result = await db.prepare(sql).bind(JSON.stringify([...ids])).all(); // ap-ok: cold path behind cachedQuery
const parents = result.results || [];
for (const p of parents) parseJsonFields(parent, p);
Expand Down
61 changes: 23 additions & 38 deletions workers/api/handlers/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function fetchEntityHeader(db, tag, id) {
const meta = ENTITY_META[tag];
const cols = ['id', meta.nameCol, ...(meta.extraCols || [])]; // ap-ok: cold path, max 4 cols per entity header
const colList = cols.map(c => '"' + c + '"').join(', '); // ap-ok: avoids nested template literal (sonar rule)
const sql = `SELECT ${colList} FROM "${meta.table}" WHERE "id" = ? AND "status" = 'ok'`; // ap-ok: cold path, builds SQL once per header lookup
const sql = `SELECT ${colList} FROM "${meta.table}" WHERE "id" = ?`; // ap-ok: cold path, builds SQL once per header lookup
const row = await db.prepare(sql).bind(id).first();
if (!row) return null;
return { tag, ...row };
Expand Down Expand Up @@ -131,8 +131,6 @@ async function overlapNetNet(db, idA, idB) {
JOIN peeringdb_ixlan ixlan ON a.ixlan_id = ixlan.id
JOIN peeringdb_ix ix ON ixlan.ix_id = ix.id
WHERE a.net_id = ? AND b.net_id = ?
AND a.status = 'ok' AND b.status = 'ok'
AND ix.status = 'ok'
ORDER BY ix.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

// Shared facilities: networks present at the same facility
Expand All @@ -143,8 +141,6 @@ async function overlapNetNet(db, idA, idB) {
JOIN peeringdb_network_facility b ON a.fac_id = b.fac_id
JOIN peeringdb_facility f ON a.fac_id = f.id
WHERE a.net_id = ? AND b.net_id = ?
AND a.status = 'ok' AND b.status = 'ok'
AND f.status = 'ok'
ORDER BY f.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

// Only-side IXPs (present for the first bind but not the second). Run twice
Expand All @@ -154,12 +150,12 @@ async function overlapNetNet(db, idA, idB) {
FROM peeringdb_network_ixlan a
JOIN peeringdb_ixlan ixlan ON a.ixlan_id = ixlan.id
JOIN peeringdb_ix ix ON ixlan.ix_id = ix.id
WHERE a.net_id = ? AND a.status = 'ok' AND ix.status = 'ok'
WHERE a.net_id = ?
AND ix.id NOT IN (
SELECT ix2.id FROM peeringdb_network_ixlan b
JOIN peeringdb_ixlan ixlan2 ON b.ixlan_id = ixlan2.id
JOIN peeringdb_ix ix2 ON ixlan2.ix_id = ix2.id
WHERE b.net_id = ? AND b.status = 'ok'
WHERE b.net_id = ?
)
ORDER BY ix.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

Expand All @@ -169,10 +165,10 @@ async function overlapNetNet(db, idA, idB) {
f.latitude, f.longitude
FROM peeringdb_network_facility a
JOIN peeringdb_facility f ON a.fac_id = f.id
WHERE a.net_id = ? AND a.status = 'ok' AND f.status = 'ok'
WHERE a.net_id = ?
AND f.id NOT IN (
SELECT b.fac_id FROM peeringdb_network_facility b
WHERE b.net_id = ? AND b.status = 'ok'
WHERE b.net_id = ?
)
ORDER BY f.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

Expand Down Expand Up @@ -214,8 +210,6 @@ async function overlapIxIx(db, idA, idB) {
JOIN peeringdb_ix_facility b ON a.fac_id = b.fac_id
JOIN peeringdb_facility f ON a.fac_id = f.id
WHERE a.ix_id = ? AND b.ix_id = ?
AND a.status = 'ok' AND b.status = 'ok'
AND f.status = 'ok'
ORDER BY f.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

// Shared member networks (present on both IXes via netixlan → ixlan → ix)
Expand All @@ -227,8 +221,6 @@ async function overlapIxIx(db, idA, idB) {
JOIN peeringdb_ixlan ixlanB ON b.ixlan_id = ixlanB.id
JOIN peeringdb_network n ON a.net_id = n.id
WHERE ixlanA.ix_id = ? AND ixlanB.ix_id = ?
AND a.status = 'ok' AND b.status = 'ok'
AND n.status = 'ok'
ORDER BY n.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

// Only-side facilities — same SQL, swapped ids for only_a / only_b.
Expand All @@ -237,10 +229,10 @@ async function overlapIxIx(db, idA, idB) {
f.latitude, f.longitude
FROM peeringdb_ix_facility a
JOIN peeringdb_facility f ON a.fac_id = f.id
WHERE a.ix_id = ? AND a.status = 'ok' AND f.status = 'ok'
WHERE a.ix_id = ?
AND f.id NOT IN (
SELECT b.fac_id FROM peeringdb_ix_facility b
WHERE b.ix_id = ? AND b.status = 'ok'
WHERE b.ix_id = ?
)
ORDER BY f.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

Expand All @@ -250,11 +242,11 @@ async function overlapIxIx(db, idA, idB) {
FROM peeringdb_network_ixlan a
JOIN peeringdb_ixlan ixlan ON a.ixlan_id = ixlan.id
JOIN peeringdb_network n ON a.net_id = n.id
WHERE ixlan.ix_id = ? AND a.status = 'ok' AND n.status = 'ok'
WHERE ixlan.ix_id = ?
AND n.id NOT IN (
SELECT DISTINCT b.net_id FROM peeringdb_network_ixlan b
JOIN peeringdb_ixlan ixlan2 ON b.ixlan_id = ixlan2.id
WHERE ixlan2.ix_id = ? AND b.status = 'ok'
WHERE ixlan2.ix_id = ?
)
ORDER BY n.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

Expand Down Expand Up @@ -299,8 +291,6 @@ async function overlapIxNet(db, ixId, netId) {
JOIN peeringdb_network_facility b ON a.fac_id = b.fac_id
JOIN peeringdb_facility f ON a.fac_id = f.id
WHERE a.ix_id = ? AND b.net_id = ?
AND a.status = 'ok' AND b.status = 'ok'
AND f.status = 'ok'
ORDER BY f.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(ixId, netId).all(),

Expand All @@ -309,10 +299,10 @@ async function overlapIxNet(db, ixId, netId) {
SELECT f.id AS fac_id, f.name AS fac_name, f.city, f.country, f.latitude, f.longitude
FROM peeringdb_ix_facility a
JOIN peeringdb_facility f ON a.fac_id = f.id
WHERE a.ix_id = ? AND a.status = 'ok' AND f.status = 'ok'
WHERE a.ix_id = ?
AND f.id NOT IN (
SELECT b.fac_id FROM peeringdb_network_facility b
WHERE b.net_id = ? AND b.status = 'ok'
WHERE b.net_id = ?
)
ORDER BY f.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(ixId, netId).all(),
Expand All @@ -322,10 +312,10 @@ async function overlapIxNet(db, ixId, netId) {
SELECT f.id AS fac_id, f.name AS fac_name, f.city, f.country, f.latitude, f.longitude
FROM peeringdb_network_facility b
JOIN peeringdb_facility f ON b.fac_id = f.id
WHERE b.net_id = ? AND b.status = 'ok' AND f.status = 'ok'
WHERE b.net_id = ?
AND f.id NOT IN (
SELECT a.fac_id FROM peeringdb_ix_facility a
WHERE a.ix_id = ? AND a.status = 'ok'
WHERE a.ix_id = ?
)
ORDER BY f.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(netId, ixId).all(),
Expand All @@ -336,7 +326,6 @@ async function overlapIxNet(db, ixId, netId) {
FROM peeringdb_network_ixlan a
JOIN peeringdb_ixlan ixlan ON a.ixlan_id = ixlan.id
WHERE a.net_id = ? AND ixlan.ix_id = ?
AND a.status = 'ok' AND ixlan.status = 'ok'
LIMIT ${COMPARE_ROW_LIMIT}
`).bind(netId, ixId).all(),
]);
Expand Down Expand Up @@ -368,19 +357,18 @@ async function overlapFacNet(db, facId, netId) {
JOIN peeringdb_ixlan ixlan ON ixlan.ix_id = ix.id
JOIN peeringdb_network_ixlan n ON n.ixlan_id = ixlan.id
WHERE ixfac.fac_id = ? AND n.net_id = ?
AND ixfac.status = 'ok' AND ix.status = 'ok' AND ixlan.status = 'ok' AND n.status = 'ok'
ORDER BY ix.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(facId, netId).all(),

db.prepare(`
SELECT ix.id AS ix_id, ix.name AS ix_name, ix.country, ix.city
FROM peeringdb_ix_facility a
JOIN peeringdb_ix ix ON a.ix_id = ix.id
WHERE a.fac_id = ? AND a.status = 'ok' AND ix.status = 'ok'
WHERE a.fac_id = ?
AND ix.id NOT IN (
SELECT ixlan2.ix_id FROM peeringdb_network_ixlan b
JOIN peeringdb_ixlan ixlan2 ON b.ixlan_id = ixlan2.id
WHERE b.net_id = ? AND b.status = 'ok'
WHERE b.net_id = ?
)
ORDER BY ix.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(facId, netId).all(),
Expand All @@ -391,10 +379,10 @@ async function overlapFacNet(db, facId, netId) {
FROM peeringdb_network_ixlan b
JOIN peeringdb_ixlan ixlan ON b.ixlan_id = ixlan.id
JOIN peeringdb_ix ix ON ixlan.ix_id = ix.id
WHERE b.net_id = ? AND b.status = 'ok' AND ix.status = 'ok' AND ixlan.status = 'ok'
WHERE b.net_id = ?
AND ix.id NOT IN (
SELECT a.ix_id FROM peeringdb_ix_facility a
WHERE a.fac_id = ? AND a.status = 'ok'
WHERE a.fac_id = ?
)
ORDER BY ix.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(netId, facId).all(),
Expand Down Expand Up @@ -425,19 +413,18 @@ async function overlapFacIx(db, facId, ixId) {
JOIN peeringdb_network_ixlan b ON b.net_id = n.id
JOIN peeringdb_ixlan ixlan ON b.ixlan_id = ixlan.id
WHERE a.fac_id = ? AND ixlan.ix_id = ?
AND a.status = 'ok' AND n.status = 'ok' AND b.status = 'ok' AND ixlan.status = 'ok'
ORDER BY n.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(facId, ixId).all(),

db.prepare(`
SELECT DISTINCT n.id AS net_id, n.name AS net_name, n.asn
FROM peeringdb_network_facility a
JOIN peeringdb_network n ON a.net_id = n.id
WHERE a.fac_id = ? AND a.status = 'ok' AND n.status = 'ok'
WHERE a.fac_id = ?
AND n.id NOT IN (
SELECT b.net_id FROM peeringdb_network_ixlan b
JOIN peeringdb_ixlan ixlan ON b.ixlan_id = ixlan.id
WHERE ixlan.ix_id = ? AND b.status = 'ok'
WHERE ixlan.ix_id = ?
)
ORDER BY n.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(facId, ixId).all(),
Expand All @@ -447,10 +434,10 @@ async function overlapFacIx(db, facId, ixId) {
FROM peeringdb_network_ixlan b
JOIN peeringdb_ixlan ixlan ON b.ixlan_id = ixlan.id
JOIN peeringdb_network n ON b.net_id = n.id
WHERE ixlan.ix_id = ? AND b.status = 'ok' AND ixlan.status = 'ok' AND n.status = 'ok'
WHERE ixlan.ix_id = ?
AND n.id NOT IN (
SELECT a.net_id FROM peeringdb_network_facility a
WHERE a.fac_id = ? AND a.status = 'ok'
WHERE a.fac_id = ?
)
ORDER BY n.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}
`).bind(ixId, facId).all(),
Expand Down Expand Up @@ -478,7 +465,6 @@ async function overlapFacFac(db, idA, idB) {
JOIN peeringdb_network_facility b ON a.net_id = b.net_id
JOIN peeringdb_network n ON a.net_id = n.id
WHERE a.fac_id = ? AND b.fac_id = ?
AND a.status = 'ok' AND b.status = 'ok' AND n.status = 'ok'
ORDER BY n.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

const sharedIxpsSql = `
Expand All @@ -487,23 +473,22 @@ async function overlapFacFac(db, idA, idB) {
JOIN peeringdb_ix_facility b ON a.ix_id = b.ix_id
JOIN peeringdb_ix ix ON a.ix_id = ix.id
WHERE a.fac_id = ? AND b.fac_id = ?
AND a.status = 'ok' AND b.status = 'ok' AND ix.status = 'ok'
ORDER BY ix.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

// Only-side networks / ixps — same SQL, swapped ids for only_a / only_b.
const onlyNetsSql = `
SELECT DISTINCT n.id AS net_id, n.name AS net_name, n.asn
FROM peeringdb_network_facility a
JOIN peeringdb_network n ON a.net_id = n.id
WHERE a.fac_id = ? AND a.status = 'ok' AND n.status = 'ok'
WHERE a.fac_id = ?
AND n.id NOT IN (SELECT net_id FROM peeringdb_network_facility WHERE fac_id = ? AND status='ok')
ORDER BY n.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

const onlyIxpsSql = `
SELECT ix.id AS ix_id, ix.name AS ix_name, ix.country, ix.city
FROM peeringdb_ix_facility a
JOIN peeringdb_ix ix ON a.ix_id = ix.id
WHERE a.fac_id = ? AND a.status = 'ok' AND ix.status = 'ok'
WHERE a.fac_id = ?
AND ix.id NOT IN (SELECT ix_id FROM peeringdb_ix_facility WHERE fac_id = ? AND status='ok')
ORDER BY ix.name COLLATE NOCASE LIMIT ${COMPARE_ROW_LIMIT}`;

Expand Down
10 changes: 3 additions & 7 deletions workers/api/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,9 @@ function buildWherePagination(entity, filters, opts, singleId, tableAlias) {
params.push(since);
}

// Default to status=ok if no explicit status filter is present
// (matches upstream PeeringDB behaviour — deleted records excluded by default)
const hasStatusFilter = filters.some(f => f.field === 'status');
if (!hasStatusFilter && getFilterType(entity, 'status')) {
clauses.push(`${pfx}"status" = ?`);
params.push('ok');
}
// No default status filter: D1 holds only status='ok' rows (the sync
// removes any other status, sync/rows.js isListed), which is upstream's
// default listing. An explicit ?status= still filters like any field.

// Restricted-entity visibility (e.g. poc). Anonymous callers may only ever
// see the allowed value (visible=Public). Enforced HERE — the single WHERE
Expand Down
Loading
Loading