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
14 changes: 13 additions & 1 deletion db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ CREATE TABLE IF NOT EXISTS discount_codes (
CREATE INDEX IF NOT EXISTS idx_discount_codes_pubkey ON discount_codes(pubkey);
CREATE INDEX IF NOT EXISTS idx_discount_codes_code ON discount_codes(code);

-- Failed relay publish tracking table
CREATE TABLE IF NOT EXISTS failed_relay_publishes (
event_id TEXT PRIMARY KEY,
event_data TEXT,
relays TEXT NOT NULL,
created_at BIGINT NOT NULL,
retry_count INTEGER DEFAULT 0
);

CREATE INDEX IF NOT EXISTS idx_failed_relay_publishes_created_at ON failed_relay_publishes(created_at ASC);
CREATE INDEX IF NOT EXISTS idx_failed_relay_publishes_retry_count ON failed_relay_publishes(retry_count);

-- MCP API Keys table
CREATE TABLE IF NOT EXISTS mcp_api_keys (
id SERIAL PRIMARY KEY,
Expand Down Expand Up @@ -164,4 +176,4 @@ CREATE TABLE IF NOT EXISTS mcp_orders (
CREATE INDEX IF NOT EXISTS idx_mcp_orders_order_id ON mcp_orders(order_id);
CREATE INDEX IF NOT EXISTS idx_mcp_orders_buyer_pubkey ON mcp_orders(buyer_pubkey);
CREATE INDEX IF NOT EXISTS idx_mcp_orders_seller_pubkey ON mcp_orders(seller_pubkey);
CREATE INDEX IF NOT EXISTS idx_mcp_orders_api_key_id ON mcp_orders(api_key_id);
CREATE INDEX IF NOT EXISTS idx_mcp_orders_api_key_id ON mcp_orders(api_key_id);
21 changes: 15 additions & 6 deletions pages/api/db/get-failed-publishes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ export default async function handler(
`SELECT event_id, event_data, relays, retry_count
FROM failed_relay_publishes
WHERE retry_count < 5
AND event_data IS NOT NULL
ORDER BY created_at ASC
LIMIT 50`
);

const failedPublishes = result.rows
.filter((row: any) => row.event_data)
.map((row: any) => ({
eventId: row.event_id,
relays: JSON.parse(row.relays),
event: JSON.parse(row.event_data),
retryCount: row.retry_count,
}));
.map((row: any) => {
try {
return {
eventId: row.event_id,
relays: JSON.parse(row.relays),
event: JSON.parse(row.event_data),
retryCount: row.retry_count,
};
} catch (e) {
console.error('Failed to parse row:', row.event_id, e);
return null;
}
})
.filter(Boolean);

return res.status(200).json(failedPublishes);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion pages/api/db/track-failed-publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default async function handler(
created_at = EXCLUDED.created_at`,
[
eventId,
JSON.stringify(event),
event ? JSON.stringify(event) : null,
JSON.stringify(relays),
Math.floor(Date.now() / 1000),
]
Expand Down