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

Add provider deployments endpoint to api #58

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
77 changes: 76 additions & 1 deletion api/src/db/deploymentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as v1 from "@src/proto/akash/v1beta1";
import * as v2 from "@src/proto/akash/v1beta2";
import { decodeMsg } from "@src/utils/protobuf";
import { Transaction } from "@shared/dbSchemas/base";
import { Deployment } from "@shared/dbSchemas/akash";
import { Deployment, Lease } from "@shared/dbSchemas/akash";
import { Op } from "sequelize";
import { Block, Message } from "@shared/dbSchemas";

Expand Down Expand Up @@ -69,3 +69,78 @@ export async function getDeploymentRelatedMessages(owner: string, dseq: string)
type: msg.type
}));
}

export async function getProviderDeployments(provider: string, skip: number, limit: number, status?: "active" | "closed") {
let leaseFilter = { providerAddress: provider };

if (status) {
leaseFilter["closedHeight"] = status === "active" ? null : { [Op.ne]: null };
}

const deploymentDseqs = await Deployment.findAll({
attributes: ["dseq"],
include: [{ model: Lease, required: true, where: leaseFilter }],
order: [["createdHeight", "DESC"]],
offset: skip,
limit: limit,
subQuery: false
});

const deployments = await Deployment.findAll({
where: {
dseq: { [Op.in]: deploymentDseqs.map((d) => d.dseq) }
},
include: [
{
model: Lease,
required: true,
where: { providerAddress: provider },
include: [
{ model: Block, required: true, as: "createdBlock" },
{ model: Block, required: false, as: "closedBlock" }
]
},
{ model: Block, required: true, as: "createdBlock" },
{ model: Block, required: false, as: "closedBlock" }
]
});

return deployments.map((d) => ({
owner: d.owner,
dseq: d.dseq,
denom: d.denom,
createdHeight: d.createdHeight,
createdDate: d.createdBlock.datetime,
closedHeight: d.closedHeight,
closedDate: d.closedHeight ? d.closedBlock.datetime : null,
status: d.closedHeight ? "closed" : "active",
balance: d.balance,
transferred: d.withdrawnAmount,
settledAt: d.lastWithdrawHeight,
resources: {
cpu: d.leases.reduce((acc, l) => acc + l.cpuUnits, 0),
memory: d.leases.reduce((acc, l) => acc + l.memoryQuantity, 0),
gpu: d.leases.reduce((acc, l) => acc + l.gpuUnits, 0),
ephemeralStorage: d.leases.reduce((acc, l) => acc + l.ephemeralStorageQuantity, 0),
persistentStorage: d.leases.reduce((acc, l) => acc + l.persistentStorageQuantity, 0)
},
leases: d.leases.map((l) => ({
provider: l.providerAddress,
gseq: l.gseq,
oseq: l.oseq,
price: l.price,
createdHeight: l.createdHeight,
createdDate: l.createdBlock.datetime,
closedHeight: l.closedHeight,
closedDate: l.closedHeight ? l.closedBlock.datetime : null,
status: l.closedHeight ? "closed" : "active",
resources: {
cpu: l.cpuUnits,
memory: l.memoryQuantity,
gpu: l.gpuUnits,
ephemeralStorage: l.ephemeralStorageQuantity,
persistentStorage: l.persistentStorageQuantity
}
}))
}));
}
19 changes: 19 additions & 0 deletions api/src/routers/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import axios from "axios";
import { getMarketData } from "@src/providers/marketDataProvider";
import { getAuditors, getProviderAttributesSchema } from "@src/providers/githubProvider";
import { getProviderRegions } from "@src/db/providerDataProvider";
import { getProviderDeployments } from "@src/db/deploymentProvider";

export const apiRouter = express.Router();

Expand Down Expand Up @@ -192,6 +193,24 @@ apiRouter.get(
})
);

apiRouter.get(
"/providers/:provider/deployments/:skip/:limit/:status?",
asyncHandler(async (req, res) => {
const skip = parseInt(req.params.skip);
const limit = Math.min(100, parseInt(req.params.limit));
const statusParam = req.params.status as "active" | "closed" | undefined;

if (statusParam && statusParam !== "active" && statusParam !== "closed") {
res.status(400).send(`Invalid status filter: "${statusParam}". Valid values are "active" and "closed".`);
return;
}

const deployments = await getProviderDeployments(req.params.provider, skip, limit, statusParam);

res.send(deployments);
})
);

apiRouter.get(
"/validators",
asyncHandler(async (req, res) => {
Expand Down
6 changes: 4 additions & 2 deletions shared/dbSchemas/akash/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Column, Default, HasMany, Model, PrimaryKey, Table } from "sequelize-typescript";
import { BelongsTo, Column, Default, HasMany, Model, PrimaryKey, Table } from "sequelize-typescript";
import { DataTypes, UUIDV4 } from "sequelize";
import { DeploymentGroup } from "./deploymentGroup";
import { Lease } from "./lease";
import { Message } from "../base/message";
import { Message, Block } from "../base";
import { Required } from "../decorators/requiredDecorator";

@Table({ modelName: "deployment" })
Expand All @@ -18,6 +18,8 @@ export class Deployment extends Model {
@Required @Column(DataTypes.DOUBLE) withdrawnAmount!: number;
@Column closedHeight?: number;

@BelongsTo(() => Block, "createdHeight") createdBlock: Block;
@BelongsTo(() => Block, "closedHeight") closedBlock: Block;
@HasMany(() => DeploymentGroup, "deploymentId") deploymentGroups: DeploymentGroup[];
@HasMany(() => Lease, "deploymentId") leases: Lease[];
@HasMany(() => Message, { foreignKey: "relatedDeploymentId", constraints: false }) relatedMessages: Message[];
Expand Down
3 changes: 3 additions & 0 deletions shared/dbSchemas/akash/lease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DeploymentGroup } from "./deploymentGroup";
import { Deployment } from "./deployment";
import { Provider } from "./provider";
import { Required } from "../decorators/requiredDecorator";
import { Block } from "../base";

@Table({
modelName: "lease",
Expand Down Expand Up @@ -37,6 +38,8 @@ export class Lease extends Model {
@Required @Column(DataTypes.BIGINT) ephemeralStorageQuantity: number;
@Required @Column(DataTypes.BIGINT) persistentStorageQuantity: number;

@BelongsTo(() => Block, "createdHeight") createdBlock: Block;
@BelongsTo(() => Block, "closedHeight") closedBlock: Block;
@BelongsTo(() => DeploymentGroup, "deploymentGroupId") deploymentGroup: DeploymentGroup;
@BelongsTo(() => Deployment, "deploymentId") deployment: Deployment;
@BelongsTo(() => Provider, "providerAddress") provider: Provider;
Expand Down