Skip to content

Commit

Permalink
Update rebalance folder name
Browse files Browse the repository at this point in the history
Signed-off-by: hemahg <[email protected]>
  • Loading branch information
hemahg committed Sep 30, 2024
1 parent a03331c commit e493ffb
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
81 changes: 81 additions & 0 deletions ui/api/rebalance/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"use server";
import { logger } from "@/utils/logger";
import {
RebalanceResponse,
RebalanceResponseSchema,
RebalanceSchema,
RebalancesResponse,
RebalanceStatus,
} from "./schema";
import { filterUndefinedFromObj } from "@/utils/filterUndefinedFromObj";
import { getHeaders } from "@/api/api";

const log = logger.child({ module: "rebalance-api" });

export async function getRebalancesList(
kafkaId: string,
params: {
name?: string;
mode?: string;
status?: RebalanceStatus[];
pageSize?: number;
pageCursor?: string;
sort?: string;
sortDir?: string;
},
): Promise<RebalancesResponse> {
const sp = new URLSearchParams(
filterUndefinedFromObj({
"fields[kafkaRebalances]":
"name,namespace,creationTimestamp,status,mode,brokers",
"filter[name]": params.name ? `like,*${params.name}*` : undefined,
"filter[status]":
params.status && params.status.length > 0
? `in,${params.status.join(",")}`
: undefined,
"filter[mode]": params.mode ? `like,*${params.mode}*` : undefined,
"page[size]": params.pageSize,
"page[after]": params.pageCursor,
sort: params.sort
? (params.sortDir !== "asc" ? "-" : "") + params.sort
: undefined,
}),
);
const rebalanceQuery = sp.toString();
const url = `${process.env.BACKEND_URL}/api/kafkas/${kafkaId}/rebalances?${rebalanceQuery}`;
const res = await fetch(url, {
headers: await getHeaders(),
next: {
tags: ["rebalances"],
},
});

log.debug({ url }, "getRebalanceList");
const rawData = await res.json();
log.trace({ url, rawData }, "getRebalanceList response");
return RebalanceResponseSchema.parse(rawData);
}

export async function getRebalanceDetails(
kafkaId: string,
rebalanceId: string,
): Promise<RebalanceResponse> {
const url = `${process.env.BACKEND_URL}/api/kafkas/${kafkaId}/rebalances/${rebalanceId}`;
const decodedRebalanceId = decodeURIComponent(rebalanceId);
const body = {
data: {
type: "kafkaRebalances",
id: decodedRebalanceId,
meta: {},
attributes: {},
},
};
log.debug({ url }, "Fetching rebalance details");
const res = await fetch(url, {
headers: await getHeaders(),
method: "PATCH",
body: JSON.stringify(body),
});
const rawData = await res.json();
return RebalanceSchema.parse(rawData.data);
}
92 changes: 92 additions & 0 deletions ui/api/rebalance/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { z } from "zod";

const RebalanceStatusSchema = z.union([
z.literal("New"),
z.literal("PendingProposal"),
z.literal("ProposalReady"),
z.literal("Rebalancing"),
z.literal("Stopped"),
z.literal("NotReady"),
z.literal("Ready"),
z.literal("ReconciliationPaused"),
]);

const OptimizationResultSchema = z.object({
numIntraBrokerReplicaMovements: z.number(),
numReplicaMovements: z.number(),
onDemandBalancednessScoreAfter: z.number(),
afterBeforeLoadConfigMap: z.string(),
intraBrokerDataToMoveMB: z.number(),
monitoredPartitionsPercentage: z.number(),
provisionRecommendation: z.string(),
excludedBrokersForReplicaMove: z.array(z.string()).nullable(),
excludedBrokersForLeadership: z.array(z.string()).nullable(),
provisionStatus: z.string(),
onDemandBalancednessScoreBefore: z.number(),
recentWindows: z.number(),
dataToMoveMB: z.number(),
excludedTopics: z.array(z.string()).nullable(),
numLeaderMovements: z.number(),
});

export const RebalanceSchema = z.object({
id: z.string(),
type: z.literal("kafkaRebalances"),
meta: z
.object({
autoApproval: z.boolean().optional(),
allowedActions: z.array(z.string()),
})
.optional(),
attributes: z.object({
name: z.string(),
namespace: z.string(),
creationTimestamp: z.string(),
status: RebalanceStatusSchema,
mode: z.string().optional(),
brokers: z.array(z.number()).nullable(),
sessionId: z.string().optional(),
optimizationResult: OptimizationResultSchema,
}),
});

const RebalancesListSchema = z.object({
id: z.string(),
type: z.literal("kafkaRebalances"),
meta: z.object({
page: z.object({
cursor: z.string(),
}),
autoApproval: z.boolean(),
managed: z.boolean().optional(),
}),
attributes: RebalanceSchema.shape.attributes.pick({
name: true,
status: true,
creationTimestamp: true,
mode: true,
brokers: true,
}),
});

export const RebalanceResponseSchema = z.object({
meta: z.object({
page: z.object({
total: z.number(),
pageNumber: z.number().optional(),
}),
}),
links: z.object({
first: z.string().nullable(),
prev: z.string().nullable(),
next: z.string().nullable(),
last: z.string().nullable(),
}),
data: z.array(RebalancesListSchema),
});
export type RebalanceList = z.infer<typeof RebalancesListSchema>;
export type RebalancesResponse = z.infer<typeof RebalanceResponseSchema>;

export type RebalanceResponse = z.infer<typeof RebalanceSchema>;

export type RebalanceStatus = z.infer<typeof RebalanceStatusSchema>;

0 comments on commit e493ffb

Please sign in to comment.