Skip to content

Commit

Permalink
Add leases duration endpoint (#124)
Browse files Browse the repository at this point in the history
Add new endpoint for querying leases duration
  • Loading branch information
Redm4x authored Mar 11, 2024
1 parent fc0eb72 commit f4e514c
Showing 1 changed file with 77 additions and 2 deletions.
79 changes: 77 additions & 2 deletions api/src/routers/internalRouter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Provider } from "@shared/dbSchemas/akash";
import { Block } from "@shared/dbSchemas";
import { Lease, Provider } from "@shared/dbSchemas/akash";
import { chainDb } from "@src/db/dbConnection";
import { isValidBech32Address } from "@src/utils/addresses";
import { round } from "@src/utils/math";
import { differenceInSeconds } from "date-fns";
import { Hono } from "hono";
import * as semver from "semver";
import { QueryTypes } from "sequelize";
import { Op, QueryTypes } from "sequelize";

export const internalRouter = new Hono();

Expand Down Expand Up @@ -154,3 +156,76 @@ internalRouter.get("/gpu", async (c) => {

return c.json(response);
});

internalRouter.get("leases-duration/:owner", async (c) => {
const dateFormat = /^\d{4}-\d{2}-\d{2}$/;

let startTime: Date = new Date("2000-01-01");
let endTime: Date = new Date("2100-01-01");

const { dseq, startDate, endDate } = c.req.query();

if (dseq && isNaN(parseInt(dseq))) {
return c.text("Invalid dseq", 400);
}

if (startDate) {
if (!startDate.match(dateFormat)) return c.text("Invalid start date, must be in the following format: YYYY-MM-DD", 400);

const startMs = Date.parse(startDate);

if (isNaN(startMs)) return c.text("Invalid start date", 400);

startTime = new Date(startMs);
}

if (endDate) {
if (!endDate.match(dateFormat)) return c.text("Invalid end date, must be in the following format: YYYY-MM-DD", 400);

const endMs = Date.parse(endDate);

if (isNaN(endMs)) return c.text("Invalid end date", 400);

endTime = new Date(endMs);
}

if (endTime <= startTime) {
return c.text("End time must be greater than start time", 400);
}

const closedLeases = await Lease.findAll({
where: {
owner: c.req.param("owner"),
closedHeight: { [Op.not]: null },
"$closedBlock.datetime$": { [Op.gte]: startTime, [Op.lte]: endTime },
...(dseq ? { dseq: dseq } : {})
},
include: [
{ model: Block, as: "createdBlock" },
{ model: Block, as: "closedBlock" }
]
});

const leases = closedLeases.map((x) => ({
dseq: x.dseq,
oseq: x.oseq,
gseq: x.gseq,
provider: x.providerAddress,
startHeight: x.createdHeight,
startDate: x.createdBlock.datetime,
closedHeight: x.closedHeight,
closedDate: x.closedBlock.datetime,
durationInBlocks: x.closedHeight - x.createdHeight,
durationInSeconds: differenceInSeconds(x.closedBlock.datetime, x.createdBlock.datetime),
durationInHours: differenceInSeconds(x.closedBlock.datetime, x.createdBlock.datetime) / 3600
}));

const totalSeconds = leases.map((x) => x.durationInSeconds).reduce((a, b) => a + b, 0);

return c.json({
leaseCount: leases.length,
totalDurationInSeconds: totalSeconds,
totalDurationInHours: totalSeconds / 3600,
leases
});
});

0 comments on commit f4e514c

Please sign in to comment.