Skip to content

Commit

Permalink
Add pagination to metadata-service (#471)
Browse files Browse the repository at this point in the history
* Adding pagination of /v2/hotspots and adding /v2/hotspots/pagination-metadata

* Add sorting

* Making PAGE_SIZE env var

* Revising based on pre PR review

* Address dupe bug
  • Loading branch information
deasydoesit authored Nov 6, 2023
1 parent 04954c5 commit a02ee87
Showing 1 changed file with 85 additions and 7 deletions.
92 changes: 85 additions & 7 deletions packages/metadata-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
} from "@solana/spl-token";
import { PublicKey } from "@solana/web3.js";

const pageSize = Number(process.env.PAGE_SIZE) || 1000;

const server: FastifyInstance = Fastify({
logger: true,
});
Expand Down Expand Up @@ -110,12 +112,12 @@ server.get<{ Params: { wallet: string } }>(
);

server.get<{ Querystring: { subnetwork: string } }>(
"/v2/hotspots",
"/v2/hotspots/pagination-metadata",
async (request, reply) => {
const { subnetwork } = request.query;

if (subnetwork === "iot") {
const ktas = await KeyToAsset.findAll({
const count = await KeyToAsset.count({
include: [
{
model: IotHotspotInfo,
Expand All @@ -124,28 +126,104 @@ server.get<{ Querystring: { subnetwork: string } }>(
],
});

return ktas.map((kta) => {
let result = {
pageSize,
totalItems: count,
totalPages: Math.ceil(count / pageSize),
};

return result;
} else if (subnetwork === "mobile") {
const count = await KeyToAsset.count({
include: [
{
model: MobileHotspotInfo,
required: true,
},
],
});

let result = {
pageSize,
totalItems: count,
totalPages: Math.ceil(count / pageSize),
};

return result;
}

return reply.code(400).send("Invalid subnetwork");
}
);

server.get<{ Querystring: { subnetwork: string, page: string } }>(
"/v2/hotspots",
async (request, reply) => {
const { subnetwork, page: pageStr } = request.query;
const pageInt = pageStr ? Number(pageStr) : 1;

const offset = (pageInt - 1) * pageSize;
const limit = pageSize;

if (subnetwork === "iot") {
const { count, rows: ktas } = await KeyToAsset.findAndCountAll({
offset,
limit,
include: [
{
model: IotHotspotInfo,
required: true,
},
],
order: [
[IotHotspotInfo, "created_at", "ASC"],
[IotHotspotInfo, "address", "ASC"], // Need to sort additionally by address otherwise there are dupes across pages because of tight created_at coupling
],
});

let result = {
currentPage: pageInt,
nextPage: (offset + limit) < count ? pageInt + 1 : null,
items: [] as { key_to_asset_key: string; }[]
};

result.items = ktas.map((kta) => {
return {
key_to_asset_key: kta.address,
is_active: kta.iot_hotspot_info!.is_active,
};
});

return result;
} else if (subnetwork === "mobile") {
const ktas = await KeyToAsset.findAll({
const { count, rows: ktas } = await KeyToAsset.findAndCountAll({
offset,
limit,
include: [
{
model: MobileHotspotInfo,
required: true,
},
],
order: [
[MobileHotspotInfo, "created_at", "ASC"],
[MobileHotspotInfo, "address", "ASC"], // Need to sort additionally by address otherwise there are dupes across pages because of tight created_at coupling
],
});
return ktas.map((kta) => {

let result = {
currentPage: pageInt,
nextPage: (offset + limit) < count ? pageInt + 1 : null,
items: [] as { key_to_asset_key: string; device_type: string; }[],
};

result.items = ktas.map((kta) => {
return {
key_to_asset_key: kta.address,
is_active: kta.mobile_hotspot_info!.is_active,
device_type: kta.mobile_hotspot_info!.device_type,
};
});

return result;
}

return reply.code(400).send("Invalid subnetwork");
Expand Down

0 comments on commit a02ee87

Please sign in to comment.