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

feat: Render service definition in front end #51

Merged
merged 5 commits into from
Jan 12, 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
16 changes: 2 additions & 14 deletions admin/app/clusters/[clusterId]/ClusterLiveTables.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,15 @@ export function ClusterLiveTables({
status: string;
functionExecutionTime: number | null;
}[];
services: Array<{
name: string;
functions: Array<{
name: string;
totalSuccess: number;
totalFailure: number;
avgExecutionTimeSuccess: number | null;
avgExecutionTimeFailure: number | null;
}>;
}>;
definitions: Array<{
name: string;
functions: Array<{
functions?: Array<{
name: string;
}>;
}>;
}>({
machines: [],
jobs: [],
services: [],
definitions: [],
});

Expand All @@ -85,8 +74,7 @@ export function ClusterLiveTables({
setData({
machines: clusterResult.body.machines,
jobs: clusterResult.body.jobs,
services: clusterResult.body.services,
definitions: clusterResult.body.definitions || [],
definitions: clusterResult.body.definitions,
});
} else {
toast.error("Failed to fetch cluster details.");
Expand Down
6 changes: 3 additions & 3 deletions admin/app/clusters/[clusterId]/services/ServiceSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Link from "next/link";
const Service = (props: {
clusterId: string;
name: string;
functions: Array<{ name: string }>;
functions?: Array<{ name: string }>;
}) => {
return (
<Link
Expand All @@ -26,7 +26,7 @@ const Service = (props: {
<CardContent>
<p className="text-sm text-gray-500">functions</p>
<ul>
{props.functions.map((fn) => (
{props.functions?.map((fn) => (
<li className="font-mono" key={fn.name}>
{fn.name}()
</li>
Expand All @@ -40,7 +40,7 @@ const Service = (props: {

export const ServiceSummary = (props: {
clusterId: string;
services: Array<{ name: string; functions: Array<{ name: string }> }>;
services: Array<{ name: string; functions?: Array<{ name: string }> }>;
}) => {
return (
<div className="flex">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ export function ServiceLiveTables({
}[];
service?: {
name: string;
functions: Array<{
functions?: Array<{
name: string;
totalSuccess: number;
totalFailure: number;
avgExecutionTimeSuccess: number | null;
avgExecutionTimeFailure: number | null;
idempotent?: boolean | null;
rate?: { per: "minute" | "hour"; limit: number } | null;
cacheTTL?: number | null;
}>;
};
}>({
Expand Down Expand Up @@ -63,7 +62,7 @@ export function ServiceLiveTables({
jobs: clusterResult.body.jobs
.filter((f) => f.service == serviceName)
.slice(-10),
service: clusterResult.body.services
service: clusterResult.body.definitions
.filter((s) => s.name == serviceName)
.pop(),
});
Expand All @@ -87,27 +86,25 @@ export function ServiceLiveTables({
{(data.service !== undefined && (
<div>
<div className="mt-12">
<h2 className="text-xl mb-4">Function Registry</h2>
<p className="text-gray-400 mb-8">
These are the functions that are registered for this service.
</p>
<DataTable
data={data.service.functions.map((s) => ({
Function: s.name,
"Total Requests": s.totalSuccess + s.totalFailure,
"Failure Rate": `${(
(s.totalFailure / (s.totalSuccess + s.totalFailure)) *
99
).toFixed(2)}%`,
"Average Execution Time (Success)": `${
s.avgExecutionTimeSuccess === undefined ||
s.avgExecutionTimeSuccess === null
? "N/A"
: `${s.avgExecutionTimeSuccess?.toFixed(2)}ms`
}`,
"Average Execution Time (Failure)": `${
s.avgExecutionTimeFailure === undefined ||
s.avgExecutionTimeFailure === null
? "N/A"
: `${s.avgExecutionTimeFailure?.toFixed(2)}ms`
}`,
}))}
data={
data.service.functions?.map((s) => ({
Function: s.name,
Idempotent: s.idempotent ? "Yes" : "No",
"Rate Limit":
s.rate === null || s.rate === undefined
? "N/A"
: `${s.rate?.limit}/${s.rate?.per}`,
"Cache TTL":
s.cacheTTL === null || s.cacheTTL === undefined
? "N/A"
: `${s.cacheTTL}s`,
})) ?? []
}
noDataMessage="No functions have been detected recently."
/>
</div>
Expand Down
49 changes: 18 additions & 31 deletions admin/client/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const NextJobSchema = z.object({
});

export const contract = c.router({
getNextJobs: {
method: "GET",
path: "/jobs",
createJobsRequest: {
method: "POST",
path: "/jobs-request",
headers: z.object({
authorization: z.string(),
"x-machine-id": z.string(),
}),
query: z.object({
body: z.object({
limit: z.coerce.number().default(1),
service: z.string(),
ttl: z.coerce.number().min(5000).max(20000).default(20000),
Expand Down Expand Up @@ -208,36 +208,23 @@ export const contract = c.router({
functionExecutionTime: z.number().nullable(),
})
),
services: z.array(
z.object({
name: z.string(),
functions: z.array(
z.object({
name: z.string(),
totalSuccess: z.number(),
totalFailure: z.number(),
avgExecutionTimeSuccess: z.number().nullable(),
avgExecutionTimeFailure: z.number().nullable(),
})
),
})
),
definitions: z.array(
z.object({
name: z.string(),
functions: z.array(
z.object({
name: z.string(),
idempotent: z.boolean().optional(),
rate: z
.object({
per: z.enum(["minute", "hour"]),
limit: z.number(),
})
.optional(),
cacheTTL: z.number().optional(),
})
),
functions: z
.array(
z.object({
name: z.string(),
idempotent: z.boolean().optional(),
rate: z
.object({
per: z.enum(["minute", "hour"]),
limit: z.number(),
})
.optional(),
cacheTTL: z.number().optional(),
})
).optional()
})
),
}),
Expand Down
14 changes: 0 additions & 14 deletions control-plane/src/modules/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,6 @@ export const contract = c.router({
functionExecutionTime: z.number().nullable(),
})
),
services: z.array(
z.object({
name: z.string(),
functions: z.array(
z.object({
name: z.string(),
totalSuccess: z.number(),
totalFailure: z.number(),
avgExecutionTimeSuccess: z.number().nullable(),
avgExecutionTimeFailure: z.number().nullable(),
})
),
})
),
definitions: z.array(
z.object({
name: z.string(),
Expand Down
74 changes: 0 additions & 74 deletions control-plane/src/modules/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,6 @@ export const createCluster = async ({
.execute();
};

type FunctionDetails = {
name: string;
avgExecutionTimeSuccess: number | null;
avgExecutionTimeFailure: number | null;
totalSuccess: number;
totalFailure: number;
};
export const getClusterDetailsForUser = async ({
managementToken,
clusterId,
Expand Down Expand Up @@ -117,10 +110,6 @@ export const getClusterDetailsForUser = async ({
createdAt: Date;
functionExecutionTime: number | null;
}>;
services: Array<{
name: string;
functions: Array<FunctionDetails>;
}>;
definitions: Array<ServiceDefinition>;
}
| undefined
Expand Down Expand Up @@ -191,75 +180,12 @@ export const getClusterDetailsForUser = async ({
)
);

// Fetch all function / service combinations for the cluster within the last 12 hours
// This can be replaced with something more robust once we have a catalog of service / functions
const functions = await data.db
.select({
service: data.jobs.service,
target_fn: data.jobs.target_fn,
avgExecutionTime:
sql`avg(${data.jobs.function_execution_time_ms})`.mapWith(Number),
total: sql`count(${data.jobs.id})`.mapWith(Number),
result_type: data.jobs.result_type,
})
.from(data.jobs)
.groupBy(data.jobs.service, data.jobs.target_fn, data.jobs.result_type)
.where(
and(
eq(data.jobs.owner_hash, clusterId),
// in the last 12 hours
gte(data.jobs.created_at, new Date(Date.now() - 1000 * 60 * 60 * 12))
)
);

// Build a map of service -> function -> details merging the error and success results
const serviceFnMap = functions.reduce(
(acc, current) => {
const serviceName = current.service;
if (!serviceName) {
return acc;
}

const isSuccess = current.result_type === "resolution";

const service = acc.get(serviceName) ?? new Map();
service.set(current.target_fn, {
...(isSuccess
? { avgExecutionTimeSuccess: current.avgExecutionTime }
: { avgExecutionTimeFailure: current.avgExecutionTime }),
...(isSuccess
? { totalSuccess: current.total }
: { totalFailure: current.total }),
...service.get(current.target_fn),
});

acc.set(serviceName, service);

return acc;
},
new Map() as Map<string, Map<string, Omit<FunctionDetails, "name">>>
);

const serviceResult = Array.from(serviceFnMap).map(([name, functionMap]) => ({
name: name,
functions: Array.from(functionMap).map(([fnName, fnDetails]) => ({
name: fnName,
...fnDetails,
// If there is no success or failure, default to 0
totalSuccess: fnDetails.totalSuccess ?? 0,
totalFailure: fnDetails.totalFailure ?? 0,
})),
}));

console.log({ clusters });

return {
id: clusters[0].id,
apiSecret: clusters[0].apiSecret,
createdAt: clusters[0].createdAt,
definitions: parseServiceDefinition(clusters.map((c) => c.definitions)),
machines,
jobs,
services: serviceResult,
};
};
Loading