Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): remove cache from account token inventory #549

Merged
merged 1 commit into from
Oct 15, 2024
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
37 changes: 37 additions & 0 deletions apps/api/src/libs/schema/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,47 @@ const fees = z.object({
period: z.enum(['day', 'week']).optional().default('day'),
});

const nodes = z.object({
agent: z.object({
build: z.string(),
name: z.string(),
version: z.string(),
}),
chain: z.object({
account_id: z
.string()
.min(2)
.max(64)
.regex(/^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$/i)
.transform((value) => value.toLowerCase())
.nullish(),
block_production_tracking_delay: z.number().optional(),
is_validator: z.boolean(),
latest_block_hash: z.string().min(43).max(44),
latest_block_height: z.number(),
max_block_production_delay: z.number().optional(),
max_block_wait_delay: z.number().optional(),
min_block_production_delay: z.number().optional(),
node_id: z.string(),
num_peers: z.number(),
status: z.string(),
}),
signature: z.string().optional(),
system: z.object({
bandwidth_download: z.number(),
bandwidth_upload: z.number(),
boot_time_seconds: z.number().optional(),
cpu_usage: z.number(),
memory_usage: z.number(),
}),
});

export type Supply = z.infer<typeof supply>;
export type Fees = z.infer<typeof fees>;
export type Nodes = z.infer<typeof nodes>;

export default {
fees,
nodes,
supply,
};
41 changes: 0 additions & 41 deletions apps/api/src/libs/schema/node.ts

This file was deleted.

2 changes: 0 additions & 2 deletions apps/api/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import keys from '#routes/keys';
import kitwallet from '#routes/kitwallet';
import legacy from '#routes/legacy';
import nfts from '#routes/nfts';
import node from '#routes/node';
import search from '#routes/search';
import stats from '#routes/stats';
import sync from '#routes/sync';
Expand All @@ -33,7 +32,6 @@ const routes = () => {
kitwallet(app);
legacy(app);
nfts(app);
node(app);
search(app);
stats(app);
sync(app);
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/routes/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const routes = (app: Router) => {
route.get('/fees', validator(schema.fees), legacy.fees);

route.get('/ping', legacy.ping);

route.post('/nodes', validator(schema.nodes), legacy.nodes);
};

export default routes;
15 changes: 0 additions & 15 deletions apps/api/src/routes/node.ts

This file was deleted.

12 changes: 2 additions & 10 deletions apps/api/src/services/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,9 @@ const inventory = catchAsync(
) AS meta ON TRUE
`;

const inventory = await redis.cache(
`account:${account}:inventory`,
async () => {
const [fts, nfts] = await Promise.all([ftQuery, nftQuery]);

return { fts, nfts };
},
EXPIRY * 1, // 1 mins
);
const [fts, nfts] = await Promise.all([ftQuery, nftQuery]);

return res.status(200).json({ inventory });
return res.status(200).json({ inventory: { fts, nfts } });
},
);

Expand Down
56 changes: 53 additions & 3 deletions apps/api/src/services/legacy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Response } from 'express';
import geoip from 'geoip-lite';

import catchAsync from '#libs/async';
import dayjs from '#libs/dayjs';
import db from '#libs/db';
import sql from '#libs/postgres';
import { Fees, Supply } from '#libs/schema/legacy';
import sql, { writeSql } from '#libs/postgres';
import { Fees, Nodes, Supply } from '#libs/schema/legacy';
import { msToNsTime, yoctoToNear } from '#libs/utils';
import { RequestValidator } from '#types/types';

Expand Down Expand Up @@ -130,4 +131,53 @@ const ping = catchAsync(async (_req: Request, res: Response) => {
return res.send('pong');
});

export default { fees, ping, supply, total };
const nodes = catchAsync(
async (req: RequestValidator<Nodes>, res: Response) => {
const data = req.validator.data;
const geo = geoip.lookup(req.ip!);
const node = {
account_id: data.chain.account_id ?? null,
agent_build: data.agent.build,
agent_name: data.agent.name,
agent_version: data.agent.version,
bandwidth_download: data.system.bandwidth_download,
bandwidth_upload: data.system.bandwidth_upload,
block_production_tracking_delay:
data.chain.block_production_tracking_delay ?? null,
boot_time_seconds: data.system.boot_time_seconds
? new Date(data.system.boot_time_seconds * 1000)
: null,
city: geo ? geo.city : null,
cpu_usage: data.system.cpu_usage,
ip_address: req.ip!,
is_validator: data.chain.is_validator,
last_hash: data.chain.latest_block_hash,
last_height: data.chain.latest_block_height,
last_seen: new Date(),
latitude: geo ? geo.ll[0] : null,
longitude: geo ? geo.ll[1] : null,
max_block_production_delay: data.chain.max_block_production_delay ?? null,
max_block_wait_delay: data.chain.max_block_wait_delay ?? null,
memory_usage: data.system.memory_usage,
min_block_production_delay: data.chain.min_block_production_delay ?? null,
moniker: data.chain.account_id ?? null,
node_id: data.chain.node_id,
peer_count: data.chain.num_peers,
signature: data.signature ?? null,
status: data.chain.status,
};

await writeSql`
INSERT INTO
nodes ${writeSql(node)}
ON CONFLICT (node_id) DO
UPDATE
SET
${writeSql(node)}
`;

return res.status(200).json();
},
);

export default { fees, nodes, ping, supply, total };
58 changes: 0 additions & 58 deletions apps/api/src/services/node.ts

This file was deleted.

Loading