Skip to content

Commit

Permalink
Optimize slot lag graph
Browse files Browse the repository at this point in the history
1. past month gets 8640 points, only send back 720
2. transmit numbers; formatting happens on client
3. always show HH:mm in timestamp
  • Loading branch information
serprex committed Mar 12, 2024
1 parent 3af43e0 commit adf5ef9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
38 changes: 15 additions & 23 deletions ui/app/api/peers/slots/[name]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,22 @@ export async function GET(
break;
}

const lagPoints = await prisma.peer_slot_size.findMany({
select: {
updated_at: true,
slot_size: true,
},
where: {
slot_name: context.params.name,
updated_at: {
gte: new Date(Date.now() - forThePastThisMuchTime),
},
},
});
const lagPoints = await prisma.$queryRaw<
{ updated_at: Date; slot_size: bigint }[]
>`
select updated_at, slot_size
from peerdb_stats.peer_slot_size
where slot_size is not null
and slot_name = ${context.params.name}
and updated_at > ${new Date(Date.now() - forThePastThisMuchTime)}
order by random()
limit 720
`;

// convert slot_size to string
const stringedLagPoints: SlotLagPoint[] = lagPoints.map((lagPoint) => {
return {
// human readable
updatedAt:
lagPoint.updated_at.toDateString() +
' ' +
lagPoint.updated_at.toLocaleTimeString(),
slotSize: lagPoint.slot_size?.toString(),
};
});
const stringedLagPoints: SlotLagPoint[] = lagPoints.map((lagPoint) => ({
updatedAt: +lagPoint.updated_at,
slotSize: Number(lagPoint.slot_size) / 1000,
}));

return NextResponse.json(stringedLagPoints);
}
4 changes: 2 additions & 2 deletions ui/app/dto/PeersDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ export type CatalogPeer = {
export type PeerSetter = React.Dispatch<React.SetStateAction<PeerConfig>>;

export type SlotLagPoint = {
updatedAt: string;
slotSize?: string;
updatedAt: number;
slotSize: number;
};
16 changes: 6 additions & 10 deletions ui/app/peers/[peerName]/lagGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function LagGraph({ slotNames }: { slotNames: string[] }) {
}
);
const points: SlotLagPoint[] = await pointsRes.json();
setLagPoints(points);
setLagPoints(points.sort((x, y) => x.updatedAt - y.updatedAt));
}, [selectedSlot, timeSince]);

const handleChange = (val: string) => {
Expand All @@ -34,15 +34,11 @@ function LagGraph({ slotNames }: { slotNames: string[] }) {
};

const graphValues = useMemo(() => {
let lagDataDot = lagPoints.map((point) => [
point.updatedAt,
point.slotSize,
]);
return lagDataDot.map((data) => ({
time: formatGraphLabel(new Date(data[0]!), timeSince),
'Lag in GB': parseInt(data[1] || '0', 10) / 1000,
return lagPoints.map((data) => ({
time: formatGraphLabel(new Date(data.updatedAt!), 'hour'),
'Lag in GB': data.slotSize,
}));
}, [lagPoints, timeSince]);
}, [lagPoints]);

const [mounted, setMounted] = useState(false);
useEffect(() => {
Expand Down Expand Up @@ -78,7 +74,7 @@ function LagGraph({ slotNames }: { slotNames: string[] }) {
>
<ReactSelect
className='w-1/4'
placeholder='Select a replication slot'
placeholder='Select a replication slot asdf'
options={
slotNames.length === 0
? undefined
Expand Down

0 comments on commit adf5ef9

Please sign in to comment.