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 54ab279 commit 5611319
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
40 changes: 16 additions & 24 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 slotLagPoints: SlotLagPoint[] = lagPoints.map((lagPoint) => ({
updatedAt: +lagPoint.updated_at,
slotSize: Number(lagPoint.slot_size) / 1000,
}));

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

export type SlotLagPoint = {
updatedAt: string;
slotSize?: string;
updatedAt: number;
slotSize: number;
};

export type UPublicationsResponse = {
Expand Down
28 changes: 13 additions & 15 deletions ui/app/peers/[peerName]/lagGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { formatGraphLabel, timeOptions } from '@/app/utils/graph';
import { Label } from '@/lib/Label';
import { ProgressCircle } from '@/lib/ProgressCircle/ProgressCircle';
import { LineChart } from '@tremor/react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import ReactSelect from 'react-select';
import { useLocalStorage } from 'usehooks-ts';

function LagGraph({ slotNames }: { slotNames: string[] }) {
const [lagPoints, setLagPoints] = useState<SlotLagPoint[]>([]);
const [lagPoints, setLagPoints] = useState<
{ time: string; 'Lag in GB': number }[]
>([]);
const [defaultSlot, setDefaultSlot] = useLocalStorage('defaultSlot', '');
const [selectedSlot, setSelectedSlot] = useState<string>(defaultSlot);
let [timeSince, setTimeSince] = useState('hour');
Expand All @@ -25,25 +27,21 @@ function LagGraph({ slotNames }: { slotNames: string[] }) {
}
);
const points: SlotLagPoint[] = await pointsRes.json();
setLagPoints(points);
setLagPoints(
points
.sort((x, y) => x.updatedAt - y.updatedAt)
.map((data) => ({
time: formatGraphLabel(new Date(data.updatedAt!), 'hour'),
'Lag in GB': data.slotSize,
}))
);
}, [selectedSlot, timeSince]);

const handleChange = (val: string) => {
setDefaultSlot(val);
setSelectedSlot(val);
};

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,
}));
}, [lagPoints, timeSince]);

const [mounted, setMounted] = useState(false);
useEffect(() => {
setMounted(true);
Expand Down Expand Up @@ -107,7 +105,7 @@ function LagGraph({ slotNames }: { slotNames: string[] }) {
</div>
<LineChart
index='time'
data={graphValues}
data={lagPoints}
categories={['Lag in GB']}
colors={['rose']}
showXAxis={false}
Expand Down

0 comments on commit 5611319

Please sign in to comment.